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() | |
} | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your script, saved me a lot of time.
However I've found that it doesn't work in case the Content element has children, like this for example :
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
What I do then is simply change the if clause to match only self-closing Content elements like this :
if ($line -like '*<Content Include=*/>') {
Then I create a clean csproj file, then revert the test and run the report option and delete those manually (as there were not that many of them)