-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ps1
More file actions
63 lines (55 loc) · 1.98 KB
/
bootstrap.ps1
File metadata and controls
63 lines (55 loc) · 1.98 KB
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
function TryCreateSymlink($source, $target) {
if (-Not (Test-Path $source)) {
Write-Host "Error while creating symlink: item $source does not exist" -ForegroundColor Red
return
}
if (Test-Path $target) {
# TODO: check if it is a symlink and points to the same target
Write-Host "Item $target already exists. Do you want to overwrite it? (y/n) " -ForegroundColor Yellow -NoNewline
$answer = Read-Host
if ($answer -ne "y" -and $answer -ne "Y") {
return
}
try {
Remove-Item $target -Recurse -Force -ErrorAction Stop
}
catch {
Write-Host "Error while removing $target" -ForegroundColor Red
return
}
}
try {
$null = New-Item -ItemType SymbolicLink -Path $target -Value $source -Force -ErrorAction Stop
Write-Host "Symlink created successfully at $target" -ForegroundColor Green
}
catch {
Write-Host "Error while creating symlink at $target" -ForegroundColor Red
}
}
$getSourceFunctionName = "GetSource"
$getTargetFunctionName = "GetTarget"
$moduleSuffix = "links.psm1"
$rootDirectory = Get-Location
$modules = Get-ChildItem -Path $rootDirectory -File -Filter "*$moduleSuffix" -Recurse
$moduleNumber = 1
foreach ($module in $modules) {
Import-Module $module.FullName -Prefix $moduleNumber -Force
try {
$source = Invoke-Expression "$moduleNumber$getSourceFunctionName $rootDirectory"
}
catch {
Write-Host "Error: $module.FullName does not define function $getSourceFunctionName" -ForegroundColor Red
continue
}
# TODO: DRY
try {
$target = Invoke-Expression "$moduleNumber$getTargetFunctionName $rootDirectory"
}
catch {
Write-Host "Error: $module.FullName does not define function $getTargetFunctionName" -ForegroundColor Red
continue
}
TryCreateSymlink $source $target
$moduleNumber++
}
Write-Host "Bootstrap finished." -ForegroundColor Blue