Skip to content

Instantly share code, notes, and snippets.

@sdurandeu
Created April 29, 2014 19:17
Show Gist options
  • Save sdurandeu/11409404 to your computer and use it in GitHub Desktop.
Save sdurandeu/11409404 to your computer and use it in GitHub Desktop.
Automatically finding missing and duplicate files in CSProj (Revisited)
Param(
[string]$filePath = $(throw "You must supply a file path")
)
clear
"--- Working... ---"
$filePath = Resolve-Path $filePath
$xml = ([xml] ( gc $filePath ))
$dirPath = (split-path $filePath)
$filesIncluded = $xml.Project.ItemGroup.Compile `
| % {
if ($_.Include -ne $null)
{
(resolve-path (join-path $dirPath $_.Include)).Path
}
}
$filesOnDisk = gci -Path $dirPath -Include *.cs -Recurse | select -ExpandProperty FullName
$uniquePaths = $filesIncluded | select –unique
$duplicatePaths = Compare-object –referenceobject $uniquePaths –differenceobject $filesIncluded `
| select -ExpandProperty InputObject
$fileDiff = Compare-object –referenceobject $filesIncluded –differenceobject $filesOnDisk
$missingInCsProj = $fileDiff | ? { $_.sideIndicator -eq '=>' } | select -ExpandProperty inputObject
$missingOnDisk = $fileDiff | ? { $_.sideIndicator -eq '<=' } | select -ExpandProperty inputObject
# Just double check whether the files are really missing. They might not be
# cs files or they might be in another project
$missingOnDisk = foreach($path in $missingOnDisk)
{
if(-not (test-path $path)){
$path
}
}
clear
if($missingOnDisk -or $duplicatePaths -or $missingInCsProj)
{
if($missingOnDisk)
{
""
"---- Files in the CSProj missing from disk ----"
$missingOnDisk
}
if($duplicatePaths)
{
""
"---- File paths appearing in the CSProj multiple times ----"
$duplicatePaths
}
if($missingInCsProj)
{
""
"---- .CS files missing from the CSProj ----"
$missingInCsProj
}
}
else
{
"=> No errors found in $filePath"
}
""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment