-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-config-windows.ps1
332 lines (203 loc) · 8.7 KB
/
deploy-config-windows.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
<#
.SYNOPSIS
Install and manage symlinks for configuration files in a Windows development environment.
.DESCRIPTION
This script reads environment variables from a .env file and creates symbolic links for
configuration files based on paths specified in each subfolder's "paths.txt" file.
Symbolic links allow the configurations to be used without copying files directly,
facilitating easy updates and management.
.PARAMETER dryRun
When set to $true, performs a dry run without making any actual changes.
.PARAMETER debug
When set to $true, outputs additional debug information for each operation.
.EXAMPLE
.\install.ps1 -dryRun $true -debug $true
Performs a dry run with debug information enabled.
.EXAMPLE
.\install.ps1 -dryRun $false
Executes the script, creating symlinks as specified in the configuration files.
.NOTES
#>
param (
[bool] $dryRun = $true,
[bool] $debug = $true
)
# Path to the logger module file
$modulePath = "./utils/powershell/logging-and-output-functions.psm1"
# Import the module
Import-Module $modulePath
function Read-AndSetEnvironmentVariables {
# Define the path to your .env file
$envFilePath = ".env"
# Check if the .env file exists
if (Test-Path -Path $envFilePath) {
# Read each line in the .env file
Get-Content -Path $envFilePath | ForEach-Object {
$line = $_.Trim()
# Ignore empty lines or lines that start with #
if ($line -and -not $line.StartsWith("#")) {
# Split at the first "=" to get the key and value
$key, $value = $line -split "=", 2
$key = $key.Trim()
$value = $value.Trim()
# Expand if any Environment paths
$expandedValue = Invoke-Expression -Command "`"$value`""
# Set the environment variable (PowerShell will automatically expand $Env: variables)
[System.Environment]::SetEnvironmentVariable($key, $expandedValue, "Process")
}
}
} else {
Write-Message "Environment variables (.env) file not found!" "WARNING"
}
}
function Test-ForEnvironmentVariable {
param(
[string] $path
)
# Regular expression pattern to match $Env:<variable>
$pattern = '\$Env:([a-zA-Z_][\w]*)|%([a-zA-Z_][\w]*)%'
# Replace all occurrences of environment variables in the path
if ( $path -match $pattern ) {
# Determine if it's a $Env: or %...% format and retrieve the variable name
$envVariable = if ($matches[1]) { $matches[1] } else { $matches[2] }
# Check if the environment variable is set
$envValue = [System.Environment]::GetEnvironmentVariable($envVariable)
# If the environment variable is not defined, return $null for the function
if (-not $envValue) {
Write-Message "Environment variable '$envVariable' is not set" "WARNING"
return $null
} else {
$resolvedPath = $path -replace $pattern, $envValue
return $resolvedPath
}
} else {
return $path
}
}
function Test-ForAbsolutePath {
param (
[string]$path,
[string]$baseFolder # Optional base folder for relative paths
)
# Regex pattern to match a Windows drive letter path, e.g., "C:\" or "D:\"
$driveLetterPattern = '^[a-zA-Z]:\\'
# Check if the path is absolute
if ($path -match $driveLetterPattern) {
# Absolute path, return it as is without checking existence
return [System.IO.Path]::GetFullPath($path)
} else {
# Relative path: resolve it against the base folder or current directory
$resolvedPath = if ($baseFolder) {
Join-Path -Path $baseFolder -ChildPath $path
} else {
Join-Path -Path (Get-Location) -ChildPath $path
}
# Check if the resolved relative path exists
if (Test-Path -Path $resolvedPath) {
return [System.IO.Path]::GetFullPath($resolvedPath)
} else {
Write-Message "Path must exist if relative (i.e. targe path): '$path'." "WARNING"
# Path does not exist; return null
return $null
}
}
}
function Test-AndResolvePath {
param (
[string] $path,
[string] $baseFolder
)
Write-Message "Verifying and resolving path ($path)..." "DEBUG"
if ( $resolvedPath = Test-ForEnvironmentVariable $path ) {
return Test-ForAbsolutePath $resolvedPath $baseFolder
} else {
return $null
}
}
function New-DirectoryIfMissing {
param(
[string]$path
)
if (-not (Test-Path -Path $path)) {
Write-Host "Creating symlink parent directory ($path)..." "DEBUG"
New-Item -ItemType Directory -Path $path -Force
}
}
function New-Symlink {
param(
[string] $linkPath,
[string] $targetPath
)
# Check if the linkPath exists
if (Test-Path $linkPath) {
# Get the item at linkPath
$item = Get-Item $linkPath -Force
# Check if file is a symlink
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
# It is a symbolic link, so get the current target of the symlink
$currentTarget = (Get-Item -Path $linkPath -Force).Target
# Check if current target path is same as target from file
if ($currentTarget -eq $targetPath) {
Write-Message "Symlink already exists and points to the correct target ($targetPath)" "INFO"
# Exit the function as the symlink is already correct
return
} else {
Write-Message "Symlink exists, but points to a different target ($currentTarget). Recreating..." "INFO"
# Remove the incorrect symlink
if ( -not $dryRun ) { Remove-Item -Path $linkPath -Force }
}
} else {
# It is not a symbolic link (regular file or directory)
Write-Message "A file or directory exists in the symlink path ($linkPath) and is not a symlink. Replacing..." "INFO"
if ( -not $dryRun ) { Remove-Item -Path $linkPath -Force -Recurse }
}
}
# If we reach here, either the path didn't exist, or it was deleted. Create the symlink.
Write-Message "Creating new symlink: $linkPath -> $targetPath" "INFO"
# Create new symlink
if ( -not $dryRun ) { New-Item -ItemType SymbolicLink -Path $linkPath -Target $targetPath }
}
function Update-Symlinks {
# Iterate through each subfolder of the root directory
Get-ChildItem -Path $(Get-Location) -Recurse -Directory | ForEach-Object {
$folder = $_.FullName
$folderName = $_.Name
# Check if paths.txt exists in the folder
$pathsFile = Join-Path -Path $folder -ChildPath "paths.txt"
# Check if paths file exists
if (Test-Path $pathsFile) {
Write-Message "Processing symlink paths for '$folderName'..." "INFO"
# Read each line from the paths.txt
Get-Content -Path $pathsFile | ForEach-Object {
Write-Message "Processing new line ($_)" "DEBUG"
# Get and trim new line
$line = $_.Trim()
# Check if line has content and starts with a "w" for "Windows"
if ( $line -and $line.StartsWith("w") ) {
# Split the line into source file and symlink path
$parts = $line -split "\s+", 3
# Check if two strings / paths
if ($parts.Count -eq 3) {
# Verify paths and resolve to full path
$targetPath = Test-AndResolvePath $parts[1] $folder
$symlinkPath = Test-AndResolvePath $parts[2] $folder
if ( $targetPath -and $symlinkPath) {
# Ensure the parent directory of the symlink exists
$symlinkDir = Split-Path -Path $symlinkPath -Parent
# Create symlink parent directories if they don't exist
New-DirectoryIfMissing -path $symlinkDir
# Create the symlink
New-Symlink -linkPath $symlinkPath -targetPath $targetPath
} else {
Write-Message "Issues with either target file path or symlink path." "ERROR"
}
} else {
Write-Message "Invalid line format in paths file. Entry must contain space separated system (w/l), target- and symlink path." "ERROR"
}
}
}
}
}
}
Read-AndSetEnvironmentVariables
Update-Symlinks