pf-module_publish.ps1
function Update-ModulePsm1($path) { $folder = Split-Path $path -Parent $files = Get-ScriptList -path $folder -NoExcludeThisScript $fullnameList = $files.fullname | ForEach-Object { ([string]$_).Substring($folder.Length + 1) } # The following function defines text to insert at the begining of each module # this is why is not indented function DefineModuleHeader { $ErrorActionPreference = 'stop' $script:scriptFolder = Split-Path ( Get-PSCallStack )[0].ScriptName -Parent function Get-Metadata_Path { join-path $script:scriptFolder ModuleMetadata.psd1 } function Get-Dependencies () { Get-Module_ExternalModuleDependencies ( Get-Metadata_Path ) } } $content = $fullnameList | ForEach-Object { "`n" + '. $scriptFolder\' + $_ } $DefineModuleHeader = Get-ContentToInclude -functionName DefineModuleHeader $content = $DefineModuleHeader + $content Set-Content -Value $content -Path $path } function Update-ModulePsm1:::Example { Update-ModulePsm1 -path C:\code\PowerFrame\pf-azFuncDeploy\pf-azFuncDeploy.psm1 } function Import-Module_EnsureManifest { [CmdletBinding()] param ( [Parameter()] $Path, $FunctionsToExport, $Revision, $RequiredModules=@() ) $moduleFolder = Split-Path $Path -Parent $moduleName = Split-Path $Path -LeafBase function NewModuleManifest_Default { New-ModuleManifest -Path $Path -ModuleVersion "1.0.0.1" -Description $moduleName | Out-Null } if (-not (Test-Path $Path) ) { NewModuleManifest_Default } $psd = Import-PowerShellDataFile -Path $path -ErrorAction SilentlyContinue if (-not $psd) { # psd1 likely to be invalid so recreate one NewModuleManifest_Default } $description = if ($psd.Description) { $psd.Description } else { "$moduleName" } $ModuleVersion = if ($psd.ModuleVersion) { [Version]$psd.ModuleVersion } else { "$moduleName" } $Revision = if ($Revision) { $Revision } else { [Math]::Max($ModuleVersion.Revision, 1) } $ModuleVersion = [Version]::new([Math]::Max(1,$ModuleVersion.Major), [Math]::Max(0,$ModuleVersion.Minor), [Math]::Max(0,$ModuleVersion.Build), $Revision) $functionsToExport = if ( $functionsToExport ) { $functionsToExport } else { $psd.functionsToExport } $ExternalModuleDependencies = @() + $RequiredModules.ModuleName $ManifestExtra = @{ ExternalModuleDependencies = $ExternalModuleDependencies } $ManifestExtra | ConvertTo-Pson | Set-Content -Path ( join-path $moduleFolder ModuleMetadata.psd1 ) -Force Update-ModuleManifest -Path $Path -FunctionsToExport $functionsToExport ` -Description $description -ModuleVersion $ModuleVersion -RootModule "$moduleName.psm1" function SetManifestRequiredDependencies { Install-Module_BlockScope -Name $ExternalModuleDependencies -script { if ($ExternalModuleDependencies) { Update-ModuleManifest -Path $Path -RequiredModules $ExternalModuleDependencies ` -ExternalModuleDependencies $ExternalModuleDependencies } }.GetNewClosure() } # SetManifestRequiredDependencies # Remove comments in order to avoid commit changes because of header containg dates Remove-PowershellComments -path $Path } function Import-Module_EnsureManifest:::Example { Import-Module_EnsureManifest -Path C:\code\PowerFrame\pf-git\pf-git.psd1 -Revision 20 } function Remove-Modules_Required { Param( [Parameter(ValueFromPipeline=$true)] $path ) process { $path = $path.FullName ?? $path $ScriptName = 'ConvertTo-Expression' Import-Script_AsModule -ScriptName $ScriptName -RequiredVersion 3.3.0 -WhenNotDefined $ScriptName $str = Get-Content -Raw -Path $path $data = $str | ConvertFrom-Pson -ForceOrderedHashTables $data.Remove("RequiredModules") $data.PrivateData.PSData.Remove("ExternalModuleDependencies") $result = $data | ConvertTo-Pson $result = $result.Replace('[ordered]@{','@{') $altPath = $path $pretty | Set-Content -Force -NoNewline -Path $altPath # Testing Import-PowerShellDataFile -Path $altPath -ErrorAction SilentlyContinue | Out-Null } } function Remove-Modules_Required:::Example { Get-ChildItem -file -Recurse -Filter *.psd1 | Remove-Modules_Required } function Initialize-Module_Manifest_EnsureFiles { param( [Parameter(ValueFromPipeline=$true)]$module, $Revision ) begin { $approvedVerbs = Get-Verb | ForEach-Object { $_.Verb } # $approvedVerbs += "Ensure" $approvedVerbsRegEx = [string]::Join( '|', ( $approvedVerbs | ForEach-Object { "$_-" } ) ) $approvedVerbsRegEx += "|Assert" } process { $modulePath = $module.DirectoryName ?? $module $moduleName = Split-Path $modulePath -Leaf $fullPathNoExtension = "$modulePath\$moduleName" function Update-ModuleManifest_FunctionsToExport { $scriptList = Get-ScriptList -NoExcludeThisScript -path $modulePath $functionList = $scriptList | Get-Functions $functionsToExport = $functionList | Where-Object name -NotLike '*:*' | Where-Object name -Match $approvedVerbsRegEx | Sort-Object name $moduleList = $modulePath | Get-ChildItem -filter *.ps1 -Recurse | Get-ModuleDependencies | Sort-Object -Unique $RequiredModules = @() if ($moduleList) { $rr = $moduleList | ForEach-Object { @{ ModuleName = $_.Name.ToString() ModuleVersion = $_.Version.ToString() Guid = $_.Guid.ToString() } } $RequiredModules += $rr } Import-Module_EnsureManifest -Path "$fullPathNoExtension.psd1" ` -FunctionsToExport $functionsToExport.Name -Revision $Revision ` -RequiredModules $RequiredModules } Initialize-File_InNotPresent -path "$fullPathNoExtension.Format.ps1xml" Update-ModulePsm1 -path "$fullPathNoExtension.psm1" Update-ModuleManifest_FunctionsToExport } } function Initialize-Module_Manifest_EnsureFiles:::Example { $folderModules = Get-Modulefolders -path C:\code\PowerFrame $folderModules.FullName | Initialize-Module_Manifest_EnsureFiles -Revision 3 } |