-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonFunctions_Update.ps1
More file actions
123 lines (111 loc) · 4.37 KB
/
CommonFunctions_Update.ps1
File metadata and controls
123 lines (111 loc) · 4.37 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
<#
.SYNOPSIS
Compares a local CommonFunctions.au3 against the GitHub master version and lets you decide what to do.
.PARAMETER Path
Path to the local CommonFunctions.au3 file or a directory to search. Defaults to the current directory.
.PARAMETER Recurse
Recursively search the provided path for all CommonFunctions.au3 files and process each one.
#>
param(
[string]$Path,
[switch]$Recurse
)
$GitHubRawUrl = "https://raw.githubusercontent.com/jmclaren7/autoit-scripts/master/CommonFunctions.au3"
# Download the GitHub version to a temp file once
$tempFile = Join-Path $env:TEMP "CommonFunctions_github.au3"
try {
Write-Host "Downloading latest version from GitHub..." -ForegroundColor Cyan
Invoke-WebRequest -Uri $GitHubRawUrl -OutFile $tempFile -UseBasicParsing
} catch {
Write-Host "Failed to download from GitHub: $_" -ForegroundColor Red
exit 1
}
$githubContent = Get-Content $tempFile
function Compare-AndPrompt($FilePath) {
Write-Host "`n========================================" -ForegroundColor DarkGray
Write-Host "Local file: $FilePath" -ForegroundColor Cyan
$localContent = Get-Content $FilePath
$diff = Compare-Object $localContent $githubContent -IncludeEqual
$changes = $diff | Where-Object { $_.SideIndicator -ne "==" }
if ($changes.Count -eq 0) {
Write-Host " Identical. No update needed." -ForegroundColor Green
return
}
$localOnly = $changes | Where-Object { $_.SideIndicator -eq "<=" }
$githubOnly = $changes | Where-Object { $_.SideIndicator -eq "=>" }
Write-Host " Lines only in LOCAL: $($localOnly.Count)" -ForegroundColor Magenta
Write-Host " Lines only in GITHUB: $($githubOnly.Count)" -ForegroundColor Green
Write-Host ""
$maxLines = 40
$shown = 0
foreach ($line in $changes) {
if ($shown -ge $maxLines) {
Write-Host " ... ($($changes.Count - $maxLines) more lines, use VS Code diff to see all)" -ForegroundColor DarkGray
break
}
if ($line.SideIndicator -eq "<=") {
Write-Host " - $($line.InputObject)" -ForegroundColor Magenta
} else {
Write-Host " + $($line.InputObject)" -ForegroundColor Green
}
$shown++
}
Write-Host ""
do {
Write-Host " [D] Open diff in VS Code [R] Replace [B] Backup + Replace [S] Skip" -ForegroundColor Yellow
$choice = (Read-Host " Choice").ToUpper()
switch ($choice) {
"D" {
Write-Host " Opening diff in VS Code..." -ForegroundColor Cyan
code --diff $FilePath $tempFile
Write-Host ""
}
"R" {
Copy-Item $tempFile $FilePath -Force
Write-Host " Replaced with GitHub version." -ForegroundColor Green
}
"B" {
$backupPath = "$FilePath.bak"
Copy-Item $FilePath $backupPath -Force
Write-Host " Backup saved to: $backupPath" -ForegroundColor Cyan
Copy-Item $tempFile $FilePath -Force
Write-Host " Replaced with GitHub version." -ForegroundColor Green
}
default {
Write-Host " Skipped." -ForegroundColor Gray
}
}
} while ($choice -eq "D")
}
# Build the list of files to process
if ($Recurse) {
if (-not $Path) { $Path = $PWD }
if (-not (Test-Path $Path)) {
Write-Host "Path not found: $Path" -ForegroundColor Red
Remove-Item $tempFile -Force
exit 1
}
$files = Get-ChildItem -Path $Path -Filter "CommonFunctions.au3" -Recurse -File
if ($files.Count -eq 0) {
Write-Host "No CommonFunctions.au3 files found under: $Path" -ForegroundColor Red
Remove-Item $tempFile -Force
exit 1
}
Write-Host "Found $($files.Count) CommonFunctions.au3 file(s) under: $Path" -ForegroundColor Cyan
foreach ($file in $files) {
Compare-AndPrompt $file.FullName
}
} else {
if (-not $Path) {
$Path = Join-Path $PWD "CommonFunctions.au3"
}
$Path = Resolve-Path $Path -ErrorAction SilentlyContinue
if (-not $Path -or -not (Test-Path $Path)) {
Write-Host "CommonFunctions.au3 not found at: $Path" -ForegroundColor Red
Remove-Item $tempFile -Force
exit 1
}
Compare-AndPrompt $Path
}
Remove-Item $tempFile -Force
Write-Host "`nDone." -ForegroundColor Cyan