mirror of
https://github.com/Baipyrus/dotfiles.git
synced 2024-12-26 03:11:45 +00:00
modularize windows install script by extracting functions
This commit is contained in:
parent
f9db026b16
commit
ad9ffc0a19
@ -1,150 +1,17 @@
|
||||
Import-Module ./util/windows.psm1
|
||||
|
||||
|
||||
# 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"
|
||||
|
||||
# Function to determine if the current directory is the dotfiles repository
|
||||
function IsGitRepository
|
||||
{
|
||||
param (
|
||||
[string]$dir,
|
||||
[string]$url
|
||||
)
|
||||
|
||||
try
|
||||
{
|
||||
$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
|
||||
|
||||
if (IsGitRepository -dir $currentDir -url $repoUrl)
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
# Function to handle URL files: download files or clone repositories
|
||||
function ProcessUrlFiles
|
||||
{
|
||||
param (
|
||||
[string]$sourceDir,
|
||||
[Parameter(Mandatory=$false)][string]$destinationDir,
|
||||
[Parameter(Mandatory=$false)][string]$fileExt
|
||||
)
|
||||
|
||||
# Ensure the destination directory exists
|
||||
if ($destinationDir -and (-not (Test-Path $destinationDir)))
|
||||
{
|
||||
Write-Host "Creating destination directory $destinationDir..." -ForegroundColor Cyan
|
||||
New-Item -ItemType Directory -Path $destinationDir | Out-Null
|
||||
}
|
||||
|
||||
# Create temporary directory for curl
|
||||
$appname = $sourceDir.Split('\')[-1]
|
||||
$tmpApp = "$env:TMP\$appname-config"
|
||||
if (-not (Test-Path $tmpApp))
|
||||
{ New-Item -ItemType Directory -Path $tmpApp | Out-Null
|
||||
}
|
||||
Set-Location $tmpApp
|
||||
|
||||
# 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"
|
||||
|
||||
if (-not ($url -match 'git@|https://.*\.git'))
|
||||
{
|
||||
# Otherwise, download the file
|
||||
$extension = [System.IO.Path]::GetExtension($url)
|
||||
# Force apply $fileExt if given
|
||||
if ($fileExt)
|
||||
{
|
||||
$extension = $fileExt
|
||||
$url += $fileExt
|
||||
}
|
||||
$destinationPath = "$destinationDir\$fileName$extension"
|
||||
|
||||
# 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
|
||||
$tmpDestination = "$tmpApp\$fileName$extension"
|
||||
|
||||
# Copy only if destination is provided
|
||||
if ($destinationDir)
|
||||
{ CopyFileWithPrompt $tmpDestination $destinationPath
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
# Use $tmpApp as destination in case of git repo
|
||||
if (-not $destinationDir)
|
||||
{ $destinationPath = "$tmpApp\$fileName"
|
||||
}
|
||||
|
||||
# If the URL is a git repository, pull it
|
||||
if (IsGitRepository -dir $destinationPath -url $url)
|
||||
{
|
||||
Write-Host "Pulling inside existing repository $fileName..." -ForegroundColor Cyan
|
||||
git -C $destinationPath pull
|
||||
continue
|
||||
}
|
||||
# Or otherwise clone it
|
||||
Write-Host "Cloning $fileName from $url to $destinationPath..." -ForegroundColor Cyan
|
||||
git clone $url $destinationPath
|
||||
}
|
||||
Set-Location -
|
||||
}
|
||||
ReadyDotfilesRepo -cwd $currentDir -url $repoUrl -destination $dotfilesRepo
|
||||
|
||||
# Setting up Alacritty Configuration
|
||||
Write-Host "Setting up Alacritty configuration..." -ForegroundColor Cyan
|
||||
@ -178,33 +45,6 @@ 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
|
||||
|
||||
function UnzipAndInstall
|
||||
{
|
||||
param (
|
||||
[string]$source
|
||||
)
|
||||
|
||||
# Create temporary directory for unzip
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
||||
# Installing Nerd Fonts
|
||||
Write-Host "Installing Nerd Fonts..." -ForegroundColor Cyan
|
||||
ProcessUrlFiles -sourceDir "$dotfilesRepo\nerd-fonts" -fileExt ".zip"
|
||||
|
182
util/windows.psm1
Normal file
182
util/windows.psm1
Normal file
@ -0,0 +1,182 @@
|
||||
# Function to determine if the current directory is the dotfiles repository
|
||||
function IsGitRepository
|
||||
{
|
||||
param (
|
||||
[string]$dir,
|
||||
[string]$url
|
||||
)
|
||||
|
||||
try
|
||||
{
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
||||
function ReadyDotfilesRepo
|
||||
{
|
||||
param (
|
||||
[string]$cwd,
|
||||
[string]$url,
|
||||
[Parameter(Mandatory=$false)][string]$destination
|
||||
)
|
||||
|
||||
# Clone dotfiles repository to TMP if not already inside it; otherwise pull changes
|
||||
if (IsGitRepository -dir $cwd -url $url)
|
||||
{
|
||||
Write-Host "Already inside the dotfiles repository. Skipping clone step and pulling..." -ForegroundColor Yellow
|
||||
git pull
|
||||
return $cwd
|
||||
}
|
||||
|
||||
# Mandatory parameter missing
|
||||
if (-not $PSBoundParameters.ContainsKey('destination'))
|
||||
{
|
||||
throw "Missing mandatory parameter for Repo location."
|
||||
}
|
||||
|
||||
# Clone new repo in destination dir if not exists
|
||||
if (-not (Test-Path $destination))
|
||||
{
|
||||
Write-Host "Cloning dotfiles repository..." -ForegroundColor Cyan
|
||||
git clone $url $destination
|
||||
return $destination
|
||||
}
|
||||
|
||||
Write-Host "Pulling latest changes from dotfiles repository..." -ForegroundColor Cyan
|
||||
git -C $destination pull
|
||||
return $destination
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
# Function to handle URL files: download files or clone repositories
|
||||
function ProcessUrlFiles
|
||||
{
|
||||
param (
|
||||
[string]$sourceDir,
|
||||
[Parameter(Mandatory=$false)][string]$destinationDir,
|
||||
[Parameter(Mandatory=$false)][string]$fileExt
|
||||
)
|
||||
|
||||
# Ensure the destination directory exists
|
||||
if ($destinationDir -and (-not (Test-Path $destinationDir)))
|
||||
{
|
||||
Write-Host "Creating destination directory $destinationDir..." -ForegroundColor Cyan
|
||||
New-Item -ItemType Directory -Path $destinationDir | Out-Null
|
||||
}
|
||||
|
||||
# Create temporary directory for curl
|
||||
$appname = $sourceDir.Split('\')[-1]
|
||||
$tmpApp = "$env:TMP\$appname-config"
|
||||
if (-not (Test-Path $tmpApp))
|
||||
{ New-Item -ItemType Directory -Path $tmpApp | Out-Null
|
||||
}
|
||||
Set-Location $tmpApp
|
||||
|
||||
# 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"
|
||||
|
||||
if (-not ($url -match 'git@|https://.*\.git'))
|
||||
{
|
||||
# Otherwise, download the file
|
||||
$extension = [System.IO.Path]::GetExtension($url)
|
||||
# Force apply $fileExt if given
|
||||
if ($fileExt)
|
||||
{
|
||||
$extension = $fileExt
|
||||
$url += $fileExt
|
||||
}
|
||||
$destinationPath = "$destinationDir\$fileName$extension"
|
||||
|
||||
# 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
|
||||
$tmpDestination = "$tmpApp\$fileName$extension"
|
||||
|
||||
# Copy only if destination is provided
|
||||
if ($destinationDir)
|
||||
{ CopyFileWithPrompt $tmpDestination $destinationPath
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
# Use $tmpApp as destination in case of git repo
|
||||
if (-not $destinationDir)
|
||||
{ $destinationPath = "$tmpApp\$fileName"
|
||||
}
|
||||
|
||||
# If the URL is a git repository, pull it
|
||||
if (IsGitRepository -dir $destinationPath -url $url)
|
||||
{
|
||||
Write-Host "Pulling inside existing repository $fileName..." -ForegroundColor Cyan
|
||||
git -C $destinationPath pull
|
||||
continue
|
||||
}
|
||||
# Or otherwise clone it
|
||||
Write-Host "Cloning $fileName from $url to $destinationPath..." -ForegroundColor Cyan
|
||||
git clone $url $destinationPath
|
||||
}
|
||||
Set-Location -
|
||||
}
|
||||
|
||||
# Function to expand zip archives and copy to destination paths
|
||||
function UnzipAndInstall
|
||||
{
|
||||
param (
|
||||
[string]$source
|
||||
)
|
||||
|
||||
# Create temporary directory for unzip
|
||||
$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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user