Skip to content

Instantly share code, notes, and snippets.

@cezarypiatek
Last active January 21, 2016 20:28
Show Gist options
  • Save cezarypiatek/b009bbe8e9b9f6b950ba to your computer and use it in GitHub Desktop.
Save cezarypiatek/b009bbe8e9b9f6b950ba to your computer and use it in GitHub Desktop.
function Search-ItemsRecursive{
[CmdletBinding()]
param($projectItems, $filter, [switch]$recurse=$false)
foreach ($item in $projectItems) {
if($(. $filter $item))
{
$item
}else{
if(($item.ProjectItems -ne $null) -and $recurse){
Search-ItemsRecursive -projectItems $item.ProjectItems -filter $filter -recurse:$recurse
}
}
}
}
function Find-OrphantFilesInSolution([switch]$IgnoreObj=$true){
Get-Project -All | Find-OrphantFilesInProject -IgnoreObj:$IgnoreObj
}
function Find-OrphantFilesInProject{
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)]$Projects, [switch]$IgnoreObj=$true)
process{
$toProcess = if($Projects){$Projects}else{Get-Project}
foreach($p in $toProcess){
$projectPath = Split-Path $p.FullName -Parent
$csFilesInProject = Search-ItemsRecursive -projectItems $p.ProjectItems -filter {param($item)$item.Name -like "*.cs"} -recurse |% {$_.Properties.Item("FullPath").Value}
dir $projectPath -Filter "*.cs" -Recurse |? { $csFilesInProject -notcontains $_.FullName } |? {(-not $IgnoreObj) -or ($_.Directory -notlike "*\obj*")} |select FullName
}
}
}
Export-ModuleMember -Function Find-OrphantFilesInSolution, Find-OrphantFilesInProject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment