98 lines
3.3 KiB
PowerShell
98 lines
3.3 KiB
PowerShell
function Compare-Folders {
|
|
[CmdletBinding()]
|
|
[OutputType([string[]])]
|
|
param (
|
|
[switch]$FilenamesOnly,
|
|
[Parameter(Position = 0)]
|
|
[string]$CompareFrom,
|
|
[Parameter(Position = 1)]
|
|
[string]$CompareTo,
|
|
[int]$SurroundingLines = 2,
|
|
[switch]$OnlyNewFrom,
|
|
[switch]$OnlyNewTo,
|
|
[string[]]$ExcludedPaths = @("bin","obj","dist","packages","TestResults","Debug","Release")
|
|
)
|
|
|
|
$directorySeparator = [IO.Path]::DirectorySeparatorChar
|
|
if (Test-IsWindowsPlatform) {
|
|
$directorySeparator = [IO.Path]::DirectorySeparatorChar + [IO.Path]::DirectorySeparatorChar
|
|
}
|
|
<#
|
|
$excludedPathFull = @()
|
|
foreach ($path in $ExcludedPaths) {
|
|
$excludedPathFull = "{0}$path{0}" -f $directorySeparator
|
|
}
|
|
#>
|
|
|
|
$CompareFrom = Resolve-Path $CompareFrom
|
|
$CompareTo = Resolve-Path $CompareTo
|
|
|
|
$filesFrom = (Get-ChildItem -Path $CompareFrom -Recurse -File)
|
|
$filesTo = (Get-ChildItem -Path $CompareTo -Recurse -File)
|
|
|
|
if ($FilenamesOnly) {
|
|
return (Compare-Object $filesFrom $filesTo)
|
|
}
|
|
|
|
$pathsFrom = @()
|
|
$pathsTo = @()
|
|
|
|
# $filesFrom[22].FullName.Replace($CompareFrom,"")
|
|
# $filesFrom[22].FullName.Replace($CompareFrom,"") -split $directorySeparator
|
|
# return
|
|
|
|
foreach ($file in $filesFrom) {
|
|
$path = $file.FullName.Replace($CompareFrom,"")
|
|
$skipAdd = $false
|
|
[string[]]$split = ($path -split $directorySeparator)
|
|
if (([System.Linq.Enumerable]::Intersect($split,$ExcludedPaths)).Count -gt 0) {
|
|
$skipAdd = $true
|
|
}
|
|
if (!$skipAdd) {
|
|
$pathsFrom += $path
|
|
}
|
|
}
|
|
|
|
foreach ($file in $filesTo) {
|
|
$path = $file.FullName.Replace($CompareTo,"")
|
|
$skipAdd = $false
|
|
[string[]]$split = ($path -split $directorySeparator)
|
|
# if ($path.IndexOf($excludedPath) -ne -1) {
|
|
if (([System.Linq.Enumerable]::Intersect($split,$ExcludedPaths)).Count -gt 0) {
|
|
$skipAdd = $true
|
|
}
|
|
if (!$skipAdd) {
|
|
$pathsTo += $path
|
|
}
|
|
}
|
|
|
|
$resultLines = @()
|
|
$resultFiles = @()
|
|
|
|
if ($OnlyNewFrom) {
|
|
Compare-Object -ReferenceObject $pathsFrom -DifferenceObject $pathsTo | ? { $_.SideIndicator -eq "<="}
|
|
} elseif ($OnlyNewTo) {
|
|
Compare-Object -ReferenceObject $pathsFrom -DifferenceObject $pathsTo | ? { $_.SideIndicator -eq "=>"}
|
|
} else {
|
|
$paths = $pathsFrom + $pathsTo | Sort-Object -Unique
|
|
foreach ($file in $paths) {
|
|
$pathFromContains = $pathsFrom -contains $file
|
|
$pathToContains = $pathsTo -contains $file
|
|
$fromPath = (Join-Path $CompareFrom $file)
|
|
$toPath = (Join-Path $CompareTo $file)
|
|
if ($pathFromContains -and $pathToContains) {
|
|
if ((Get-Item -Path $fromPath).Length -ne (Get-Item -Path $toPath).Length) {
|
|
$lines = Compare-File -CompareFrom $fromPath -CompareTo $toPath -Porcelain -SurroundingLines $SurroundingLines
|
|
if ($lines.Count -gt 0) {
|
|
$resultLines += $lines
|
|
$resultLines += "`n`n"
|
|
}
|
|
}
|
|
} else {
|
|
$resultFiles += "File [$file] only present on $(if($pathFromContains) {$CompareFrom} else {$CompareTo})"
|
|
}
|
|
}
|
|
$resultFiles
|
|
$resultLines
|
|
}
|
|
} |