-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNewMTModule.ps1
More file actions
132 lines (120 loc) · 5.13 KB
/
NewMTModule.ps1
File metadata and controls
132 lines (120 loc) · 5.13 KB
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
129
130
131
132
<#
.SYNOPSIS
Create module scaffolding along with project.json file to easily build and manage modules
.DESCRIPTION
This command creates folder structure and project.json file easily. Use this to quikcly setup a ModuleTools compatible module.
.PARAMETER Path
Path where module will be created. Provide root folder path, module folder will be created as subdirectory. Path should be valid.
.EXAMPLE
New-MTModule -Path c:\work
# Creates module inside c:\work folder
.NOTES
The structure of the ModuleTools module is meticulously designed according to PowerShell best practices for module development. While some design decisions may seem unconventional, they are made to ensure that ModuleTools and the process of building modules remain straightforward and easy to manage.
#>
function New-MTModule {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[string]$Path = (Get-Location).Path
)
$ErrorActionPreference = 'Stop'
Push-Location
if (-not(Test-Path $Path)) { Write-Error 'Not a valid path' }
$Questions = [ordered]@{
ProjectName = @{
Caption = 'Module Name'
Message = 'Enter Module name of your choice, should be single word with no special characters'
Prompt = 'Name'
Default = 'MANDATORY'
}
Description = @{
Caption = 'Module Description'
Message = 'What does your module do? Describe in simple words'
Prompt = 'Description'
Default = 'ModuleTools Module'
}
Version = @{
Caption = 'Semantic Version'
Message = 'Starting Version of the module (Default: 0.0.1)'
Prompt = 'Version'
Default = '0.0.1'
}
Author = @{
Caption = 'Module Author'
Message = 'Enter Author or company name'
Prompt = 'Name'
Default = 'PS'
}
PowerShellHostVersion = @{
Caption = 'Supported PowerShell Version'
Message = 'What is minimum supported version of PowerShell for this module (Default: 7.4)'
Prompt = 'Version'
Default = '7.4'
}
EnableGit = @{
Caption = 'Git Version Control'
Message = 'Do you want to enable version controlling using Git'
Prompt = 'EnableGit'
Default = 'No'
Choice = @{
Yes = 'Enable Git'
No = 'Skip Git initialization'
}
}
EnablePester = @{
Caption = 'Pester Testing'
Message = 'Do you want to enable basic Pester Testing'
Prompt = 'EnablePester'
Default = 'No'
Choice = @{
Yes = 'Enable pester to perform testing'
No = 'Skip pester testing'
}
}
}
$Answer = @{}
$Questions.Keys | ForEach-Object {
$Answer.$_ = Read-AwesomeHost -Ask $Questions.$_
}
# TODO check other components
if ($Answer.ProjectName -notmatch '^[A-Za-z][A-Za-z0-9_.]*$') {
Write-Error 'Module Name invalid. Module should be one word and contain only Letters,Numbers and '
}
$DirProject = Join-Path -Path $Path -ChildPath $Answer.ProjectName
$DirSrc = Join-Path -Path $DirProject -ChildPath 'src'
$DirPrivate = Join-Path -Path $DirSrc -ChildPath 'private'
$DirPublic = Join-Path -Path $DirSrc -ChildPath 'public'
$DirResources = Join-Path -Path $DirSrc -ChildPath 'resources'
$DirClasses = Join-Path -Path $DirSrc -ChildPath 'classes'
$DirTests = Join-Path -Path $DirProject -ChildPath 'tests'
$ProjectJSONFile = Join-Path $DirProject -ChildPath 'project.json'
if (Test-Path $DirProject) {
Write-Error 'Project already exists, aborting' | Out-Null
}
# Setup Module
Write-Message "`nStarted Module Scaffolding" -color Green
Write-Message 'Setting up Directories'
($DirProject, $DirSrc, $DirPrivate, $DirPublic, $DirResources, $DirClasses) | ForEach-Object {
'Creating Directory: {0}' -f $_ | Write-Verbose
New-Item -ItemType Directory -Path $_ | Out-Null
}
if ( $Answer.EnablePester -eq 'Yes') {
Write-Message 'Include Pester Configs'
New-Item -ItemType Directory -Path $DirTests | Out-Null
}
if ( $Answer.EnableGit -eq 'Yes') {
Write-Message 'Initialize Git Repo'
New-InitiateGitRepo -DirectoryPath $DirProject
}
## Create ProjectJSON
$JsonData = Get-Content "$PSScriptRoot\resources\ProjectTemplate.json" -Raw | ConvertFrom-Json -AsHashtable
$JsonData.ProjectName = $Answer.ProjectName
$JsonData.Description = $Answer.Description
$JsonData.Version = $Answer.version
$JsonData.Manifest.Author = $Answer.Author
$JsonData.Manifest.PowerShellHostVersion = $Answer.PowerShellHostVersion
$JsonData.Manifest.GUID = (New-Guid).GUID
if ($Answer.EnablePester -eq 'No') { $JsonData.Remove('Pester') }
Write-Verbose $JsonData
$JsonData | ConvertTo-Json | Out-File $ProjectJSONFile
'Module {0} scaffolding complete' -f $Answer.ProjectName | Write-Message -color Green
}