Skip to content

Commit 1375d9e

Browse files
xxthunderchawyehsu
andauthored
feat: Support using external git for initialization (#40)
Co-authored-by: Chawye Hsu <[email protected]>
1 parent 6ed4f1f commit 1375d9e

File tree

3 files changed

+113
-36
lines changed

3 files changed

+113
-36
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ jobs:
2121
- name: Test Scoop Installer
2222
shell: powershell
2323
run: ./test/bin/test.ps1
24+
- name: Test Scoop Install command
25+
shell: powershell
26+
run: |
27+
./install.ps1 -RunAsAdmin
28+
echo "$Env:USERPROFILE\scoop\shims" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
29+
- name: Test scoop command availability
30+
shell: powershell
31+
run: scoop help
2432
test_pwsh:
2533
name: PowerShell
2634
runs-on: windows-latest
@@ -37,3 +45,11 @@ jobs:
3745
- name: Test Scoop Installer
3846
shell: pwsh
3947
run: ./test/bin/test.ps1
48+
- name: Test Scoop Install command
49+
shell: pwsh
50+
run: |
51+
./install.ps1 -RunAsAdmin
52+
echo "~\scoop\shims" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
53+
- name: Test scoop command availability
54+
shell: pwsh
55+
run: scoop help

install.ps1

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
.PARAMETER Proxy
4646
Specifies proxy to use during the installation.
4747
.PARAMETER ProxyCredential
48-
Specifies credential for the given prxoy.
48+
Specifies credential for the given proxy.
4949
.PARAMETER ProxyUseDefaultCredentials
5050
Use the credentials of the current user for the proxy server that is specified by the -Proxy parameter.
5151
.PARAMETER RunAsAdmin
@@ -518,6 +518,14 @@ function Add-DefaultConfig {
518518
Add-Config -Name 'last_update' -Value ([System.DateTime]::Now.ToString('o')) | Out-Null
519519
}
520520

521+
function Test-CommandAvailable {
522+
param (
523+
[Parameter(Mandatory = $True, Position = 0)]
524+
[String] $Command
525+
)
526+
return [Boolean](Get-Command $Command -ErrorAction Ignore)
527+
}
528+
521529
function Install-Scoop {
522530
Write-InstallInfo "Initializing..."
523531
# Validate install parameters
@@ -527,43 +535,64 @@ function Install-Scoop {
527535
# Enable TLS 1.2
528536
Optimize-SecurityProtocol
529537

530-
# Download scoop zip from GitHub
531-
Write-InstallInfo "Downloading..."
538+
# Download scoop from GitHub
539+
Write-InstallInfo "Downloading ..."
532540
$downloader = Get-Downloader
533-
# 1. download scoop
534-
$scoopZipfile = "$SCOOP_APP_DIR\scoop.zip"
535-
if (!(Test-Path $SCOOP_APP_DIR)) {
536-
New-Item -Type Directory $SCOOP_APP_DIR | Out-Null
537-
}
538-
Write-Verbose "Downloading $SCOOP_PACKAGE_REPO to $scoopZipfile"
539-
$downloader.downloadFile($SCOOP_PACKAGE_REPO, $scoopZipfile)
540-
# 2. download scoop main bucket
541-
$scoopMainZipfile = "$SCOOP_MAIN_BUCKET_DIR\scoop-main.zip"
542-
if (!(Test-Path $SCOOP_MAIN_BUCKET_DIR)) {
543-
New-Item -Type Directory $SCOOP_MAIN_BUCKET_DIR | Out-Null
544-
}
545-
Write-Verbose "Downloading $SCOOP_MAIN_BUCKET_REPO to $scoopMainZipfile"
546-
$downloader.downloadFile($SCOOP_MAIN_BUCKET_REPO, $scoopMainZipfile)
547-
548-
# Extract files from downloaded zip
549-
Write-InstallInfo "Extracting..."
550-
# 1. extract scoop
551-
$scoopUnzipTempDir = "$SCOOP_APP_DIR\_tmp"
552-
Write-Verbose "Extracting $scoopZipfile to $scoopUnzipTempDir"
553-
Expand-ZipArchive $scoopZipfile $scoopUnzipTempDir
554-
Copy-Item "$scoopUnzipTempDir\scoop-*\*" $SCOOP_APP_DIR -Recurse -Force
555-
# 2. extract scoop main bucket
556-
$scoopMainUnzipTempDir = "$SCOOP_MAIN_BUCKET_DIR\_tmp"
557-
Write-Verbose "Extracting $scoopMainZipfile to $scoopMainUnzipTempDir"
558-
Expand-ZipArchive $scoopMainZipfile $scoopMainUnzipTempDir
559-
Copy-Item "$scoopMainUnzipTempDir\Main-*\*" $SCOOP_MAIN_BUCKET_DIR -Recurse -Force
560-
561-
# Cleanup
562-
Remove-Item $scoopUnzipTempDir -Recurse -Force
563-
Remove-Item $scoopZipfile
564-
Remove-Item $scoopMainUnzipTempDir -Recurse -Force
565-
Remove-Item $scoopMainZipfile
566541

542+
if (Test-CommandAvailable('git')) {
543+
$old_https = $env:HTTPS_PROXY
544+
$old_http = $env:HTTP_PROXY
545+
try {
546+
if ($downloader.Proxy) {
547+
#define env vars for git when behind a proxy
548+
$Env:HTTP_PROXY = $downloader.Proxy.Address
549+
$Env:HTTPS_PROXY = $downloader.Proxy.Address
550+
}
551+
Write-Verbose "Cloning $SCOOP_PACKAGE_GIT_REPO to $SCOOP_APP_DIR"
552+
git clone -q $SCOOP_PACKAGE_GIT_REPO $SCOOP_APP_DIR
553+
Write-Verbose "Cloning $SCOOP_MAIN_BUCKET_GIT_REPO to $SCOOP_MAIN_BUCKET_DIR"
554+
git clone -q $SCOOP_MAIN_BUCKET_GIT_REPO $SCOOP_MAIN_BUCKET_DIR
555+
} catch {
556+
Get-Error $_
557+
} finally {
558+
$env:HTTPS_PROXY = $old_https
559+
$env:HTTP_PROXY = $old_http
560+
}
561+
} else {
562+
# 1. download scoop
563+
$scoopZipfile = "$SCOOP_APP_DIR\scoop.zip"
564+
if (!(Test-Path $SCOOP_APP_DIR)) {
565+
New-Item -Type Directory $SCOOP_APP_DIR | Out-Null
566+
}
567+
Write-Verbose "Downloading $SCOOP_PACKAGE_REPO to $scoopZipfile"
568+
$downloader.downloadFile($SCOOP_PACKAGE_REPO, $scoopZipfile)
569+
# 2. download scoop main bucket
570+
$scoopMainZipfile = "$SCOOP_MAIN_BUCKET_DIR\scoop-main.zip"
571+
if (!(Test-Path $SCOOP_MAIN_BUCKET_DIR)) {
572+
New-Item -Type Directory $SCOOP_MAIN_BUCKET_DIR | Out-Null
573+
}
574+
Write-Verbose "Downloading $SCOOP_MAIN_BUCKET_REPO to $scoopMainZipfile"
575+
$downloader.downloadFile($SCOOP_MAIN_BUCKET_REPO, $scoopMainZipfile)
576+
577+
# Extract files from downloaded zip
578+
Write-InstallInfo "Extracting..."
579+
# 1. extract scoop
580+
$scoopUnzipTempDir = "$SCOOP_APP_DIR\_tmp"
581+
Write-Verbose "Extracting $scoopZipfile to $scoopUnzipTempDir"
582+
Expand-ZipArchive $scoopZipfile $scoopUnzipTempDir
583+
Copy-Item "$scoopUnzipTempDir\scoop-*\*" $SCOOP_APP_DIR -Recurse -Force
584+
# 2. extract scoop main bucket
585+
$scoopMainUnzipTempDir = "$SCOOP_MAIN_BUCKET_DIR\_tmp"
586+
Write-Verbose "Extracting $scoopMainZipfile to $scoopMainUnzipTempDir"
587+
Expand-ZipArchive $scoopMainZipfile $scoopMainUnzipTempDir
588+
Copy-Item "$scoopMainUnzipTempDir\Main-*\*" $SCOOP_MAIN_BUCKET_DIR -Recurse -Force
589+
590+
# Cleanup
591+
Remove-Item $scoopUnzipTempDir -Recurse -Force
592+
Remove-Item $scoopZipfile
593+
Remove-Item $scoopMainUnzipTempDir -Recurse -Force
594+
Remove-Item $scoopMainZipfile
595+
}
567596
# Create the scoop shim
568597
Import-ScoopShim
569598
# Finially ensure scoop shims is in the PATH
@@ -616,6 +645,9 @@ $SCOOP_CONFIG_FILE = "$SCOOP_CONFIG_HOME\scoop\config.json"
616645
$SCOOP_PACKAGE_REPO = "https://github.com/ScoopInstaller/Scoop/archive/master.zip"
617646
$SCOOP_MAIN_BUCKET_REPO = "https://github.com/ScoopInstaller/Main/archive/master.zip"
618647

648+
$SCOOP_PACKAGE_GIT_REPO = "https://github.com/ScoopInstaller/Scoop.git"
649+
$SCOOP_MAIN_BUCKET_GIT_REPO = "https://github.com/ScoopInstaller/Main.git"
650+
619651
# Quit if anything goes wrong
620652
$oldErrorActionPreference = $ErrorActionPreference
621653
$ErrorActionPreference = 'Stop'

test/install.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BeforeAll {
2+
# Load SUT
3+
$sut = (Split-Path -Leaf $PSCommandPath).Replace('.Tests.ps1', '.ps1')
4+
. ".\$sut"
5+
}
6+
7+
Describe 'Get-Downloader' -Tag 'Proxy' {
8+
Context 'No proxy given via script parameter' {
9+
It 'Returns WebClient without proxy' {
10+
$NoProxy = $true
11+
Test-ValidateParameter
12+
(Get-Downloader).Proxy | Should -Be $null
13+
}
14+
It 'Returns WebClient without proxy although proxy is given' {
15+
$NoProxy = $true
16+
$Proxy = New-Object System.Uri('http://donotcare')
17+
Test-ValidateParameter
18+
(Get-Downloader).Proxy | Should -Be $null
19+
}
20+
}
21+
Context 'Proxy given via script parameter' {
22+
It 'Returns WebClient with proxy' {
23+
$ProxyString = 'http://some.proxy.with.port:8080'
24+
$Proxy = New-Object System.Uri($ProxyString)
25+
Test-ValidateParameter
26+
(Get-Downloader).Proxy.Address | Should -Be "$ProxyString/"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)