-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsrc.Build.ps1
More file actions
150 lines (134 loc) · 6.1 KB
/
Copy pathsrc.Build.ps1
File metadata and controls
150 lines (134 loc) · 6.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
FormatTaskName "-------- {0} --------"
. "$PSScriptRoot\Settings.build.ps1"
<# Throw error about not empty git status if .git exists.#>
task -name "GitStatus" -precondition {Test-Path ".git"} -action {
$status = exec { git status -s }
if ($status) {
Write-Error -Message "Git status: $($status -join ', ')"
}
}#task GitStatus
$UnloadModule_Properties = @{
description = "Removes Module from RAM"
requiredVariables = "ModuleName"
precondition = { (Get-module $ModuleName).count -gt 0 }
}
Task -name "UnLoad Module" @UnloadModule_Properties -action {
Remove-Module $ModuleName
} #Unload module
task -name "Clean temp" -precondition {Test-Path "$env:temp\$ModuleName"} -action {
Get-ChildItem -Path "$env:temp\$ModuleName" | Remove-Item -Recurse
}
task -name "Clean" -description "Cleans up temp and module directories" -depends "Unload Module","Clean temp" -precondition { Test-Path $InstallPath } -action {
Get-ChildItem $InstallPath | Remove-Item -Recurse
}#Clean task
task -name "Analyze" -Action {
$saResults = Invoke-ScriptAnalyzer -Path $SrcRootDir -Severity @('Error', 'Warning') -Settings $ScriptAnalyzerSettingsPath -Recurse #-Verbose:$false
$Errors = $saResults | Where-Object -FilterScript {$_.Severity -like 'Error'}
$Warnings = $saResults | Where-Object -FilterScript {$_.Severity -like 'Warning'}
if ($Errors) {
$Errors | Format-Table
Write-Error -Message 'One or more Script Analyzer errors where found. Build cannot continue!'
}
if ( $Warnings){
$Warnings | Format-Table
Write-Warning -Message "One or more Script Analyzer warnings were found!"
}
}
task -name "Test" -depends "Unload Module" -action {
$testResults = Invoke-Pester -Path $UnitTestDir -PassThru -Show None
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester tests failed. Build cannot continue!'
}
}
task -name "Manifest" -description "Generates module manifest" -requiredVariables Version,InstallPath -action {
<#Collect functions available and add them to the "nested modules" #>
$NestedModules = @()
$FunctionsToExport = @()
Get-ChildItem -Path $FunctionDir -Include *.psm1 -Recurse | Select-Object Name, BaseName | ForEach-Object -Process {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$NestedModules += (".\Functions\" + $_.Name)
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$FunctionsToExport += $_.BaseName
}
$Manifest_Params = @{
Path = "$SrcRootDir\$ModuleName.psd1"
GUID = $GUID
Author = $Author
CompanyName = $CompanyName
CopyRight = $CopyRight
ModuleVersion = $Version
PowerShellVersion = $PowerShellVersion
NestedModules = $NestedModules
Description = $Description
FunctionsToExport = $FunctionsToExport
ReleaseNotes = $ReleaseNotes
}
if ( Test-path "$SrcRootDir\$ModuleName.psm1") {
$Manifest_Params.Add( "RootModule", "$ModuleName.psm1")
}
if ( Test-Path "$SrcRootDir\$ModuleName.psd1") {
$Manifest = Get-Item "$SrcRootDir\$ModuleName.psd1"
$Settings = Get-Item "$PSScriptRoot\Settings.build.ps1"
if ( $Manifest.LastWriteTime -lt $Settings.LastWriteTime) {
Update-ModuleManifest @Manifest_Params
}#if
}
else {
New-ModuleManifest @Manifest_Params
}#if manifest doesn't exist
}#Task create manifest
Task -name "Module" -description "Populates module directory" -requiredVariables InstallPath, Version -action {
Write-Verbose -Message "$InstallPath"
Get-ChildItem -Path $SrcRootDir | Copy-Item -Recurse -Destination $InstallPath
}
Task -name "Help" -description "Updates external help files and nested readme.md" -depends "Module" -action {
New-MarkdownHelp -Module $ModuleName -AlphabeticParamsOrder -OutputFolder "$env:temp\$ModuleName\Docs" -Force | Out-Null
New-ExternalHelp -Path "$env:temp\$ModuleName\Docs" -OutputPath "$InstallPath\en-US" -Force | Out-Null
#Get-ChildItem -Path "$env:temp\$ModuleName\Docs" -File | ForEach-Object { Move-Item -Path $_ -Destination ("Functions\" + $_.BaseName + "\ReadMe.MD") -Force }
Get-ChildItem -Path "$InstallPath\Functions" -Include *.psm1 -Recurse | ForEach-Object -Process {
<#now remove comment based help from functions#>
#Write-Host "Processing $_"
$Content = Get-Content -LiteralPath $_.FullName -Raw
# the .IndexOf() method is case sensitive
# that is why i used -match = to get the actual string that is used
$Index = $Content.IndexOf("<#")
$End = $Content.IndexOf("#>") + 2
if ( ($Index -gt 0) -and ($End -gt 0) ) {
$OutStuff = $Content.Substring(0, $Index)
$Length = $Content.Length - $End
$OutStuff += $Content.Substring($End, $Length)
Out-File -FilePath $_.FullName -InputObject $OutStuff
}
}#foreach function in InstallPath
}
$CodeSigning_Params = @{
name = "Code Signing"
description = "Signs all module files"
requiredVariables = @("ModuleName", "Version")
precondition = { $env:USERNAME -like "*.admin" }
}
task @CodeSigning_Params -action {
$Cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert
New-FileCatalog -CatalogVersion 2 -CatalogFilePath "$InstallPath\$ModuleName.cat" -Path $InstallPath
Get-childitem -Exclude *.xml -File -Path $InstallPath -Recurse | Set-AuthenticodeSignature -Certificate $Cert
}
$PublishProd_Params = @{
name = "CorePublish"
precondition = {Get-PSRepository -Name $RepositoryName}
description = "Publishes module to ProGet distribution points"
requiredVariables = "Prod_ApiKey"
}
task @PublishProd_Params -action {
Publish-Module -Name $ModuleName -Repository $RepositoryName -NuGetApiKey $Prod_ApiKey -RequiredVersion $Version
}
task -name "Success" -depends "GitStatus" -action {
Get-Module -ListAvailable $ModuleName
} #-precondition "GitStatus"
Task ? -description 'Lists the available tasks' {
"Available tasks:"
$psake.context.Peek().Tasks.Keys | Sort-Object
}
task -name "Default" -depends Build
task -name "Build" -depends "Analyze", "Test", "Clean", "Module", "Manifest", "Help","Clean temp"
#task -name "Publish" -depends "GitStatus","Analyze","Test","Clean","Module","Manifest","Help","Publish","Success"