-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.ps1
170 lines (149 loc) · 6.26 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
<#
.SYNOPSIS
Script to configure my dotfiles
.DESCRIPTION
This script helps automate the deployment and configuration of your dotfiles.
.NOTES
This script can be only run in powershell 7+
.LINK
https://github.com/Deep7k/windows-dotfiles/blob/master/README.md
.EXAMPLE
.\setup.ps1 -WithFonts
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[switch]
$WithFonts,
[Parameter(Mandatory = $false)]
[switch]
$WithOptionalApps
)
# Perform important tests before main script execution
# Check if powershell is running as administrator
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Please run this script as an Administrator!" -ForegroundColor Red
break
}
# Check the powershell version
$version = $PSVersionTable.PSVersion
if ($version.Major -ne 7) {
Write-Host "This script requires PowerShell 7. The current version is $version." -ForegroundColor Red
exit
}
# Test for internet availability
function Test-InternetConnection {
try {
Test-Connection -ComputerName www.google.com -Count 1 -ErrorAction Stop > $null
return $true
}
catch {
Write-Warning "Internet connection is required but not available. Please connect to WiFi/Ethernet "
return $false
}
}
if (-not (Test-InternetConnection)) {
break
}
# Check and install App Dependencies
Write-Host "Installing App Dependencies..."
try {
winget install -e --accept-source-agreements --accept-package-agreements Microsoft.WindowsTerminal > $null
winget install -e --accept-source-agreements --accept-package-agreements JanDeDobbeleer.OhMyPosh > $null
}
catch {
Write-Error "Failed to install Dependencies. Error: $_"
}
# Install optional apps if parameter is specified
try {
if ($WithOptionalApps) {
Write-Host "Installing Optional Apps..."
winget import ".\optional-apps.json" --accept-source-agreements --accept-package-agreements --ignore-versions
}
}
catch {
Write-Error "Failed to install Optinal apps Error: $_"
}
# Install CaskaydiaCove NerdFont. Required for Oh-my-posh prompt
try {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$fontFamilies = (New-Object System.Drawing.Text.InstalledFontCollection).Families.Name
if (($fontFamilies -notcontains "CaskaydiaCove NF") -and ($WithFonts) ) {
Write-Host "Installing Cascadiacode NF..."
Invoke-WebRequest -Uri "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/CascadiaCode.zip" -OutFile ".\CascadiaCode.zip"
Expand-Archive -Path ".\CascadiaCode.zip" -DestinationPath ".\CascadiaCode" -Force
$destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path ".\CascadiaCode" -Recurse -Filter "*.ttf" | ForEach-Object {
If (-not(Test-Path "C:\Windows\Fonts\$($_.Name)")) {
$destination.CopyHere($_.FullName, 0x10)
}
}
Remove-Item -Path ".\CascadiaCode" -Recurse -Force
Remove-Item -Path ".\CascadiaCode.zip" -Force
}
# Install Firacode Nerd Font if parameter is specified
if (($fontFamilies -notcontains "FiraCode Nerd Font") -and ($WithFonts) ) {
Write-Host "Installing Fira Code NF..."
Invoke-WebRequest -Uri "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/FiraCode.zip" -OutFile ".\FiraCode.zip"
Expand-Archive -Path ".\FiraCode.zip" -DestinationPath ".\FiraCode" -Force
$destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path ".\FiraCode" -Recurse -Filter "*.ttf" | ForEach-Object {
If (-not(Test-Path "C:\Windows\Fonts\$($_.Name)")) {
$destination.CopyHere($_.FullName, 0x10)
}
}
Remove-Item -Path ".\FiraCode" -Recurse -Force
Remove-Item -Path ".\FiraCode.zip" -Force
}
}
catch {
Write-Error "Failed to download or install the Nerd font. Error: $_"
}
Write-Host "Configuring Dotfiles..."
$wtSettingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$wtSettingsTarget = (Resolve-Path "$PSScriptRoot\windows-terminal-settings.json").Path
$powershellDirPath = "$env:USERPROFILE\Documents\Powershell"
$powershellDirTarget = (Resolve-Path "$PSScriptRoot\Powershell").Path
$gitconfigPath = "$env:USERPROFILE\.gitconfig"
$gitconfigTarget = (Resolve-Path "$PSScriptRoot\gitconfig").Path
$notepadTarget = (Resolve-Path "$PSScriptRoot\Notepad++").Path
function BackupAndLink {
param (
[string]$path,
[string]$target
)
if (Test-Path $path) {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$backupName = $path + ".old." + $timestamp
Rename-Item -Path $path -NewName $backupName -Force -ErrorAction Stop > $null
Write-Host "Backup created: $backupName"
}
New-Item -ItemType SymbolicLink -Path $path -Target $target -ErrorAction Stop > $null
Write-Host "Symbolic link created: $path -> $target"
}
try {
BackupAndLink -path $wtSettingsPath -target $wtSettingsTarget
BackupAndLink -path $powershellDirPath -target $powershellDirTarget
BackupAndLink -path $gitconfigPath -target $gitconfigTarget
}
catch {
Write-Error "Unable to create symlinks Error: $_"
}
Compress-Archive -Path $env:APPDATA\Notepad++ -DestinationPath $env:APPDATA\Notepad++\ConfigBackup$(Get-Date -Format "yyyyMMddHH") -CompressionLevel Fastest
Copy-Item -Path $notepadTarget -Destination $env:APPDATA\Notepad++ -Force
# Check and install PS Modules
Write-Host "Installing Powershell modules..."
try {
Install-Module -Name Terminal-Icons -Repository PSGallery -Force
Install-Module -Name posh-git -Repository PSGallery -Force
}
catch {
Write-Error "Failed to install Terminal Icons and posh git. Error: $_"
}
# Final check and message to the user
if ((Test-Path -Path $PROFILE) -and (winget list --name "OhMyPosh" -e) -and ($fontFamilies -contains "CaskaydiaCove NF")) {
Write-Host "Setup completed successfully. Please restart your PowerShell session to apply changes." -ForegroundColor Green
}
else {
Write-Warning "Setup completed with errors. Please check the error messages above."
}