From 14ca0b03122d9e1471cf9c6b31049b4c464f9c2c Mon Sep 17 00:00:00 2001 From: Jeff Patton Date: Thu, 18 Jul 2024 13:22:46 -0500 Subject: [PATCH] Condensed the code for this script --- createpowershellmanifest.ps1 | 145 ++++++++++++++--------------------- 1 file changed, 58 insertions(+), 87 deletions(-) diff --git a/createpowershellmanifest.ps1 b/createpowershellmanifest.ps1 index 6044b11..52d6352 100644 --- a/createpowershellmanifest.ps1 +++ b/createpowershellmanifest.ps1 @@ -6,126 +6,99 @@ param ( [string]$Assemblies = "", [string]$Debug = 'false' ) -try -{ + +try { Write-Host "::group::Starting the Create PowerShell Module task..." Write-Host "::group::Setting up variables" - [bool]$Debug = [System.Convert]::ToBoolean($Debug) - - Write-Host "DebugMode Enabled : $($Debug)" - Write-Host "Root: $($PWD)" - Write-Host "Workspace: $( $env:GITHUB_WORKSPACE)" + $Debug = [System.Convert]::ToBoolean($Debug) - if ([string]::IsNullOrEmpty($Source)) - { - $sourcePath = "$($env:GITHUB_WORKSPACE)" - } - else - { - $sourcePath = "$($env:GITHUB_WORKSPACE)\$($Source)" + if ($Debug) { + Write-Host "DebugMode Enabled : $Debug" + Write-Host "Root: $PWD" + Write-Host "Workspace: $env:GITHUB_WORKSPACE" } - if ([string]::IsNullOrEmpty($Output)) - { - $outputPath = "$($env:GITHUB_WORKSPACE)\output" - } - else - { - $outputPath = "$($env:GITHUB_WORKSPACE)\$($Output)" - } + $sourcePath = if ([string]::IsNullOrEmpty($Source)) { $env:GITHUB_WORKSPACE } else { Join-Path $env:GITHUB_WORKSPACE $Source } + $outputPath = if ([string]::IsNullOrEmpty($Output)) { Join-Path $env:GITHUB_WORKSPACE "output" } else { Join-Path $env:GITHUB_WORKSPACE $Output } - $Module = Get-ChildItem -Path $SourcePath -Filter "$($ModuleName).psd1" -Recurse + $Module = Get-ChildItem -Path $sourcePath -Filter "$ModuleName.psd1" -Recurse $ModuleRoot = $Module.Directory.FullName - $Destination = "$($outputPath)\$($ModuleName)" - $ManifestPath = "$($Destination)\$($ModuleName).psd1" - - if ($Debug) - { - Write-Host "ModuleName : $($ModuleName)" - Write-Host "SourcePath : $($sourcePath)" - Write-Host "OutputPath : $($outputPath)" - Write-Host "Destination : $($Destination)" - Write-Host "ManifestPath : $($ManifestPath)" - Write-Host "ModuleRoot : $($ModuleRoot)" - Write-Host "Imports : $($imports)" - Write-Host "Assemblies : $($Assemblies)" + $Destination = Join-Path $outputPath $ModuleName + $ManifestPath = Join-Path $Destination "$ModuleName.psd1" + + if ($Debug) { + Write-Host "ModuleName : $ModuleName" + Write-Host "SourcePath : $sourcePath" + Write-Host "OutputPath : $outputPath" + Write-Host "Destination : $Destination" + Write-Host "ManifestPath : $ManifestPath" + Write-Host "ModuleRoot : $ModuleRoot" + Write-Host "Imports : $Imports" + Write-Host "Assemblies : $Assemblies" } - $importFolders = $imports.Split(',') - - if ([string]::IsNullOrEmpty($Assemblies)) - { - $AssemblyDirectories = @() - } - else - { - $AssemblyDirectories = $Assemblies.Split(',') - } + $importFolders = $Imports.Split(',') + $AssemblyDirectories = if ([string]::IsNullOrEmpty($Assemblies)) { @() } else { $Assemblies.Split(',') } Write-Host "::endgroup::" Write-Host "::group::Install BuildHelpers module" - if (-not (Get-Module -ListAvailable -Name BuildHelpers)) - { + + if (-not (Get-Module -ListAvailable -Name BuildHelpers)) { Install-Module -Name BuildHelpers -Scope CurrentUser -Force } Import-Module BuildHelpers + Write-Host "::endgroup::" + Write-Host "::group::Testing Output" - Write-Host "::group::Testing Output [$($outputPath)]" - Write-Host "::group::Testing Output [$($outputPath)]" if (Test-Path -Path $outputPath) { - $null = Remove-Item -Path $outputPath -Recurse -Force - } else { - $null = New-Item -ItemType Directory -Path $Destination + Remove-Item -Path $outputPath -Recurse -Force } + New-Item -ItemType Directory -Path $Destination | Out-Null + Write-Host "::endgroup::" + Write-Host "::group::Updating manifest at $ManifestPath" - Write-Host "::group::Updating manifest at [$($ManifestPath)]" - Copy-Item -Path "$($ModuleRoot)\$($ModuleName).psd1" -Destination $ManifestPath + Copy-Item -Path (Join-Path $ModuleRoot "$ModuleName.psd1") -Destination $ManifestPath Write-Host "Copied module manifest to destination" - Write-Host "::endgroup::" + Write-Host "::endgroup::" Write-Host "::group::Collecting Functions" + $Functions = @() - foreach ($importFolder in $importFolders) - { - if (Test-Path "$($ModuleRoot)\$($importFolder)") - { - Write-Host "Processing public functions in folder: $($importFolder)" - $FileList = Get-ChildItem "$($ModuleRoot)\$($importFolder)\*.ps1" -Exclude "*.Tests.ps1" - foreach ($File in $FileList) - { + foreach ($importFolder in $importFolders) { + $importFolderPath = Join-Path $ModuleRoot $importFolder + if (Test-Path -Path $importFolderPath) { + Write-Host "Processing public functions in folder: $importFolder" + $FileList = Get-ChildItem -Path (Join-Path $importFolderPath "*.ps1") -Exclude "*.Tests.ps1" + foreach ($File in $FileList) { $Code = Get-Content -Path $File.FullName -Raw - $Function = [System.Management.Automation.Language.Parser]::ParseInput($code, [ref]$null, [ref]$null).FindAll({ + $Function = [System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$null, [ref]$null).FindAll({ param($ast) $ast -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true) - if ($Debug) - { + if ($Debug) { Write-Host "$($Function.Name)" } $Functions += $Function.Name } - } - else - { - Write-Host "##[warning]Public function folder not found: $($importFolder)" + } else { + Write-Host "##[warning]Public function folder not found: $importFolder" } } - Write-Host "::endgroup::" + Write-Host "::endgroup::" Write-Host "::group::Collecting Assemblies" - if ($AssemblyDirectories.Count -gt 0) - { + + if ($AssemblyDirectories.Count -gt 0) { $RequiredAssemblies = @() - foreach ($Assembly in $AssemblyDirectories) - { - $AssemblyDestinationPath = "$($outputPath)\assemblies\$($Assembly)" - Write-Host "Processing assemblies in directory: $($Assembly)" - $AssemblyFile = Get-Item -Path (((Get-ChildItem -Path "$($AssemblyDestinationPath)").GetFiles("*$($Assembly)*").FullName) | Where-Object { $_.EndsWith('dll') }) - $AssemblyPath = $AssemblyFile.FullName.Replace("$($outputPath)\", '') + foreach ($Assembly in $AssemblyDirectories) { + $AssemblyDestinationPath = Join-Path $outputPath "assemblies\$Assembly" + Write-Host "Processing assemblies in directory: $Assembly" + $AssemblyFile = Get-Item -Path (Get-ChildItem -Path $AssemblyDestinationPath -Filter "*$Assembly*.dll").FullName + $AssemblyPath = $AssemblyFile.FullName.Replace("$outputPath\", '') $RequiredAssemblies += $AssemblyPath Write-Host "Found DLL: $AssemblyPath" } @@ -133,21 +106,19 @@ try Write-Host "Updating manifest metadata" Update-Metadata -Path $ManifestPath -PropertyName RequiredAssemblies -Value $RequiredAssemblies Write-Host "Updated RequiredAssemblies in manifest" - } - else - { + } else { Write-Host "##[warning]No assembly directories specified, skipping RequiredAssemblies update" } - Write-Host "::endgroup::" + Write-Host "::endgroup::" Write-Host "::group::Updating Manifest" + Update-Metadata -Path $ManifestPath -PropertyName FunctionsToExport -Value $Functions Write-Host "Updated FunctionsToExport in manifest" Write-Host "::endgroup::" Write-Host "::endgroup::" } -catch -{ +catch { Write-Host "##[error]An error occurred: $($_.Exception.Message)" exit 1 -} \ No newline at end of file +}