-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevi.ps1
595 lines (563 loc) · 20.9 KB
/
devi.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env pwsh
# New devi install engine
Function MakeDirReturnFatat {
param(
[String]$Dir
)
if (!(Test-Path $Dir)) {
try {
New-Item -ItemType Directory -Force $Dir | Out-Null
}
catch {
Write-Host -ForegroundColor Red "unable create dir $Dir failed: $_"
exit 1
}
}
}
Function InitializeWebProxy {
$proxyobj = Get-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if ($proxyobj.ProxyEnable -ne 0 -and ![System.String]::IsNullOrEmpty($proxyobj.ProxyServer)) {
[environment]::SetEnvironmentVariable("HTTPS_PROXY", $proxyobj.ProxyServer)
[environment]::SetEnvironmentVariable("HTTP_PROXY", $proxyobj.ProxyServer)
}
elseif (![system.string]::IsNullOrEmpty($proxyobj.AutoConfigURL)) {
#$uri = [uri]$proxyobj.AutoConfigURL
[environment]::SetEnvironmentVariable("HTTPS_PROXY", $proxyobj.AutoConfigURL)
[environment]::SetEnvironmentVariable("HTTP_PROXY", $proxyobj.AutoConfigURL)
}
}
## global values
."$PSScriptRoot\PreInitialize.ps1"
Import-Module -Name "$ClangbuilderRoot\modules\Devi" # Package Manager
Import-Module -Name "$ClangbuilderRoot\modules\Utils"
# pre initialize
PreInitializeEnv
$LinkedDir = "$ClangbuilderRoot\bin\pkgs\.linked"
MakeDirReturnFatat -Dir $LinkedDir
$LockDir = "$ClangbuilderRoot\bin\pkgs\.locks"
MakeDirReturnFatat -Dir $LockDir
$CleanDir = "$ClangbuilderRoot\bin\pkgs\.temp"
MakeDirReturnFatat -Dir $CleanDir
$SzExe = "$ClangbuilderRoot\bin\pkgs\7z\7z.exe"
# We use installed curl if exists or system curl (Windows 10 >17063)
$curlExe = "$ClangbuilderRoot\bin\pkgs\curl\bin\curl.exe"
$deviUA = "Wget/5.0 (MSVC devi)" # TO Set UA as wget.
$IsWindows64 = [System.Environment]::Is64BitOperatingSystem
$curlCommand = Get-Command -CommandType Application curl -ErrorAction SilentlyContinue
if ($null -ne $curlCommand) {
$curlExeFallback = $curlCommand[0].Source
InitializeWebProxy
}
$MutexName = "Clangbuilder.devi.lock"
Function WinGet {
param(
[String]$URL,
[String]$OutFile
)
$TlsArg = "--proto-redir =https"
if (!$URL.StartsWith("https://")) {
$TlsArg = ""
}
$curlargv = "-A `"$deviUA`" --progress-bar -fS --connect-timeout 15 --retry 3 -o `"$OutFile`" -L $TlsArg $URL"
if (Test-Path $curlExe) {
Write-Host "devdownload (curl-devi): $URL"
$ex = ProcessExec -FilePath $curlExe -Argv $curlargv -WD $PWD
if ($ex -ne 0) {
Remove-Item -Force $OutFile -ErrorAction SilentlyContinue
return $false
}
return $true
}
elseif ($null -ne $curlExeFallback) {
Write-Host "devdownload (curl-fallback): $URL"
$ex = ProcessExec -FilePath $curlExeFallback -Argv $curlargv -WD $PWD
if ($ex -ne 0) {
Remove-Item -Force $OutFile -ErrorAction SilentlyContinue
return $false
}
return $true
}
Write-Host "devdownload (pwsh wget): $URL ..."
#$xuri = [uri]$Uri
try {
Remove-Item -Force $OutFile -ErrorAction SilentlyContinue
Invoke-WebRequest -Uri $URL -OutFile $OutFile -UserAgent $deviUA -UseBasicParsing
}
catch {
Write-Host -ForegroundColor Red "download failed: $_"
Remove-Item -Force $OutFile -ErrorAction SilentlyContinue
return $false
}
return $true
}
Function Usage {
$Version = Get-Content "$ClangbuilderRoot\version" -ErrorAction SilentlyContinue | ForEach-Object {
return $_
}
if ($null -eq $Version) {
$Version = "1.0"
}
Write-Host "devi $Version portable package manager
Usage: devi cmd package-name
list list installed package
search search ported package
install install package
uninstall uninstall package
upgrade upgrade all upgradeable packages
help print help message
version print devi version and exit
"
}
Function Get-Installed {
Get-ChildItem -Path "$LockDir/*.json" | ForEach-Object {
$obj = Get-Content -Path $_.FullName -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -eq $obj -or ($null -eq $obj.version) ) {
Write-Host -ForegroundColor Red "Invalid file locks: $($_.FullName)"
return
}
$S = $_.BaseName.PadRight(20) + $obj.version
Write-Host $S
}
}
Function Repair-Port {
param(
[String]$Name
)
Write-Host -ForegroundColor Yellow "Try to repair $Name"
$obj = Get-Content -Path "$LockDir/$Name.json" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$pkobj = Get-Content -Path "$ClangbuilderRoot/ports/$Name.json" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -eq $obj -or ($null -eq $pkobj)) {
Write-Host "unable parse $Name.json. please uninstall it and retry install"
return
}
$launchers = $pkobj.launchers
if ($IsWindows64 -and $null -ne $pkobj.launchers64) {
$launchers = $pkobj.launchers64
}
if ($null -ne $launchers) {
foreach ($lnk in $launchers) {
$lnkfile = Get-Item "$ClangbuilderRoot/bin/pkgs/$Name/$lnk" -ErrorAction SilentlyContinue
if ($null -eq $lnkfile) {
continue
}
$lna = Split-Path -Leaf $lnkfile
$launcher = "$LinkedDir\$lna"
if (Test-Path $launcher) {
Write-Host -ForegroundColor Green "launcher: $launcher exists"
continue
}
Write-Host -ForegroundColor Yellow "Please run mklauncher install $Name"
}
return
}
$links = $pkobj.links
if ($IsWindows64 -and $null -ne $pkobj.links64) {
links$links=$pkobj.links64
}
foreach ($lnk in $links) {
$lnkfile = Get-Item "$ClangbuilderRoot/bin/pkgs/$Name/$lnk" -ErrorAction SilentlyContinue
if ($null -eq $lnkfile) {
continue
}
$lna = Split-Path -Leaf $lnkfile
$symlinkfile = "$LinkedDir\$lna"
if (Test-Path $symlinkfile) {
Write-Host -ForegroundColor Green "link: $symlinkfile exists"
continue
}
if (Test-Path "$ClangbuilderRoot/bin/blast.exe" ) {
&"$ClangbuilderRoot/bin/blast.exe" --link $lnkfile.FullName "$symlinkfile"
}
else {
$symlinkfile = $symlinkfile.Replace("/", "\")
cmd /c mklink "$symlinkfile" $lnkfile.FullName ## < Windows 10 need Admin
}
if ($LASTEXITCODE -ne 0) {
throw "failed create symlink: $($lnkfile.FullName)"
}
Write-Host -ForegroundColor Green "link $($lnkfile.FullName) to $symlinkfile success."
}
}
Function Repair-Ports {
Get-ChildItem -Path "$LockDir/*.json" | ForEach-Object {
Repair-Port -Name $_.BaseName
}
}
# if failed return 1
Function Search-Port {
param(
[String]$Name
)
$Portfile = "$ClangbuilderRoot/ports/$Name.json"
$cj = Get-Content "$Portfile" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -eq $cj) {
Write-Host -ForegroundColor Red "Port: $Name not found. path: $Portfile"
return 1
}
$xversion = $cj.version
if ($xversion.Length -gt 20) {
$xversion = $xversion.Substring(0, 15) + "..."
}
$S = $Name.PadRight(20) + $xversion.PadRight(20) + $cj.description
Write-Host $S
return 0
}
Function Show-Ports {
Write-Host -ForegroundColor Green "devi portable package manager, found ports:"
Get-ChildItem -Path "$ClangbuilderRoot/ports/*.json" | ForEach-Object {
$cj = Get-Content $_.FullName -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -ne $cj) {
$xversion = $cj.version
if ($xversion.Length -gt 20) {
$xversion = $xversion.Substring(0, 15) + "..."
}
$S = $_.BaseName.PadRight(20) + $xversion.PadRight(20) + $cj.description
Write-Host $S
}
}
}
# install uninstall
Function Uninstall-Port {
param(
[String]$Name
)
$lockfile = "$LockDir/$Name.json"
if ((Test-Path "$lockfile")) {
$instmd = Get-Content $lockfile -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -ne $instmd.links) {
foreach ($link in $instmd.links) {
if ( ![System.String]::IsNullOrEmpty($link)) {
Remove-Item -Force "$LinkedDir/$link" -ErrorAction SilentlyContinue
}
}
}
Remove-Item $lockfile -Force
}
else {
Write-Host -ForegroundColor Red "Not found $Name in $LockDir"
Remove-Item -Force -Recurse "$ClangbuilderRoot/bin/pkgs/$Name" -ErrorAction SilentlyContinue | Out-Null
return $false
}
Remove-Item -Force -Recurse "$ClangbuilderRoot/bin/pkgs/$Name" -ErrorAction SilentlyContinue | Out-Null
Write-Host -ForegroundColor Yellow "devi: uninstall $Name done."
return $True
}
Function Install-Port {
param(
[String]$Name
)
if (!(Test-Path "$ClangbuilderRoot/ports/$Name.json")) {
Write-Host -ForegroundColor Red "devi: `'$Name`' not yet ported."
return $false
}
$pkobj = Get-Content "$ClangbuilderRoot/ports/$Name.json" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ( $null -eq $pkobj) {
Write-Host -ForegroundColor Red "devi: '$Name' not yet ported."
return $false
}
if ($null -eq $pkobj.version) {
Write-Host -ForegroundColor Red "devi: '$Name' port config invalid."
return $false
}
$ext = $pkobj.extension
$AllowedExtensions = "exe", "zip", "msi", "7z"
if (!$AllowedExtensions.Contains($ext)) {
Write-Host -ForegroundColor Red "devi: extension '$ext' not allowed."
return $false
}
if ($pkobj.extension -eq "7z") {
if (!(Test-Path $SzExe)) {
Write-Host -ForegroundColor Red "devi: This port '$Name' extension is `'7z`', but 7z not install. Please run devi install 7z."
return $false
}
}
$oldtable = Get-Content "$LockDir/$Name.json" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$pkversion = $pkobj.version
if ($null -ne $oldtable -and $oldtable.version -eq $pkversion) {
Write-Host -ForegroundColor Yellow "devi: $Name is up to date. version: $($oldtable.version)"
return $true
}
$xurl = $pkobj.url
if ($IsWindows64 -and ($null -ne $pkobj.url64)) {
$xurl = $pkobj.url64
}
$besturl = $null
if ($xurl -is [array]) {
$besturl = Test-BestSourcesURL -Urls $xurl
}
else {
$besturl = $xurl
}
if ($null -eq $besturl) {
Write-Host -ForegroundColor Red "$Name not provider download url!"
return $false
}
$outfile = "$CleanDir\$Name.$ext"
$tempinstalldir = "$CleanDir\$Name"
if (!(WinGet -URL $besturl -OutFile $outfile)) {
return $false
}
########### unpack file from temp dir
try {
Switch ($ext) {
"zip" {
Expand-Archive -Path $outfile -DestinationPath $tempinstalldir
Initialize-FlatTarget -TopDir $tempinstalldir -MoveTo $tempinstalldir
}
"msi" {
$ret = Expand-Msi -Path $outfile -DestinationPath $tempinstalldir
if ($ret -ne 0) {
throw "Expand $outfile failed"
}
Initialize-MsiArchive -Path $tempinstalldir
}
"exe" {
New-Item -ItemType Directory -Force -ErrorAction SilentlyContinue $tempinstalldir | Out-Null
Copy-Item -Path $outfile -Destination $tempinstalldir -Force
}
"7z" {
$ret = ProcessExec -FilePath $SzExe -Argv "e -spf -y `"$outfile`" `"-o$tempinstalldir`""
if ($ret -ne 0) {
throw "decompress $outfile by 7z failed"
}
Initialize-FlatTarget -TopDir $tempinstalldir -MoveTo $tempinstalldir
}
}
}
catch {
Write-Host -ForegroundColor Red "$_"
Remove-Item -Force -Recurse $outfile -ErrorAction SilentlyContinue | Out-Null
Remove-Item -Force -Recurse $tempinstalldir -ErrorAction SilentlyContinue | Out-Null
return $false
}
$packDir = "$ClangbuilderRoot\bin\pkgs\$Name"
$backupDir = "$packDir.$PID"
try {
Rename-Item -Force -Path $packDir -NewName $backupDir -ErrorAction SilentlyContinue
Move-Item -Force -Path $tempinstalldir -Destination $packDir
}
catch {
Rename-Item -Force -Path $backupDir -NewName $packDir -ErrorAction SilentlyContinue
Write-Host "unable apply $Name to $packDir $_"
}
finally {
Remove-Item -Force -Recurse $backupDir -ErrorAction SilentlyContinue
}
$versiontable = @{ }
$versiontable["version"] = $pkversion
$launchers = $portobj.launchers
if ($IsWindows64 -and $null -ne $portobj.launchers64) {
$launchers = $portobj.launchers64
}
[System.Collections.ArrayList]$mlinks = @()
if ($null -ne $oldtable.links) {
[System.Collections.ArrayList]$lav = @()
if ($null -ne $launchers) {
foreach ($l in $launchers) {
$lna = Split-Path -Leaf $l
$lav.Add($lna) | Out-Null
}
}
foreach ($olink in $oldtable.links) {
if ($lav.Contains($olink)) {
Write-Host -ForegroundColor Green "Find the old launcher '$olink', Please run mklauncher to rebuild it."
$mlinks.Add($olink) | Out-Null
continue
}
if (![System.String]::IsNullOrEmpty($olink)) {
Remove-Item -Force -Recurse "$LinkedDir/$olink" -ErrorAction SilentlyContinue
}
}
}
$links = $pkobj.links
if ($IsWindows64 -and $null -ne $pkobj.links64) {
links$links=$pkobj.links64
}
if ($null -ne $links) {
try {
foreach ($lnfile in $links) {
$item = Get-Item "$packDir/$lnfile"
$symlinkfile = Join-Path -Path $LinkedDir -ChildPath $item.Name
Remove-Item -Force $symlinkfile -ErrorAction SilentlyContinue
if (Test-Path "$ClangbuilderRoot/bin/blast.exe" ) {
&"$ClangbuilderRoot/bin/blast.exe" --link "$($item.FullName)" "$symlinkfile"
}
else {
$xsymlinkfile = $symlinkfile.Replace("/", "\")
cmd /c mklink "$xsymlinkfile" "$($item.FullName)" ## < Windows 10 need Admin
}
if ($LASTEXITCODE -ne 0) {
throw "failed create symlink: $symlinkfile"
}
[void]$mlinks.Add($item.Name)
Write-Host -ForegroundColor Green "link $($item.FullName) to $symlinkfile success."
}
}
catch {
Write-Host -ForegroundColor Red "create symbolic link failed: $_"
}
}
if ($mlinks.Count -gt 0) {
$versiontable["links"] = $mlinks
$versiontable["linked"] = $true
}
if ($null -ne $pkobj.mount) {
$versiontable["mount"] = $pkobj.mount
}
ConvertTo-Json $versiontable | Out-File -Force -FilePath "$LockDir/$Name.json"
Write-Host -ForegroundColor Green "devi: install $Name success, version: $pkversion"
return $true
}
# Update ports
Function Update-Ports {
param(
[Switch]$Default
)
$pkgtable = @{ }
Get-ChildItem -Path "$LockDir/*.json" | ForEach-Object {
$obj = Get-Content -Path $_.FullName -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ( $null -eq $obj -or ( $null -eq $obj.version) ) {
Write-Host -ForegroundColor Red "Invalid file locks: $($_.FullName)"
return $false
}
$xname = $_.BaseName
if (!(Test-Path "$ClangbuilderRoot/ports/$xname.json")) {
if ($Default) {
Write-Host -ForegroundColor Yellow "remove deprecated package: $xname"
Uninstall-Port $xname
return $true
}
}
$pkgtable["$xname"] = $obj.version
Install-Port -Name $xname Out-Null
}
if ($Default) {
$devcore = Get-Content "$ClangbuilderRoot/config/devi.json" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
if ($null -eq $devcore.core) {
Write-Host -ForegroundColor Red "devi default ports config missing, file: $ClangbuilderRoot/config/devi.json"
return $false
}
foreach ($n in $devcore.core) {
if (!$pkgtable.ContainsKey($n)) {
Install-Port -ClangbuilderRoot $ClangbuilderRoot -Name $n | Out-Null
}
}
$git = Get-Command -CommandType Application -Name "git.exe" -ErrorAction SilentlyContinue
if ($null -eq $git) {
if (!(Test-Path "$ClangbuilderRoot/bin/pkgs/git/cmd/git.exe")) {
Write-Host -ForegroundColor Yellow "git not installed, please run devi install git"
}
}
# Test git
}
return $true
}
if ($args.Count -eq 0) {
Usage
exit 1
}
$subcmd = $args[0]
if ($subcmd -eq "list" -or $subcmd -eq "--list" -or $subcmd -eq "-l") {
Get-Installed
## get installed packages
exit 0
}
if ($subcmd -eq "search" -or $subcmd -eq "--search" -or $subcmd -eq "-s") {
if ($args.Count -eq 1) {
Show-Ports
exit 0
}
[int]$ret = 0
for ($i = 1; $i -lt $args.Count; $i++) {
$ret2 = Search-Port -Name $args[$i]
if ($ret2 -ne 0) {
$ret = $ret2
}
}
exit $ret
}
$uninstallTable = "uninstall", "--uninstall", "remove", "--remove", "-r"
if ($uninstallTable.Contains($subcmd)) {
if ($args.Count -lt 2) {
Write-Host -ForegroundColor Red "devi uninstall missing argument`nexample: devi uninstall putty"
exit 1
}
$mtx = New-Object System.Threading.Mutex($false, $MutexName)
$mtxresult = $mtx.WaitOne(1000)
if ($mtxresult -eq $false) {
Write-Host -ForegroundColor Red "Another devi process is running, please try again later."
exit 1
}
for ($i = 1; $i -lt $args.Count; $i++) {
if (!(Uninstall-Port -Name $args[$i])) {
$mtx.ReleaseMutex()
exit 1
}
}
$mtx.ReleaseMutex()
exit 0
}
if ($subcmd -eq "repair") {
Repair-Ports
exit 0
}
if ($subcmd -eq "install" -or $subcmd -eq "--install" -or $subcmd -eq "-i") {
if ($args.Count -lt 2) {
Write-Host -ForegroundColor Red "devi install missing argument`nexample: devi install cmake"
exit 1
}
$mtx = New-Object System.Threading.Mutex($false, $MutexName)
$mtxresult = $mtx.WaitOne(1000)
if ($mtxresult -eq $false) {
Write-Host -ForegroundColor Red "Another devi process is running, please try again later."
exit 1
}
for ($i = 1; $i -lt $args.Count; $i++) {
if (!(Install-Port -Name $args[$i])) {
$mtx.ReleaseMutex()
exit 1
}
}
$mtx.ReleaseMutex()
exit 0
}
$upgradeTable = "upgrade", "--upgrade", "-U"
if ($upgradeTable.Contains($subcmd)) {
$ret = $false
$mtx = New-Object System.Threading.Mutex($false, $MutexName)
$mtxresult = $mtx.WaitOne(1000)
if ($mtxresult -eq $false) {
Write-Host -ForegroundColor Red "Another devi process is running, please try again later."
exit 1
}
if ($args.Count -gt 1 -and $args[1] -eq "--default") {
Write-Host "devi: Use upgrade --default, will install devi.json#core."
$ret = Update-Ports -Default
}
else {
$ret = Update-Ports
}
if ($ret -eq $false) {
$mtx.ReleaseMutex()
exit 1
}
$mtx.ReleaseMutex()
Write-Host "Update package completed."
exit 0
}
$versionTable = "-v", "--version", "version"
if ($versionTable.Contains($subcmd)) {
Get-Content "$ClangbuilderRoot\version" -ErrorAction SilentlyContinue | ForEach-Object {
Write-Host "devi $_"
exit 0
}
Write-Host "devi 1.0"
exit 0
}
$helpTable = "-h", "-?", "/?", "--help", "help"
if ($helpTable.Contains($subcmd)) {
Usage
exit 0
}
Write-Host -ForegroundColor Red "Bad command 'devi $subcmd'`nPlease run devi --help see usage"
exit 1