Last active
July 20, 2019 00:58
-
-
Save Kevin-Bronsdijk/5aed0833de62fcbd6013bda325e8bf83 to your computer and use it in GitHub Desktop.
Remove Missing Includes within csproj file powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RemoveMissingInclude -path "D:\Projects\gtmetrix-net\src\WebTest\WebTest.csproj" | Out-File D:\Projects\gtmetrix-net\src\WebTest\WebTestClean.csproj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RemoveMissingInclude -path "D:\Projects\gtmetrix-net\src\WebTest\WebTest.csproj" -report $true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ExtractInclude ($line) | |
{ | |
if ($line -like '*Content Include=*') { | |
return $line.Split('"') | select -Skip 1 | select -First 1 | |
} | |
} | |
function RemoveMissingInclude ([string]$path, [bool]$report) { | |
$reader = [System.IO.File]::OpenText($path) | |
$projectPath = (Split-Path $path) + "/" | |
try { | |
for() { | |
$line = $reader.ReadLine() | |
if ($line -eq $null) { break } | |
$pathInclude = ExtractInclude($line) | |
if ($report) { | |
if ($pathInclude -ne "") { | |
if (-not (Test-Path "$projectPath$pathInclude")) { $pathInclude } | |
} | |
} else { | |
if ($pathInclude -ne "") { | |
if (Test-Path "$projectPath$pathInclude") { $line } | |
} else { | |
$line | |
} | |
} | |
} | |
} | |
finally { | |
$reader.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lovely script; thank you so much!
One issue I have that PS is encoding the project file in Unicode, causing some unexpected errors for the (ASP.NET) compiler. Re-exporting it in UTF-8 worked. Commenting here for others who might run into the issue.