-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-ModuleTemplate.ps1
122 lines (98 loc) · 3.46 KB
/
New-ModuleTemplate.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
<#
.SYNOPSIS
Creates a new PowerShell module in the monorepo using Invoke-PSMDTemplate.
.DESCRIPTION
This script automates the process of setting up a new module by:
- Creating the module in ./modules/
- Initializing it with Invoke-PSMDTemplate
- Setting up a basic folder structure (public, private, tests)
- Adding a README and test scaffolding
.PARAMETER Name
The name of the new module.
.EXAMPLE
.\tools\New-ModuleTemplate.ps1 -Name MyNewModule
#>
param (
[Parameter(Mandatory = $true, HelpMessage = "The name of the new module.")]
[string]$Name
)
# Ensure we're in the repository root
$RepoRoot = Split-Path -Parent $PSScriptRoot
$ModulesPath = ( Join-Path -Path ( Join-Path -Path $RepoRoot -ChildPath "Modules" ) -ChildPath "Custom" )
$ModulePath = Join-Path -Path $ModulesPath -ChildPath $Name
if ( -Not ( Test-Path -Path $ModulesPath ) ) {
Write-Warning "Modules path '$($ModulesPath)' does not exist. Creating path."
try {
New-Item -Path "$($ModulesPath)" -ItemType "directory"
}
catch {
Write-Error "Error creating path '$($ModulesPath)'. Details: $($_.Exception.Message)"
exit 1
}
}
If ( -Not (Get-Command Invoke-PSMDTemplate -ErrorAction SilentlyContinue) ) {
Write-Warning "This script requires the Invoke-PSMDTemplate module. Attempting to install."
try {
Install-Module PSModuleDevelopment -Scope CurrentUser -Force
}
catch {
Write-Error "Failed to install required module: PSModuleDevelopment. Details: $($_.Exception.Message)"
exit 1
}
}
# Check if the module already exists
if (Test-Path $ModulePath) {
Write-Error "Module '$Name' already exists in the monorepo."
exit 1
}
Write-Output "Creating module: $Name in $ModulesPath..."
try {
Invoke-PSMDTemplate -TemplateName "Module" -Name $Name -OutPath $ModulesPath
}
catch {
Write-Error "Error creating new module from template. Details: $($_.Exception.Message)"
exit 1
}
# Ensure required directories exist
$Directories = @("public", "private", "tests")
foreach ($Dir in $Directories) {
New-Item -Path (Join-Path $ModulePath $Dir) -ItemType Directory -Force | Out-Null
}
# Create a README.md
$ReadmePath = Join-Path $ModulePath "README.md"
if (-not (Test-Path $ReadmePath)) {
@"
# $Name
This module is part of the PowerShell monorepo.
## Installation
\`\`\`powershell
Import-Module (Join-Path `$(PSScriptRoot) $Name.psm1`)
\`\`\`
## Description
TODO: Describe the module.
"@ | Set-Content -Path $ReadmePath -Encoding utf8
}
# Create an empty Pester test file
$TestFile = Join-Path -Path (Join-Path -Path $ModulePath -ChildPath "tests") -ChildPath "$Name.Tests.ps1"
if (-not (Test-Path $TestFile)) {
@"
# Pester tests for $Name module
Describe '$Name' {
It 'Should import the module without errors' {
Import-Module (Join-Path `$PSScriptRoot ".." "$Name.psm1") -Force
`$Module = Get-Module -Name $Name
`$Module -eq `$null | Should Be `$false
}
}
"@ | Set-Content -Path $TestFile -Encoding utf8
}
$AppendModuleFunctionExportString = @"
## Export each function
foreach (`$function in (Get-ChildItem "`$ModuleRoot/public" -Recurse -File -Filter "*.ps1")) {
. Import-ModuleFile -Path `$function.FullName
`$functionName = `$function.BaseName
Export-ModuleMember -Function `$functionName
}
"@
$AppendModuleFunctionExportString | Out-File -FilePath $ModulePath\$Name.psm1 -Append -Encoding utf8
Write-Output "Module '$Name' has been initialized successfully."