Twitter
Powered By
Powered by Squarespace
Navigation
Main | Deleting all bin and obj folders from a solution »
Tuesday
Oct162012

Using a script to set the Copy Local flag to false

As with my previous post I recently came across a repeatable task that we will probably want to repeat in the future so with my aim from The Pragmatic Programmer I decided to automate it.

The problem was that an architecual requirement of this project was to rely on DependencyInjection for all library references. To help enforce this every project outside of the DI one would require the Copy Local flag on all references set to false.

I started doing this manually but figured out it'd take a long time to go through all 40+ projects and this would happen in the future. So automation time it was.

A quick web search did not show that anyone had solved this problem before so I figured out I would have to learn some Powershell and make it myself.

As csproj files are simply XML I did some research to find out how easy it was to manipulate XML in Powershell. It turned out this is one of Powershell's strengths. However the first implementation had issues with namesapces so I had to use the Select-Xml command introduced in Powershell v2.

Building the XPath queries was fairly simple. The one hiccup to remember is that csproj xml has a default namespace of "http://schemas.microsoft.com/developer/msbuild/2003" so you need to remember to use that and the msb namespace prefix when making your XPath queries. To specify the namespace in Select-Xml you use the -namespace option.

Select-Xml -namespace @{msb = $projectNamespace} -xpath $privateXPath

The next step was saving out the changes. This proved to be an initial roadblock as all the files were set to readonly. As we are using TFS you have to explicitly checkout the files before you can edit them. This resulted in me looking into how to use the TFS command line executable "tf.exe". This proved to be fairly nice as I could simply pipe the collection of csproj files I wanted checked out to a chunk of script that would iterate through the collection and execute the checkout command on each file with the provided TFS credentials.

I explicitly did not attempt to check in the changes as I want the user to review the changes and make sure the solutions are still working. This is something you'd run once a month to make sure the requirement is still being followed.

The final hiccup was that the .NET XML classes Powershell uses has an issue with putting in a default empty namespace whenever you create a new element. This caused the project to fail to load in VisualStudio as the namespace was incorrect. The fix for this was pretty quick and easy. Take the file and replace any occurance of xmlns="" with an empty string. This is accomplished in Powershell with line

(Get-Content $projFilenameFull) | Foreach-Object {$_ -replace ' xmlns=""', ""} | Set-Content $projFilenameFull

So my first non-trivial powershell script was a fun and fiddly dive into scripting all my troubles away. So far so good. ;)

SetCopyLocalInAllCsProjFiles.ps1

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (26)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Charlotte "Charlie" Parkhurst was a lady. A single certain doctor claimed that at some degree in her each day life, she had been a mother. Unknowingly, Parkhurst could declare a national first.
  • Response
    But only you possibly can preserve heading and see that those road blocks may perhaps be there but you may handle and determine a way for you to get past them and maintain transferring forward. This basic basic principle applies to every thing you need to do from finding up in ...
  • Response
    Response: burberry服
    burberryのブラックレーベル
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: belinda broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: groupwise inc
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: Belinda Broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: belinda broido
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Using a script to set the Copy Local flag to false - Thoughts - TFG
  • Response
    Response: seo reseller
    Using a script to set the Copy Local flag to false - Thoughts - TFG

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>