Skip to content

Instantly share code, notes, and snippets.

@jrotello
Created October 24, 2014 15:07
Show Gist options
  • Save jrotello/80ecac2bdd6125a02527 to your computer and use it in GitHub Desktop.
Save jrotello/80ecac2bdd6125a02527 to your computer and use it in GitHub Desktop.
PowerShell script to force "Copy Local" in .csproj files.
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$RootDir,
[string[]]$Assemblies = @(
'System.Net.Http.Formatting',
'System.Web.Http',
'System.Web.Http.WebHost',
'System.Web.Mvc'
)
)
function isMatchingReference {
param($reference)
if ($reference -eq $null) {
return $false
}
$include = ($reference.Include -split ',')[0]
return $Assemblies -contains $include
}
$ErrorActionPreference = "Stop"
$projects = Get-ChildItem $RootDir -Include *.csproj -Recurse
$projects | % {
$modified = $false
[xml]$project = Get-Content $_
$references = $project.Project.ItemGroup.Reference | ? { $_ -ne $null }
# ensure that copy local is set for each matching assembly reference
$references | ? { isMatchingReference($_) } | % {
if ($_.Private -eq $null) {
$privateElement = $project.CreateElement("Private", $null)
$_.AppendChild($privateElement) | Out-Null
}
if ($_.Private -ne 'True') {
$_.Private = 'True'
$modified = $true
}
}
if ($modified) {
# get rid of unnecessary empty "xmlns" attributes
$project = [xml] $project.OuterXml.Replace(' xmlns=""', '')
$project.save($_)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment