Skip to content

Instantly share code, notes, and snippets.

@daxian-dbw
Created October 22, 2016 05:08
Show Gist options
  • Save daxian-dbw/f73eae620e9d774f9028cf7e8f71204e to your computer and use it in GitHub Desktop.
Save daxian-dbw/f73eae620e9d774f9028cf7e8f71204e to your computer and use it in GitHub Desktop.
Add <Link> tags to the '<Compile>', '<None>' and '<EmbeddedResource>' for the *.csproj files in PowerShell/PowerShell. This is to make sure in Visual Studio all file items are organized in the appropriate folders.
function Add-LinkElement
{
param(
[Parameter(Mandatory=$true)]
[string]$csprojPath,
[Parameter(Mandatory=$true)]
[string]$projectName,
[Parameter(Mandatory=$true)]
[string]$outputPath
)
$relativePath = "..\$projectName\"
$xmlDoc = [xml](Get-Content $csprojPath)
foreach ($itemGroup in $xmlDoc.Project.ItemGroup)
{
foreach ($childNode in $itemGroup.ChildNodes)
{
if ($childNode.Name -eq "Compile" -or
$childNode.Name -eq "None" -or
$childNode.Name -eq "EmbeddedResource")
{
$link = $childNode.Include.Substring($relativePath.Length)
$linkNode = $xmlDoc.CreateElement("Link", $xmlDoc.DocumentElement.NamespaceURI)
$linkNode.InnerText = $link
$childNode.AppendChild($linkNode) > $null
}
}
}
$xmlWriterSetting = [System.Xml.XmlWriterSettings]::new()
$xmlWriterSetting.Indent = $true
$xmlWriterSetting.IndentChars = " "
$xmlWriter = [System.Xml.XmlWriter]::Create($outputPath, $xmlWriterSetting)
$xmlDoc.Save($xmlWriter)
$xmlWriter.Flush()
$xmlWriter.Dispose()
}
Add-LinkElement -csprojPath .\src\vs-csproj\Microsoft.PowerShell.Commands.Management.csproj `
-projectName Microsoft.PowerShell.Commands.Management `
-outputPath C:\arena\tmp\Microsoft.PowerShell.Commands.Management.csproj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment