-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packager.ps1
78 lines (65 loc) · 2.35 KB
/
Packager.ps1
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
param(
[string]$scriptName
)
$whiteListedEntries = @("Packager.ps1", "Releases", "*.txt", ".gitignore", "logs", ".vscode")
$releaseBasePath = "Releases"
$releasePath = Join-Path -Path $releaseBasePath -ChildPath $scriptName
$assetsPath = Join-Path -Path $releaseBasePath -ChildPath "assets"
# Remove existing release directory if it exists
Remove-Item -Force $releasePath -Recurse -ErrorAction SilentlyContinue
# Ensure the Releases directory exists
if (-not (Test-Path -Path $releaseBasePath)) {
New-Item -ItemType Directory -Path $releaseBasePath | Out-Null
}
# Ensure the assets directory exists
if (-not (Test-Path -Path $assetsPath)) {
New-Item -ItemType Directory -Path $assetsPath | Out-Null
}
# Get all top-level items from the current directory, excluding the Releases directory
$items = Get-ChildItem -Path . | Where-Object {
$_.FullName -notmatch "^\.\\Releases(\\|$)"
}
# Create a hashtable for quick whitelist lookup
$whitelistHash = @{}
foreach ($whitelist in $whiteListedEntries) {
$whitelistHash[$whitelist] = $true
}
# Create a hashtable to store asset files and directories for quick lookup
$assetItems = @{}
Get-ChildItem -Path $assetsPath -Recurse | ForEach-Object {
$assetItems[$_.Name] = $_.FullName
}
# Filter and replace items efficiently
$filteredItems = @()
foreach ($item in $items) {
$itemName = $item.Name
# Check for whitelist
$isWhitelisted = $false
foreach ($key in $whitelistHash.Keys) {
if ($itemName -like $key) {
$isWhitelisted = $true
break
}
}
if (-not $isWhitelisted) {
if ($assetItems.ContainsKey($itemName)) {
$filteredItems += Get-Item -Path $assetItems[$itemName]
} else {
$filteredItems += $item
}
}
}
# Create the release directory named after the script
if (-not (Test-Path -Path $releasePath)) {
New-Item -ItemType Directory -Path $releasePath | Out-Null
}
# Copy the filtered items to the release directory
foreach ($item in $filteredItems) {
$destinationPath = Join-Path -Path $releasePath -ChildPath $item.Name
if ($item.PSIsContainer) {
Copy-Item -Path $item.FullName -Destination $destinationPath -Recurse -Force
} else {
Copy-Item -Path $item.FullName -Destination $destinationPath -Force
}
}
Write-Output "Files and directories have been copied to $releasePath"