From abb9c04deb19736115634a1ecde563203446679f Mon Sep 17 00:00:00 2001 From: Timothy Aldrich Date: Tue, 2 Jan 2024 19:01:38 -0500 Subject: [PATCH] build: update initialize phase --- .build/profiles/default/runbook.ps1 | 6 +- .../Tasks/set.psmodulepath.build.ps1 | 85 +++++++++++++------ 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/.build/profiles/default/runbook.ps1 b/.build/profiles/default/runbook.ps1 index 0a9fda4..cc9c336 100644 --- a/.build/profiles/default/runbook.ps1 +++ b/.build/profiles/default/runbook.ps1 @@ -31,8 +31,12 @@ Add-BuildTask . 'Build' #------------------------------------------------------------------------------- #region Initialize +'install.psdepend' | before 'install.requirement' + 'Initialize' | jobs @( - { logDebug '-- Begin Initialize phase --' } + 'install.psdepend', + 'install.requirement', + 'set.psmodulepath' ) #endregion Initialize #------------------------------------------------------------------------------- diff --git a/source/stitch/BuildScripts/Tasks/set.psmodulepath.build.ps1 b/source/stitch/BuildScripts/Tasks/set.psmodulepath.build.ps1 index 2487d81..1240f0b 100644 --- a/source/stitch/BuildScripts/Tasks/set.psmodulepath.build.ps1 +++ b/source/stitch/BuildScripts/Tasks/set.psmodulepath.build.ps1 @@ -1,4 +1,16 @@ +using namespace System.Diagnostics.CodeAnalysis +<# +.SYNOPSIS + Update the PSModulePath environment variable with paths from the requirements.psd1 +.DESCRIPTION + `set.psmodulepath` task loads the requirements.psd1 files looking for module paths specified in the `Target` + property. If any are found and they do not exist in the $env:PSModulePath variable, they are added +#> +[SuppressMessageAttribute( + 'PSReviewUnusedParameter','', + Justification = 'Parameters used inside task scope' +)] param( [Parameter()] [switch]$SkipDependencyCheck = ( @@ -11,44 +23,61 @@ param( ) -#synopsis: Install modules required for developing powershell modules using PSDepend2 +#synopsis: Update the PSModulePath environment variable task set.psmodulepath { if (-not($SkipDependencyCheck)) { $targets = [System.Collections.ArrayList]@() - $targets = Get-Dependency -Tags:$tags -Recurse:$true | - Select-Object -ExpandProperty Target -Unique | - Where-Object { - ($_ -notlike 'CurrentUser') -or - ($_ -notlike 'AllUsers') - } + logInfo "Checking dependencies with tags $($DependencyTags -join ', ')" + $targets = Get-Dependency -Tags:$DependencyTags -Recurse:$true + | Select-Object -ExpandProperty Target -Unique + | Where-Object { + ($_ -notlike 'CurrentUser') -or + ($_ -notlike 'AllUsers') } + | ForEach-Object { Resolve-Path $_ } + + if ($targets.Count -gt 0) { + $originalModulePaths = [System.Collections.ArrayList]@( + ([Environment]::GetEnvironmentVariable('PSModulePath') -split ';') + ) + + $options = @{ + ReferenceObject = $originalModulePaths + DifferenceObject = $targets + } - if ($targets.count -gt 0) { - logInfo 'Checking the Target option for the dependencies' - $modulePaths = ([Environment]::GetEnvironmentVariable('PSModulePath') -split ';') - $pathCount = $modulePaths.Count - logDebug = "PSModulePath contains $pathCount paths" - foreach ($target in $targets) { - if (Test-Path $target) { - logDebug "Looking for $target in PSModulePath" - if ($modulePaths -contains $targetPath) { - logInfo "$target already set" + #! get paths that are not in the current PSModulePath already + $notInModulePath = Compare-Object @options + | Where-Object SideIndicator -Like '=>' + + + if ($notInModulePath.count -gt 0) { + foreach ($modulePath in $notInModulePath) { + if (Test-Path $target) { + logDebug "Looking for $target in PSModulePath" + if ($modulePaths -contains $targetPath) { + logInfo "$target already set" + } else { + logInfo "Prepending $target on PSModulePath" + $modulePaths = $target , $modulePaths + } } else { - logInfo "Prepending $target on PSModulePath" - $modulePaths = $target , $modulePaths + logWarn "Skipping $targetPath because it does not exist" } - } else { - logWarn "Skipping $targetPath because it does not exist" } + } else { + logInfo 'No targets specified in requirements with given tags' + } + #! did the count increase since we started? + if ($modulePaths.Count -gt $pathCount) { + logInfo 'Updating PSModulePath environment variable' + [Environment]::SetEnvironmentVariable('PSModulePath', ($new_mod_paths -join ';')) + } else { + logInfo 'No paths need to be added' } - } - #! did the count increase since we started? - if ($modulePaths.Count -gt $pathCount) { - logInfo "Updating PSModulePath environment variable" - [Environment]::SetEnvironmentVariable('PSModulePath', ($new_mod_paths -join ';')) } else { - logInfo "No paths need to be added" + logInfo 'No paths found in dependency targets' } } else { - logInfo "Module dependency check skipped (-SkipDependencyCheck was set)" + logInfo 'Module dependency check skipped (-SkipDependencyCheck was set)' } }