2024-11-24 17:41:24 +00:00
|
|
|
# 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
|
2024-11-25 14:28:58 +00:00
|
|
|
Write-Host "$(git pull)"
|
2024-11-24 17:41:24 +00:00
|
|
|
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
|
2024-11-28 14:07:31 +00:00
|
|
|
if ((Test-Path $destination) -and (IsGitRepository -dir $destination -url $url))
|
2024-11-24 17:41:24 +00:00
|
|
|
{
|
2024-11-28 14:07:31 +00:00
|
|
|
Write-Host "Pulling latest changes from dotfiles repository..." -ForegroundColor Cyan
|
|
|
|
Write-Host "$(git -C $destination pull)"
|
2024-11-24 17:41:24 +00:00
|
|
|
return $destination
|
|
|
|
}
|
|
|
|
|
2024-11-28 14:08:23 +00:00
|
|
|
# Try using current directory
|
|
|
|
$request = Read-Host "Clone the dotfiles repository anew? [y/N]"
|
|
|
|
if ($request.ToLower() -ne 'y')
|
|
|
|
{
|
|
|
|
Write-Warning "Fallback to current directory (Errors without necessary files)!"
|
|
|
|
return $cwd
|
|
|
|
}
|
|
|
|
|
2024-11-28 14:07:31 +00:00
|
|
|
Write-Host "Cloning dotfiles repository..." -ForegroundColor Cyan
|
|
|
|
git clone $url $destination
|
2024-11-24 17:41:24 +00:00
|
|
|
return $destination
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to copy files with overwrite prompt
|
|
|
|
function CopyFileWithPrompt
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[string]$source,
|
|
|
|
[string]$destination
|
|
|
|
)
|
|
|
|
|
|
|
|
if (Test-Path $destination)
|
|
|
|
{
|
2024-11-28 14:07:58 +00:00
|
|
|
$overwrite = Read-Host "File $destination exists. Overwrite? (y/N)"
|
|
|
|
if ($overwrite.ToLower() -ne 'y')
|
2024-11-24 17:41:24 +00:00
|
|
|
{
|
|
|
|
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 (
|
2024-11-24 20:12:23 +00:00
|
|
|
[string]$source,
|
|
|
|
[Parameter(Mandatory=$false)][string]$destination,
|
2024-11-25 14:03:16 +00:00
|
|
|
[Parameter(Mandatory=$false)][string]$fileExt,
|
|
|
|
[bool]$progress=$true
|
2024-11-24 17:41:24 +00:00
|
|
|
)
|
|
|
|
|
2024-11-25 14:03:16 +00:00
|
|
|
# Disable progressbar for faster download
|
|
|
|
$progressPreference = 'continue'
|
|
|
|
if (-not $progress)
|
|
|
|
{ $progressPreference = 'silentlyContinue'
|
|
|
|
}
|
|
|
|
|
2024-11-24 17:41:24 +00:00
|
|
|
# Ensure the destination directory exists
|
2024-11-24 20:12:23 +00:00
|
|
|
if ($destination -and (-not (Test-Path $destination)))
|
2024-11-24 17:41:24 +00:00
|
|
|
{
|
2024-11-24 20:12:23 +00:00
|
|
|
Write-Host "Creating destination directory $destination..." -ForegroundColor Cyan
|
|
|
|
New-Item -ItemType Directory -Path $destination | Out-Null
|
2024-11-24 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create temporary directory for curl
|
2024-11-28 14:49:20 +00:00
|
|
|
$appname = Split-Path -Leaf $source
|
2024-11-24 17:41:24 +00:00
|
|
|
$tmpApp = "$env:TMP\$appname-config"
|
|
|
|
if (-not (Test-Path $tmpApp))
|
|
|
|
{ New-Item -ItemType Directory -Path $tmpApp | Out-Null
|
|
|
|
}
|
2024-11-24 21:55:12 +00:00
|
|
|
Push-Location
|
2024-11-24 17:41:24 +00:00
|
|
|
Set-Location $tmpApp
|
|
|
|
|
|
|
|
# Find all .url files in the source directory
|
2024-11-24 20:12:23 +00:00
|
|
|
$urlFiles = Get-ChildItem -Path $source -Filter '*.url'
|
2024-11-24 17:41:24 +00:00
|
|
|
|
|
|
|
foreach ($file in $urlFiles)
|
|
|
|
{
|
|
|
|
$url = Get-Content $file.FullName
|
|
|
|
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
|
2024-11-24 20:12:23 +00:00
|
|
|
$destinationPath = "$destination\$fileName"
|
2024-11-24 17:41:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-11-24 20:12:23 +00:00
|
|
|
$destinationPath = "$destination\$fileName$extension"
|
2024-11-24 17:41:24 +00:00
|
|
|
|
|
|
|
# Respond only if destination is provided
|
|
|
|
$conditional = " to $destinationPath"
|
2024-11-24 20:12:23 +00:00
|
|
|
if (-not $destination)
|
2024-11-24 17:41:24 +00:00
|
|
|
{ $conditional = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "Downloading $fileName from $url$conditional..." -ForegroundColor Cyan
|
2024-11-25 14:33:04 +00:00
|
|
|
Invoke-WebRequest $url -OutFile $fileName$extension
|
2024-11-24 17:41:24 +00:00
|
|
|
$tmpDestination = "$tmpApp\$fileName$extension"
|
|
|
|
|
|
|
|
# Copy only if destination is provided
|
2024-11-24 20:12:23 +00:00
|
|
|
if ($destination)
|
2024-11-24 17:41:24 +00:00
|
|
|
{ CopyFileWithPrompt $tmpDestination $destinationPath
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
# Use $tmpApp as destination in case of git repo
|
2024-11-24 20:12:23 +00:00
|
|
|
if (-not $destination)
|
2024-11-24 17:41:24 +00:00
|
|
|
{ $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
|
|
|
|
}
|
2024-11-24 21:55:12 +00:00
|
|
|
Pop-Location
|
2024-11-24 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
2024-11-28 13:15:50 +00:00
|
|
|
# Function to unzip and install nerd fonts
|
|
|
|
function InstallNerdFont
|
2024-11-24 17:41:24 +00:00
|
|
|
{
|
|
|
|
param (
|
2024-11-28 13:15:50 +00:00
|
|
|
[string]$source
|
2024-11-24 17:41:24 +00:00
|
|
|
)
|
|
|
|
|
2024-11-28 13:15:50 +00:00
|
|
|
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($source)
|
2024-11-28 14:17:33 +00:00
|
|
|
$parent = Get-ChildItem $source | `
|
2024-11-28 15:04:33 +00:00
|
|
|
Select-Object -ExpandProperty Directory | `
|
2024-11-28 14:17:33 +00:00
|
|
|
Select-Object -ExpandProperty FullName
|
2024-11-28 13:15:50 +00:00
|
|
|
$destination = "$parent\$fileName"
|
2024-11-24 17:41:24 +00:00
|
|
|
|
2024-11-28 13:15:50 +00:00
|
|
|
# Create destination directory
|
2024-11-25 13:34:36 +00:00
|
|
|
if (-not (Test-Path $destination))
|
|
|
|
{ New-Item -ItemType Directory -Path $destination | Out-Null
|
|
|
|
}
|
|
|
|
|
2024-11-28 13:15:50 +00:00
|
|
|
# Extract contents of zip archive
|
|
|
|
Write-Host "Extracting archive $source to $destination..." -ForegroundColor Cyan
|
2024-11-28 15:12:55 +00:00
|
|
|
Expand-Archive $source -DestinationPath $destination -Force | Out-Null
|
2024-11-24 17:41:24 +00:00
|
|
|
|
2024-11-28 13:15:50 +00:00
|
|
|
# Install extracted fonts
|
|
|
|
$fontFiles = Get-ChildItem -Path $destination -Include "*.ttf", "*.otf"
|
|
|
|
|
|
|
|
foreach ($font in $fontFiles)
|
2024-11-24 17:41:24 +00:00
|
|
|
{
|
2024-11-28 13:17:31 +00:00
|
|
|
InstallFont $font
|
2024-11-28 13:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-24 17:41:24 +00:00
|
|
|
|
2024-11-28 13:17:31 +00:00
|
|
|
# Reference: https://www.alkanesolutions.co.uk/2021/12/06/installing-fonts-with-powershell/
|
|
|
|
function InstallFont
|
|
|
|
{
|
|
|
|
param
|
|
|
|
(
|
|
|
|
[System.IO.FileInfo]$file
|
|
|
|
)
|
|
|
|
|
|
|
|
# Prerequisites / Constants
|
|
|
|
Add-Type -AssemblyName PresentationCore
|
|
|
|
$destination = "$env:LOCALAPPDATA\Microsoft\Windows\Fonts\"
|
|
|
|
$regKey = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts"
|
|
|
|
|
2024-11-28 15:12:55 +00:00
|
|
|
# Create destination directory
|
|
|
|
if (-not (Test-Path $destination))
|
|
|
|
{ New-Item -ItemType Directory -Path $destination | Out-Null
|
|
|
|
}
|
|
|
|
|
2024-11-28 13:17:31 +00:00
|
|
|
# Get Glyph
|
|
|
|
$glyph = New-Object -TypeName Windows.Media.GlyphTypeface -ArgumentList $file.FullName
|
|
|
|
|
|
|
|
$family = $glyph.Win32FamilyNames['en-us']
|
|
|
|
if ($null -eq $family)
|
|
|
|
{ $family = $glyph.Win32FamilyNames.Values | Select-Object -First 1
|
|
|
|
}
|
|
|
|
|
|
|
|
$face = $glyph.Win32FaceNames['en-us']
|
|
|
|
if ($null -eq $face)
|
|
|
|
{ $face = $glyph.Win32FaceNames.Values | Select-Object -First 1
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = ("$family $face").Trim()
|
|
|
|
switch ($file.Extension)
|
|
|
|
{
|
|
|
|
".ttf"
|
|
|
|
{ $name = "$name (TrueType)"
|
|
|
|
}
|
|
|
|
".otf"
|
|
|
|
{ $name = "$name (OpenType)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$destFileName = Join-Path -Path $destination -ChildPath $file.Name
|
|
|
|
Write-Host "Installing font: $file with font name '$name'"
|
|
|
|
If (-not (Test-Path $destFileName))
|
|
|
|
{ Copy-Item -Path $file.FullName -Destination $destFileName -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "Registering font: $file with font name '$name'"
|
|
|
|
$keyValue = Get-ItemProperty -Name $name -Path $regKey -ErrorAction SilentlyContinue
|
|
|
|
If (-not $keyValue)
|
|
|
|
{
|
|
|
|
New-ItemProperty -Name $name -Path $regKey `
|
|
|
|
-PropertyType string -Value $file.Name `
|
|
|
|
-Force -ErrorAction SilentlyContinue | Out-Null
|
2024-11-24 17:41:24 +00:00
|
|
|
}
|
|
|
|
}
|