-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.ps1
270 lines (226 loc) · 8.96 KB
/
setup.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
# This PowerShell script downloads and installs various tools onto my Windows
# machine, then downloads the proper configuration files from this GitHub
# repository to configure each of them.
# ================================= Helpers ================================== #
# Constructs and returns a string that indicates where the repository should be
# installed on the Windows system.
function get_install_path
{
# construct the path to the directory
$result = Join-Path -Path "$home" -ChildPath ".shuggtools"
# if the folder doesn't already exist, create it
if (!(Test-Path -Path "$result"))
{ New-Item -ItemType Directory -Path "$result" }
return $result
}
# Generates and installs a `profile.ps1` file to the appropriate location in
# Windows, such that all of my PowerShell aliases/utilities are available
# whenever PowerShell is launched.
#
# Returns 0 on success, and non-zero on failure.
#
# See this link for more information on customizing the PowerShell environment:
# https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/creating-profiles
function install_powershell_profile
{
Param
(
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$InstallPath
)
# retrieve a list of all PowerShell scripts in the source directory
# (`*.ps1`). These will represent all of the files that need to be added to
# the `profile.ps1` file we're about to generate
$scripts = Get-ChildItem -Path "$SourcePath" -Filter "*.ps1" -Recurse
if ($scripts.Length -eq 0)
{
Write-Warning "Could not find any PowerShell scripts within: `"$SourcePath`"."
return 1
}
# make sure the parent directory (and grandparent, great-grandparent, etc.)
# exists, before we attempt to write to the PowerShell profile file
$install_parent_path = Split-Path -Path "$InstallPath"
if (!(Test-Path -Path "$install_parent_path"))
{ New-Item -ItemType Directory -Path "$install_parent_path" -Force }
# open the file for writing (forcefully create it if it doesn't exist yet)
Set-Content -Path "$InstallPath" `
-Force `
-Value "# Connor's PowerShell Profile`n"
# get the full path to *this* script we're currently executing (we'll use
# this in the below loop)
$this_script_path = (Get-Item -Path "$PSCommandPath").FullName
# now, for each of the scripts we collected earlier, add a line to the file
# that sources the script
foreach ($script in $scripts)
{
# resolve the script's path to ensure we are working with the full,
# absolute file path
$script_path = (Get-Item -Path "$script").FullName
# make sure this particular script isn't the one we're executing *right
# now*. Skip it, if so
if ("$script_path" -eq "$this_script_path")
{ continue }
# add a line to the file
Add-Content -Path "$InstallPath" `
-Value ". $script_path"
}
return 0
}
# Takes in information about a GitHub repository and pings GitHub to retrieve
# information about a repo's release. If the release version is not specified,
# the latest release is used.
function github_retrieve_release
{
# TODO
}
# =================================== Vim ==================================== #
# Main function for downloading and setting up Vim.
#
# Returns 0 on success, and non-zero on failure.
function vim_main
{
Param
(
[Parameter(Mandatory=$true)]
[string]$SourcePath
)
# TODO: Install Vim from here: https://github.com/vim/vim-win32-installer/releases
# if the `vimfiles` directory doesn't exist yet, create it
$install_path = Join-Path -Path "$home" -ChildPath "vimfiles"
if (!(Test-Path -Path "$install_path"))
{ New-Item -ItemType Directory -Path "$install_path" }
# install my vimrc
$result = vim_install_vimrc -SourcePath "$SourcePath" `
-InstallPath "$install_path"
if ($result -ne 0)
{ return $result }
# install my color scheme
$result = vim_install_colorscheme -SourcePath "$SourcePath" `
-InstallPath "$install_path"
if ($result -ne 0)
{ return $result }
# eventually, I want this script to install my Vim plugins
# TODO
return 0
}
# Installs my vimrc file.
function vim_install_vimrc
{
Param
(
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$InstallPath
)
# create source and destination paths for the vimrc
$vim_src = Join-Path -Path "$SourcePath" -ChildPath "vim"
$vimrc_src = Join-Path -Path "$vim_src" -ChildPath "vimrc.vim"
$vimrc_dst = Join-Path -Path "$InstallPath" -ChildPath "vimrc"
if (!(Test-Path -Path "$vimrc_src"))
{
Write-Warning "Could not find vimrc at: `"$vimrc_src`". Install failed."
return 1
}
# copy the file over
Copy-Item -Path "$vimrc_src" -Destination "$vimrc_dst" -Force
Write-Host "Installed vimrc to: `"$vimrc_dst`"."
return 0
}
# Installs my preferred Vim colorscheme.
function vim_install_colorscheme
{
Param
(
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$InstallPath
)
# first, create the vim colors directory if it doesn't exist
$colors_path = Join-Path -Path "$InstallPath" -ChildPath "colors"
if (!(Test-Path -Path "$colors_path"))
{ New-Item -ItemType Directory -Path "$colors_path" }
# next, download my preferred colorscheme from github
# TODO
return 0
}
# ================================= WezTerm ================================== #
# Main function for downloading and setting up WezTerm.
#
# Returns 0 on success, and non-zero on failure.
function wezterm_main
{
Param
(
[Parameter(Mandatory=$true)]
[string]$SourcePath
)
# TODO: Install WezTerm from here: https://github.com/wezterm/wezterm/releases/latest
# create source/destination paths for my wezterm config file
$wezterm_src = Join-Path -Path "$SourcePath" -ChildPath "wezterm"
$wezterm_config_src = Join-Path -Path "$wezterm_src" -ChildPath "wezterm.lua"
$wezterm_config_dst = Join-Path -Path "$home" -ChildPath ".wezterm.lua"
# make sure the source config file exists
if (!(Test-Path -Path "$wezterm_config_src"))
{
Write-Warning "Could not find WezTerm config file at: `"$wezterm_config_src`". Install failed."
return 1
}
# copy the config file into the destination
Copy-Item -Path "$wezterm_config_src" `
-Destination "$wezterm_config_dst" `
-Force
Write-Host "Installed WezTerm config to: `"$wezterm_config_dst`"."
return 0
}
# ============================ Main Functionality ============================ #
# Main function.
function main
{
Param
(
[Parameter(Mandatory=$false)]
[string]$SourcePath=$PSScriptRoot
)
# retrieve the source path, at which the repository's contents exist
$source = (Resolve-Path -Path "$SourcePath").ProviderPath
Write-Host "Source path: `"$source`""
# set upand retrieve the path at which we'll install the repo into Windows
$destination = get_install_path
Write-Host "Install path: `"$destination`""
# copy all files from the source path into the destination path (excluding
# some directories, like the `.git` metadata and the `links` directory,
# which is used in Linux, but not Windows)
Copy-Item -Path "${source}\*" `
-Destination "${destination}" `
-Recurse `
-Force `
-Exclude @("links", ".git")
Write-Host "Copied all files to install path: `"${destination}`"."
# next, we need to create a `profile.ps1` file that contains all of my
# powershell utilities in `powershell/`. This `profile.ps1` file is what
# PowerShell sources whenever it's launched
$profile_path = $PROFILE.CurrentUserAllHosts
$result = install_powershell_profile `
-SourcePath "$source" `
-InstallPath "$profile_path"
if ($result -ne 0)
{
Write-Warning "Failed to install PowerShell profile."
return 1
}
Write-Host "Installed PowerShell profile to: `"$profile_path`"."
# install Vim configuration/plugin files
$result = vim_main -SourcePath "$source"
if ($result -ne 0)
{ Write-Warning "Failed to install Vim configurations and plugins." }
# install WezTerm configuration file
$result = wezterm_main -SourcePath "$source"
if ($result -ne 0)
{ Write-Warning "Failed to install WezTerm configuration." }
}
$result = main @args
exit $result