SSPowerShellBoilerplate/Module.build.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<%
if ($PLASTER_PARAM_SourceType -contains 'PowerShell') {
    '#requires -Module InvokeBuild, PSScriptAnalyzer, Pester, PlatyPS -Version 5.1'
} else {
    '#requires -Module InvokeBuild, Pester, PlatyPS -Version 5.1'
}
if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
@'
 
[CmdletBinding()]
param(
    [ValidateSet('Debug', 'Release')]
    [string]
    $Configuration = 'Debug',
 
    [ValidateSet('net40')]
    [string]
    $Framework = 'net40'
)
'@

} else {
'[CmdletBinding()]
param()'

}
%>

$moduleName = '<%=$PLASTER_PARAM_ModuleName%>'
$manifest   = Test-ModuleManifest -Path          $PSScriptRoot\module\$moduleName.psd1 `
                                  -ErrorAction   Ignore `
                                  -WarningAction Ignore

$script:Settings = @{
    Name          = $moduleName
    Manifest      = $manifest
    Version       = $manifest.Version
<%
    if ($PLASTER_PARAM_SourceType -contains 'PowerShell') {
        ' ShouldAnalyze = $true
'

    }
%>    ShouldTest    = $true
}

$script:Folders  = @{
    PowerShell = "$PSScriptRoot\module"
<%
    if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
@'
    CSharp = "$PSScriptRoot\src"
    Build = '{0}\src\{1}\bin\{2}\{3}' -f $PSScriptRoot, $moduleName, $Configuration, $Framework
 
'@

    }
%>    Release    = '{0}\Release\{1}\{2}' -f $PSScriptRoot, $moduleName, $manifest.Version
    Docs       = "$PSScriptRoot\docs"
    Test       = "$PSScriptRoot\test"
<%
    if ($PLASTER_PARAM_SourceType -contains 'PowerShell') {
    ' PesterCC = "$PSScriptRoot\*.psm1", "$PSScriptRoot\Public\*.ps1", "$PSScriptRoot\Private\*.ps1"
'

    }
%>}

$script:Discovery = @{
    HasDocs       = Test-Path ('{0}\{1}\*.md' -f $Folders.Docs, $PSCulture)
    HasTests      = Test-Path ('{0}\*.Test.ps1' -f $Folders.Test)
}

task Clean {
    if (Test-Path $script:Folders.Release) {
        Remove-Item $script:Folders.Release -Recurse
    }
    $null = New-Item $script:Folders.Release -ItemType Directory
<%
if ($PLASTER_PARAM_SourceType -contains 'CSharp') {' dotnet clean
'
}
%>}

task BuildDocs -If { $script:Discovery.HasDocs } {
    $null = New-ExternalHelp -Path       $PSScriptRoot\docs\$PSCulture `
                             -OutputPath ('{0}\{1}' -f $script:Folders.Release, $PSCulture)
}

<%
if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
'task BuildDll {
    dotnet build -c $Configuration -f $Framework
}
 
'

}
%>task CopyToRelease  {
    Copy-Item -Path ('{0}\*' -f $script:Folders.PowerShell) `
              -Destination $script:Folders.Release `
              -Recurse `
              -Force
<%
    if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
        @'
 
    Copy-Item -Path ('{0}\{1}*' -f $script:Folders.Build, $script:Settings.Name) `
              -Destination $script:Folders.Release
 
'@

    }
%>}

<%
if ($PLASTER_PARAM_SourceType -contains 'PowerShell') {
'task Analyze -If { $script:Settings.ShouldAnalyze } {
    Invoke-ScriptAnalyzer -Path $script:Folders.Release `
                          -Settings $PSScriptRoot\ScriptAnalyzerSettings.psd1 `
                          -Recurse
}
 
'

}
%>task Test -If { $script:Discovery.HasTests -and $script:Settings.ShouldTest } {
    Invoke-Pester <%=if ($PLASTER_PARAM_SourceType -contains 'CSharp') { '-CodeCoverage $script:Folders.PesterCC ' }%>-PesterOption @{ IncludeVSCodeMarker = $true }
}

task DoInstall {
    $installBase = $Home
    if ($profile) { $installBase = $profile | Split-Path }
    $installPath = '{0}\Modules\{1}\{2}' -f $installBase, $script:Settings.Name, $script:Settings.Version

    if (-not (Test-Path $installPath)) {
        $null = New-Item $installPath -ItemType Directory
    }

    Copy-Item -Path ('{0}\*' -f $script:Folders.Release) `
              -Destination $installPath `
              -Force `
              -Recurse
}

task DoPublish {
<%
if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
@'
    if ($Configuration -eq 'Debug') {
        throw 'Configuration must not be Debug to publish!'
    }
 
 
'@

}
%>    if (-not (Test-Path $env:USERPROFILE\.PSGallery\apikey.xml)) {
        throw 'Could not find PSGallery API key!'
    }

    $apiKey = (Import-Clixml $env:USERPROFILE\.PSGallery\apikey.xml).GetNetworkCredential().Password
    Publish-Module -Name $script:Folders.Release -NuGetApiKey $apiKey -Confirm
}

<%
if ($PLASTER_PARAM_SourceType -contains 'CSharp') {
    'task Build -Jobs Clean, BuildDll, CopyToRelease'
} else {
    'task Build -Jobs Clean, CopyToRelease'
}
'' # Adds a new line
if ($PLASTER_PARAM_SourceType -contains 'PowerShell') {
    'task PreRelease -Jobs Build, Analyze, Test'
} else {
    'task PreRelease -Jobs Build, Test'
}
%>

task Install -Jobs PreRelease, DoInstall

task Publish -Jobs PreRelease, DoPublish

task . Build