Skip to content

Commit e69ace9

Browse files
committed
👷 Updates Build Scripts
1 parent ede2eab commit e69ace9

10 files changed

+136
-43
lines changed

build/funcs/Expand-PackageExportOutput.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function Expand-PackageExportOutput {
1515
[string] $psgalleryNupkgFullname = $null
1616

1717
# Find the NuPkg.
18-
[System.IO.FileInfo[]] $outputPsgalleryNupkgs = @(Get-ChildItem -Path $out -Filter "*.nupkg" -Recurse -File -Force)
18+
[System.IO.FileInfo[]] $outputPsgalleryNupkgs = @()
19+
$outputPsgalleryNupkgs = @(Get-ChildItem -Path $out -Filter "*.nupkg" -Recurse -File -Force)
1920
if ($outputPsgalleryNupkgs.Count -eq 0) {
2021
throw "No nupkg files were found in '$out'. Did you forgot to build?"
2122
} elseif ($outputPsgalleryNupkgs.Count -gt 1) {
@@ -37,7 +38,8 @@ function Expand-PackageExportOutput {
3738

3839
# Find the psd1 file that matches the NuPkg.
3940
Write-Information "Determining the psd1 file that matches '$psgalleryNupkgName'."
40-
[System.IO.FileInfo] $psd1 = `
41+
[System.IO.FileInfo] $psd1 = $null
42+
$psd1 = `
4143
Get-ChildItem -Path $moduleLocation -Filter "*.psd1" -Recurse -File -Force `
4244
| Sort-Object -Property FullName `
4345
| Where-Object {
@@ -56,7 +58,8 @@ function Expand-PackageExportOutput {
5658

5759
# Move the expanded NuPkg to the psd1's name, which is a requirement before it can be imported.
5860
Write-Information "Renaming folder '$moduleLocation' to match the module name."
59-
[string] $newModuleLocation = Join-Path -Path (Split-Path $moduleLocation -Parent) -ChildPath $psd1.BaseName
61+
[string] $newModuleLocation = $null
62+
$newModuleLocation = Join-Path -Path (Split-Path $moduleLocation -Parent) -ChildPath $psd1.BaseName
6063
if (Test-Path $newModuleLocation -ErrorAction SilentlyContinue) {
6164
Remove-Item -Path $newModuleLocation -Recurse -Force | Out-Null
6265
}

build/funcs/Get-ModuleExports.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function Get-ModuleExports {
2121
}
2222
[string] $ds = [System.IO.Path]::DirectorySeparatorChar
2323
. "${PSScriptRoot}${ds}Import-PSGalleryModuleNested.ps1"
24-
[PSModuleInfo[]] $modulesLoaded = $null
25-
$modulesLoaded = Import-PSGalleryModuleNested -RuntimeDependencies -SkipAlreadyLoaded
24+
[PSModuleInfo[]] $modulesToUnload = $null
25+
$modulesToUnload = Import-PSGalleryModuleNested -RuntimeDependencies -SkipAlreadyLoaded
2626
try {
2727
[bool] $madeGlobalVariable = $false
2828
if (-not (Get-Variable -Name PWSHRC_FORCE_MODULES_EXPORT_UNSUPPORTED -Scope Global -ErrorAction SilentlyContinue)) {
@@ -54,8 +54,10 @@ function Get-ModuleExports {
5454
}
5555
}
5656
} finally {
57-
if ($null -ne $modulesLoaded) {
58-
$modulesLoaded | Remove-Module -Force
57+
if ($null -ne $modulesToUnload) {
58+
[array]::Reverse($modulesToUnload) `
59+
| Select-Object -ExpandProperty path -Unique `
60+
| Remove-Module -Force -ErrorAction SilentlyContinue
5961
}
6062
}
6163

build/funcs/Get-PSGalleryModuleNested.ps1

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,44 @@ function Get-PSGalleryModuleNested {
2727
[string] $ds = [System.IO.Path]::DirectorySeparatorChar
2828
[string] $packagesConfigFile = "${PSScriptRoot}${ds}..${ds}..${ds}packages.PSGallery.config"
2929

30-
[xml] $packagesConfig = [xml](Get-Content -Raw -Path $packagesConfigFile -Encoding UTF8)
31-
[System.Xml.XmlElement[]] $results = $packagesConfig.packages.package | Where-Object {
32-
if ($id) {
33-
if ($_.id -ieq $id) {
34-
if ($version) {
35-
if ($_.version -ieq $version) {
30+
[xml] $packagesConfig = $null
31+
$packagesConfig = [xml](Get-Content -Raw -Path $packagesConfigFile -Encoding UTF8)
32+
[System.Xml.XmlElement[]] $results = @()
33+
if ($packagesConfig.packages -and $packagesConfig.packages.package) {
34+
$results = $packagesConfig.packages.package | Where-Object {
35+
if ($id) {
36+
if ($_.id -ieq $id) {
37+
if ($version) {
38+
if ($_.version -ieq $version) {
39+
return $true
40+
}
41+
} else {
3642
return $true
3743
}
38-
} else {
44+
} elseif (-not $id) {
3945
return $true
4046
}
41-
} elseif (-not $id) {
42-
return $true
43-
}
44-
} elseif ($_ | Get-Member -Name developmentDependency -ErrorAction SilentlyContinue) {
45-
if ([bool] $_.developmentDependency) {
46-
return [bool]$DevelopmentDependencies
47+
} elseif ($_ | Get-Member -Name developmentDependency -ErrorAction SilentlyContinue) {
48+
if ([bool] $_.developmentDependency) {
49+
return [bool]$DevelopmentDependencies
50+
} else {
51+
return [bool]$RuntimeDependencies
52+
}
4753
} else {
4854
return [bool]$RuntimeDependencies
4955
}
50-
} else {
51-
return [bool]$RuntimeDependencies
5256
}
5357
}
5458

5559
foreach ($item in $results) {
5660
[string] $location = "${PSScriptRoot}${ds}..${ds}..${ds}lib${ds}$($item.id).$($item.version)"
5761
$location = (Resolve-Path -Path $location -ErrorAction SilentlyContinue) ?? $location
58-
[string] $psd1Path = `
62+
[string] $psd1Path = $null
63+
$psd1Path = `
5964
Get-ChildItem -Path $location -Filter "$($item.id).psd1" -Recurse -File -Force `
6065
| Select-Object -First 1 -ExpandProperty FullName
61-
[string] $psm1Path = `
66+
[string] $psm1Path = $null
67+
$psm1Path = `
6268
Get-ChildItem -Path $location -Filter "$($item.id).psm1" -Recurse -File -Force `
6369
| Select-Object -First 1 -ExpandProperty FullName
6470

build/funcs/Get-PackageCopyright.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function Get-PackageCopyright {
1313
if (-not (Test-Path -Path $LicenseFilePath -ErrorAction SilentlyContinue)) {
1414
throw "The file '${LicenseFilePath}' does not exist."
1515
}
16-
[string] $copyright = ((Get-Content -Path $LicenseFilePath -Encoding UTF8 |
16+
[string] $copyright = $null
17+
$copyright = ((Get-Content -Path $LicenseFilePath -Encoding UTF8 |
1718
Where-Object { $_ -match "(\(c\)|©️)" } |
1819
ForEach-Object { $_.Trim().TrimEnd(",").TrimEnd(";").TrimEnd(".") }
1920
) -join "; "+[Environment]::NewLine) + "."

build/funcs/Get-PackageGuid.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ function Get-PackageGuid {
99
if (-not (Test-Path -Path $GuidFilePath -ErrorAction SilentlyContinue)) {
1010
throw "The file '${GuidFilePath}' does not exist."
1111
}
12-
[string] $guid = (Get-Content -Raw -Path $GuidFilePath -Encoding UTF8).Trim()
12+
[string] $guid = $null
13+
$guid = (Get-Content -Raw -Path $GuidFilePath -Encoding UTF8).Trim()
1314
if ([string]::IsNullOrEmpty($guid)) {
1415
throw "The file '${GuidFilePath}' is empty."
1516
}

build/funcs/Get-PackageSynopsis.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function Get-PackageSynopsis {
1616
Write-Error "The file '${synopsisFilePath}' does not exist."
1717
return
1818
}
19-
[string] $synopsis = (Get-Content -Raw -Path $synopsisFilePath -Encoding UTF8)
19+
[string] $synopsis = $null
20+
$synopsis = (Get-Content -Raw -Path $synopsisFilePath -Encoding UTF8)
2021
if ([string]::IsNullOrEmpty($synopsis)) {
2122
if ($AllowEmpty) {
2223
return

build/funcs/Get-PackageTags.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function Get-PackageTags {
5151
Write-Error "The file '${tagsFile}' does not exist."
5252
return
5353
}
54-
[string[]] $tags = ((Get-Content -Path $tagsFile -Encoding UTF8 | ForEach-Object { $_.Trim() } | Where-Object { -not [string]::IsNullOrEmpty($_) }))
54+
[string[]] $tags = @()
55+
$tags = ((Get-Content -Path $tagsFile -Encoding UTF8 | ForEach-Object { $_.Trim() } | Where-Object { -not [string]::IsNullOrEmpty($_) }))
5556

5657
if ((-not $tags) -and (-not $ForNuSpec)) {
5758
if ($AllowEmpty) {
@@ -83,8 +84,9 @@ function Get-PackageTags {
8384
$tags += @("PSIncludes_DscResource")
8485
$tags += @($ModuleExports["DscResources"] | ForEach-Object { "PSDscResource_${_}" })
8586
}
86-
[System.IO.FileInfo[]] $roleCapabilityFiles = @(Get-ChildItem -Path "${PSScriptRoot}${ds}..${ds}..${ds}" -Filter "*.psrc" -Recurse -File -Force -ErrorAction SilentlyContinue)
87-
if ($roleCapabilityFiles.Count -gt 0) {
87+
[System.IO.FileInfo[]] $roleCapabilityFiles = @()
88+
$roleCapabilityFiles = @(Get-ChildItem -Path "${PSScriptRoot}${ds}..${ds}..${ds}" -Filter "*.psrc" -Recurse -File -Force -ErrorAction SilentlyContinue)
89+
if ($roleCapabilityFiles) {
8890
[string[]] $roleCapabilities = $roleCapabilityFiles | ForEach-Object { $_.Name.Replace(".psrc", "") }
8991
$tags += @("PSIncludes_RoleCapability")
9092
$tags += @($roleCapabilities | ForEach-Object { "PSRoleCapability_$_" })
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env pwsh
2+
$ErrorActionPreference = "Stop"
3+
Set-StrictMode -Version Latest
4+
5+
6+
<#
7+
.SYNOPSIS
8+
Gets the required version of PowerShell for the current project, which is the maximum of the versions that its source files require - and also its nested dependencies.
9+
#>
10+
function Get-RequiredPwshVersion {
11+
[OutputType([Version])]
12+
param(
13+
)
14+
15+
[string] $ds = [System.IO.Path]::DirectorySeparatorChar
16+
17+
[version[]] $nestedModulesRequiredPwshVersions = @()
18+
. "${PSScriptRoot}${ds}Import-PSGalleryModuleNested.ps1"
19+
[PSModuleInfo[]] $modulesToUnload = @()
20+
# We don't `-SkipAlreadyLoaded` because we want to make sure we get all the required Pwsh versions.
21+
$modulesToUnload += @(Import-PSGalleryModuleNested -RuntimeDependencies)
22+
try {
23+
$nestedModulesRequiredPwshVersions += @($modulesToUnload | Select-Object -ExpandProperty PowerShellVersion)
24+
} finally {
25+
if ($null -ne $modulesToUnload) {
26+
[array]::Reverse($modulesToUnload) `
27+
| Select-Object -ExpandProperty path -Unique `
28+
| Remove-Module -Force -ErrorAction SilentlyContinue
29+
}
30+
}
31+
32+
# Parse all the source files for the current module and look for the `#requires -version` directive.
33+
[string] $thisModuleSrcDir = "${PSScriptRoot}${ds}..${ds}..${ds}src"
34+
[System.IO.FileInfo[]] $thisModuleSrcFiles = @(
35+
@('ps1', 'psm1') | ForEach-Object { Get-ChildItem -Path $thisModuleSrcDir -Filter "*.$_" -File -Recurse -Force }
36+
)
37+
if ($thisModuleSrcFiles) {
38+
[version[]] $thisModuleRequiredPwshVersions = @()
39+
foreach ($thisModuleSrcFile in $thisModuleSrcFiles) {
40+
# Get the ast of the file.
41+
[System.Management.Automation.Language.Ast] $ast = $null
42+
[System.Management.Automation.Language.Token[]] $tokens = @()
43+
[System.Management.Automation.Language.ParseError[]] $errors = @()
44+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($thisModuleSrcFile.FullName, [ref] $tokens, [ref] $errors)
45+
46+
# Look for the `#requires -version` directive.
47+
[string[]] $requiresVersions = $null
48+
$requiresVersions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.ScriptBlockAst] }, $true) `
49+
| ForEach-Object {
50+
if ($_.Extent.Text -match '#requires\s+-version\s+(?<version>[^\s]+)') {
51+
return $matches['version']
52+
}
53+
}
54+
if ($requiresVersions) {
55+
$thisModuleRequiredPwshVersions += @($requiresVersions | ForEach-Object { [version] $_ })
56+
}
57+
}
58+
}
59+
60+
# Return the maximum of the required Pwsh versions.
61+
[version[]] $allRequiredPwshVersions = @($thisModuleRequiredPwshVersions + $nestedModulesRequiredPwshVersions)
62+
$allRequiredPwshVersions += @([version]"7.0")
63+
return ($allRequiredPwshVersions | Sort-Object -Descending | Select-Object -First 1)
64+
}

build/funcs/Import-PSGalleryModuleNested.ps1

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,35 @@ function Import-PSGalleryModuleNested {
6060
. "$PSScriptRoot${ds}Get-PSGalleryModuleNested.ps1"
6161

6262
if ($DevelopmentDependencies) {
63-
[object[]] $nestedDevelopmentPsgalleryModules = Get-PSGalleryModuleNested -DevelopmentDependencies
64-
$nestedDevelopmentPsgalleryModules | ForEach-Object {
65-
Write-Information "Importing nested development dependency module '$($_.id)'."
66-
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
63+
[object[]] $nestedDevelopmentPsgalleryModules = @()
64+
$nestedDevelopmentPsgalleryModules = Get-PSGalleryModuleNested -DevelopmentDependencies
65+
if ($nestedDevelopmentPsgalleryModules) {
66+
$nestedDevelopmentPsgalleryModules | ForEach-Object {
67+
Write-Information "Importing nested development dependency module '$($_.id)'."
68+
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
69+
}
6770
}
6871
}
6972

7073
if ($RuntimeDependencies) {
71-
[object[]] $nestedRuntimePsgalleryModules = Get-PSGalleryModuleNested -RuntimeDependencies
72-
$nestedRuntimePsgalleryModules | ForEach-Object {
73-
Write-Information "Importing nested runtime dependency module '$($_.id)'."
74-
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
74+
[object[]] $nestedRuntimePsgalleryModules = @()
75+
$nestedRuntimePsgalleryModules = Get-PSGalleryModuleNested -RuntimeDependencies
76+
if ($nestedRuntimePsgalleryModules) {
77+
$nestedRuntimePsgalleryModules | ForEach-Object {
78+
Write-Information "Importing nested runtime dependency module '$($_.id)'."
79+
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
80+
}
7581
}
7682
}
7783

7884
if ($id) {
79-
[object[]] $nestedPsgalleryModules = Get-PSGalleryModuleNested -id $id
80-
$nestedPsgalleryModules | ForEach-Object {
81-
Write-Information "Importing nested module '$($_.id)'."
82-
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
85+
[object[]] $nestedPsgalleryModules = @()
86+
$nestedPsgalleryModules = Get-PSGalleryModuleNested -id $id
87+
if ($nestedPsgalleryModules) {
88+
$nestedPsgalleryModules | ForEach-Object {
89+
Write-Information "Importing nested module '$($_.id)'."
90+
$modulesLoaded += @($_ | Import-PSGalleryModuleNested -SkipAlreadyLoaded:$SkipAlreadyLoaded)
91+
}
8392
}
8493
}
8594
}

build/funcs/New-ModuleManifestCustomized.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ function New-ModuleManifestCustomized {
141141
)
142142
}
143143

144+
. "${PSScriptRoot}${ds}Get-RequiredPwshVersion.ps1"
145+
[version] $requiredPwshVersion = $null
146+
$requiredPwshVersion = Get-RequiredPwshVersion
147+
144148
# This is just where we write the psd1 _for now_.
145149
# The project file generated later will decide where in the NuPkg it goes (and therefor where it lands when the NuPkg is expanded.)
146150
[string] $moduleManifestPath = "${PSScriptRoot}${ds}..${ds}..${ds}out${ds}${PackageId}.psd1"
@@ -161,7 +165,7 @@ function New-ModuleManifestCustomized {
161165
-LicenseUri "${ProjectUrlAtVersion}/${LicenseFileName}" `
162166
-ProjectUri $ProjectUrl `
163167
-IconUri $PackageIconUrl `
164-
-PowerShellVersion "7.0" `
168+
-PowerShellVersion $requiredPwshVersion.ToString() `
165169
-NestedModules $nestedModules `
166170
@additionNewModuleManifestArgs
167171

0 commit comments

Comments
 (0)