-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
159 lines (142 loc) · 6.08 KB
/
Copy pathbuild.ps1
File metadata and controls
159 lines (142 loc) · 6.08 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env pwsh
#Requires -Version 7.0
<#
.SYNOPSIS
Builds the Toml module from source into ./output/Toml/.
.DESCRIPTION
Assembles all source files (classes, init, private functions, public functions)
into a single psm1 and creates a manifest, so tests can be run locally without
the PSModule CI pipeline.
#>
[CmdletBinding()]
param(
# Version to stamp into the manifest.
[Alias('ModuleVersion')]
[string] $Version,
# Optional prerelease label to stamp into PrivateData.PSData.Prerelease.
[string] $Prerelease
)
$ErrorActionPreference = 'Stop'
$moduleName = 'Toml'
$srcPath = Join-Path $PSScriptRoot 'src'
$outputPath = Join-Path -Path $PSScriptRoot -ChildPath 'output' -AdditionalChildPath $moduleName
$resolvedVersion = if (-not [string]::IsNullOrWhiteSpace($Version)) {
$Version
} elseif (-not [string]::IsNullOrWhiteSpace($env:PSMODULE_BUILD_PSMODULE_INPUT_Version)) {
$env:PSMODULE_BUILD_PSMODULE_INPUT_Version
} else {
'0.0.1'
}
$resolvedPrerelease = if (-not [string]::IsNullOrWhiteSpace($Prerelease)) {
$Prerelease
} elseif (-not [string]::IsNullOrWhiteSpace($env:PSMODULE_BUILD_PSMODULE_INPUT_Prerelease)) {
$env:PSMODULE_BUILD_PSMODULE_INPUT_Prerelease
} else {
''
}
# ── clean / create output directory ─────────────────────────────────────────
if (Test-Path $outputPath) {
Remove-Item $outputPath -Recurse -Force
}
$null = New-Item -ItemType Directory -Path $outputPath -Force
# ── build psm1 ───────────────────────────────────────────────────────────────
$psm1Path = Join-Path $outputPath "$moduleName.psm1"
$sb = [System.Text.StringBuilder]::new()
# header
$headerPath = Join-Path $srcPath 'header.ps1'
if (Test-Path $headerPath) {
$null = $sb.AppendLine((Get-Content $headerPath -Raw))
} else {
$null = $sb.AppendLine('$ErrorActionPreference = ''Stop''')
$null = $sb.AppendLine()
}
# init
foreach ($f in (Get-ChildItem (Join-Path $srcPath 'init') -Filter '*.ps1' -ErrorAction SilentlyContinue)) {
$null = $sb.AppendLine((Get-Content $f.FullName -Raw))
}
# enums
$enumsPath = Join-Path $srcPath 'enums'
foreach ($f in (Get-ChildItem $enumsPath -Filter '*.ps1' -Recurse -ErrorAction SilentlyContinue)) {
$null = $sb.AppendLine((Get-Content $f.FullName -Raw))
}
# classes private then public
foreach ($visibility in @('private', 'public')) {
$classPath = Join-Path $srcPath "classes/$visibility"
foreach ($f in (Get-ChildItem $classPath -Filter '*.ps1' -Recurse -ErrorAction SilentlyContinue)) {
$null = $sb.AppendLine((Get-Content $f.FullName -Raw))
}
}
# private functions
foreach ($f in (Get-ChildItem (Join-Path $srcPath 'functions/private') -Filter '*.ps1' -Recurse -ErrorAction SilentlyContinue)) {
$null = $sb.AppendLine((Get-Content $f.FullName -Raw))
}
# public functions
$publicFunctions = [System.Collections.Generic.List[string]]::new()
foreach ($f in (Get-ChildItem (Join-Path $srcPath 'functions/public') -Filter '*.ps1' -Recurse -ErrorAction SilentlyContinue)) {
$null = $sb.AppendLine((Get-Content $f.FullName -Raw))
$publicFunctions.Add([System.IO.Path]::GetFileNameWithoutExtension($f.Name))
}
# finally
$finallyPath = Join-Path $srcPath 'finally.ps1'
if (Test-Path $finallyPath) {
$null = $sb.AppendLine((Get-Content $finallyPath -Raw))
}
# class exporter — registers public classes as type accelerators (required by PSModule framework)
$null = $sb.AppendLine(@'
#region Class exporter
$TypeAcceleratorsClass = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
$ExportableEnums = @(
[TomlValueKind]
)
$ExportableEnums | ForEach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
foreach ($Type in $ExportableEnums) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing enum '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
}
$ExportableClasses = @(
[TomlDocument]
)
$ExportableClasses | ForEach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
foreach ($Type in $ExportableClasses) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Class already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing class '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
}
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
$null = $TypeAcceleratorsClass::Remove($Type.FullName)
}
}.GetNewClosure()
#endregion Class exporter
'@)
# Export-ModuleMember
$null = $sb.AppendLine("Export-ModuleMember -Function @($($publicFunctions | ForEach-Object { "'$_'" } | Join-String -Separator ', '))")
[System.IO.File]::WriteAllText($psm1Path, $sb.ToString(), [System.Text.UTF8Encoding]::new($true))
# ── write manifest ────────────────────────────────────────────────────────────
$psd1Path = Join-Path $outputPath "$moduleName.psd1"
$manifest = @{
Path = $psd1Path
ModuleVersion = $resolvedVersion
RootModule = "$moduleName.psm1"
FunctionsToExport = $publicFunctions.ToArray()
PowerShellVersion = '7.6'
Description = 'PowerShell module for reading and writing TOML data.'
Author = 'PSModule'
CompanyName = 'PSModule'
GUID = '6f1b6f8d-1234-4321-abcd-ef0123456789'
}
New-ModuleManifest @manifest
if (-not [string]::IsNullOrWhiteSpace($resolvedPrerelease)) {
Write-Output "Built $moduleName $resolvedVersion-$resolvedPrerelease -> $outputPath"
} else {
Write-Output "Built $moduleName $resolvedVersion -> $outputPath"
}
Write-Output "Public functions: $($publicFunctions -join ', ')"