-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatepowershellmodule.ps1
More file actions
96 lines (83 loc) · 3.13 KB
/
createpowershellmodule.ps1
File metadata and controls
96 lines (83 loc) · 3.13 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
param (
[string]$ModuleName,
[string]$Source = "",
[string]$Output = "",
[string]$Imports,
[string]$Debug = 'false'
)
try
{
Write-Host "::group::Starting the Create PowerShell Module task..."
Write-Host "::group::Setting up variables"
$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
# If multiple modules found, exclude any that are in the output directory
if ($Module -is [array])
{
$Module = $Module | Where-Object { $_.FullName -notlike "*$($outputPath)*" } | Select-Object -First 1
}
$ModuleRoot = $Module.Directory.FullName
$Destination = Join-Path $outputPath $ModuleName
$modulePath = Join-Path $Destination "$ModuleName.psm1"
if ($Debug)
{
Write-Host "ModuleName : $ModuleName"
Write-Host "SourcePath : $sourcePath"
Write-Host "OutputPath : $outputPath"
Write-Host "Destination : $Destination"
Write-Host "ModuleRoot : $ModuleRoot"
Write-Host "Imports : $Imports"
}
$stringbuilder = [System.Text.StringBuilder]::new()
$importFolders = $Imports.Split(',')
Write-Host "::endgroup::"
Write-Host "::group::Copying Manifest"
if ($Debug)
{
Write-Host "Testing Output"
}
if (-not (Test-Path -Path $outputPath))
{
New-Item -ItemType Directory -Path $outputPath | Out-Null
}
Write-Host "::group::Processing import folders..."
foreach ($importFolder in $importFolders)
{
Write-Host "Importing from [$ModuleRoot\$importFolder]"
if ($Debug)
{
Write-Host "Testing ImportFolder"
}
if (Test-Path -Path (Join-Path $ModuleRoot $importFolder))
{
$fileList = Get-ChildItem -Path (Join-Path $ModuleRoot $importFolder) -Filter "*.ps1" | Where-Object { $_.Name -notlike "*.Tests.ps1" }
foreach ($file in $fileList)
{
Write-Host " Importing [.$($file.BaseName)]"
if ($Debug)
{
Write-Host "Reading file: $file.FullName"
}
$stringbuilder.AppendLine("# .$($file.BaseName)") | Out-Null
$stringbuilder.AppendLine([System.IO.File]::ReadAllText($file.FullName)) | Out-Null
}
}
else
{
Write-Host "##[warning]Folder $importFolder not found at $ModuleRoot"
}
}
Write-Host "::endgroup::"
Write-Host "::group::Creating module [$modulePath]"
New-Item -ItemType Directory -Path (Split-Path $modulePath -Parent) -Force | Out-Null
$stringbuilder.ToString() | Set-Content -Path $modulePath -Encoding utf8
Write-Host "BuildModule task completed successfully."
Write-Host "::endgroup::"
Write-Host "::endgroup::"
}
catch
{
Write-Host "##[error]An error occurred: $($_.Exception.Message)"
exit 1
}