-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatepowershellmanifest.ps1
More file actions
131 lines (106 loc) · 4.83 KB
/
createpowershellmanifest.ps1
File metadata and controls
131 lines (106 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
param (
[string]$ModuleName,
[string]$Source = "",
[string]$Output = "",
[string]$Imports,
[string]$Assemblies = "",
[string]$Debug = 'false'
)
try {
Write-Host "::group::Starting the Create PowerShell Module task..."
Write-Host "::group::Setting up variables"
if ($Debug) {
Write-Host "DebugMode Enabled : $Debug"
Write-Host "Root: $PWD"
Write-Host "Workspace: $env:GITHUB_WORKSPACE"
}
$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 }
$Manifest = Get-ChildItem -Path $sourcePath -Filter "$ModuleName.psd1" -Recurse
$ManifestRoot = $Manifest.Directory.FullName
$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 "ManifestRoot : $ManifestRoot"
Write-Host "Imports : $Imports"
Write-Host "Assemblies : $Assemblies"
}
$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)) {
Install-Module -Name BuildHelpers -Scope CurrentUser -Force
}
Import-Module BuildHelpers
Write-Host "::endgroup::"
Write-Host "::group::Testing Output"
if (Test-Path -Path $outputPath) {
Remove-Item -Path $outputPath -Recurse -Force
}
New-Item -ItemType Directory -Path $Destination | Out-Null
Write-Host "::endgroup::"
Write-Host "::group::Updating manifest at $ManifestPath"
Copy-Item -Path (Join-Path $ManifestRoot "$ModuleName.psd1") -Destination $ManifestPath
Write-Host "Copied module manifest to destination"
Write-Host "::endgroup::"
Write-Host "::group::Collecting Functions"
$Functions = [System.Collections.Generic.List[string]]::new()
foreach ($importFolder in $importFolders) {
$importFolderPath = Join-Path $ManifestRoot $importFolder
if (Test-Path -Path $importFolderPath) {
Write-Host "Processing public functions in folder: $importFolder"
$FileList = Get-ChildItem -Path $importFolderPath -Filter "*.ps1" |
Where-Object { $_.Name -notlike "*.Tests.ps1" }
foreach ($File in $FileList) {
$Code = Get-Content -Path $File.FullName -Raw
$FunctionAsts = [System.Management.Automation.Language.Parser]::ParseInput(
$Code, [ref]$null, [ref]$null
).FindAll({
param($ast)
$ast -is [System.Management.Automation.Language.FunctionDefinitionAst]
}, $true)
foreach ($fn in $FunctionAsts) {
if ($Debug) { Write-Host $fn.Name }
$Functions.Add($fn.Name.Trim())
}
}
}
else {
Write-Host "##[warning]Public function folder not found: $importFolder"
}
}
Write-Host "::endgroup::"
Write-Host "::group::Collecting Assemblies"
if ($AssemblyDirectories.Count -gt 0) {
$RequiredAssemblies = @()
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 {
Write-Host "##[warning]No assembly directories specified, skipping RequiredAssemblies update"
}
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 {
Write-Host "##[error]An error occurred: $($_.Exception.Message)"
exit 1
}