dotfiles/install_windows.ps1

215 lines
7.3 KiB
PowerShell
Raw Normal View History

2024-08-27 10:32:13 +00:00
# Define paths for tools and configurations
$dotfilesRepo = "$env:TMP\dotfiles"
$alacrittyConfigDir = "$env:APPDATA\alacritty"
$psProfile = "$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1"
$repoUrl = "https://github.com/Baipyrus/dotfiles.git"
2024-08-27 10:33:44 +00:00
# Function to determine if the current directory is the dotfiles repository
2024-08-27 11:53:53 +00:00
function IsGitRepository
2024-08-27 10:33:44 +00:00
{
2024-08-27 11:53:53 +00:00
param (
[string]$dir,
[string]$url
)
try
{
2024-08-27 11:53:53 +00:00
$isRepo = git -C $dir rev-parse --is-inside-work-tree 2>$null
$originUrl = git -C $dir remote get-url origin 2>$null
return $isRepo -eq 'true' -and $originUrl -eq $url
} catch
{
return $false
}
}
# Check if the script is running inside the dotfiles repository
$currentDir = (Get-Location).Path
2024-08-27 11:53:53 +00:00
if (IsGitRepository -dir $currentDir -url $repoUrl)
2024-08-27 10:33:44 +00:00
{
Write-Host "Already inside the dotfiles repository. Skipping clone step and pulling..." -ForegroundColor Yellow
$dotfilesRepo = $currentDir
git pull
} else
{
# Clone dotfiles repository to TMP if not already inside it
if (-not (Test-Path $dotfilesRepo))
{
Write-Host "Cloning dotfiles repository..." -ForegroundColor Cyan
git clone $repoUrl $dotfilesRepo
} else
{
Write-Host "Pulling latest changes from dotfiles repository..." -ForegroundColor Cyan
git -C $dotfilesRepo pull
}
}
2024-08-27 10:35:30 +00:00
# Function to copy files with overwrite prompt
function CopyFileWithPrompt
{
param (
[string]$source,
[string]$destination
)
if (Test-Path $destination)
{
$overwrite = Read-Host "File $destination exists. Overwrite? (y/n)"
if ($overwrite -ne 'y')
{
Write-Host "Skipping $destination" -ForegroundColor Yellow
return
}
}
Copy-Item -Path $source -Destination $destination -Force
2024-08-27 10:33:44 +00:00
}
# Function to handle URL files: download files or clone repositories
function ProcessUrlFiles
{
param (
[string]$sourceDir,
2024-08-27 13:38:57 +00:00
[Parameter(Mandatory=$false)][string]$destinationDir,
[Parameter(Mandatory=$false)][string]$fileExt
)
# Ensure the destination directory exists
2024-08-27 13:38:57 +00:00
if ($destinationDir -and (-not (Test-Path $destinationDir)))
{
2024-08-27 13:01:57 +00:00
Write-Host "Creating destination directory $destinationDir..." -ForegroundColor Cyan
2024-08-27 14:33:27 +00:00
New-Item -ItemType Directory -Path $destinationDir | Out-Null
}
2024-08-27 11:53:53 +00:00
# Create temporary directory for curl
$appname = $sourceDir.Split('\')[-1]
$tmpApp = "$env:TMP\$appname-config"
2024-08-27 11:53:53 +00:00
if (-not (Test-Path $tmpApp))
2024-08-27 14:33:27 +00:00
{ New-Item -ItemType Directory -Path $tmpApp | Out-Null
2024-08-27 11:53:53 +00:00
}
2024-09-02 06:53:27 +00:00
Set-Location $tmpApp
2024-08-27 11:53:53 +00:00
# Find all .url files in the source directory
$urlFiles = Get-ChildItem -Path $sourceDir -Filter '*.url'
foreach ($file in $urlFiles)
{
$url = Get-Content $file.FullName
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$destinationPath = "$destinationDir\$fileName"
2024-08-27 11:53:53 +00:00
if (-not ($url -match 'git@|https://.*\.git'))
{
# Otherwise, download the file
$extension = [System.IO.Path]::GetExtension($url)
2024-08-27 13:38:57 +00:00
# Force apply $fileExt if given
if ($fileExt)
{
$extension = $fileExt
$url += $fileExt
}
$destinationPath = "$destinationDir\$fileName$extension"
2024-08-27 13:38:57 +00:00
# Respond only if destination is provided
$conditional = " to $destinationPath"
if (-not $destinationDir)
{ $conditional = ''
}
Write-Host "Downloading $fileName from $url$conditional..." -ForegroundColor Cyan
Invoke-RestMethod $url -OutFile $fileName$extension
2024-08-27 11:53:53 +00:00
$tmpDestination = "$tmpApp\$fileName$extension"
2024-08-27 13:38:57 +00:00
# Copy only if destination is provided
if ($destinationDir)
{ CopyFileWithPrompt $tmpDestination $destinationPath
}
2024-08-27 11:53:53 +00:00
continue
}
# Use $tmpApp as destination in case of git repo
if (-not $destinationDir)
{ $destinationPath = "$tmpApp\$fileName"
}
2024-08-27 11:53:53 +00:00
# If the URL is a git repository, pull it
if (IsGitRepository -dir $destinationPath -url $url)
{
2024-08-27 13:01:57 +00:00
Write-Host "Pulling inside existing repository $fileName..." -ForegroundColor Cyan
2024-08-27 11:53:53 +00:00
git -C $destinationPath pull
continue
}
2024-08-27 11:53:53 +00:00
# Or otherwise clone it
Write-Host "Cloning $fileName from $url to $destinationPath..." -ForegroundColor Cyan
git clone $url $destinationPath
}
2024-09-02 06:53:27 +00:00
Set-Location -
2024-08-27 10:35:30 +00:00
}
# Setting up Alacritty Configuration
Write-Host "Setting up Alacritty configuration..." -ForegroundColor Cyan
ProcessUrlFiles -sourceDir "$dotfilesRepo\alacritty" -destinationDir $alacrittyConfigDir
2024-08-27 10:35:30 +00:00
# Copy the main Alacritty configuration file
CopyFileWithPrompt "$dotfilesRepo\alacritty\alacritty.toml" "$alacrittyConfigDir\alacritty.toml"
# Setting up Neovim Configuration
Write-Host "Setting up Neovim configuration..." -ForegroundColor Cyan
$ubuntu = wsl.exe -l --all | Where-Object { $_.Replace("`0", "") -match '^Ubuntu' }
if ($null -eq $ubuntu)
{ ProcessUrlFiles -sourceDir "$dotfilesRepo\nvim" -destinationDir "$env:LOCALAPPDATA"
} else
{
2024-10-21 21:46:06 +00:00
# TODO: Expand into generic WSL setup while installing required dev tools
ProcessUrlFiles -sourceDir "$dotfilesRepo\nvim"
Set-Location "$env:TMP\nvim-config"
Write-Host "Copying (forcably) configuration to WSL..." -ForegroundColor Yellow
wsl.exe cp -rf . ~/.config/ 2>$null | Out-Null
Set-Location -
}
2024-08-27 10:35:30 +00:00
# Setting up PowerShell Profile
Write-Host "Setting up PowerShell profile..." -ForegroundColor Cyan
CopyFileWithPrompt "$dotfilesRepo\PowerShell\Microsoft.PowerShell_profile.ps1" $psProfile
# Setting up self-made ProxySwitcher
Write-Host "============================================" -ForegroundColor DarkGray
Write-Host "Setting up ProxySwitcher via subscript..." -ForegroundColor Cyan
Invoke-RestMethod 'https://raw.githubusercontent.com/Baipyrus/ProxySwitcher/main/install.ps1' | Invoke-Expression
Write-Host "============================================" -ForegroundColor DarkGray
2024-08-27 14:33:55 +00:00
function UnzipAndInstall
{
param (
2024-10-21 17:53:27 +00:00
[string]$source
2024-08-27 14:33:55 +00:00
)
2024-10-21 18:06:29 +00:00
# Create temporary directory for unzip
2024-08-27 14:33:55 +00:00
$appname = $source.Split('\')[-1]
$tmpApp = "$env:TMP\$appname-config"
if (-not (Test-Path $tmpApp))
{ New-Item -ItemType Directory -Path $tmpApp | Out-Null
}
# Find all .url files in the source directory
$urlFiles = Get-ChildItem -Path $source -Filter '*.url'
foreach ($file in $urlFiles)
{
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
Write-Host "Extracting archive $tmpApp\$fileName.zip to $tmpApp\$fileName\..." -ForegroundColor Cyan
unzip -o "$tmpApp\$fileName.zip" -d "$tmpApp\$fileName" | Out-Null
Write-Host "Installing fonts from $tmpApp\$fileName\ for current user..." -ForegroundColor Cyan
Copy-Item -Path "$tmpApp\$fileName\*" -Destination "$env:LOCALAPPDATA\Microsoft\Windows\Fonts\" -Force -ErrorAction SilentlyContinue
}
}
2024-08-27 13:38:57 +00:00
# Installing Nerd Fonts
Write-Host "Installing Nerd Fonts..." -ForegroundColor Cyan
ProcessUrlFiles -sourceDir "$dotfilesRepo\nerd-fonts" -fileExt ".zip"
2024-10-21 17:53:27 +00:00
UnzipAndInstall -source "$dotfilesRepo\nerd-fonts"
2024-08-27 13:38:57 +00:00
2024-08-27 10:35:30 +00:00
# Final message
Write-Host "Windows setup complete!" -ForegroundColor Green