Private/Add-SpecPSM1.ps1
function Add-SpecPSM1 { <# .SYNOPSIS Creates a PowerShell module loader (.psm1) file for a specified module. .DESCRIPTION The Add-SpecPSM1 function generates a module loader file (.psm1) for a PowerShell module. It imports the public and private function definition files from the module's "Public" and "Private" subdirectories, respectively. The module loader file ensures that the module functions are available for use. .PARAMETER moduleName Specifies the name of the module. .PARAMETER modulePath Specifies the path to the module's root directory. .EXAMPLE Add-SpecPSM1 -moduleName "MyModule" -modulePath "C:\Path\To\Module" This example generates a module loader file (MyModule.psm1) for a module named "MyModule" located at "C:\Path\To\Module". .NOTES Author : owen.heaume Version : 1.1 #> [cmdletbinding()] param ( [parameter (mandatory = $true)] [string]$moduleName, [parameter (mandatory = $true)] [string]$modulePath ) begin { $fullModulePath = Join-Path $modulePath $moduleName # .psm1 module loader code $moduleLoaderCode = @' #Get public and private function definition files. $Public = @(Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue) $Private = @(Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue) #Dot source the files Foreach($import in @($Public + $Private)) { Try { . $import.fullname } Catch { Write-Error -Message "Failed to import function $($import.fullname): $_" } } Export-ModuleMember -Function $Public.Basename '@ } process { # Create .psm1 module loader try { Write-verbose "Creating $moduleName.psm1 module loader" new-item -path "$fullModulePath\$modulename" -Name "$moduleName.psm1" -ItemType File | Out-Null -ErrorAction stop -ev x Set-Content -Path (Join-Path "$fullModulePath\$modulename" "$moduleName.psm1") -Value $moduleLoaderCode return $true } catch { write-warning "Unable to create $moduleName.psm1 file" write-warning "The error was $x" return $false } } } |