Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ following order:
1. Adds a module header from `header.ps1` if it exists and removes the file from the module folder.
1. Adds a data loader that loads files from the `data` folder as variables in the module scope, if the folder exists. The variables are available
using the `$script:<filename>` syntax.
1. Adds content from subfolders into the root module file and removes them from the module folder in the following order:
- `init`
- `classes/private`
- `classes/public`
- `functions/private`
- `functions/public`
- `variables/private`
- `variables/public`
- `*.ps1` on module root
1. Adds content from the following folders into the root module file. The files on the root of a folder is added before recursivelfy going to the next
folder in alphabetical order. Once the file is processed, it is removed from the module folder.
1. `init`
1. `classes/private`
1. `classes/public`
1. `functions/private`
1. `functions/public`
1. `variables/private`
1. `variables/public`
1. `*.ps1` on module root
1. Adds a `class` and `enum` exporter that exports the ones from `classes/public` folder to the caller session, using [TypeAccelerators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.4#exporting-classes-with-type-accelerators).
1. Adds the `Export-ModuleMember` function to the end of the file, to make sure that only the functions, cmdlets, variables and aliases that are
defined in the `public` folders are exported.
Expand Down
23 changes: 9 additions & 14 deletions scripts/helpers/Build/Add-ContentFromItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@
$relativeFolderPath = $relativeFolderPath -Join ' - '

Add-Content -Path $RootModuleFilePath -Force -Value @"
#region - From $relativeFolderPath
#region $relativeFolderPath
Write-Debug "[`$scriptName] - $relativeFolderPath - Processing folder"

"@

$subFolders = $Path | Get-ChildItem -Directory -Force | Sort-Object -Property Name
foreach ($subFolder in $subFolders) {
Add-ContentFromItem -Path $subFolder.FullName -RootModuleFilePath $RootModuleFilePath -RootPath $RootPath
}

$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
foreach ($file in $files) {
$relativeFilePath = $file.FullName -Replace $RootPath, ''
Expand All @@ -51,21 +45,22 @@ Write-Debug "[`$scriptName] - $relativeFolderPath - Processing folder"
$relativeFilePath = $relativeFilePath -Join ' - '

Add-Content -Path $RootModuleFilePath -Force -Value @"
#region - From $relativeFilePath
#region $relativeFilePath
Write-Debug "[`$scriptName] - $relativeFilePath - Importing"

"@
Get-Content -Path $file.FullName | Add-Content -Path $RootModuleFilePath -Force
Add-Content -Path $RootModuleFilePath -Value @"

Write-Debug "[`$scriptName] - $relativeFilePath - Done"
#endregion - From $relativeFilePath
#endregion $relativeFilePath
"@
}
Add-Content -Path $RootModuleFilePath -Force -Value @"

$subFolders = $Path | Get-ChildItem -Directory -Force | Sort-Object -Property Name
foreach ($subFolder in $subFolders) {
Add-ContentFromItem -Path $subFolder.FullName -RootModuleFilePath $RootModuleFilePath -RootPath $RootPath
}
Add-Content -Path $RootModuleFilePath -Force -Value @"
Write-Debug "[`$scriptName] - $relativeFolderPath - Done"
#endregion - From $relativeFolderPath

#endregion $relativeFolderPath
"@
}
31 changes: 13 additions & 18 deletions scripts/helpers/Build/Build-PSModuleRootModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function Build-PSModuleRootModule {
$classes = Get-PSModuleClassesToExport -SourceFolderPath $classesFolder
if ($classes.count -gt 0) {
$classExports += @'
#region Class exporter
# Get the internal TypeAccelerators class to use its static methods.
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
'System.Management.Automation.TypeAccelerators'
Expand Down Expand Up @@ -116,6 +117,7 @@ $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
$TypeAcceleratorsClass::Remove($Type.FullName)
}
}.GetNewClosure()
#endregion Class exporter
'@
}
}
Expand All @@ -141,28 +143,23 @@ $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
param()
'@
}
#endregion - Module header

# Add a variable $script:PSModuleInfo to the root module, which contains the module manifest information.
#region - Module post-header
Add-Content -Path $rootModuleFile -Force -Value @'
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
$script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1"
$script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ }
$scriptName = $script:PSModuleInfo.Name
Write-Debug "[$scriptName] - Importing module"
'@
#endregion - Module header

#region - Module post-header
Add-Content -Path $rootModuleFile -Force -Value @"
`$scriptName = '$ModuleName'
Write-Debug "[`$scriptName] - Importing module"

"@
#endregion - Module post-header

#region - Data loader
if (Test-Path -Path (Join-Path -Path $ModuleOutputFolder -ChildPath 'data')) {

Add-Content -Path $rootModuleFile.FullName -Force -Value @'
#region - Data import
#region Data importer
Write-Debug "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Write-Debug "[$scriptName] - [data] - [$dataFolder]"
Expand All @@ -171,10 +168,8 @@ Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' -ErrorAction
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
Write-Debug "[$scriptName] - [data] - [$($_.BaseName)] - Done"
}

Write-Debug "[$scriptName] - [data] - Done"
#endregion - Data import

#endregion Data importer
'@
}
#endregion - Data loader
Expand All @@ -201,7 +196,7 @@ Write-Debug "[$scriptName] - [data] - Done"
#endregion - Add content from subfolders

#region - Add content from *.ps1 files on module root
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
foreach ($file in $files) {
$relativePath = $file.FullName -Replace $ModuleOutputFolder, ''
$relativePath = $relativePath -Replace $file.Extension, ''
Expand All @@ -210,16 +205,14 @@ Write-Debug "[$scriptName] - [data] - Done"
$relativePath = $relativePath -Join ' - '

Add-Content -Path $rootModuleFile -Force -Value @"
#region - From $relativePath
#region $relativePath
Write-Debug "[`$scriptName] - $relativePath - Importing"

"@
Get-Content -Path $file.FullName | Add-Content -Path $rootModuleFile -Force

Add-Content -Path $rootModuleFile -Force -Value @"
Write-Debug "[`$scriptName] - $relativePath - Done"
#endregion - From $relativePath

#endregion $relativePath
"@
$file | Remove-Item -Force
}
Expand All @@ -236,8 +229,10 @@ Write-Debug "[`$scriptName] - $relativePath - Done"
Path = $rootModuleFile
Force = $true
Value = @"
#region Member exporter
`$exports = $exportsString
Export-ModuleMember @exports
#endregion Member exporter
"@
}
Add-Content @params
Expand Down
2 changes: 1 addition & 1 deletion scripts/helpers/Build/Get-PSModuleClassesToExport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
[string] $SourceFolderPath
)

$files = Get-ChildItem -Path $SourceFolderPath -Recurse -Include '*.ps1'
$files = Get-ChildItem -Path $SourceFolderPath -Recurse -Include '*.ps1' | Sort-Object -Property FullName

foreach ($file in $files) {
$content = Get-Content -Path $file.FullName -Raw
Expand Down