-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUpdateExternalDeps.ps1
More file actions
163 lines (138 loc) · 6.56 KB
/
UpdateExternalDeps.ps1
File metadata and controls
163 lines (138 loc) · 6.56 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
# Download and extract external dependencies if they don't exist
$ErrorActionPreference = "Stop"
# Define dependencies
$dependencies = @(
)
# Ensure external directory exists
$externalDir = Join-Path -Path $PSScriptRoot -ChildPath "external"
if (-not (Test-Path $externalDir)) {
Write-Host "Creating external directory..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path $externalDir | Out-Null
}
# Process each dependency
foreach ($dep in $dependencies) {
$targetPath = Join-Path $externalDir $dep.Name
if (Test-Path $targetPath) {
Write-Host " $($dep.Name) already exists, skipping..." -ForegroundColor Green
continue
}
Write-Host "Downloading $($dep.Name)..." -ForegroundColor Cyan
$archiveName = Split-Path $dep.Url -Leaf
$archivePath = Join-Path $externalDir $archiveName
try {
# Download the file
Invoke-WebRequest -Uri $dep.Url -OutFile $archivePath -UseBasicParsing
Write-Host " Downloaded to $archivePath" -ForegroundColor Gray
# Extract based on archive type
Write-Host "Extracting $archiveName..." -ForegroundColor Cyan
if ($dep.ArchiveType -eq "zip") {
# Extract ZIP file
Expand-Archive -Path $archivePath -DestinationPath $externalDir -Force
Write-Host " Extracted $($dep.Name)" -ForegroundColor Green
}
elseif ($dep.ArchiveType -eq "tar.bz2") {
# Extract tar.bz2 file using tar (available in Windows 10+)
$originalLocation = Get-Location
Set-Location $externalDir
tar -xjf $archivePath
Set-Location $originalLocation
Write-Host " Extracted $($dep.Name)" -ForegroundColor Green
}
# Clean up archive file
Remove-Item $archivePath -Force
Write-Host " Cleaned up archive file" -ForegroundColor Gray
}
catch {
Write-Host " Error processing $($dep.Name): $_" -ForegroundColor Red
if (Test-Path $archivePath) {
Remove-Item $archivePath -Force -ErrorAction SilentlyContinue
}
throw
}
}
# =============================================================================
# Git Submodule Setup
# =============================================================================
# Handles first-time submodule init after pull, standalone clones that need
# converting, and stale .git/modules cache entries.
Write-Host "`nChecking git submodules..." -ForegroundColor Cyan
$gitmodulesPath = Join-Path $PSScriptRoot ".gitmodules"
if (Test-Path $gitmodulesPath) {
$gitmodulesContent = Get-Content $gitmodulesPath -Raw
# Parse [submodule] blocks for path and name
$submoduleBlocks = [regex]::Matches($gitmodulesContent,
'(?ms)\[submodule\s+"([^"]+)"\].*?path\s*=\s*(\S+)')
foreach ($block in $submoduleBlocks) {
$subName = $block.Groups[1].Value
$subPath = $block.Groups[2].Value
$fullPath = Join-Path $PSScriptRoot $subPath
$gitEntry = Join-Path $fullPath ".git"
Write-Host " Submodule: $subPath" -ForegroundColor Gray
if (-not (Test-Path $fullPath)) {
Write-Host " Not yet created, will initialize." -ForegroundColor Yellow
}
elseif (Test-Path $gitEntry -PathType Leaf) {
# .git is a file pointing to the parent repo's modules dir -- correct
Write-Host " OK (proper submodule reference)" -ForegroundColor Green
# Clean untracked files and reset modifications so checkout can succeed
Write-Host " Cleaning working tree..." -ForegroundColor Gray
$savedEAP = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
git -C $fullPath clean -ffd
git -C $fullPath checkout .
$ErrorActionPreference = $savedEAP
}
elseif (Test-Path $gitEntry -PathType Container) {
# .git is a directory -- this is a standalone clone, not a submodule
Write-Host " Standalone git clone detected at submodule path." -ForegroundColor Yellow
Write-Host " Removing so it can be re-initialized as a submodule..." -ForegroundColor Yellow
Remove-Item -Recurse -Force $fullPath
}
elseif ((Get-ChildItem $fullPath -Force | Measure-Object).Count -gt 0) {
# Directory has content but no .git at all
Write-Host " Non-empty directory blocking submodule init." -ForegroundColor Yellow
Write-Host " Removing so it can be re-initialized as a submodule..." -ForegroundColor Yellow
Remove-Item -Recurse -Force $fullPath
}
else {
Write-Host " Empty directory, will be populated during init." -ForegroundColor Yellow
}
# Check for stale .git/modules cache that can block re-init
$moduleCachePath = Join-Path -Path $PSScriptRoot -ChildPath ".git\modules\$subName"
if ((Test-Path $moduleCachePath) -and -not (Test-Path $gitEntry -PathType Leaf)) {
Write-Host " Clearing stale .git/modules/$subName cache..." -ForegroundColor Yellow
Remove-Item -Recurse -Force $moduleCachePath
}
}
}
try {
Write-Host "`nInitializing and updating submodules..." -ForegroundColor Cyan
git submodule update --init --recursive
if ($LASTEXITCODE -ne 0) {
throw "git submodule update --init --recursive failed (exit code $LASTEXITCODE)"
}
Write-Host " Git submodules updated successfully." -ForegroundColor Green
# Verify final state
$subStatus = git submodule status
foreach ($line in $subStatus) {
$line = $line.Trim()
if ($line -match '^\-') {
Write-Host " WARN $line" -ForegroundColor Yellow
}
elseif ($line -match '^\+') {
Write-Host " NOTE $line (commit differs from index)" -ForegroundColor Yellow
}
else {
Write-Host " OK $line" -ForegroundColor Green
}
}
}
catch {
Write-Host " Error updating git submodules: $_" -ForegroundColor Red
throw
}
Write-Host "`nSubmodules and external dependencies are ready!" -ForegroundColor Green
$ultraLightArchive = "ultralight-free-sdk-1.4.1-dev-win-x64.7z"
if (-not (Test-Path (Join-Path $externalDir $ultraLightArchive))) {
Write-Host "`n $ultraLightArchive not found in $externalDir. Download it from https://ultralig.ht/download" -ForegroundColor Yellow
}