Last active
August 17, 2024 11:42
-
-
Save JustinGrote/ecdf96b4179da43fb017dccbd1cc56f6 to your computer and use it in GitHub Desktop.
Bootstrap Script for a High Performance Module Installer
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
using namespace System.Net.Http | |
#requires -version 7.2 | |
# This is the bootstrap script for Modules | |
[CmdletBinding(PositionalBinding = $false)] | |
param ( | |
#Specify a specific release to use, otherwise 'latest' is used | |
[string]$Release = 'latest', | |
#Specify the user | |
[string]$User = 'JustinGrote', | |
#Specify the repo | |
[string]$Repo = 'ModuleFast', | |
#Specify the module file | |
[string]$ModuleFile = 'ModuleFast.psm1', | |
#Entrypoint to be used if additional args are specified | |
[string]$EntryPoint = 'Install-ModuleFast', | |
#Specify the module name | |
[string]$ModuleName = 'ModuleFast', | |
#Path of the module to bootstrap. You normally won't change this but you can override it if you want | |
[string]$Uri = $( | |
$base = "https://github.com/$User/$Repo/releases/{0}/$ModuleFile"; | |
$version = $Release -eq 'latest' ? 'latest/download' : "download/$Release"; | |
$base -f $version | |
), | |
#All additional arguments passed to this script will be passed to Install-ModuleFast | |
[Parameter(ValueFromRemainingArguments)]$installArgs | |
) | |
$ErrorActionPreference = 'Stop' | |
if (Get-Module $ModuleName) { | |
Write-Warning "Module $ModuleName already loaded, skipping bootstrap." | |
return | |
} | |
Write-Debug "Fetching $ModuleName from $Uri" | |
$ProgressPreference = 'SilentlyContinue' | |
try { | |
$httpClient = [HttpClient]::new() | |
$httpClient.DefaultRequestHeaders.AcceptEncoding.Add('gzip') | |
$response = $httpClient.GetStringAsync($Uri).GetAwaiter().GetResult() | |
} catch { | |
$PSItem.ErrorDetails = "Failed to fetch $ModuleName from $Uri`: $PSItem" | |
$PSCmdlet.ThrowTerminatingError($PSItem) | |
} | |
Write-Debug 'Fetched response' | |
$scriptBlock = [ScriptBlock]::Create($response) | |
$ProgressPreference = 'Continue' | |
#We don't use New-Module here because it has some eccentricies like not being unloadable | |
$bootstrapModule = New-Module -Name $ModuleName -ScriptBlock $scriptblock | Import-Module -PassThru | |
Write-Debug "Loaded Module $ModuleName" | |
if ($installArgs) { | |
Write-Debug "Detected we were started with args, running $Entrypoint $($installArgs -join ' ')" | |
& $EntryPoint @installArgs | |
#Remove the bootstrap module if args were specified, otherwise persist it in memory | |
Remove-Module $bootstrapModule | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ayyyy! 🥳