Skip to content

Commit

Permalink
build: update initialize phase
Browse files Browse the repository at this point in the history
  • Loading branch information
aldrichtr committed Jan 3, 2024
1 parent cebef5e commit abb9c04
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .build/profiles/default/runbook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
#-------------------------------------------------------------------------------
Expand Down
85 changes: 57 additions & 28 deletions source/stitch/BuildScripts/Tasks/set.psmodulepath.build.ps1
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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)'
}
}

0 comments on commit abb9c04

Please sign in to comment.