-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCI-CD.ps1
More file actions
398 lines (350 loc) · 15.8 KB
/
CI-CD.ps1
File metadata and controls
398 lines (350 loc) · 15.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# CI-CD.ps1
# --------------------------- Configuration --------------------------- #
# Define exclusions
$excludedDirs = @('Promo') # Additional directories to exclude
$excludedFiles = @('.gitignore', 'CI-CD.ps1', '.aider.chat.history.md', '.aider.input.history', 'notes.txt', 'instructions.txt', 'Full_logo.png', 'prompt-update.md') # Files to exclude
# Define the source directory as the current working directory
$source = Get-Location
# Initialize array for gitignore patterns
$gitignorePatterns = @()
# Check if .gitignore exists and parse it
$gitignorePath = Join-Path $source ".gitignore"
if (Test-Path $gitignorePath) {
Write-Host "Found .gitignore file, parsing patterns..."
try {
$gitignoreContent = Get-Content $gitignorePath -ErrorAction Stop
foreach ($line in $gitignoreContent) {
# Skip empty lines and comments
if (-not [string]::IsNullOrWhiteSpace($line) -and -not $line.StartsWith('#')) {
# Clean up the pattern (trim spaces, etc.)
$pattern = $line.Trim()
# Add the pattern to our list
$gitignorePatterns += $pattern
Write-Verbose "Added gitignore pattern: $pattern"
}
}
Write-Host "Parsed $($gitignorePatterns.Count) patterns from .gitignore"
}
catch {
Write-Warning "Error reading .gitignore file: $_"
# Continue with empty gitignore patterns
}
}
# Define the parent directory (one level up from the source)
$parent = Split-Path $source -Parent
# Define the target directory path
$targetFolderName = "OneClickPrompts Release"
$destination = Join-Path $parent $targetFolderName
# Define the ZIP file path
$zipFileName = "$targetFolderName.zip"
$zipPath = Join-Path $parent $zipFileName
# Initialize an array to track items that failed to copy
$failedCopies = @()
# --------------------------- Functions --------------------------- #
<#
.SYNOPSIS
Determines whether a given file or directory should be excluded.
.DESCRIPTION
The Test-ShouldExclude function calculates the relative path of the given FileSystemInfo object
from the source directory and splits it into parts. If any part of the path (i.e., a directory name)
starts with a dot or matches an entry in $excludedDirs, the item is excluded.
Additionally, for files, it checks if the filename is in the $excludedFiles list or
if the file matches any pattern in the .gitignore file.
#>
function Test-ShouldExclude {
param (
[System.IO.FileSystemInfo]$Item
)
# Get the relative path from the source directory
$relativePath = $Item.FullName.Substring($source.Path.Length).TrimStart('\')
if ([string]::IsNullOrWhiteSpace($relativePath)) {
return $false
}
# Split the path into parts
$pathParts = $relativePath -split '\\'
foreach ($part in $pathParts) {
# Exclude if any part (directory) starts with a dot
if ($part.StartsWith('.')) {
Write-Verbose "Excluding due to dot-starting folder: $($Item.FullName)"
return $true
}
# Exclude if the directory name matches one of the excluded directories
if ($excludedDirs -contains $part) {
Write-Verbose "Excluding due to matched excluded directory: $($Item.FullName)"
return $true
}
}
# If the item is a file and its name is in the list of excluded files, exclude it
if (-not $Item.PSIsContainer) {
# Exclude files in the excluded files list
if ($excludedFiles -contains $Item.Name) {
Write-Verbose "Excluding file: $($Item.FullName)"
return $true
}
# Exclude files that start with a dot
if ($Item.Name.StartsWith('.')) {
Write-Verbose "Excluding file with dot prefix: $($Item.FullName)"
return $true
}
}
# Check against gitignore patterns if we have any
if ($gitignorePatterns.Count -gt 0) {
# Convert backslashes to forward slashes for gitignore pattern matching
$gitignorePath = $relativePath.Replace('\', '/')
foreach ($pattern in $gitignorePatterns) {
# Skip empty patterns
if ([string]::IsNullOrWhiteSpace($pattern)) {
continue
}
# Handle directory-specific patterns (ending with /)
$isDirPattern = $pattern.EndsWith('/')
if ($isDirPattern -and -not $Item.PSIsContainer) {
continue # Skip directory patterns for files
}
# Remove trailing slash for comparison
$cleanPattern = $pattern.TrimEnd('/')
# Handle different pattern types
if ($cleanPattern.StartsWith('**/')) {
# Pattern like "**/foo" matches "foo" anywhere in the path
$matchPart = $cleanPattern.Substring(3)
if ($gitignorePath -like "*/$matchPart*" -or $gitignorePath -eq $matchPart) {
Write-Verbose "Excluding due to gitignore pattern (subfolder anywhere): $cleanPattern -> $($Item.FullName)"
return $true
}
}
elseif ($cleanPattern.EndsWith('/**')) {
# Pattern like "foo/**" matches everything inside "foo" directory
$matchPart = $cleanPattern.Substring(0, $cleanPattern.Length - 3)
if ($gitignorePath.StartsWith("$matchPart/")) {
Write-Verbose "Excluding due to gitignore pattern (all inside): $cleanPattern -> $($Item.FullName)"
return $true
}
}
elseif ($cleanPattern.Contains('*')) {
# Handle wildcards
if ($gitignorePath -like $cleanPattern) {
Write-Verbose "Excluding due to gitignore wildcard pattern: $cleanPattern -> $($Item.FullName)"
return $true
}
}
else {
# Direct match or directory path
if ($gitignorePath -eq $cleanPattern -or $gitignorePath.StartsWith("$cleanPattern/")) {
Write-Verbose "Excluding due to gitignore exact pattern: $cleanPattern -> $($Item.FullName)"
return $true
}
}
}
}
return $false
}
# --------------------------- Main Script --------------------------- #
try {
# ------------------- Interaction Prompt ------------------- #
Write-Host "--------------------------------------------------------" -ForegroundColor Cyan
Write-Host "Select mode:" -ForegroundColor Cyan
Write-Host " [ENTER] Automatic Version Increase + Git Commit" -ForegroundColor Green
Write-Host " [c] Increase Version ONLY (No Commit)" -ForegroundColor Yellow
Write-Host " [v] Proceed with NO changes (Build only)" -ForegroundColor Gray
Write-Host "--------------------------------------------------------" -ForegroundColor Cyan
$doVersionIncrease = $false
$doCommit = $false
while ($true) {
$keyInfo = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($keyInfo.VirtualKeyCode -eq 13) {
# Enter
$doVersionIncrease = $true
$doCommit = $true
Write-Host "Selected: Version Increase + Git Commit" -ForegroundColor Green
break
}
elseif ($keyInfo.Character -eq 'c') {
$doVersionIncrease = $true
Write-Host "Selected: Version Increase ONLY" -ForegroundColor Yellow
break
}
elseif ($keyInfo.Character -eq 'v') {
Write-Host "Selected: No Changes (Build Only)" -ForegroundColor Gray
break
}
}
# ------------------- Version Management ------------------- #
$manifestPath = Join-Path $source "manifest.json"
$popupPath = Join-Path $source "popup.html"
$newVersion = $null
if ($doVersionIncrease) {
Write-Host "Proceeding with version increase..."
if (Test-Path $manifestPath) {
$manifestContent = Get-Content $manifestPath -Raw
# Regex for "version": "X.X.X.X"
$versionPattern = '"version":\s*"(\d+\.\d+\.\d+\.\d+)"'
if ($manifestContent -match $versionPattern) {
$currentVersion = $matches[1]
Write-Host "Current Version: $currentVersion"
$parts = $currentVersion.Split('.') | ForEach-Object { [int]$_ }
if ($parts.Count -eq 4) {
$parts[3]++
# Handle decimal carry-over (e.g. 0.0.0.9 -> 0.0.1.0)
for ($i = 3; $i -gt 0; $i--) {
if ($parts[$i] -gt 9) {
$parts[$i] = 0
$parts[$i - 1]++
}
}
$newVersion = $parts -join '.'
# Update Manifest
$manifestContent = $manifestContent -replace $versionPattern, "`"version`": `"$newVersion`""
$manifestContent | Set-Content $manifestPath -NoNewline
Write-Host "Updated manifest.json to $newVersion"
# Update popup.html
if (Test-Path $popupPath) {
$popupContent = Get-Content $popupPath -Raw
# Escape dots for regex matching of the literal version string
$escapedCurrentVersion = $currentVersion.Replace('.', '\.')
# Match "Version X.X.X.X"
$popupVersionPattern = "Version\s+$escapedCurrentVersion"
if ($popupContent -match $popupVersionPattern) {
$popupContent = $popupContent -replace $popupVersionPattern, "Version $newVersion"
$popupContent | Set-Content $popupPath -NoNewline
Write-Host "Updated popup.html to $newVersion"
}
else {
Write-Host "CRITICAL ERROR: Could not find 'Version $currentVersion' in popup.html" -ForegroundColor Red -BackgroundColor Black
}
}
else {
Write-Host "CRITICAL ERROR: popup.html not found at $popupPath" -ForegroundColor Red -BackgroundColor Black
}
}
else {
Write-Host "CRITICAL ERROR: Version format in manifest is not X.X.X.X (Found: $currentVersion)" -ForegroundColor Red -BackgroundColor Black
}
}
else {
Write-Host "CRITICAL ERROR: Could not find version pattern in manifest.json" -ForegroundColor Red -BackgroundColor Black
}
}
else {
Write-Host "CRITICAL ERROR: manifest.json not found at $manifestPath" -ForegroundColor Red -BackgroundColor Black
}
}
Write-Host "Starting the copy and archive process..."
# ------------------- Purge Existing Destination Directory ------------------- #
if (Test-Path -Path $destination) {
Write-Host "Purging existing destination directory: $destination"
try {
Remove-Item -Path $destination -Recurse -Force -ErrorAction Stop
Write-Host "Successfully purged the destination directory."
}
catch {
Write-Error "Failed to purge the destination directory: $destination"
throw $_ # Exit the script if purge fails
}
}
# Create the destination directory
Write-Host "Creating destination directory: $destination"
New-Item -ItemType Directory -Path $destination -Force | Out-Null
# ------------------- Gather Items to Copy ------------------- #
Write-Host "Retrieving items to copy..."
$itemsToCopy = Get-ChildItem -Path $source -Recurse -Force | Where-Object {
-not (Test-ShouldExclude -Item $_)
}
# Report the found items to copy
Write-Host "Found $($itemsToCopy.Count) items to copy:"
foreach ($item in $itemsToCopy) {
Write-Host " - $($item.FullName)"
}
# ------------------- Copy Items ------------------- #
Write-Host "Starting copy process..."
foreach ($item in $itemsToCopy) {
# Determine the relative path and destination path for the current item
$relativePath = $item.FullName.Substring($source.Path.Length).TrimStart("\")
$destPath = Join-Path $destination $relativePath
if ($item.PSIsContainer) {
# For directories, ensure the destination directory exists
if (-not (Test-Path -Path $destPath)) {
try {
New-Item -ItemType Directory -Path $destPath -Force | Out-Null
Write-Host "Created directory: $destPath"
}
catch {
Write-Warning "Failed to create directory: $destPath"
$failedCopies += $destPath
}
}
}
else {
# For files, ensure the destination directory exists and copy the file
$destDir = Split-Path $destPath
if (-not (Test-Path -Path $destDir)) {
try {
New-Item -ItemType Directory -Path $destDir -Force | Out-Null
Write-Host "Created directory for file: $destDir"
}
catch {
Write-Warning "Failed to create directory for file: $destDir"
$failedCopies += $destDir
continue
}
}
try {
Copy-Item -Path $item.FullName -Destination $destPath -Force -ErrorAction Stop
Write-Host "Copied file: $($item.FullName) -> $destPath"
}
catch {
Write-Warning "Failed to copy file: $($item.FullName) -> $destPath"
$failedCopies += $item.FullName
}
}
}
# ------------------- Copy Errors Report ------------------- #
if ($failedCopies.Count -gt 0) {
Write-Host "`nCopy process completed with errors."
Write-Host "The following items were not copied successfully:"
foreach ($failed in $failedCopies) {
Write-Host " - $failed"
}
}
else {
Write-Host "`nAll items copied successfully."
}
# ------------------- Create ZIP Archive ------------------- #
Write-Host "`nCreating ZIP archive..."
Compress-Archive -Path "$destination\*" -DestinationPath $zipPath -Force
Write-Host "ZIP archive created at: $zipPath"
# ------------------- Git Commit ------------------- #
if ($doCommit -and $newVersion) {
$commitMsg = "Version $newVersion"
Write-Host "Committing changes with message: '$commitMsg'..."
# Execute Git commands
try {
git add .
if ($LASTEXITCODE -eq 0) {
git commit -m "$commitMsg"
if ($LASTEXITCODE -eq 0) {
Write-Host "Git commit successful." -ForegroundColor Green
}
else {
Write-Warning "Git commit failed (perhaps nothing to commit?)"
}
}
else {
Write-Error "Git add failed."
}
}
catch {
Write-Warning "Git command execution failed. Ensure git is installed and available."
}
}
elseif ($doCommit -and -not $newVersion) {
Write-Warning "Commit was requested but new version was not generated. Skipping commit."
}
}
catch {
Write-Error "An unexpected error occurred: $_"
exit 1 # Exit the script with an error code
}
# ------------------- Final Report and Exit ------------------- #
Write-Host "`nProcess completed."
Write-Host "Exiting in 2 seconds..."
Start-Sleep -Seconds 2