Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Build/New-ParallelKitModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# .\Build\New-ParallelKitModule.ps1 -OutputDirectory C:\Modules\ParallelKit
#
# The folder has to be called ParallelKit, or be a version folder inside one
# (ParallelKit\4.0), for Import-Module to find it by name.
# (ParallelKit\4.0.0), for Import-Module to find it by name.
#
# The manifest's version always has three parts: the kit's 4.0 is the module's
# 4.0.0, and 3.4.1 stays 3.4.1.
#
# The .psm1 is the .ps1 without the wrapper that loads it as an in-memory
# module: the Get-Module | Remove-Module line, and the New-Module wrapper's first
Expand Down Expand Up @@ -128,6 +131,13 @@ try {
if ($header.Count -ne 1 -or $inCode.Count -ne 1) { Stop-PkBuild ("expected one '# Version X.Y' line and one `$version = [version]'X.Y' line, found " + $header.Count + " and " + $inCode.Count + ".") }
$version = $header[0].Groups[1].Value
if ($version -cne $inCode[0].Groups[1].Value) { Stop-PkBuild ("the header says version " + $version + " and Initialize-ParallelKit " + $inCode[0].Groups[1].Value + ".") }
# Three parts in the manifest. The package gets three whatever the manifest
# says, Windows PowerShell's built-in PowerShellGet installs into a folder
# named after the package's version, and PowerShell doesn't find a module
# whose version folder isn't its manifest's version. The harness works the
# same version out for its module folder (Get-HarnessModuleVersion).
$moduleVersion = $version
if ($moduleVersion -notmatch '^\d+\.\d+\.\d+$') { $moduleVersion += '.0' }

# Written out rather than made by New-ModuleManifest, which puts the date
# and the user's name in the file.
Expand Down Expand Up @@ -157,13 +167,13 @@ __FUNCTIONS__
}
}
'@
$manifest = $manifest.Replace("`r`n", "`n").Replace('__VERSION__', $version).Replace('__MODULEGUID__', $moduleGuid).Replace('__FUNCTIONS__', $fnLines) + "`n"
$manifest = $manifest.Replace("`r`n", "`n").Replace('__VERSION__', $moduleVersion).Replace('__MODULEGUID__', $moduleGuid).Replace('__FUNCTIONS__', $fnLines) + "`n"
$null = Get-PkBuildAst -Text $manifest -What 'the manifest made from it'

