|
| 1 | +# --------------------------- Configuration --------------------------- # |
| 2 | + |
| 3 | +# Define exclusions |
| 4 | +$excludedDirs = @('.vscode', '.git', '.archive', '.Promo') # Added '.Promo' to exclude this folder |
| 5 | +$excludedFiles = @('.gitignore', 'CI-CD.ps1') # Files to exclude |
| 6 | + |
| 7 | +# Define the source directory as the current working directory |
| 8 | +$source = Get-Location |
| 9 | + |
| 10 | +# Define the parent directory (one level up from the current directory) |
| 11 | +$parent = Split-Path $source -Parent |
| 12 | + |
| 13 | +# Define the target directory path |
| 14 | +$targetFolderName = "ChatGPT Quick Buttons For Your Text Release" |
| 15 | +$destination = Join-Path $parent $targetFolderName |
| 16 | + |
| 17 | +# Define the ZIP file path |
| 18 | +$zipFileName = "$targetFolderName.zip" |
| 19 | +$zipPath = Join-Path $parent $zipFileName |
| 20 | + |
| 21 | +# Initialize an array to track files that failed to copy |
| 22 | +$failedCopies = @() |
| 23 | + |
| 24 | +# --------------------------- Functions --------------------------- # |
| 25 | + |
| 26 | +<# |
| 27 | +.SYNOPSIS |
| 28 | + Determines whether a given file or directory should be excluded based on predefined exclusion lists. |
| 29 | +
|
| 30 | +.DESCRIPTION |
| 31 | + The Test-ShouldExclude function checks if the provided FileSystemInfo object matches any of the excluded directories or files. |
| 32 | + It performs case-insensitive comparisons and ensures that excluded directories and their subdirectories are appropriately handled. |
| 33 | +
|
| 34 | +.PARAMETER Item |
| 35 | + The FileSystemInfo object representing the file or directory to be evaluated. |
| 36 | +
|
| 37 | +.EXAMPLE |
| 38 | + Test-ShouldExclude -Item (Get-Item "C:\Projects\MyApp\.git") |
| 39 | +
|
| 40 | + Returns $true if the item is an excluded directory or file; otherwise, returns $false. |
| 41 | +#> |
| 42 | +function Test-ShouldExclude { |
| 43 | + param ( |
| 44 | + [System.IO.FileSystemInfo]$Item |
| 45 | + ) |
| 46 | + |
| 47 | + # Normalize paths for case-insensitive comparison |
| 48 | + $itemPathLower = $Item.FullName.ToLower() |
| 49 | + $sourcePathLower = $source.Path.ToLower() |
| 50 | + |
| 51 | + # Check if the item is within any of the excluded directories |
| 52 | + foreach ($exDir in $excludedDirs) { |
| 53 | + $exDirLower = $exDir.ToLower() |
| 54 | + |
| 55 | + |
| 56 | + if ($Item.PSIsContainer) { |
| 57 | + # If the item is a directory, check for exact match or if it's a subdirectory |
| 58 | + $excludedDirFullPath = (Join-Path $sourcePathLower $exDirLower).ToLower() |
| 59 | + if ($itemPathLower -eq $excludedDirFullPath -or |
| 60 | + $itemPathLower -like "*\$exDirLower\*") { |
| 61 | + Write-Verbose "Excluding directory: $($Item.FullName)" |
| 62 | + return $true |
| 63 | + } |
| 64 | + } |
| 65 | + else { |
| 66 | + # If the item is a file, check if its parent directory is excluded |
| 67 | + $parentDirLower = $Item.DirectoryName.ToLower() |
| 68 | + $excludedDirFullPath = (Join-Path $sourcePathLower $exDirLower).ToLower() |
| 69 | + if ($parentDirLower -eq $excludedDirFullPath -or |
| 70 | + $parentDirLower -like "*\$exDirLower\*") { |
| 71 | + Write-Verbose "Excluding file within excluded directory: $($Item.FullName)" |
| 72 | + return $true |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Check if the item is an excluded file |
| 78 | + if (-not $Item.PSIsContainer -and $excludedFiles -contains $Item.Name) { |
| 79 | + Write-Verbose "Excluding file: $($Item.FullName)" |
| 80 | + return $true |
| 81 | + } |
| 82 | + |
| 83 | + return $false |
| 84 | +} |
| 85 | + |
| 86 | +# --------------------------- Main Script --------------------------- # |
| 87 | + |
| 88 | +try { |
| 89 | + # ------------------- Purge Existing Destination ------------------- # |
| 90 | + if (Test-Path -Path $destination) { |
| 91 | + Write-Host "Purging existing destination directory: $destination" |
| 92 | + try { |
| 93 | + Remove-Item -Path $destination -Recurse -Force -ErrorAction Stop |
| 94 | + Write-Host "Successfully purged the destination directory." |
| 95 | + } |
| 96 | + catch { |
| 97 | + Write-Error "Failed to purge the destination directory: $destination" |
| 98 | + throw $_ # Exit the script if purge fails |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + # Create the destination directory |
| 103 | + Write-Host "Creating destination directory: $destination" |
| 104 | + New-Item -ItemType Directory -Path $destination -Force | Out-Null |
| 105 | + |
| 106 | + # Retrieve all items from the source, excluding specified folders and files |
| 107 | + Write-Host "Retrieving items to copy..." |
| 108 | + $itemsToCopy = Get-ChildItem -Path $source -Recurse -Force | Where-Object { |
| 109 | + -not (Test-ShouldExclude -Item $_) |
| 110 | + } |
| 111 | + |
| 112 | + # Optional: Output the items to be copied for verification |
| 113 | + Write-Host "Items to be copied:" |
| 114 | + foreach ($item in $itemsToCopy) { |
| 115 | + Write-Host " - $($item.FullName)" |
| 116 | + } |
| 117 | + |
| 118 | + # Copy each item to the destination |
| 119 | + Write-Host "Starting copy process..." |
| 120 | + foreach ($item in $itemsToCopy) { |
| 121 | + # Determine the destination path |
| 122 | + $relativePath = $item.FullName.Substring($source.Path.Length).TrimStart("\") |
| 123 | + $destPath = Join-Path $destination $relativePath |
| 124 | + |
| 125 | + if ($item.PSIsContainer) { |
| 126 | + # It's a directory; ensure it exists |
| 127 | + if (-not (Test-Path -Path $destPath)) { |
| 128 | + try { |
| 129 | + New-Item -ItemType Directory -Path $destPath -Force | Out-Null |
| 130 | + Write-Verbose "Created directory: $destPath" |
| 131 | + } |
| 132 | + catch { |
| 133 | + Write-Warning "Failed to create directory: $destPath" |
| 134 | + $failedCopies += $destPath |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + else { |
| 139 | + # It's a file; copy and overwrite if necessary |
| 140 | + $destDir = Split-Path $destPath |
| 141 | + if (-not (Test-Path -Path $destDir)) { |
| 142 | + try { |
| 143 | + New-Item -ItemType Directory -Path $destDir -Force | Out-Null |
| 144 | + Write-Verbose "Created directory for file: $destDir" |
| 145 | + } |
| 146 | + catch { |
| 147 | + Write-Warning "Failed to create directory for file: $destDir" |
| 148 | + $failedCopies += $destDir |
| 149 | + continue |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + try { |
| 154 | + Copy-Item -Path $item.FullName -Destination $destPath -Force -ErrorAction Stop |
| 155 | + Write-Verbose "Copied file: $($item.FullName) to $destPath" |
| 156 | + } |
| 157 | + catch { |
| 158 | + Write-Warning "Failed to copy file: $($item.FullName) to $destPath" |
| 159 | + $failedCopies += $item.FullName |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + # Report any failed copy operations |
| 165 | + if ($failedCopies.Count -gt 0) { |
| 166 | + Write-Host "`nCopy Process Completed with Errors." |
| 167 | + Write-Host "The following items were not copied successfully:" |
| 168 | + $failedCopies | ForEach-Object { Write-Host " - $_" } |
| 169 | + } |
| 170 | + else { |
| 171 | + Write-Host "`nAll files copied successfully." |
| 172 | + } |
| 173 | + |
| 174 | + # Compress the target directory into a ZIP file |
| 175 | + Write-Host "`nCreating ZIP archive..." |
| 176 | + Compress-Archive -Path "$destination\*" -DestinationPath $zipPath -Force |
| 177 | + |
| 178 | + Write-Host "ZIP archive created at: $zipPath" |
| 179 | + |
| 180 | +} |
| 181 | +catch { |
| 182 | + Write-Error "An unexpected error occurred: $_" |
| 183 | + exit 1 # Exit the script with an error code |
| 184 | +} |
| 185 | + |
| 186 | +# --------------------------- Exit Prompt --------------------------- # |
| 187 | + |
| 188 | +Write-Host "`nProcess completed. Press any key to exit." |
| 189 | + |
| 190 | +# Exiting in 5 seconds: |
| 191 | +Write-Host "Exiting in 5 seconds..." |
| 192 | +Start-Sleep -Seconds 5 |
0 commit comments