Public/New-PSModule.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<#
.SYNOPSIS Scaffolds new module structure. .DESCRIPTION Function to initialize and scaffolc the structure for a PowerShell script/function module. .PARAMETER Name The name of the module. .PARAMETER Path Path to where the module should be created. .PARAMETER Author Name of the author. .PARAMETER Description Description of the module. .PARAMETER BuildPipeline Switch to determine if Build Pipeline for Invoke-Build should be setup along with the module. .NOTES This scaffolding script is inspired by Rambling Cookie Monster, http://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/ For yet more advanced features, modify the resulting module manifest manually. Made as a function in a module by Karl Wallenius, Redeploy AB. #> function New-PSModule { [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0)] [string] $Name, [Parameter(Mandatory = $false, Position = 1)] [string] $Path, [Parameter(Mandatory = $true, Position = 2)] [string] $Author, [Parameter(Mandatory = $false, Position = 3)] [string] $Description, [Parameter(Mandatory = $false)] [switch] $BuildPipeline ) begin { # Import templates from variables. $TemplatesPath = Merge-Path $PSScriptRoot, "..", "templates" . (Merge-Path $TemplatesPath, "t_module.ps1") . (Merge-Path $TemplatesPath, "t_help.ps1") . (Merge-Path $TemplatesPath, "t_readme.ps1") # Handle the path to the module. $sep = ([IO.Path]::DirectorySeparatorChar) if ([string]::IsNullOrEmpty($Path)) { $Path = (Get-Location).Path } elseif ($Path -eq ".") { $Path = (Resolve-Path ".").Path } else { $Path = $Path.TrimEnd($sep) } } #> process { # Create directories for the project Write-Verbose "Creating directory structure." $ProjectPath = Merge-Path $Path, $Name $ModulePath = Merge-Path $ProjectPath, $Name $TestsPath = Merge-Path $ProjectPath, "Tests" $Locale = "en-US" [void](New-Item -ItemType Directory -Path (Merge-Path $ModulePath)) [void](New-Item -ItemType Directory -Path (Merge-Path $ModulePath, "Private")) [void](New-Item -ItemType Directory -Path (Merge-Path $ModulePath, "Public")) [void](New-Item -ItemType Directory -Path (Merge-Path $ModulePath, $Locale)) [void](New-Item -ItemType Directory -Path (Merge-Path $TestsPath)) [void](New-Item -ItemType Directory -Path (Merge-Path $TestsPath, "Private")) [void](New-Item -ItemType Directory -Path (Merge-Path $TestsPath, "Public")) # Create files for the project Write-Verbose "Creating module project files." $ModuleFileName = "$Name.psm1" $ModuleFilePath = Merge-Path $ModulePath, $ModuleFileName $ManifestFileName = "$Name.psd1" $ManifestFilePath = Merge-Path $ModulePath, $ManifestFileName $HelpFileName = "about_$Name.help.txt" $HelpFilePath = Merge-Path $ModulePath, $Locale, $HelpFileName $ReadmeName = "README.md" $ReadmePath = Merge-Path $ProjectPath, $ReadmeName [void](New-Item $ModuleFilePath -ItemType File) [void](New-Item (Merge-Path $ModulePath, $Locale, $HelpFileName) -ItemType File) $getModuleParams = @{ Path = $ManifestFilePath RootModule = $ModuleFileName Description = $Description PowerShellVersion = "3.0" Author = $Author ModuleVersion = "0.1.0" } New-ModuleManifest @getModuleParams Write-Verbose "Creating module script file." $ModuleFileContent | Out-File $ModuleFilePath -Encoding utf8 Write-Verbose "Creating help file." $HelpFileContent -replace "<module>", "$Name" | Out-File $HelpFilePath -Encoding utf8 Write-Verbose "Creating $ReadmeName file." $ReadmeContent -replace "<module>", "$Name" | Out-File $ReadmePath -Encoding utf8 if ($BuildPipeline) { New-PSBuildPipeline -Module $ProjectPath } } } |