Visual Studio Macro – Identifier to String Literal

When implementing public methods with parameters it’s important to write boiler-plate code to do argument validation and throw exceptions when needed, ArgumentException and ArgumentNullException being the most recurrent.

Another thing that is important is to correctly specify the parameter causing the exception through the proper exception constructor.

In order to take advantage of IntelliSense completion in these scenarios I use a Visual Studio macro binded to a keyboard shortcut that converts the identifier at the cursor position to a string literal.

And here’s the macro:

Sub ConvertIdentifierToStringLiteral()
    Dim targetWord As String
    Dim document As EnvDTE.TextDocument

    document = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument)

    If document.Selection.Text.Length > 0 Then
        targetWord = document.Selection.Text
        document.Selection.ReplacePattern(targetWord, """" + targetWord + """")
    Else
        Dim cursorPoint As EnvDTE.TextPoint

        cursorPoint = document.Selection.ActivePoint()

        Dim editPointLeft As EnvDTE.EditPoint
        Dim editPointRight As EnvDTE.EditPoint

        editPointLeft = cursorPoint.CreateEditPoint()
        editPointLeft.WordLeft(1)

        editPointRight = editPointLeft.CreateEditPoint()
        editPointRight.WordRight(1)

        targetWord = editPointLeft.GetText(editPointRight)
        editPointLeft.ReplaceText(editPointRight, """" + targetWord + """", 0)
    End If
End Sub
Advertisements

VS 2010 SP1 BETA – App.config XML Transformation Fix

The current version for App.config XML tranformations as described in a previous post does not support the SP1 BETA version of Visual Studio. I did some quick tests and decided to provide a different version with a compatibility fix for those already experimenting with the beta release of the service pack.

This is a quick workaround to the build errors I found when using the transformations in SP1 beta and is pretty much untested since I’ll wait for the final release of the service pack to take a closer look to any possible problems.

But for now, those that already installed SP1 beta can use the following transformations:

VS 2010 SP1 BETA – App.config XML Transformation

And those with the RTM release of Visual Studio can continue to use the original version of the transformations available from:

VS 2010 RTM – App.config XML Transformation

Visual Studio App.config XML Transformation

Visual Studio 2010 introduced a much-anticipated feature, Web configuration transformations. This feature allows to configure a web application project to transform the web.config file during deployment based on the current build configuration (Debug, Release, etc).

If you haven’t already tried it there is a nice step-by-step introduction post to XML transformations on the Visual Web Developer Team Blog and for a quick reference on the supported syntax you have this MSDN entry.

Unfortunately there are some bad news, this new feature is specific to web application projects since it resides in the Web Publishing Pipeline (WPP) and therefore is not officially supported in other project types like such as a Windows applications. The keyword here is officially because Vishal Joshi has a nice blog post on how to extend it’s support to app.config transformations.

However, the proposed workaround requires that the build action for the app.config file be changed to Content instead of the default None. Also from the comments to the said post it also seems that the workaround will not work for a ClickOnce deployment.

Working around this I tried to remove the build action change requirement and at the same time add ClickOnce support. This effort resulted in a single MSBuild project file (AppConfig.Transformation.targets) available for download from GitHub. It integrates itself in the build process so in order to add app.config transformation support to an existing Windows Application Project you just need to import this targets file after all the other import directives that already exist in the *.csproj file.

Before – Without App.config transformation support

  ...
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
</Project>

After – With App.config transformation support

  ...
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="C:\MyExtensions\AppConfig.Transformation.targets" />
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
</Project>

As a final disclaimer, the testing time was limited so any problem that you find let me know. The MSBuild project invokes the mage tool so the Framework SDK must be installed.


Update:

I finally had some spare time and was able to check the problem reported by Geoff Smith and believe the problem is solved. The Publish command inside Visual Studio triggers a build workflow different than through MSBuild command line and this was causing problems. I posted a new version in GitHub that should now support ClickOnce deployment with app.config tranformation from within Visual Studio and MSBuild command line.

Thanks to Geoff for spotting the problem.


Update 2:

There’s a new version available at GitHub that includes a fix by Ryan Milligan for a ClickOnce deployment problem. I did some minor changes to Ryan’s fix, but I did some tests and I believe I didn’t mess it up.

Thanks again to Ryan for finding the problem and fixing it.