Last active
January 11, 2024 00:51
-
-
Save Ragnoroct/c1fcb707c496fd06a93e2d7c8b956300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = "Stop" | |
$global:ProgressPreference = "SilentlyContinue" | |
# Import the necessary .NET classes | |
Add-Type -TypeDefinition @' | |
using System.IO; | |
public class DirectoryHelper { | |
public static void CreateDirectoryIfNeeded(string path) { | |
if (!Directory.Exists(path)) { | |
Directory.CreateDirectory(path); | |
} | |
} | |
} | |
'@ | |
Add-Type -TypeDefinition @' | |
using System.Net; | |
using System.IO; | |
using System.Diagnostics; | |
public class DownloadHelper { | |
public static void DownloadFile(string url, string outputPath) { | |
using (WebClient client = new WebClient()) { | |
client.DownloadFile(url, outputPath); | |
} | |
} | |
} | |
'@ | |
$argRepo = $args[0] | |
$argEmail = $args[1] | |
$pythonVersion = "3.12.1" | |
$gitVersion = "2.43.0" | |
$sshIdentity = "personal_$((Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Name).ToLower())" | |
function DownloadAndExtractPython() { | |
Write-Output "downloading python $pythonVersion..." | |
$downloadUrl = "https://www.python.org/ftp/python/$pythonVersion/python-$pythonVersion-embed-amd64.zip" | |
$outputDirectory = Join-Path $HOME -ChildPath ".dotfiles/.local/programs/python" | |
[DirectoryHelper]::CreateDirectoryIfNeeded($outputDirectory) | |
$outputPath = Join-Path $outputDirectory -ChildPath "python-$pythonVersion-embed-amd64.zip" | |
if (Test-Path $outputPath) { | |
Remove-Item -Path $outputPath -Force | |
} | |
[DownloadHelper]::DownloadFile($downloadUrl, $outputPath) | |
$extractedFolderPath = Join-Path $outputDirectory -ChildPath "python-$pythonVersion" | |
if (Test-Path $extractedFolderPath) { | |
Remove-Item -Path $extractedFolderPath -Force -Recurse | |
} | |
Expand-Archive -Path $outputPath -DestinationPath $extractedFolderPath | |
Remove-Item -Path $outputPath | |
} | |
# https://github.com/git-for-windows/git/blob/4991b615ecfc2a56e8086e49fc623229eb2a5926/azure-pipelines.yml#L151 | |
function DownloadAndExtractGit() { | |
Write-Output "downloading git $gitVersion..." | |
$downloadUrl = "https://github.com/git-for-windows/git/releases/download/v$gitVersion.windows.1/PortableGit-$gitVersion-64-bit.7z.exe" | |
$outputDirectory = Join-Path $HOME -ChildPath ".dotfiles/.local/programs" | |
[DirectoryHelper]::CreateDirectoryIfNeeded($outputDirectory) | |
$outputPath = Join-Path $outputDirectory -ChildPath "git.7z.exe" | |
$extractPath = Join-Path $outputDirectory -ChildPath "git" | |
if (Test-Path $outputPath) { | |
Remove-Item -Path $outputPath -Force | |
} | |
[DownloadHelper]::DownloadFile($downloadUrl, $outputPath) | |
if (Test-Path $extractPath) { | |
Remove-Item -Path $extractPath -Force -Recurse | |
} | |
Start-Process -WindowStyle hidden "$outputPath" "-y", "-o$extractPath" | |
while (-not @(Remove-Item -ErrorAction SilentlyContinue $outputPath; $?)) { sleep 1 } # Wait until it is unpacked | |
} | |
function GenerateSSHKey() { | |
$sshKeyPath = Join-Path $HOME -ChildPath ".ssh/$sshIdentity" | |
if (Test-Path $sshKeyPath) { | |
Write-Output "ssh key $sshIdentity already exists" | |
} | |
else { | |
[DirectoryHelper]::CreateDirectoryIfNeeded($(Join-Path $HOME -ChildPath ".ssh")) | |
ssh-keygen -t ed25519 -f $sshKeyPath -N '""' -C "$argEmail" | |
Write-Output "ssh key $sshIdentity created" | |
Write-Host "title: personal_${computerName}" | |
Write-Host "content:`n$(Get-Content "$sshKeyPath.pub")" | |
Start-Process "https://github.com/settings/ssh/new" | |
} | |
} | |
function DownloadDotfiles() { | |
$env:Path += ";$(Join-Path $HOME -ChildPath ".dotfiles/.local/programs/git/bin/")" | |
Set-Location $(Join-Path $HOME -ChildPath ".dotfiles") | |
# update known hosts and ssh config | |
$ssh_keys_content = "github.com " + ((Invoke-RestMethod -Uri "https://api.github.com/meta").ssh_keys -join "`ngithub.com ") + "`n" | |
Add-Content -Path ~/.ssh/known_hosts -Value "$ssh_keys_content" -NoNewline -Encoding UTF8 | |
Add-Content -Path ~/.ssh/config -Value "`nHost github.com`n IdentityFile ~/.ssh/$sshIdentity" -NoNewline -Encoding UTF8 | |
# remove BOM | |
[IO.File]::WriteAllLines($(Resolve-Path ~/.ssh/config), $(Get-Content ~/.ssh/config)) | |
[IO.File]::WriteAllLines($(Resolve-Path ~/.ssh/known_hosts), $(Get-Content ~/.ssh/known_hosts)) | |
git init | |
git remote rm origin | |
git remote add origin $argRepo | |
git pull | |
git checkout main -f | |
git branch --set-upstream-to origin/main | |
} | |
function ExecuteInstallDotfiles() { | |
$pythonPath = Join-Path $HOME -ChildPath ".dotfiles/.local/programs/python/python-$pythonVersion/python.exe" | |
& $pythonPath $(Join-Path $HOME -ChildPath ".dotfiles/install") | |
} | |
DownloadAndExtractPython | |
DownloadAndExtractGit | |
GenerateSSHKey | |
DownloadDotfiles | |
ExecuteInstallDotfiles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment