Public/Initialize-Psm.ps1
Function Initialize-Psm { <# .Synopsis A function that initializes a module in the current directory .DESCRIPTION A function that initializes a module in the current directory .EXAMPLE Initialize-Psm Initialize-Psm -y Initialize-Psm -default Initialize-Psm -default -Whatif Initialize-Psm -Whatif #> [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter( Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias("Default", "y")] [switch]$ShouldUseDefault ) BEGIN { Write-Verbose "Starting Initialize-Psm..." $psd1 = $null $parentFolder = (Get-Location).path.split("\")[-1] $prefix = $parentFolder.Substring(0,3) $defaultPsd1 = @{ RootModule = $parentFolder DefaultCommandPrefix = $prefix ModuleVersion = "0.0.0" Author = $env:USERNAME CompanyName = "" Copyright = "(c) $((Get-Date).Year)..." Description = "" Tags = "" ProjectUri = "" } $moduleFolders = @( "Public", "Internal", "Tests", "Data", "Module-Start", "Module-Stop", "Module-Test" ) }#begin PROCESS{ if ($ShouldUseDefault) { if ($psCmdlet.ShouldProcess("New Module", "Using default params: $defaultPsd1")) { Write-Verbose "Should use default" $psd1 = New-Psd1 @defaultPsd1 } } else { if ($psCmdlet.ShouldProcess("New Module", "Invoking read-host for all of the new module properties")) { Write-Verbose "Should not use default" $psd1 = @{ RootModule = Read-Host "RootModule ($parentFolder)" DefaultCommandPrefix = Read-Host "DefaultCommandPrefix ($prefix)" ModuleVersion = Read-Host "ModuleVersion (0.0.0)" Author = Read-Host "Author ($env:UserName)" CompanyName = Read-Host "CompanyName" Copyright = Read-Host "Copyright ((c) $((Get-Date).Year)...)" Description = Read-Host "Description" Tags = Read-Host "Tags (space separated)" ProjectUri = Read-Host "ProjectUri" } $psd1 = New-Psd1 @psd1 } } Write-Verbose " Psd1 File: $($psd1 | ConvertTo-Json)" Write-Verbose "Creating module folders: $($moduleFolders -join "$([Environment]::NewLine) - ")" New-Item $moduleFolders -ItemType Directory -Force | Out-Null # Write-Verbose "Creating $($psd1.RootModule).psm1" # Copy-Item .\Data\MetaModule\template.psm1 .\$($psd1.RootModule).psm1 Write-Verbose "Creating module psd1 xml file: .\module-psd1.xml" $psd1 | Export-Clixml .\module-psd1.xml }#process END{ Write-Verbose "Finished Initialize-Psm..." }#end } Function New-Psd1 { param( [string]$RootModule, [string]$DefaultCommandPrefix, [string]$ModuleVersion, [string]$Author=$env:USERNAME, [string]$CompanyName, [string]$Copyright, [string]$Description="...", [string]$Tags="", [string]$ProjectUri ) $parentFolder = (Get-Location).path.split("\")[-1] if ([String]::IsNullOrWhiteSpace($RootModule)) { $RootModule = $parentFolder } if ([String]::IsNullOrWhiteSpace($Copyright)) { $Copyright = "(c) $((Get-Date).Year)..." } if ([String]::IsNullOrWhiteSpace($Description)) { $Description = "A great module..." } $moduleName = $RootModule.Replace(".psd1", "").Replace(".psm1", "") @{ ModuleName = $moduleName RootModule = "$moduleName.psm1" ModuleVersion = $ModuleVersion Author = $Author CompanyName = $CompanyName Copyright = $Copyright Description = $Description DefaultCommandPrefix = $DefaultCommandPrefix # private data -start Tags = $Tags.Trim().Split(' ') ProjectUri = $ProjectUri LicenseUri = '' IconUri = '' ReleaseNotes = '' # private data -end PowerShellVersion = '' PowerShellHostName = '' PowerShellHostVersion = '' RequiredModules = @() RequiredAssemblies = @() NestedModules = @() FunctionsToExport = @() CmdletsToExport = @() VariablesToExport = @() AliasesToExport = @() } } |