benchpress/build.ps1

111 строки
3.4 KiB
PowerShell
Исходник Обычный вид История

#159 Refactor file structure * Initial commit with all but one unit test functioning correctly. Confirm-Resource.Tests.ps1 is failing due to "Unable to find type [ResourceType]", which makes no sense, since Get-ResourceByType uses the same enum, but the unit tests pass without issue. * Merged examples into grouped folders. Removed modules from the psd1. * Added a build script that can be used both during development and to build the final .psm1 for publishing to PS Gallery. * Minor code cleanup. * Updating CI/CD workflows. * Updated the installation and contributing docs. * Moved pr-psgallery.yml workflow into ci.yml. Now that there is a "build" process for the module this should happen in the CI pipeline and not the PR pipeline. * Fixing working directory for unit tests. * Swapped out relative paths for the PS Gallery module name in the examples, because the relative path will no longer work without building the module. * Moved the ci-publish-docs-branch.yml into the ci.yml. It made sense logically once I tried rewriting it after the refactor. * Fixing broken reference in the 'ci_cd_workflows.md' file. * Minor update to 'ci_cd_workflows.md' * I honestly couldn't figure out why `ResourceType` was not able to be found. I tried every which way of `using`/`Import`ing/dot sourcing that can be imagined. I think that this is a fundamental issue with PowerShell. The more I researched the more I found about enums and classes not being available from nested modules. I'm not sure why the classes work and enums do not though. * Fixing broken unit test's reference to `ResourceType.psm1`.
2023-03-01 03:32:31 +03:00
#! /usr/bin/pwsh
[CmdletBinding()]
param (
[switch] $Load,
[switch] $Clean,
[switch] $Inline,
[switch] $Import
)
function Copy-Content ($Content) {
foreach ($c in $content) {
$source, $destination = $c
$null = New-Item -Force $destination -ItemType Directory
Get-ChildItem $source -File | Copy-Item -Destination $destination
}
}
$ErrorActionPreference = 'Stop'
if (-not $PSBoundParameters.ContainsKey("Inline")) {
# Force inlining by env variable, build.ps1 is used in multiple places and passing the $inline everywhere is difficult.
#159 Refactor file structure * Initial commit with all but one unit test functioning correctly. Confirm-Resource.Tests.ps1 is failing due to "Unable to find type [ResourceType]", which makes no sense, since Get-ResourceByType uses the same enum, but the unit tests pass without issue. * Merged examples into grouped folders. Removed modules from the psd1. * Added a build script that can be used both during development and to build the final .psm1 for publishing to PS Gallery. * Minor code cleanup. * Updating CI/CD workflows. * Updated the installation and contributing docs. * Moved pr-psgallery.yml workflow into ci.yml. Now that there is a "build" process for the module this should happen in the CI pipeline and not the PR pipeline. * Fixing working directory for unit tests. * Swapped out relative paths for the PS Gallery module name in the examples, because the relative path will no longer work without building the module. * Moved the ci-publish-docs-branch.yml into the ci.yml. It made sense logically once I tried rewriting it after the refactor. * Fixing broken reference in the 'ci_cd_workflows.md' file. * Minor update to 'ci_cd_workflows.md' * I honestly couldn't figure out why `ResourceType` was not able to be found. I tried every which way of `using`/`Import`ing/dot sourcing that can be imagined. I think that this is a fundamental issue with PowerShell. The more I researched the more I found about enums and classes not being available from nested modules. I'm not sure why the classes work and enums do not though. * Fixing broken unit test's reference to `ResourceType.psm1`.
2023-03-01 03:32:31 +03:00
# Only read this option here. Don't write it.
$Inline = $env:BENCPRESS_BUILD_INLINE -eq "1"
} else {
# We provided Inline explicitly, write the option. This assumes that you don't use -Inline:$false in any of the
# test scripts, otherwise the test script would reset the option incorrectly.
$env:BENCHPRESS_BUILD_INLINE = [string][int][bool] $Inline
}
Get-Module BenchPress | Remove-Module
if ($Clean -and (Test-Path "$PSScriptRoot/bin")) {
Remove-Item "$PSScriptRoot/bin" -Recurse -Force
return
#159 Refactor file structure * Initial commit with all but one unit test functioning correctly. Confirm-Resource.Tests.ps1 is failing due to "Unable to find type [ResourceType]", which makes no sense, since Get-ResourceByType uses the same enum, but the unit tests pass without issue. * Merged examples into grouped folders. Removed modules from the psd1. * Added a build script that can be used both during development and to build the final .psm1 for publishing to PS Gallery. * Minor code cleanup. * Updating CI/CD workflows. * Updated the installation and contributing docs. * Moved pr-psgallery.yml workflow into ci.yml. Now that there is a "build" process for the module this should happen in the CI pipeline and not the PR pipeline. * Fixing working directory for unit tests. * Swapped out relative paths for the PS Gallery module name in the examples, because the relative path will no longer work without building the module. * Moved the ci-publish-docs-branch.yml into the ci.yml. It made sense logically once I tried rewriting it after the refactor. * Fixing broken reference in the 'ci_cd_workflows.md' file. * Minor update to 'ci_cd_workflows.md' * I honestly couldn't figure out why `ResourceType` was not able to be found. I tried every which way of `using`/`Import`ing/dot sourcing that can be imagined. I think that this is a fundamental issue with PowerShell. The more I researched the more I found about enums and classes not being available from nested modules. I'm not sure why the classes work and enums do not though. * Fixing broken unit test's reference to `ResourceType.psm1`.
2023-03-01 03:32:31 +03:00
}
$null = New-Item "$PSScriptRoot/bin" -ItemType Directory -Force
$content = @(
, ("$PSScriptRoot/Modules/BenchPress.Azure/BenchPress.Azure.psd1", "$PSScriptRoot/bin/")
)
Copy-Content -Content $content
$publicClasses = @(Get-ChildItem -Path $PSScriptRoot/Modules/BenchPress.Azure/Classes -Recurse -Filter "*.psm1")
$publicFunctions = @(Get-ChildItem -Path $PSScriptRoot/Modules/BenchPress.Azure/Public -Recurse -Filter "*.ps1")
$privateFunctions = @(Get-ChildItem -Path $PSScriptRoot/Modules/BenchPress.Azure/Private -Recurse -Filter "*.ps1")
$sb = [System.Text.StringBuilder]""
if ($Inline) {
$allFiles = $publicClasses + $publicFunctions + $privateFunctions
foreach ($file in $allFiles) {
$lines = Get-Content $file
$relativePath = ($file.FullName -replace ([regex]::Escape($PSScriptRoot))).TrimStart('\').TrimStart('/')
$null = $sb.AppendLine("# file $($relativePath)")
$skipLine = $false
foreach ($line in $lines) {
# when inlining the code skip everything wrapped in # INLINE_SKIP, # end INLINE_SKIP
if (-not $skipLine -and $line -match '^.*#\s*INLINE_SKIP') {
$skipLine = $true
}
if (-not $skipLine) {
$null = $sb.AppendLine($line)
}
if ($skipLine -and $line -match '^.*#\s*end\s*INLINE_SKIP') {
$skipLine = $false
}
}
}
} else {
# "using" for class files must be the first line in the module script
foreach ($class in $publicClasses) {
$null = $sb.AppendLine("using module $($class.FullName)")
}
# Define this at the top of the module, after the using statements, to skip the code that is wrapped in this if in different source files.
#159 Refactor file structure * Initial commit with all but one unit test functioning correctly. Confirm-Resource.Tests.ps1 is failing due to "Unable to find type [ResourceType]", which makes no sense, since Get-ResourceByType uses the same enum, but the unit tests pass without issue. * Merged examples into grouped folders. Removed modules from the psd1. * Added a build script that can be used both during development and to build the final .psm1 for publishing to PS Gallery. * Minor code cleanup. * Updating CI/CD workflows. * Updated the installation and contributing docs. * Moved pr-psgallery.yml workflow into ci.yml. Now that there is a "build" process for the module this should happen in the CI pipeline and not the PR pipeline. * Fixing working directory for unit tests. * Swapped out relative paths for the PS Gallery module name in the examples, because the relative path will no longer work without building the module. * Moved the ci-publish-docs-branch.yml into the ci.yml. It made sense logically once I tried rewriting it after the refactor. * Fixing broken reference in the 'ci_cd_workflows.md' file. * Minor update to 'ci_cd_workflows.md' * I honestly couldn't figure out why `ResourceType` was not able to be found. I tried every which way of `using`/`Import`ing/dot sourcing that can be imagined. I think that this is a fundamental issue with PowerShell. The more I researched the more I found about enums and classes not being available from nested modules. I'm not sure why the classes work and enums do not though. * Fixing broken unit test's reference to `ResourceType.psm1`.
2023-03-01 03:32:31 +03:00
$null = $sb.AppendLine('$BENCHPRESS_BUILD=1')
$functionFiles = $publicFunctions + $privateFunctions
foreach ($file in $functionFiles) {
$null = $sb.AppendLine(". '$($file.FullName)'")
}
}
$null = $sb.AppendLine("Export-ModuleMember $($publicFunctions.BaseName -join ',')")
$sb.ToString() | Set-Content "$PSScriptRoot/bin/BenchPress.Azure.psm1" -Encoding UTF8
if ($Load) {
$powershell = Get-Process -Id $PID | Select-Object -ExpandProperty Path
& $powershell -c "'Load: ' + (Measure-Command { Import-Module '$PSScriptRoot/bin/BenchPress.Azure.psd1' -ErrorAction Stop}).TotalMilliseconds + 'ms'"
if (0 -ne $LASTEXITCODE) {
throw "load failed!"
}
}
if ($Import) {
Import-Module "$PSScriptRoot/bin/BenchPress.Azure.psd1" -Force
}