Skip to content

Commit

Permalink
Fix tooling (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherMann authored Nov 22, 2024
1 parent dd15a8e commit d806335
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 79 deletions.
76 changes: 0 additions & 76 deletions build/Package-PsModule.ps1

This file was deleted.

46 changes: 46 additions & 0 deletions build/Populate-PsModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#Requires -Version 7.4
<#
.SYNOPSIS
Populates the Powershell module.
.DESCRIPTION
This script populates the Powershell module with the assemblies
from the build output. It is intended to be called by MSBuild
during the normal build process. The script will be called once
for each target framework!
#>
[CmdletBinding()]
param(
[Parameter()]
[string]
[ValidateScript({ $_ -match '[a-zA-Z\.]+' }, ErrorMessage = "The module name '{0}' is invalid.")]
$ModuleName,
[Parameter()]
[string]
[ValidateScript({ Test-Path $_ }, ErrorMessage = "The path '{0}' is invalid.")]
$OutputDirectory,
[Parameter()]
[string]
[ValidateScript({ Test-Path $_ }, ErrorMessage = "The path '{0}' is invalid.")]
$TargetPath,
[Parameter()]
[string]
[ValidateScript({ $_ -match 'net\d+\.?\d+' }, ErrorMessage = "The target framework '{0}' is invalid.")]
$TargetFramework
)

$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

$excludedFiles = @("System.Management.Automation.dll", "JetBrains.Annotations.dll")

$modulePath = Join-Path $OutputDirectory "PsModule" $ModuleName
$isWindowsPowershell = $TargetFramework -like 'net4*'
$moduleAssemblyPath = Join-Path $modulePath ($isWindowsPowershell ? 'desktop' : 'coreclr')

# Copy the assemblies from the build output
if (Test-Path $moduleAssemblyPath) {
Remove-Item -Path $moduleAssemblyPath -Force -Recurse
}
$null = New-Item -ItemType Directory -Path $moduleAssemblyPath
$targetDirectory = (Get-Item $TargetPath).Directory.FullName
Copy-Item -Path (Join-Path $targetDirectory "*") -Destination $moduleAssemblyPath -Exclude $excludedFiles -Recurse
50 changes: 50 additions & 0 deletions build/Prepare-PsModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#Requires -Version 7.4
<#
.SYNOPSIS
Prepares the Powershell module.
.DESCRIPTION
This script prepares the Powershell module for distribution.
It is intended to be called by MSBuild during the normal build
process. The script prepares the module metadata (e.g. the manifest).
#>
[CmdletBinding()]
param(
[Parameter()]
[string]
[ValidateScript({ $_ -match '[a-zA-Z\.]+' }, ErrorMessage = "The module name '{0}' is invalid.")]
$ModuleName,
[Parameter()]
[string]
[ValidateScript({ Test-Path $_ -IsValid }, ErrorMessage = "The path '{0}' is invalid.")]
$OutputDirectory,
[Parameter()]
[string]
[ValidateScript({ $_ -match '\d+\.\d+\.\d+' }, ErrorMessage = "The version '{0}' is invalid.")]
$MajorMinorPatch,
[Parameter()]
[string]
$NuGetPreReleaseTag
)

$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

$modulePath = Join-Path $OutputDirectory "PsModule" $ModuleName

# Prepare the output directory
if (Test-Path $modulePath) {
Remove-Item -Path $modulePath -Force -Recurse
}

$null = New-Item -ItemType Directory -Path $modulePath

$isPrerelease = -not [string]::IsNullOrWhiteSpace($NuGetPreReleaseTag)

# Prepare the module manifest
$config = Get-Content (Join-Path $PSScriptRoot "$ModuleName.psd1") -Raw
$config = $config.Replace("ModuleVersion = '0.1'", "ModuleVersion = '$MajorMinorPatch'");
if ($isPrerelease) {
$config = $config.Replace("# Prerelease = ''", "Prerelease = '$NuGetPreReleaseTag'");
}
Set-Content -Path (Join-Path $modulePath "$ModuleName.psd1") -Value $config
Copy-Item -Path (Join-Path $PSScriptRoot "$ModuleName.psm1") -Destination $modulePath
41 changes: 41 additions & 0 deletions build/Test-PsModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#Requires -Version 7.4
<#
.SYNOPSIS
Test the Powershell module.
.DESCRIPTION
This script tests the fully packaged Powershell module.
It is intended to be called by MSBuild during the normal build
process.
#>
[CmdletBinding()]
param(
[Parameter()]
[string]
[ValidateScript({ $_ -match '[a-zA-Z\.]+' }, ErrorMessage = "The module name '{0}' is invalid.")]
$ModuleName,
[Parameter()]
[string]
[ValidateScript({ Test-Path $_ }, ErrorMessage = "The path '{0}' is invalid.")]
$OutputDirectory
)

$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

$modulePath = Join-Path $OutputDirectory "PsModule" $ModuleName

# Verify that all Cmdlets are exposed in the manifest. We must load the modules
# in separate Powershell processes to avoid conflicts.
$moduleCmdlets = (powershell.exe -Command "[array](Import-Module -Scope Local $modulePath -PassThru).ExportedCmdlets.Keys -join ','") -split ','
$assemblyCmdlets = (powershell.exe -Command "[array](Import-Module -Scope Local $(Join-Path $modulePath "desktop" "$ModuleName.Commands.dll") -PassThru).ExportedCmdlets.Keys -join ','") -split ','
$missingCmdlets = [Linq.Enumerable]::Except($assemblyCmdlets, $moduleCmdlets)
if ($missingCmdlets.Count -gt 0) {
throw "The following Cmdlets are not exposed in the module manifest when checking with Windows Powershell: $($missingCmdlets -join ', ')"
}

$moduleCmdlets = (pwsh.exe -Command "[array](Import-Module -Scope Local $modulePath -PassThru).ExportedCmdlets.Keys -join ','") -split ','
$assemblyCmdlets = (pwsh.exe -Command "[array](Import-Module -Scope Local $(Join-Path $modulePath "coreclr" "$ModuleName.Commands.dll") -PassThru).ExportedCmdlets.Keys -join ','") -split ','
$missingCmdlets = [Linq.Enumerable]::Except($assemblyCmdlets, $moduleCmdlets)
if ($missingCmdlets.Count -gt 0) {
throw "The following Cmdlets are not exposed in the module manifest when checking with Powershell 7: $($missingCmdlets -join ', ')"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@
<!-- Custom properties and targets for packaging the Powershell module -->
<PropertyGroup>
<PsModuleName>Eryph.ClientRuntime.Configuration</PsModuleName>
<GitVersionTargetsBefore>$(GitVersionTargetsBefore);PreparePsModule</GitVersionTargetsBefore>
<PowershellExecutable>pwsh.exe</PowershellExecutable>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
<PowershellExecutable>powershell.exe</PowershellExecutable>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="pwsh.exe -NoProfile -File &quot;$(ProjectDir)../../build/Package-PsModule.ps1&quot; -ModuleName &quot;$(PsModuleName)&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(ProjectDir), 'bin', $(Configuration)))&quot; -TargetPath &quot;$(TargetPath)&quot; -TargetFramework &quot;$(TargetFramework)&quot; -MajorMinorPatch &quot;$(GitVersion_MajorMinorPatch)&quot; -NuGetPreReleaseTag &quot;$(GitVersion_NuGetPreReleaseTag)&quot;" />
<Target Name="PreparePsModule" BeforeTargets="DispatchToInnerBuilds">
<Exec Command="pwsh.exe -NoProfile -File &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), '..', '..', 'build', 'Prepare-PsModule.ps1'))&quot; -ModuleName &quot;$(PsModuleName)&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), 'bin', $(Configuration)))&quot; -MajorMinorPatch &quot;$(GitVersion_MajorMinorPatch)&quot; -NuGetPreReleaseTag &quot;$(GitVersion_NuGetPreReleaseTag)&quot;" />
</Target>
<Target Name="PopulatePsModule" AfterTargets="PostBuildEvent">
<Exec Command="pwsh.exe -NoProfile -File &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), '..', '..', 'build', 'Populate-PsModule.ps1'))&quot; -ModuleName &quot;$(PsModuleName)&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), 'bin', $(Configuration)))&quot; -TargetPath &quot;$(TargetPath)&quot; -TargetFramework &quot;$(TargetFramework)&quot;" />
</Target>
<Target Name="TestPsModule" AfterTargets="DispatchToInnerBuilds">
<Exec Command="pwsh.exe -NoProfile -File &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), '..', '..', 'build', 'Test-PsModule.ps1'))&quot; -ModuleName &quot;$(PsModuleName)&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), 'bin', $(Configuration)))&quot;" />
</Target>
<Target Name="PostClean" AfterTargets="Clean">
<Exec Command="pwsh.exe -NoProfile -File &quot;$(ProjectDir)../../build/Clean-PsModule.ps1&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(ProjectDir), 'bin', $(Configuration)))&quot;" />
<Exec Command="pwsh.exe -NoProfile -File &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), '..', '..', 'build', 'Clean-PsModule.ps1'))&quot; -OutputDirectory &quot;$([System.IO.Path]::Combine($(MSBuildProjectDirectory), 'bin', $(Configuration)))&quot;" />
</Target>

</Project>

0 comments on commit d806335

Please sign in to comment.