$null = New-Item -ItemType Directory -Force -Path $OutputDirectory
[System.IO.File]::WriteAllBytes((Join-Path $OutputDirectory 'ParallelKit.psm1'), [System.Text.Encoding]::ASCII.GetBytes($module.Replace("`n", "`r`n")))
[System.IO.File]::WriteAllBytes((Join-Path $OutputDirectory 'ParallelKit.psd1'), [System.Text.Encoding]::ASCII.GetBytes($manifest.Replace("`n", "`r`n")))
Write-Output ("Wrote ParallelKit " + $version + " (" + @($moduleFns).Count + " functions, " + $public.Count + " public) to " + $OutputDirectory)
Write-Output ("Wrote ParallelKit " + $moduleVersion + " (" + @($moduleFns).Count + " functions, " + $public.Count + " public) to " + $OutputDirectory)
} catch {
[Console]::Error.WriteLine($_.Exception.Message)
exit 1
Expand Down
43 changes: 39 additions & 4 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ $kitPs1 = Join-Path $PSScriptRoot "ParallelKit.ps1"
$kitPath = $kitPs1
$kitSource = $kitPs1
$moduleDir = $null
function Get-HarnessModuleVersion {
# The module's version for a kit's: three parts, as the generator writes it
# in the manifest (4.0 is 4.0.0, 3.4.1 stays). Worked out here on its own,
# not read from a built manifest, so a build that gets it wrong fails.
param([string]$KitVersion)
if ($KitVersion -match '^\d+\.\d+$') { return $KitVersion + '.0' }
return $KitVersion
}
if ($AsModule) {
$moduleVersion = [regex]::Match([System.IO.File]::ReadAllText($kitPs1), '(?m)^# Version (\d+\.\d+(?:\.\d+)?)\b').Groups[1].Value
$moduleVersion = Get-HarnessModuleVersion ([regex]::Match([System.IO.File]::ReadAllText($kitPs1), '(?m)^# Version (\d+\.\d+(?:\.\d+)?)\b').Groups[1].Value)
$moduleDir = Join-Path $env:TEMP ("pk-harness-module-" + $PID)
$moduleBuilt = Join-Path $moduleDir ('mods\ParallelKit\' + $moduleVersion)
$global:LASTEXITCODE = 0
Expand Down Expand Up @@ -5862,6 +5870,9 @@ $bmDir = Join-Path $env:TEMP ("pk-harness-bm-" + $PID)
$null = New-Item -ItemType Directory -Force -Path $bmDir
$bmText = [System.IO.File]::ReadAllText($kitPs1)
$bmVersion = [regex]::Match($bmText, '(?m)^# Version (\d+\.\d+(?:\.\d+)?)\b').Groups[1].Value
# A version folder named after the manifest's version, as Install-Module makes:
# Import-Module by name doesn't find the module in one that isn't.
$bmModuleVersion = Get-HarnessModuleVersion $bmVersion

function Get-BmBuildCommand([string]$Source, [string]$Out) {
return @{ FilePath = $selfExe; Arguments = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $bmGenerator, '-SourcePath', $Source, '-OutputDirectory', $Out); Out = $Out }
Expand All @@ -5876,7 +5887,7 @@ try {

# The same bytes every time, with LF or CRLF, and only the wrapper gone.
$bmMods = Join-Path $bmDir 'mods'
$bmBuilt = Join-Path $bmMods ('ParallelKit\' + $bmVersion)
$bmBuilt = Join-Path $bmMods ('ParallelKit\' + $bmModuleVersion)
$bmLf = New-BmFile 'lf.ps1' $bmText.Replace("`r`n", "`n")
$bmCrlf = New-BmFile 'crlf.ps1' $bmText.Replace("`r`n", "`n").Replace("`n", "`r`n")
$bmBuilds = Invoke-PkProcessBatch -MaxConcurrent 4 -Commands @((Get-BmBuildCommand $kitPs1 $bmBuilt), (Get-BmBuildCommand $kitPs1 (Join-Path $bmDir 'again')), (Get-BmBuildCommand $bmLf (Join-Path $bmDir 'lf')), (Get-BmBuildCommand $bmCrlf (Join-Path $bmDir 'crlf')))
Expand Down Expand Up @@ -5931,11 +5942,35 @@ foreach ($r in (Invoke-PkProcessBatch -MaxConcurrent 4 -Commands $bmCommands).Re
Test-Assert ($bmMissed.Count -eq 0) ("a .ps1 that doesn't look as expected is refused with a message, writing nothing (" + $bmCases.Count + " cases; not: " + ($bmMissed -join '; ') + ")")

# The manifest.
$bmManifest = Test-ModuleManifest -Path (Join-Path $bmBuilt 'ParallelKit.psd1')
# Test-ModuleManifest also checks the version folder is named after the
# manifest's version, and throws if it isn't.
$bmManifestError = ''
try {
$bmManifest = Test-ModuleManifest -Path (Join-Path $bmBuilt 'ParallelKit.psd1') -ErrorAction Stop
} catch {
$bmManifestError = [string]$_
$bmManifest = [pscustomobject]@{ Version = $null; ExportedFunctions = @{}; PowerShellVersion = $null; Tags = @(); Author = ''; Guid = [guid]::Empty }
}
$bmExported = @($bmManifest.ExportedFunctions.Keys)
$bmDiffer = @($publicFunctions | Where-Object { $bmExported -notcontains $_ }) + @($bmExported | Where-Object { $publicFunctions -notcontains $_ })
Test-Assert ($bmManifest.Version -eq [version]$bmVersion -and $bmDiffer.Count -eq 0 -and $bmManifest.PowerShellVersion -eq [version]'5.1') ("its manifest is valid, with the kit's version and public functions (" + $bmManifest.Version + "; differ: " + ($bmDiffer -join ', ') + ")")
if ($bmManifestError) { $bmDiffer = @('none read: ' + $bmManifestError) }
Test-Assert ($bmManifestError -eq '' -and $bmManifest.Version -eq [version]$bmModuleVersion -and $bmDiffer.Count -eq 0 -and $bmManifest.PowerShellVersion -eq [version]'5.1') ("its manifest is valid, with the kit's version and public functions, in a folder named after its version (" + $bmManifest.Version + "; differ: " + ($bmDiffer -join ', ') + ")")
Test-Assert (@('PSEdition_Desktop', 'PSEdition_Core', 'Windows' | Where-Object { $bmManifest.Tags -notcontains $_ }).Count -eq 0 -and $bmManifest.Author -eq 'HalRiveria' -and $bmManifest.Guid -eq [guid]'1a05c60a-cafb-4e52-98e8-dd52affd2d61') ("and its identity: GUID, author and tags (" + ($bmManifest.Tags -join ', ') + ")")
# Three parts in the manifest's version, whatever the kit's has: a folder
# named after a two-part one isn't where 5.1's Install-Module puts it.
$bmVersionCases = [ordered]@{ '9.8' = '9.8.0'; '9.8.7' = '9.8.7' }
$bmVersionCommands = @(foreach ($v in $bmVersionCases.Keys) {
$text = $bmText.Replace('# Version ' + $bmVersion + ' ', '# Version ' + $v + ' ').Replace("`$version = [version]'" + $bmVersion + "'", "`$version = [version]'" + $v + "'")
$command = Get-BmBuildCommand (New-BmFile ('version-' + $v + '.ps1') $text) (Join-Path $bmDir ('version-' + $v))
$command['Kit'] = $v
$command
})
$bmVersionsWrong = @(foreach ($r in (Invoke-PkProcessBatch -MaxConcurrent 2 -Commands $bmVersionCommands).Results) {
$got = 'exit ' + $r.ExitCode
if ($r.ExitCode -eq 0) { $got = [regex]::Match([System.IO.File]::ReadAllText((Join-Path $r.Command['Out'] 'ParallelKit.psd1')), "ModuleVersion\s*=\s*'([^']*)'").Groups[1].Value }
if ($got -cne $bmVersionCases[$r.Command['Kit']]) { $r.Command['Kit'] + ' gave ' + $got }
})
Test-Assert ($bmVersionCommands.Count -eq 2 -and $bmVersionsWrong.Count -eq 0) ("the manifest's version has three parts: 9.8 is 9.8.0, 9.8.7 stays (not: " + ($bmVersionsWrong -join '; ') + ")")

# Loaded in fresh processes, by name and by path: what a caller does with it.
# -OnComplete and -ResultHandler are closures that call the kit's functions.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ Import-Module C:\Modules\ParallelKit

Installed, or built into a `ParallelKit` folder somewhere on `$env:PSModulePath`, `Import-Module ParallelKit` finds it by name, and so does a call to any of its functions without importing it first. The rest of this README applies either way.

The module's version always has three parts: the kit's X.Y (what `Initialize-ParallelKit` reports) is the module's X.Y.0, and an X.Y.Z version stays as it is. So for 4.0, `Get-Module ParallelKit` shows 4.0.0, and that's the version `Import-Module -RequiredVersion` takes.

<details>
<summary>If the import is refused</summary>

Expand Down
Loading