I am writing a powershell script to automate some of the boilerplate setup when initiating a .NET Core Blazor application, and for some reason it is not properly updating the .csproj
file. What I would like to do is add a new node to the root of the file like so:
<Project Sdk="...">
...
...
...
<Target Name="Tailwind" BeforeTargets="Build">
<Exec Command="npm run input:build" />
</Target>
</Project>
The script I am using works when I'm in a test environment using a mocked .csproj
file:
# edit csproj as xml to include npm build scripts
$dir = Split-Path -Path (Get-Location) -Leaf
Write-Host "Adding build directives to "$dir".csproj . . .";
$xml = [XML] (Get-Content $dir".csproj");
$target = $xml.CreateElement("Target");
$target.SetAttribute("Name", "Tailwind");
$target.SetAttribute("BeforeTargets", "Build");
$cmd = $xml.CreateElement("Exec");
$cmd.SetAttribute("Command", "npm run input:build");
$xml.DocumentElement.AppendChild($target);
$target.AppendChild($cmd);
$xml.Save($dir+".csproj");
Write-Host ". . . done.";
The rest of the script is running perfectly, and everything seems to be PATH'd correctly. And like I said, the script runs fine in test. Is there a reason that a powershell automation script wouldn't be able to append a node to an existing .csproj
file in a .NET project?
You'll have to excuse if my powershell looks heinous, as I am very new to it.