New-ForgeModule.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<#
Copyright 2016 Dominique Broeglin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>

function New-ForgeModule {
    <#
    .SYNOPSIS
        Generates a new module.

    .DESCRIPTION
        Generates a new skeleton module based on the arguments passed to the function.

    .EXAMPLE
        New-ForgeModule -Name MyModule

    .PARAMETER Name
        The name of the new module.

    .PARAMETER Path
        The path where the new module is created.

    .PARAMETER Description
        Description of the generated module.

    .PARAMETER License
        License to add to the generated module.

        Allowed values are:
        - Apache : Apache License
        - MIT : MIT License

    .PARAMETER Author
        Name to use as module author.

    .PARAMETER Email
        Email to use for the generated module.

    .PARAMETER Git
        Optional, if set add a default .gitignore file to the generated module.

    .PARAMETER Editor
        Optional, editor configuration to setup inside the generated module.

        Allowed values are:
            - VSCode : Visual Studio Code

    .PARAMETER Build
        Optional, if set add build support to the module.

        Allowed values are:
            - PSake : Add PSake build support to the module.
            - TODO: InvokeBuild : Add InvokeBuild support to the module.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact='Low')]
    Param(
        [Parameter(Mandatory = $true)]
        [String]$Name,

        [String]$Path = $Name,

        [String]$Description = "$Name module",

        [ValidateSet('', 'Apache', 'MIT')]
        [String]$License,

        [String]$Author,

        [String]$Email,

        [Switch]$Git,

        [ValidateSet('', 'VSCode')]
        [String]$Editor,

        [ValidateSet('', 'PSake', 'InvokeBuild')]
        [String]$Build,

        [ValidateSet('Src', 'ModuleName')]
        [String]$Layout = 'Src'
    )
    Begin {
        Initialize-ForgeContext -SourceRoot $Script:SourceRoot `
            -DestinationPath $Path
    }
    Process {
        if (!$PSCmdlet.ShouldProcess($Name, "Create module")) {
            return
        }
        $Author = Get-ValueOrGitOrDefault $Author "user.user" "John Doe"
        $Email  = Get-ValueOrGitOrDefault $Email "user.email" "JohnDoe@example.com"
        $CopyrightYear = Get-Date -UFormat %Y
        switch ($Layout) {
            "Src"         { $ModuleDir = "src"}
            "ModuleName"  { $ModuleDir = $Name}
        }

        Set-ForgeBinding @{
            Name          = $Name
            Description   = $Description
            Author        = $Author
            Email         = $Email
            CopyrightYear = $CopyrightYear
        }

        New-ForgeDirectory 
        Copy-ForgeFile -Source "README.md" 

        New-ForgeDirectory -Dest $ModuleDir 
        Copy-ForgeFile -Source "Module.psm1" -Dest "$ModuleDir\$Name.psm1" 

        New-ModuleManifest -Path "$Path\$ModuleDir\$Name.psd1" -RootModule "$Name.psm1" `
            -ModuleVersion "0.1.0" -Description $Description -Author $Author `
            -Copyright "(c) $CopyrightYear $Author. All rights reserved."

        New-ForgeDirectory -Dest "Tests" 
        Copy-ForgeFile -Source "Manifest.Tests.ps1" -Dest "Tests" 

        New-ForgeDirectory -Dest "$ModuleDir/en-US"
        Copy-ForgeFile -Source "docs/en-US/about_Module_help.txt" -Dest "$ModuleDir/en-US/about_$($Name).help.txt"

        if ($License) {
            Copy-ForgeFile -Source "LICENSE.txt.$License" -Dest "LICENSE.txt" 
        }
        if ($Git) {
            Copy-ForgeFile -Source "DotGitIgnore" -Dest ".gitignore"             
        }
        switch ($Build) {
            "PSake" {
                Copy-ForgeFile -Source "Build.PSake\build.ps1" -Dest "build.ps1"
                Copy-ForgeFile -Source "Build.PSake\build.psake.ps1" -Dest "build.psake.ps1"
                Copy-ForgeFile -Source "Build.PSake\build.settings.ps1" -Dest "build.settings.ps1"
                Copy-ForgeFile -Source "ScriptAnalyzerSettings.psd1"
            }
            "InvokeBuild" {
                Copy-ForgeFile -Source "Build.InvokeBuild\build.ps1" -Dest "build.ps1"
                Copy-ForgeFile -Source "Build.InvokeBuild\.build.ps1" -Dest ".build.ps1"
                Copy-ForgeFile -Source "Build.InvokeBuild\build.settings.ps1" -Dest "build.settings.ps1"
                Copy-ForgeFile -Source "ScriptAnalyzerSettings.psd1"
            }
        }
        switch ($Editor) {
            "VSCode" {
                New-ForgeDirectory -Dest .vscode
                Copy-ForgeFile -Source "VSCode.settings.json" -Dest ".vscode\settings.json"
                Copy-ForgeFile -Source "VSCode.tasks.json" -Dest ".vscode\tasks.json"
                Copy-ForgeFile -Source "ScriptAnalyzerSettings.psd1"
            }
        }
    }
}