diff --git a/README.md b/README.md index d0813e2..8f7f911 100644 --- a/README.md +++ b/README.md @@ -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:` 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. diff --git a/scripts/helpers/Build/Add-ContentFromItem.ps1 b/scripts/helpers/Build/Add-ContentFromItem.ps1 index de39242..f8f2fa7 100644 --- a/scripts/helpers/Build/Add-ContentFromItem.ps1 +++ b/scripts/helpers/Build/Add-ContentFromItem.ps1 @@ -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, '' @@ -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 "@ } diff --git a/scripts/helpers/Build/Build-PSModuleRootModule.ps1 b/scripts/helpers/Build/Build-PSModuleRootModule.ps1 index a4fdb22..623fb37 100644 --- a/scripts/helpers/Build/Build-PSModuleRootModule.ps1 +++ b/scripts/helpers/Build/Build-PSModuleRootModule.ps1 @@ -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' @@ -116,6 +117,7 @@ $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { $TypeAcceleratorsClass::Remove($Type.FullName) } }.GetNewClosure() +#endregion Class exporter '@ } } @@ -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]" @@ -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 @@ -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, '' @@ -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 } @@ -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 diff --git a/scripts/helpers/Build/Get-PSModuleClassesToExport.ps1 b/scripts/helpers/Build/Get-PSModuleClassesToExport.ps1 index 3ebe3cc..b01219a 100644 --- a/scripts/helpers/Build/Get-PSModuleClassesToExport.ps1 +++ b/scripts/helpers/Build/Get-PSModuleClassesToExport.ps1 @@ -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