-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.ps1
145 lines (119 loc) Β· 4.28 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
<#
.SYNOPSIS
Dotfiles setup script
.DESCRIPTION
This script automates the setup of dotfiles in a Windows environment. It checks for
required tools (Git and Winget), installs them if necessary, clones a specified
dotfiles repository, and sets up the dotfiles in the user's profile directory.
.LINK
GitHub - https://github.com/RustyTake-Off
GitHub Repo - https://github.com/RustyTake-Off/dotfiles
.NOTES
Author - RustyTake-Off
#>
[CmdletBinding(SupportsShouldProcess)]
param()
# Preferences
$errAction = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
$progressAction = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
# Configuration variables
$repoUrl = 'https://github.com/RustyTake-Off/dotfiles.git'
$dotfilesPath = "$HOME/.dotfiles"
$branchName = 'winfiles'
$dotfilesScriptPath = "$HOME/.dots/scripts/set-dotfiles.ps1"
# ANSI escape sequences for different colors
$colors = @{
red = [char]27 + '[31m'
green = [char]27 + '[32m'
yellow = [char]27 + '[33m'
blue = [char]27 + '[34m'
purple = [char]27 + '[35m'
reset = [char]27 + '[0m'
}
# Function definitions
function Write-ColoredMessage {
<#
.SYNOPSIS
Write message with color
#>
param (
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true)]
[ValidateScript({
if (-not $colors.ContainsKey($_)) {
throw "Invalid color '$($_)'. Available colors are: $($colors.Keys -join ', ')"
}
$true
})]
[string]$Color
)
Write-Host "$($colors[$Color])$Message$($colors.reset)"
}
function Install-Package {
<#
.SYNOPSIS
Installs packages
#>
param (
[Parameter(Mandatory)]
[string]$PackageName
)
if (Get-Command -Name $PackageName) {
return $false
}
Write-ColoredMessage "$PackageName is not installed" 'red'
while ($true) {
$choice = Read-Host -Prompt "Do you want to install $($colors.yellow)$PackageName$($colors.reset) (y/n)?"
$choice = $choice.Trim().ToLower() -replace ' ', ''
switch ($choice) {
'y' { return $true }
'yes' { return $true }
'n' { Write-ColoredMessage 'Stopping script. Bye, bye' 'red'; break 1 }
'no' { Write-ColoredMessage 'Stopping script. Bye, bye' 'red'; break 1 }
default { Write-ColoredMessage "Invalid input, please enter 'y' or 'n'" 'red' }
}
}
}
# Main execution logic
try {
# Check and install winget
if (Install-Package -PackageName 'winget') {
Write-ColoredMessage 'Installing Winget and its dependencies...' 'yellow'
Invoke-RestMethod -Uri 'https://github.com/asheroto/winget-install/releases/latest/download/winget-install.ps1' | Invoke-Expression
Write-ColoredMessage 'Installed Winget and its dependencies' 'green'
} else {
Write-ColoredMessage 'Winget is installed' 'green'
}
# Check and install git
if (Install-Package -PackageName 'git') {
Write-ColoredMessage 'Installing Git...' 'yellow'
Start-Process winget -ArgumentList 'install --exact --id Git.Git --source winget --interactive --accept-package-agreements --accept-source-agreements' -NoNewWindow -Wait
Write-ColoredMessage 'Installed Git' 'green'
} else {
Write-ColoredMessage 'Git is installed' 'green'
}
# Clone dotfiles
if (-not (Test-Path -Path $dotfilesPath -PathType Container)) {
Write-ColoredMessage 'Cloning dotfiles...' 'yellow'
git clone --bare $repoUrl $dotfilesPath
git --git-dir=$dotfilesPath --work-tree=$HOME checkout $branchName
git --git-dir=$dotfilesPath --work-tree=$HOME config status.showUntrackedFiles no
} else {
throw "Directory '$dotfilesPath' already exists"
}
# Run dotfiles script
if (Test-Path -Path $dotfilesScriptPath -PathType Leaf) {
Write-ColoredMessage 'Finishing dotfiles setup...' 'yellow'
& $dotfilesScriptPath -SkipClone
} else {
throw "Script file in path '$dotfilesScriptPath' does not exist"
}
} catch {
throw "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
} finally {
$ErrorActionPreference = $errAction
$ProgressPreference = $progressAction
}