-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-maker.ps1
More file actions
162 lines (140 loc) · 5.4 KB
/
diff-maker.ps1
File metadata and controls
162 lines (140 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
param (
[Parameter(Mandatory)]
[String]$sourceFolder,
[Parameter(Mandatory)]
[String]$targetFolder,
[Parameter(Mandatory)]
[String]$outputFolder,
[Parameter(Mandatory=$false)]
[switch]$NoXDelta3,
[Parameter(Mandatory=$false)]
[switch]$NonInteractive
)
# Import functions
Get-ChildItem "$PSScriptRoot/functions/*.ps1" | ForEach-Object {
. $_.FullName
}
# Validate input parameters
$sourceFolder = (Resolve-Path $SourceFolder).ProviderPath
$targetFolder = (Resolve-Path $TargetFolder).ProviderPath
$outputFolder = (Resolve-Path $OutputFolder).ProviderPath
if (-not (Test-Path $sourceFolder -PathType Container)) {
throw "Source folder not found: $sourceFolder"
}
if (-not (Test-Path $targetFolder -PathType Container)) {
throw "Target folder not found: $targetFolder"
}
if (-not $NoXDelta3) {
if (-not (Test-Path ".\xdelta3.exe" -PathType Leaf)) {
throw "xdelta3 executable not found in script folder: .\xdelta3.exe"
}
}
# Init variables for scans
[string[]]$sourceFiles = @()
[string[]]$targetFiles = @()
# Init variables for results
[string[]]$addedFiles = @()
[string[]]$deletedFiles = @()
[string[]]$modifiedFiles = @()
$movedFiles = [System.Collections.Generic.Dictionary[string,string]]::new()
# Start timer
$startTime = Get-Date
# Scan files in both folders
Get-ChildItem -Path $sourceFolder -Recurse -File | ForEach-Object {
$filePath = Get-RelativePath $_.FullName $sourceFolder
$sourceFiles += $filePath
}
Get-ChildItem -Path $targetFolder -Recurse -File | ForEach-Object {
$filePath = Get-RelativePath $_.FullName $targetFolder
$targetFiles += $filePath
}
# Loop on targetFiles to find added/modified/unmodified files
foreach ($targetFile in $targetFiles) {
if ($sourceFiles -contains $targetFile) {
if (-not (Compare-Files "$sourceFolder\$targetFile" "$targetFolder\$targetFile")) {
$modifiedFiles += $targetFile
}
} else {
$addedFiles += $targetFile
}
}
# Loop on sourceFiles to find deleted files
foreach ($sourceFile in $sourceFiles) {
if (-not ($targetFiles -contains $sourceFile)) {
$deletedFiles += $sourceFile
}
}
# At this step, it is possible than some "addedFiles" or "deletedFiles" are actually "movedFiles"
# Let's calculate the hash of each "addedFiles" and "deletedFiles" to find "movedFiles", then clean the "addedFiles" and "deletedFiles" lists
$addedHashes = @{}
foreach ($addedFile in $addedFiles) {
$addedHash = (Get-FileHash "$targetFolder\$addedFile" -Algorithm SHA1).Hash
$addedHashes[$addedHash] = $addedFile
}
foreach ($deletedFile in $deletedFiles) {
$deletedHash = (Get-FileHash "$sourceFolder\$deletedFile" -Algorithm SHA1).Hash
if ($addedHashes.ContainsKey($deletedHash)) {
$addedFile = $addedHashes[$deletedHash]
Write-Output "Moved: $deletedFile => $addedFile"
$movedFiles[$deletedFile] = $addedFile
$deletedFiles = $deletedFiles | Where-Object { $_ -ne $deletedFile }
$addedFiles = $addedFiles | Where-Object { $_ -ne $addedFile }
}
}
# Output results
Write-Output "Added files:"
$addedFiles | ForEach-Object { Write-Output " $_" }
Write-Output "Deleted files:"
$deletedFiles | ForEach-Object { Write-Output " $_" }
Write-Output "Modified files:"
$modifiedFiles | ForEach-Object { Write-Output " $_" }
Write-Output "Moved files:"
$movedFiles.GetEnumerator() | ForEach-Object { Write-Output " $($_.Key) => $($_.Value)" }
# End timer and output duration
$endTime = Get-Date
$duration = $endTime - $startTime
Write-Output "Analysis duration: $($duration.TotalSeconds) seconds"
if (-not $NonInteractive) {
Read-Host -Prompt 'Press Enter to continue to output folder'
}
foreach ($addedFile in $addedFiles) {
$targetPath = "$targetFolder\$addedFile"
$outputPath = "$outputFolder\$addedFile"
New-Item -ItemType Directory -Path (Split-Path $outputPath) -Force | Out-Null
Copy-Item -Path $targetPath -Destination $outputPath
Write-Output "[NEW_COPY] $addedFile"
}
foreach ($modifiedFile in $modifiedFiles) {
$targetPath = "$targetFolder\$modifiedFile"
$outputPath = "$outputFolder\$modifiedFile"
New-Item -ItemType Directory -Path (Split-Path $outputPath) -Force | Out-Null
if ($NoXDelta3 -or -not (Test-IsBinaryFile "$targetFolder\$modifiedFile")) {
# If xdelta3 is disabled or the file is a text file (not binary), copy it
Write-Output "[MOD_COPY] $modifiedFile"
Copy-Item -Path $targetPath -Destination $outputPath
} else {
# If xdelta3 is enabled and the file is binary, generate the xdelta patch
Write-Output "[MOD_XDT3] $modifiedFile"
$sourcePath = "$sourceFolder\$modifiedFile"
$outputPath += ".xdelta"
Start-XDelta3 -SourcePath $sourcePath -TargetPath $targetPath -OutputPath $outputPath
}
}
if ($deletedFiles.Count -eq 0 -and $movedFiles.Count -eq 0) {
Write-Output "No deleted or moved files, exiting early."
Exit
}
$nsisFilePath = "$outputFolder\@nsis.txt"
if (Test-Path $nsisFilePath) {
# Clear file if already exists
Clear-Content $nsisFilePath
} else {
# Or create empty file
New-Item -Path $nsisFilePath -ItemType File | Out-Null
}
foreach ($movedFile in $movedFiles.GetEnumerator()) {
Add-Content -Path $nsisFilePath -Value "Rename ""$($movedFile.Key)"" ""$($movedFile.Value)"""
}
foreach ($deletedFile in $deletedFiles | Sort-Object) {
Add-Content -Path $nsisFilePath -Value "Delete ""$deletedFile"""
}