An often used convention used in coding is to prefix boolean properties that are public with the prefix
Is. In this article, an automatic
refactoring technique will be explained using Powershell. Alternatives could be using Resharper or Roslyn. Since Powershell will tackle this on a file level, it will execute very fast. Using Powershell alone will not be sufficient, since Powershell does not have the needed code analysis capabilities. To tackle this, we will use
Studioshell and execute the script presented in this article.
To download Studioshell, visit the following url:
Studioshell codeplex page
As an alternative, download and install StudioShell via NuGet. Open up a new solution in Visual Studio, then choose the menu option
Tools, Library Package Manager and Package manager console. Choose your main project or what's best suited and run the command:
Install-Package StudioShell.Beta
When StudioShell is installed, check that the
Execution Policy is remote signed. Start Visual Studio as an administrator and run the following cmdlet:
Set-ExecutionPolicy RemoteSigned .
StudioShell has its own profile file. Let's first create the profile file, if necessary. Run the following cmdlet:
New-Item -ItemType File $profile
Add the following function to the $profile file, by running the following cmdlet to open up the $profile file in Notepad:
Invoke-Item $profile
The automatic refactoring function in Powershell with the help of StudioShell then looks like this:
function PrefixWithIs-Booleans(){
Set-Location dte:\solution\codemodel
dir -recurse | where { $_ -match 'property' -and $_.name -notMatch '^Is' -and $_.access -match 'public' -and
$_.type.asfullname -match 'bool' } | foreach { $newname = "Is" + $_.name; $_.renamesymbol($newname); }
}
Of course, this function could be put in a
.ps1 or
.psm1 file instead of residing in your StudioShell profile file. By putting the function or cmdlet into the $profile file, it will always be available in StudioShell. As you can also see, the cmdlet is named with verb-noun, as is the convention in Powershell.
To run this cmdlet, just type:
PrefixWithIs-Booleans
This cmdlet will automatically rename all public properties that return a boolean and that has not have the prefix
Is, and rename this public property to have the prefix
Is. This will result that all public properties start with
Is.
The cmdlet PrefixWithIs-Booleans starts with running the
dir -recurse command to recursively look at all files (and folders) in subdirectories. But first the location is changed to
dte:\solution\codemodel. Studioshell has a folder structure which is used for the analysis of code of the currently loaded solution. By then looking at all files (and folders) in the folder
dte:\solution\codemodel, effectively one is looking at the entire Visual Studio solution. The property of the items one is looking at is
CodeOwls.StudioShell.Paths.Items.CodeModel.ShellCodeProperty2. The command
dir -recurse is
piped to the cmdlet where and the long where condition effectively looks for boolean properties that are public and does not have a name starting with
Is. The result set is then furter piped to
foreach which will iterate on the current object in Powershell,
$_ and then call the
renamesymbol method of the current object. The new name is then set to
Is + name member of current object.
This shows how
Studioshell can refactor a Visual Studio solution using the
dte: folder structure of Studioshell and then to code manipulation on the Visual Studio. It is important to be careful when using
Studioshell, I would suggest using a versioning system such as
Mercurial. Studioshell can also automate a multitude of features in Visual Studio besides code manipulation and code analysis. For example, it is possible to save window layout and load up a window layout with a single command from the shell command line of Studioshell inside Visual Studio. It is also possible to toggle on and off all breakpoints and so on.
If you find yourself doing repetitive tasks in Visual Studio or changing the code in a repetitive manner, consider using
StudioShell for automating these tasks.