Private/Cs/SolnProj/Edit-ProjectAddReference.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<############################################################################
 # Add csproj as reference to another csproj
 ############################################################################>

Function Edit-ProjectAddReferencePrivate([string]$thisCsprojFile, [string]$referenceCsprojFile)
{
    # Convert full path $referenceCsprojFile to path relative to $thisCsprojFile
    # Keep final subdir and file name, put ".." in the front
    [string]$referenceCsprojFileRelative = ".."    + $referenceCsprojFile.substring((split-path (split-path $referenceCsprojFile)).length)

    Write-Host "### Fix $($thisCsprojFile) as dependent on $referenceCsprojFileRelative"
    $csprojXml = [xml] (type $thisCsprojFile)
    [xml]$useProjXml = 
        @"
      <ItemGroup>
        <ProjectReference Include="$($referenceCsprojFileRelative)" />
      </ItemGroup>
"@

    $csprojXml.project.AppendChild($csprojXml.ImportNode($useProjXml.ItemGroup, $true))
    $csprojXml.Save($thisCsprojFile)
}