Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active August 17, 2024 11:42
Show Gist options
  • Save JustinGrote/ecdf96b4179da43fb017dccbd1cc56f6 to your computer and use it in GitHub Desktop.
Save JustinGrote/ecdf96b4179da43fb017dccbd1cc56f6 to your computer and use it in GitHub Desktop.
Bootstrap Script for a High Performance Module Installer
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
}
@safeblood
Copy link

how to use this thing? give me an example Install-Modulefast VMware.PowerCLI is not work

@potatoqualitee
Copy link

Can we please get Publish-ModuleFast 😇

@JustinGrote
Copy link
Author

@safeblood I wrote this 3 years ago without changes so there might be some bugs. As mentioned in the notes, it is experimental :)

@potatoqualitee LOL, I think most of the module publishing "slowness" is actually psgallery stuff I can't fix, unless you're talking about publishing multiple modules simultaneously, dependencies would get tricky :)

@JustinGrote
Copy link
Author

This has been updated to be a bootstrap for the revamped version of modulefast here:
https://github.com/JustinGrote/ModuleFast

@potatoqualitee
Copy link

ayyyy! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment