Skip to content

Instantly share code, notes, and snippets.

@kberridge
Created April 20, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kberridge/14be12cb5397c2c5979034c83f1ebd21 to your computer and use it in GitHub Desktop.
Save kberridge/14be12cb5397c2c5979034c83f1ebd21 to your computer and use it in GitHub Desktop.
Invoke-Build direct calling bootstrap script (load or install compatible msbuild version, self updating and support different versions in different projects)
param (
[Parameter(Position=0)]
$Tasks
#... other params
)
function IsVersionCompatible($requiredVersion, $testVersion) {
# same major, and AT LEAST the required minor and build
$testVersion.Major -eq $requiredVersion.Major `
-and $testVersion.Minor -ge $requiredVersion.Minor `
-and $testVersion.Build -ge $requiredVersion.Build
}
# Direct call support, calling .\web.build.ps1 bootstraps the InvokeBuild PS module
if ([System.IO.Path]::GetFileName($MyInvocation.ScriptName) -ne 'Invoke-Build.ps1') {
$ErrorActionPreference = 'Stop'
$requiredVersion = new-object System.Version "5.5.9"
$loadedModule = get-module invokebuild
$moduleAlreadyLoaded = $loadedModule `
-and @($loadedModule).Length -eq 1 `
-and (IsVersionCompatible $requiredVersion $loadedModule.Version)
if (!$moduleAlreadyLoaded)
{
if ($loadedModule) { remove-module invokebuild }
$availableModule = get-module -ListAvailable invokebuild |
?{ IsVersionCompatible $requiredVersion $_.Version } |
sort Version -Descending | select -First 1
if ($availableModule) {
Write-Host "Importing InvokeBuild $($availableModule.Version)"
import-module invokebuild -RequiredVersion "$($availableModule.Version)" -Force
}
else {
Write-Host "Did not find a compatible version of the InvokeBuild Module, installing $requiredVersion"
install-module invokebuild -RequiredVersion "$requiredVersion" -Scope CurrentUser -Force
import-module invokebuild -RequiredVersion "$requiredVersion" -Force
}
}
else {
Write-Host "Running with InvokeBuild $($loadedModule.Version)"
}
Invoke-Build -Task $Tasks @PSBoundParameters
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment