-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.ps1
184 lines (158 loc) · 5.16 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
#
# Helper functions
#
function Get-User-Consent {
Param (
$prompt
)
$response = Read-Host -Prompt "'$prompt' [y/N]"
# TODO: match uppercase Y too.
if ($response -eq "y") {
return $true
} else {
return $false
}
}
# Sniped from https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-Command-Exists {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
if (Get-Command $command) {
return $true
}
} catch {
Write-Host "'$command' is not installed."
return $false
} finally {
$ErrorActionPreference=$oldPreference
}
}
function Update-Choco-Packages {
# Resolve the path to the chocolatey executable.
$chocoPath = Get-Command choco.exe -ErrorAction SilentlyContinue
$process = Start-Process `
-FilePath $chocoPath.Path `
-ArgumentList "upgrade all -y" `
-Verb RunAs -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Host "Chocolatey failed to update packages."
}
}
# Installs a package using chocolatey. Accepts a package name and an executable
# to check if it's already installed.
function Install-With-Choco-If-Not-Exists {
Param (
$package,
$bin_name
)
if ($null -eq $bin_name) {
$bin = $package
} else {
$bin = $bin_name
}
if (Test-Command-Exists $bin) {
Write-Host "'$package' is already installed, skipping..."
} else {
if (Get-User-Consent "Install '$package'?") {
Write-Host "Installing '$package'..."
Start-Process `
-FilePath "choco" `
-ArgumentList "install -y $package" `
-Verb RunAs `
-Wait
} else {
Write-Host "Skipping '$package'..."
}
}
}
# Installs a package using winget. Accepts a package name and an executable
# to check if it's already installed.
function Install-With-Winget-If-Not-Exists {
Param (
$package,
$bin_name
)
if ($null -eq $bin_name) {
$bin = $package
} else {
$bin = $bin_name
}
if (Test-Command-Exists $bin) {
Write-Host "'$package' is already installed, skipping..."
} else {
if (Get-User-Consent "Install '$package'?") {
Write-Host "Installing '$package'..."
Start-Process `
-FilePath "winget" `
-ArgumentList "install $package" `
-Verb RunAs `
-Wait
} else {
Write-Host "Skipping '$package'..."
}
}
}
function Install-Vim-Plugins {
Param ($plugin_names)
$github_url = "https://github.com"
foreach ($plugin_name in $plugin_names) {
Invoke-Expression `
"git clone $github_url/$plugin_name C:\Users\$env:UserName\vimfiles\plugin\$plugin_name"
}
}
if (Test-Command-Exists "choco") {
Write-Host "Choco is already installed, updating..."
# Update choco packages
Update-Choco-Packages
} else {
if (Get-User-Consent "Install Chocolatey?") {
Write-Host "Installing Chocolatey..."
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol =
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072
# Execute as admin
Start-Process `
-FilePath "powershell" `
-ArgumentList "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" `
-Verb RunAs `
-Wait
} else {
Write-Host "Chocolatey is needed to run this script. Exiting..."
return $false
}
}
# Update all winget packages
# Print that we're going to update all winget packages
Write-Host "Updating all winget packages..."
Start-Process `
-FilePath "winget" `
-ArgumentList "upgrade --all --accept-source-agreements --accept-package-agreements --accept-licenses" `
-Verb RunAs `
-Wait
# Configure PowerShell
Copy-Item `
-Path "./src/profile.ps1" `
-Destination `
"C:\Users\$env:UserName\Documents\WindowsPowerShell\profile.ps1"
# Install choco packages:
Install-With-Winget-If-Not-Exists "Git.Git" "git"
Install-With-Choco-If-Not-Exists "curl"
# Vim's a big one:
Install-With-Winget-If-Not-Exists "vim.vim" "vim"
Copy-Item -Path "./src/vimrc" -Destination "C:\Users\$env:UserName\_vimrc"
Install-With-Choco-If-Not-Exists "fzf"
## Install Vim-Plug
Invoke-WebRequest `
-useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim `
| New-Item $HOME/vimfiles/autoload/plug.vim -Force
Invoke-Expression "vim +PlugInstall +PlugClean +qall"
# Install other packages
Install-With-Winget-If-Not-Exists "Microsoft.Powershell" "pwsh"
Install-With-Winget-If-Not-Exists "Microsoft.VisualStudioCode" "code"
Install-With-Winget-If-Not-Exists "GnuWin32.Make" "make"
Install-With-Winget-If-Not-Exists "Python.Python.3.11" "python"
Install-With-Winget-If-Not-Exists "OpenJS.NodeJS.LTS" "node"
Install-With-Winget-If-Not-Exists "Insecure.Nmap" "nmap"
Install-With-Winget-If-Not-Exists "GnuWin32.Grep" "grep"