-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
85 lines (67 loc) · 3.78 KB
/
build.ps1
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
[cmdletbinding()]
param (
$SourceFolder = $PSScriptRoot,
$Tag
)
if (-not (Get-Module PSDepend -ListAvailable)) {
Install-Module PSDepend -Repository (Get-PSRepository)[0].Name -Scope CurrentUser -Force -Confirm:$false
}
Push-Location $PSScriptRoot -StackName BuildScript
Invoke-PSDepend -Path $SourceFolder -Confirm:$false
Pop-Location -StackName BuildScript
Write-Verbose -Message "Working in $SourceFolder" -verbose
$Module = Get-ChildItem -Path $SourceFolder -Filter *.psd1 -Recurse |
Where-Object {$_.FullName -notmatch 'dependencies|Output'} |
Select-String -Pattern 'RootModule' |
Select-Object -First 1 -ExpandProperty Path
$Module = Get-Item -Path $Module
$OutputFolder = Join-Path -Path $($Module.Directory.FullName) -ChildPath "..\Output\"
$null = New-Item -Path $OutputFolder -ItemType Directory -Force -Confirm:$false
$DestinationModule = Join-Path -Path $($Module.Directory.FullName) -ChildPath "..\Output\$($Module.BaseName).psm1"
$OutputManifest = Join-Path -Path $($Module.Directory.FullName) -ChildPath "..\Output\$($Module.BaseName).psd1"
Copy-Item -Path $Module.FullName -Destination $OutputManifest -Force
Write-Verbose -Message "Attempting to work with $DestinationModule" -verbose
if (Test-Path -Path $DestinationModule ) {
Remove-Item -Path $DestinationModule -Confirm:$False -force
}
$PublicFunctions = Get-ChildItem -Path $SourceFolder\src -Include 'Public', 'External' -Recurse -Directory | Get-ChildItem -Include *.ps1 -File
$PrivateFunctions = Get-ChildItem -Path $SourceFolder\src -Include 'Private', 'Internal' -Recurse -Directory | Get-ChildItem -Include *.ps1 -File
$Classes = Get-ChildItem -Path $SourceFolder\src -Include 'Classes' -Recurse -Directory | Get-ChildItem -Include *.ps1 -File
Foreach ($Class in $Classes) {
Get-Content -Path $Class.FullName | Add-Content -Path $DestinationModule
}
if ($PublicFunctions -or $PrivateFunctions) {
Write-Verbose -message "Found Private or Public functions. Will compile these into the psm1 and only export public functions."
Foreach ($PrivateFunction in $PrivateFunctions) {
Get-Content -Path $PrivateFunction.FullName | Add-Content -Path $DestinationModule
}
Write-Verbose -Message "Found $($PrivateFunctions.Count) Private functions and added them to the psm1."
}
else {
Write-Verbose -Message "Didnt' find any Private or Public functions, will assume all functions should be made public."
$PublicFunctions = Get-ChildItem -Path $SourceFolder -Include *.ps1 -Recurse -File
}
Foreach ($PublicFunction in $PublicFunctions) {
Get-Content -Path $PublicFunction.FullName | Add-Content -Path $DestinationModule
}
Write-Verbose -Message "Found $($PublicFunctions.Count) Public functions and added them to the psm1."
$PublicFunctionNames = $PublicFunctions |
Select-String -Pattern 'Function (\w+\w+) {' -AllMatches |
Foreach-Object {
$_.Matches.Groups[1].Value
}
Write-Verbose -Message "Making $($PublicFunctionNames.Count) functions available via Export-ModuleMember"
"Export-ModuleMember -Function $($PublicFunctionNames -join ',')" | Add-Content -Path $DestinationModule
$Null = Get-Command -Module Configuration
Update-Metadata -Path $OutputManifest -PropertyName FunctionsToExport -Value $PublicFunctionNames
$Aliases = [Scriptblock]::Create((Get-Content -Path $DestinationModule -raw)).Ast.FindAll( {
param ($ast)
$ast -is [System.Management.Automation.Language.StringConstantExpressionAst] -and
$ast.parent -is [System.Management.Automation.Language.AttributeAst] -and
$ast.parent.typename.Name -eq 'Alias' -and
$ast.parent.parent -is [System.Management.Automation.Language.ParamBlockAst]
}, $true)
if ($Aliases) {
$Aliases.SafeGetValue()
Update-Metadata -Path $OutputManifest -PropertyName AliasesToExport -Value $Aliases
}