Skip to content

Commit

Permalink
Condensed the code for this script
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpatton1971 committed Jul 18, 2024
1 parent a36f6ce commit 14ca0b0
Showing 1 changed file with 58 additions and 87 deletions.
145 changes: 58 additions & 87 deletions createpowershellmanifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,148 +6,119 @@ 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"
}

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
}
}

0 comments on commit 14ca0b0

Please sign in to comment.