forked from ScoopInstaller/Install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.ps1
496 lines (423 loc) · 17.7 KB
/
install.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
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
<#
.SYNOPSIS
Scoop installer.
.DESCRIPTION
The installer of Scoop. For details please check the website and wiki.
.PARAMETER ScoopDir
Specifies directory to install.
Scoop will be installed to '$env:USERPROFILE\scoop' if not specificed.
.PARAMETER ScoopGlobalDir
Specifies global app directory.
Global app will be installed to '$env:ProgramData\scoop' if not specificed.
.PARAMETER ScoopCacheDir
Specifies cache directory.
Cache directory will be '$ScoopDir\cache' if not specificed.
.PARAMETER NoProxy
Specifies bypass system proxy or not while installation.
.PARAMETER Proxy
Specifies proxy to use while installation.
.PARAMETER ProxyCredential
Specifies credential for the prxoy.
.PARAMETER ProxyUseDefaultCredentials
Use the credentials of the current user for the proxy server that is specified by the -Proxy parameter.
.PARAMETER RunAsAdmin
Force to run the installer as administrator.
.LINK
https://scoop.sh
.LINK
https://github.com/lukesampson/scoop/wiki
#>
param(
[String] $ScoopDir,
[String] $ScoopGlobalDir,
[String] $ScoopCacheDir,
[Switch] $NoProxy,
[Uri] $Proxy,
[System.Management.Automation.PSCredential] $ProxyCredential,
[Switch] $ProxyUseDefaultCredentials,
[Switch] $RunAsAdmin
)
# Disable StrictMode in this script
Set-StrictMode -Off
function Write-InstallInfo {
param(
[Parameter(Mandatory = $True, Position = 0)]
[String] $String,
[Parameter(Mandatory = $False, Position = 1)]
[System.ConsoleColor] $ForegroundColor = $host.UI.RawUI.ForegroundColor
)
$backup = $host.UI.RawUI.ForegroundColor
if ($ForegroundColor -ne $host.UI.RawUI.ForegroundColor) {
$host.UI.RawUI.ForegroundColor = $ForegroundColor
}
Write-Output "$String"
$host.UI.RawUI.ForegroundColor = $backup
}
function Deny-Install {
param(
[String] $message,
[Int] $errorCode = 1
)
Write-InstallInfo -String $message -ForegroundColor DarkRed
Write-InstallInfo "Abort."
# Don't abort if invoked with iex that would close the PS session
if ($IS_EXECUTED_FROM_IEX) {
break
} else {
exit $errorCode
}
}
function Test-ValidateParameter {
if ($null -eq $Proxy -and ($null -ne $ProxyCredential -or $ProxyUseDefaultCredentials)) {
Deny-Install "Provide a valid proxy URI for the -Proxy parameter when using the -ProxyCredential or -ProxyUseDefaultCredentials."
}
if ($ProxyUseDefaultCredentials -and $null -ne $ProxyCredential) {
Deny-Install "ProxyUseDefaultCredentials is conflict with ProxyCredential. Don't use the -ProxyCredential and -ProxyUseDefaultCredentials together."
}
}
function Test-IsAdministrator {
return ([Security.Principal.WindowsPrincipal]`
[Security.Principal.WindowsIdentity]::GetCurrent()`
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Test-Prerequisite {
# Scoop requires PowerShell 5 at least
if (($PSVersionTable.PSVersion.Major) -lt 5) {
Deny-Install "PowerShell 5 or later is required to run Scoop. Go to https://microsoft.com/powershell to get the latest version of PowerShell."
}
# Scoop requires TLS 1.2 SecurityProtocol, which exists in .NET Framework 4.5+
if ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -notcontains 'Tls12') {
Deny-Install "Scoop requires .NET Framework 4.5+ to work. Go to https://microsoft.com/net/download to get the latest version of .NET Framework."
}
# Ensure Robocopy.exe is accessible
if (!([bool](Get-Command -Name 'robocopy' -ErrorAction SilentlyContinue))) {
Deny-Install "Scoop requires 'C:\Windows\System32\Robocopy.exe' to work. Please make sure 'C:\Windows\System32' is in your PATH."
}
# Detect if RunAsAdministrator, there is no need to run as administrator when installing Scoop.
if (!$RunAsAdmin -and (Test-IsAdministrator)) {
Deny-Install "Running the installer as administrator is disabled by default, use -RunAsAdmin parameter if you know what you are doing."
}
# Show notification to change execution policy
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'ByPass')
if ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Deny-Install "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run Scoop. For example, to set the execution policy to 'RemoteSigned' please run 'Set-ExecutionPolicy RemoteSigned -Scope CurrentUser'."
}
# Test if scoop is installed, by checking if scoop command exists.
if ([bool](Get-Command -Name 'scoop' -ErrorAction SilentlyContinue)) {
Deny-Install "Scoop is already installed. Run 'scoop update' to get the latest version."
}
}
function Optimize-SecurityProtocol {
# .NET Framework 4.7+ has a default security protocol called 'SystemDefault',
# which allows the operating system to choose the best protocol to use.
# If SecurityProtocolType contains 'SystemDefault' (means .NET4.7+ detected)
# and the value of SecurityProtocol is 'SystemDefault', just do nothing on SecurityProtocol,
# 'SystemDefault' will use TLS 1.2 if the webrequest requires.
$isNewerNetFramework = ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -contains 'SystemDefault')
$isSystemDefault = ([System.Net.ServicePointManager]::SecurityProtocol.Equals([System.Net.SecurityProtocolType]::SystemDefault))
# If not, change it to support TLS 1.2
if (!($isNewerNetFramework -and $isSystemDefault)) {
# Set to TLS 1.2 (3072), then TLS 1.1 (768), and TLS 1.0 (192). Ssl3 has been superseded,
# https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netframework-4.5
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192
}
}
function Get-Downloader {
$downloadSession = New-Object System.Net.WebClient
# Set proxy to null if NoProxy is specificed
if ($NoProxy) {
$downloadSession.Proxy = $null
} elseif ($Proxy) {
# Prepend protocol if not provided
if (!$Proxy.IsAbsoluteUri) {
$Proxy = New-Object System.Uri("http://" + $Proxy.OriginalString)
}
$Proxy = New-Object System.Net.WebProxy($Proxy)
if ($null -ne $ProxyCredential) {
$Proxy.Credentials = $ProxyCredential.GetNetworkCredential()
} elseif ($ProxyUseDefaultCredentials) {
$Proxy.UseDefaultCredentials = $true
}
$downloadSession.Proxy = $Proxy
}
return $downloadSession
}
function Test-isFileLocked {
param(
[String] $path
)
$file = New-Object System.IO.FileInfo $path
if (!(Test-Path $path)) {
return $false
}
try {
$stream = $file.Open(
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::None
)
if ($stream) {
$stream.Close()
}
return $false
}
catch {
# The file is locked by a process.
return $true
}
}
function Expand-ZipArchive {
param(
[String] $path,
[String] $to
)
if (!(Test-Path $path)) {
Deny-Install "Unzip failed: can't find $path to unzip."
}
# Check if the zip file is locked, by antivirus software for example
$retries = 0
while ($retries -le 10) {
if ($retries -eq 10) {
Deny-Install "Unzip failed: can't unzip because a process is locking the file."
}
if (Test-isFileLocked $path) {
Write-InstallInfo "Waiting for $path to be unlocked by another process... ($retries/10)"
$retries++
Start-Sleep -Seconds 2
} else {
break
}
}
# PowerShell 5+: use Expand-Archive to extract zip files
Microsoft.PowerShell.Archive\Expand-Archive -Path $path -DestinationPath $to -Force
}
function Import-ScoopShim {
# The scoop executable
$path = "$SCOOP_APP_DIR\bin\scoop.ps1"
if (!(Test-Path $SCOOP_SHIMS_DIR)) {
New-Item -Type Directory $SCOOP_SHIMS_DIR | Out-Null
}
# The scoop shim
$shim = "$SCOOP_SHIMS_DIR\scoop"
# Convert to relative path
Push-Location $SCOOP_SHIMS_DIR
$relativePath = Resolve-Path -Relative $path
Pop-Location
# Setting PSScriptRoot in Shim if it is not defined, so the shim doesn't break in PowerShell 2.0
Write-Output "if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }" | Out-File "$shim.ps1" -Encoding utf8
Write-Output "`$path = Join-Path `"`$PSScriptRoot`" `"$relativePath`"" | Out-File "$shim.ps1" -Encoding utf8 -Append
Write-Output "if (`$MyInvocation.ExpectingInput) { `$input | & `$path @args } else { & `$path @args }" | Out-File "$shim.ps1" -Encoding utf8 -Append
# Make scoop accessible from cmd.exe
Write-Output "@echo off
setlocal enabledelayedexpansion
set args=%*
:: replace problem characters in arguments
set args=%args:`"='%
set args=%args:(=``(%
set args=%args:)=``)%
set invalid=`"='
if !args! == !invalid! ( set args= )
powershell -noprofile -ex unrestricted `"& '$path' %args%;exit `$lastexitcode`"" | Out-File "$shim.cmd" -Encoding ascii
# Make scoop accessible from bash or other posix shell
Write-Output "#!/bin/sh`npowershell.exe -ex unrestricted `"$path`" `"$@`"" | Out-File $shim -Encoding ascii
}
function Get-Env {
param(
[String] $name,
[Switch] $global
)
$target = if ($global) { 'Machine' } else { 'User' }
return [Environment]::GetEnvironmentVariable($name, $target)
}
function Add-ShimsDirToPath {
# Get $env:PATH of current user
$userEnvPath = Get-Env 'PATH'
if ($userEnvPath -notmatch [Regex]::Escape($SCOOP_SHIMS_DIR)) {
$h = (Get-PsProvider 'FileSystem').Home
if (!$h.EndsWith('\')) {
$h += '\'
}
if (!($h -eq '\')) {
$friendlyPath = "$SCOOP_SHIMS_DIR" -Replace ([Regex]::Escape($h)), "~\"
Write-InstallInfo "Adding $friendlyPath to your path."
} else {
Write-InstallInfo "Adding $SCOOP_SHIMS_DIR to your path."
}
# For future sessions
[System.Environment]::SetEnvironmentVariable('PATH', "$SCOOP_SHIMS_DIR;$userEnvPath", 'User')
# For current session
$env:PATH = "$SCOOP_SHIMS_DIR;$env:PATH"
}
}
function Use-Config {
if (!(Test-Path $SCOOP_CONFIG_FILE)) {
return $null
}
try {
return (Get-Content $SCOOP_CONFIG_FILE -Raw | ConvertFrom-Json -ErrorAction Stop)
} catch {
Deny-Install "ERROR loading $SCOOP_CONFIG_FILE`: $($_.Exception.Message)"
}
}
function Add-Config {
param (
[Parameter(Mandatory = $True, Position = 0)]
[String] $Name,
[Parameter(Mandatory = $True, Position = 1)]
[String] $Value
)
$scoopConfig = Use-Config
if ($scoopConfig -is [System.Management.Automation.PSObject]) {
if ($Value -eq [bool]::TrueString -or $Value -eq [bool]::FalseString) {
$Value = [System.Convert]::ToBoolean($Value)
}
if ($null -eq $scoopConfig.$Name) {
$scoopConfig | Add-Member -MemberType NoteProperty -Name $Name -Value $Value
} else {
$scoopConfig.$Name = $Value
}
} else {
$baseDir = Split-Path -Path $SCOOP_CONFIG_FILE
if (!(Test-Path $baseDir)) {
New-Item -Type Directory $baseDir | Out-Null
}
$scoopConfig = New-Object PSObject
$scoopConfig | Add-Member -MemberType NoteProperty -Name $Name -Value $Value
}
if ($null -eq $Value) {
$scoopConfig.PSObject.Properties.Remove($Name)
}
ConvertTo-Json $scoopConfig | Set-Content $SCOOP_CONFIG_FILE -Encoding ASCII
return $scoopConfig
}
function Add-DefaultConfig {
# If user-level SCOOP env not defined, save to rootPath
if (!(Get-Env 'SCOOP')) {
if ($SCOOP_DIR -ne "$env:USERPROFILE\scoop") {
Add-Config -Name 'rootPath' -Value $SCOOP_DIR | Out-Null
}
}
# Use system SCOOP_GLOBAL, or set system SCOOP_GLOBAL
# with $env:SCOOP_GLOBAL if RunAsAdmin, otherwise save to globalPath
if (!(Get-Env 'SCOOP_GLOBAL' -global)) {
if ((Test-IsAdministrator) -and $env:SCOOP_GLOBAL) {
[Environment]::SetEnvironmentVariable('SCOOP_GLOBAL', $env:SCOOP_GLOBAL, 'Machine')
} else {
if ($SCOOP_GLOBAL_DIR -ne "$env:ProgramData\scoop") {
Add-Config -Name 'globalPath' -Value $SCOOP_GLOBAL_DIR | Out-Null
}
}
}
# Use system SCOOP_CACHE, or set system SCOOP_CACHE
# with $env:SCOOP_CACHE if RunAsAdmin, otherwise save to cachePath
if (!(Get-Env 'SCOOP_CACHE' -global)) {
if ((Test-IsAdministrator) -and $env:SCOOP_CACHE) {
[Environment]::SetEnvironmentVariable('SCOOP_CACHE', $env:SCOOP_CACHE, 'Machine')
} else {
if ($SCOOP_CACHE_DIR -ne "$SCOOP_DIR\cache") {
Add-Config -Name 'cachePath' -Value $SCOOP_CACHE_DIR | Out-Null
}
}
}
# save current datatime to lastUpdate
Add-Config -Name 'lastUpdate' -Value ([System.DateTime]::Now.ToString('o')) | Out-Null
}
function Install-Scoop {
Write-InstallInfo "Initializing..."
# Validate install parameters
Test-ValidateParameter
# Check prerequisites
Test-Prerequisite
# Enable TLS 1.2
Optimize-SecurityProtocol
# Download scoop zip from GitHub
Write-InstallInfo "Downloading..."
$downloader = Get-Downloader
# 1. download scoop
$scoopZipfile = "$SCOOP_APP_DIR\scoop.zip"
if (!(Test-Path $SCOOP_APP_DIR)) {
New-Item -Type Directory $SCOOP_APP_DIR | Out-Null
}
$downloader.downloadFile($SCOOP_PACKAGE_REPO, $scoopZipfile)
# 2. download scoop main bucket
$scoopMainZipfile = "$SCOOP_MAIN_BUCKET_DIR\scoop-main.zip"
if (!(Test-Path $SCOOP_MAIN_BUCKET_DIR)) {
New-Item -Type Directory $SCOOP_MAIN_BUCKET_DIR | Out-Null
}
$downloader.downloadFile($SCOOP_MAIN_BUCKET_REPO, $scoopMainZipfile)
# Extract files from downloaded zip
Write-InstallInfo "Extracting..."
# 1. extract scoop
$scoopUnzipTempDir = "$SCOOP_APP_DIR\_tmp"
Expand-ZipArchive $scoopZipfile $scoopUnzipTempDir
Copy-Item "$scoopUnzipTempDir\scoop-*\*" $SCOOP_APP_DIR -Recurse -Force
# 2. extract scoop main bucket
$scoopMainUnzipTempDir = "$SCOOP_MAIN_BUCKET_DIR\_tmp"
Expand-ZipArchive $scoopMainZipfile $scoopMainUnzipTempDir
Copy-Item "$scoopMainUnzipTempDir\Main-*\*" $SCOOP_MAIN_BUCKET_DIR -Recurse -Force
# Cleanup
Remove-Item $scoopUnzipTempDir -Recurse -Force
Remove-Item $scoopZipfile
Remove-Item $scoopMainUnzipTempDir -Recurse -Force
Remove-Item $scoopMainZipfile
# Create the scoop shim
Write-InstallInfo "Creating shim..."
Import-ScoopShim
# Finially ensure scoop shims is in the PATH
Add-ShimsDirToPath
# Setup initial configuration of Scoop
Add-DefaultConfig
Write-InstallInfo "Scoop was installed successfully!" -ForegroundColor DarkGreen
Write-InstallInfo "Type 'scoop help' for instructions."
}
# Prepare variables
$IS_EXECUTED_FROM_IEX = ($null -eq $MyInvocation.MyCommand.Path)
# Scoop root directory
$SCOOP_DIR = $ScoopDir, $env:SCOOP, "$env:USERPROFILE\scoop" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1
# Scoop global apps directory
$SCOOP_GLOBAL_DIR = $ScoopGlobalDir, $env:SCOOP_GLOBAL, "$env:ProgramData\scoop" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1
# Scoop cache directory
$SCOOP_CACHE_DIR = $ScoopCacheDir, $env:SCOOP_CACHE, "$SCOOP_DIR\cache" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1
# Scoop shims directory
$SCOOP_SHIMS_DIR = "$SCOOP_DIR\shims"
# Scoop itself directory
$SCOOP_APP_DIR = "$SCOOP_DIR\apps\scoop\current"
# Scoop main bucket directory
$SCOOP_MAIN_BUCKET_DIR = "$SCOOP_DIR\buckets\main"
# Scoop config file location
$SCOOP_CONFIG_HOME = $env:XDG_CONFIG_HOME, "$env:USERPROFILE\.config" | Select-Object -First 1
$SCOOP_CONFIG_FILE = "$SCOOP_CONFIG_HOME\scoop\config.json"
# TODO: Use a specific version of Scoop and the main bucket
$SCOOP_PACKAGE_REPO = "https://archive.fastgit.org/slaughtering/scoop/archive/master.zip"
$SCOOP_MAIN_BUCKET_REPO = "https://archive.fastgit.org/ScoopInstaller/Main/archive/master.zip"
# Quit if anything goes wrong
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# Bootstrap function
Install-Scoop
# Reset $ErrorActionPreference to original value
$ErrorActionPreference = $oldErrorActionPreference