This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_scoop.ps1
143 lines (116 loc) · 4.65 KB
/
win_scoop.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
#!powershell
#Requires -Module Ansible.ModuleUtils.ArgvParser
#Requires -Module Ansible.ModuleUtils.CommandUtil
#AnsibleRequires -CSharpUtil Ansible.Basic
$spec = @{
options = @{
name = @{ type = "list"; elements = "str"; required = $true }
state = @{ type = "str"; default = "present"; choices = "present", "absent" }
}
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$name = $module.Params.name
$state = $module.Params.state
function Install-Scoop {
# Scoop doesn't have refreshenv like Chocolatey
# Let's try to update PATH first
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
$scoop_app = Get-Command -Name scoop.ps1 -Type ExternalScript -ErrorAction SilentlyContinue
if ($null -eq $scoop_app) {
# We need to install scoop
# Enable TLS1.1/TLS1.2 if they're available but disabled (eg. .NET 4.5)
$security_protocols = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::SystemDefault
if ([Net.SecurityProtocolType].GetMember("Tls11").Count -gt 0) {
$security_protocols = $security_protcols -bor [Net.SecurityProtocolType]::Tls11
}
if ([Net.SecurityProtocolType].GetMember("Tls12").Count -gt 0) {
$security_protocols = $security_protcols -bor [Net.SecurityProtocolType]::Tls12
}
[Net.ServicePointManager]::SecurityProtocol = $security_protocols
$client = New-Object -TypeName System.Net.WebClient
$script_url = "https://get.scoop.sh"
try {
$install_script = $client.DownloadString($script_url)
}
catch {
$module.FailJson("Failed to download Scoop script from '$script_url'; $($_.Exception.Message)", $_)
}
if (-not $module.CheckMode) {
$res = Run-Command -Command "powershell.exe -" -stdin $install_script -environment $environment
if ($res.rc -ne 0) {
$module.Result.rc = $res.rc
$module.Result.stdout = $res.stdout
$module.Result.stderr = $res.stderr
$module.FailJson("Scoop bootstrap installation failed.")
}
$module.Warn("Scoop was missing from this system, so it was installed during this task run.")
}
$module.Result.changed = $true
# Refresh PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# locate the newly installed scoop.ps1
$scoop_app = Get-Command -Name scoop.ps1 -Type ExternalScript -ErrorAction SilentlyContinue
}
if ($module.CheckMode -and $null -eq $scoop_app) {
$module.Result.skipped = $true
$module.Result.msg = "Skipped check mode run on win_scoop as scoop.ps1 cannot be found on the system"
$module.ExitJson()
}
if ($null -eq $scoop_app -or -not (Test-Path -Path $scoop_app.Path)) {
$module.FailJson("Failed to find scoop.ps1, make sure it is added to the PATH")
}
return $scoop_app.Path
}
function Get-ScoopPackages {
param(
[Parameter(Mandatory = $true)] [string]$scoop_path
)
$command = Argv-ToString -arguments @("powershell.exe", $scoop_path, "export")
$res = Run-Command -Command $command
if ($res.rc -ne 0) {
$module.Result.command = $command
$module.Result.rc = $res.rc
$module.Result.stdout = $res.stdout
$module.Result.stderr = $res.stderr
$module.FailJson("Error checking installed packages")
}
$installed_packages = $res.stdout -split "`n" |
Select-String '(.*?) \(v:(.*?)\) \[(.*?)\]' |
ForEach-Object {
New-Object PSObject -Property ([Ordered]@{
"Package" = $_.Matches[0].Groups[1].Value
"Version" = $_.Matches[0].Groups[2].Value
"Bucket" = $_.Matches[0].Groups[3].Value
})
}
return $installed_packages
}
function Install-ScoopPackage {
param(
[Parameter(Mandatory = $true)] [string]$scoop_path,
[Parameter(Mandatory = $true)] [String[]]$packages
)
$arguments = [System.Collections.ArrayList]@("powershell.exe", $scoop_path, "install")
$arguments.AddRange($packages)
$command = Argv-ToString -arguments $arguments
$res = Run-Command -Command $command
$module.Result.rc = $res.rc
if ($module.Verbosity -gt 1) {
$module.Result.stdout = $res.stdout
}
$module.Result.changed = $true
}
$scoop_path = Install-Scoop
$installed_packages = Get-ScoopPackages -scoop_path $scoop_path
if ($state -in @("present")) {
$missing_packages = [System.Collections.ArrayList]@()
foreach ($package in $name) {
if ($installed_packages.Package -notcontains $package) {
$missing_packages.Add($package)
}
}
if ($missing_packages) {
Install-ScoopPackage -scoop_path $scoop_path -packages $missing_packages
}
}
$module.ExitJson()