Using PowerShell to automate setting CopyLocal to false

 

It’s been stressed that CopyLocal is slow and evil, so I wanted to see what the impact was on our codebase.

First we need to load the project file and pull out the references sections.

[xml]$s = get-content $projectFile

$references = $s.Project.ItemGroup | Where-Object { $_.Reference -ne $null }
$projectReferences = $s.Project.ItemGroup | Where-Object { $_.ProjectReference -ne $null }

Next we can iterate through the child nodes, and if they are missing the Private tag, add it in.

foreach($reference in $references.ChildNodes)
{ 
    if($reference.Private -eq $null)
    {
        [System.Xml.XmlElement]$copyLocal = $s.CreateElement("Private", "http://schemas.microsoft.com/developer/msbuild/2003")
        $copyLocal.InnerText = "False"
        [Void]$reference.AppendChild($copyLocal) 
    }
}

We can do a similar thing with the project references if your solution builds into a single folder.

foreach($reference in $projectReferences.ChildNodes)
{ 
    if($reference.Private -eq $null)
    {
        [System.Xml.XmlElement]$copyLocal = $s.CreateElement("Private", "http://schemas.microsoft.com/developer/msbuild/2003")
        $copyLocal.InnerText = "False"
        [Void]$reference.AppendChild($copyLocal) 
    }
}

Then all we need to do is save the file and we are done!

$s.save($projectFile)

If you want to apply this too all the project files in a directory, recursively then you could do:

Get-ChildItem $directory -include *.csproj,*.vbproj -Recurse | foreach ($_) { 
    Set-CopyLocalFalse $_.fullname $true
}

You can find the source for this in the SlightlyPosher distribution, or look at the source for the file.

Preliminary tests on our build time dropped from 6:30 minutes to 3:48 minutes thanks to this!

SlightlyPosher: Console2 settings for PowerShell

 

Pascal has pointed out that out of the box, SlightlyPosher does not integrate Console2 with PowerShell. Oops.

I have committed the settings file for this, so if you run update.bat or download it you will get the changes.

If you would like to override the default configuration, you can do so with Console2.

  1. Right click on the console and go to settings.
  2. Click the Console tab.
  3. Check “Save settings to user directory”

Now when you customise your console, it will not be wiped out when you do an update.

Pascal also highlighted that if you are new to POSH, you might also want to set your environment to allow scripts to run without restriction.

Set-ExecutionPolicy Unrestricted

This should allow the modules to load and run correctly. I will look at adding this to the install.bat asking if new users would like this enabled to simplify the process.

Sorry about these omissions!

Here is a SlightlyPosher environment

 

On my personal laptop I use Console2 with a PowerShell environment loaded with useful modules like PSCX, PowerTab and LINQ. I needed to be able to use this on client machines, so I cleaned up the paths to be relative and uploaded it to a DropBox.

Now this worked great for me, however I wanted to share it with my work mates, so I needed an easier way of distributing it. Since I’ve gone through the effort why not share it with everyone.

https://github.com/naeemkhedarun/SlightlyPosher

Getting Started

 

  1. Download the latest from: https://github.com/naeemkhedarun/SlightlyPosher/zipball/master
  2. Unzip
  3. Run Install.bat – This appends the SlightlyPosher profile to your PowerShell profile.
  4. Launch Console2
  5. That’s it!

Updating your environment

 

You can update the entire environment using update.bat. This will download the latest from GitHub and unzip it into your local directory.

This will wipe out any changes to make to existing files (but not delete new ones). So please fork the project on GitHub and send me pull requests for any changes you make. It will not override changes you make to the configuration for Console2 since everyone likes their console slightly different.

What you will get

 

Console2

clip_image001

Tab Completion

clip_image005

Balloon Notifications

clip_image007 

clip_image009

Visual Studio Command Prompt

clip_image011

Get-SystemInternalsSuite (downloaded, unzipped and configured into your path)

clip_image013

clip_image015

Command line history

image

Even more stuff in modules

You can browse all the functions that are available and pre-loaded in the modules folder.

Credits

 

Since I’ve pulled in quite a bit of stuff from the web, it’s only fair that I credit others. I’ve done my best do put credits directly in the code. For example:

image

If you see code I’ve not credited, please let me know and I’ll add it in!

Enjoy

 

You can reach me @NaeemKhedarun

Newer Posts Older Posts