forked from MethodsAndPractices/vsteam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild-Module.ps1
154 lines (120 loc) · 4.76 KB
/
Build-Module.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
[CmdletBinding(DefaultParameterSetName="All")]
param(
#output path of the build module
[Parameter(ParameterSetName="All")]
[Parameter(ParameterSetName="UnitTest")]
[string]$outputDir = './dist',
# Building help is skipped by default to speed your inner loop.
# Use this flag to include building the help
[Parameter(ParameterSetName="All")]
[Parameter(ParameterSetName="UnitTest")]
[switch]$buildHelp,
# By default the build will not install dependencies
[Parameter(ParameterSetName="All")]
[Parameter(ParameterSetName="UnitTest")]
[switch]$installDep,
# built module will be imported into session
[Parameter(ParameterSetName="All")]
[Parameter(ParameterSetName="UnitTest")]
[switch]$ipmo,
# run the scripts with the PS script analyzer
[Parameter(ParameterSetName="All")]
[Parameter(ParameterSetName="UnitTest")]
[switch]$analyzeScript,
# runs the unit tests
[Parameter(ParameterSetName="UnitTest", Mandatory=$true)]
[Parameter(ParameterSetName="All")]
[switch]$runTests,
# can be used to filter the unit test parts that should be run
# see also: https://github.com/pester/Pester/wiki/Invoke%E2%80%90Pester#testname-alias-name
[Parameter(ParameterSetName="UnitTest")]
[string]$testName,
# outputs the code coverage
[Parameter(ParameterSetName="UnitTest")]
[switch]$codeCoverage
)
. ./Merge-File.ps1
if ($installDep.IsPresent -or $analyzeScript.IsPresent) {
# Load the psd1 file so you can read the required modules and install them
$manifest = Import-PowerShellDataFile .\Source\VSTeam.psd1
# Install each module
if ($manifest.RequiredModules) {
$manifest.RequiredModules | ForEach-Object { if (-not (get-module $_ -ListAvailable)) { Write-Host "Installing $_"; Install-Module -Name $_ -Repository PSGallery -F -Scope CurrentUser } }
}
}
if ([System.IO.Path]::IsPathRooted($outputDir)) {
$output = $outputDir
}
else {
$output = Join-Path (Get-Location) $outputDir
}
$output = [System.IO.Path]::GetFullPath($output)
Merge-File -inputFile ./Source/_functions.json -outputDir $output
Merge-File -inputFile ./Source/types/_types.json -outputDir $output
Merge-File -inputFile ./Source/Classes/_classes.json -outputDir $output
Merge-File -inputFile ./Source/formats/_formats.json -outputDir $output
# Build the help
if ($buildHelp.IsPresent) {
Write-Output 'Creating help files'
Push-Location
Set-Location ./.docs
Try {
./gen-help.ps1
} Finally {
Pop-Location
}
}
Write-Output 'Publishing about help files'
Copy-Item -Path ./Source/en-US -Destination "$output/" -Recurse -Force
Copy-Item -Path ./Source/VSTeam.psm1 -Destination "$output/VSTeam.psm1" -Force
Write-Output 'Updating Functions To Export'
$newValue = ((Get-ChildItem -Path "./Source/Public" -Filter '*.ps1').BaseName |
ForEach-Object -Process { Write-Output "'$_'" }) -join ','
(Get-Content "./Source/VSTeam.psd1") -Replace ("FunctionsToExport.+", "FunctionsToExport = ($newValue)") | Set-Content "$output/VSTeam.psd1"
Write-Output "Publish complete to $output"
#reload the just built module
if ($ipmo.IsPresent -or $runTests.IsPresent) {
#module needs to be unloaded if present
if((Get-Module VSTeam)){
Remove-Module VSTeam
}
Import-Module "$output/VSTeam.psd1" -Force
Set-VSTeamAlias
}
#run PSScriptAnalyzer
if ($analyzeScript.IsPresent) {
Write-Output "Starting static code analysis..."
if ($null -eq $(Get-Module -Name PSScriptAnalyzer)) {
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Force -Scope CurrentUser
}
$r = Invoke-ScriptAnalyzer -Path $output -Recurse
$r | ForEach-Object { Write-Host "##vso[task.logissue type=$($_.Severity);sourcepath=$($_.ScriptPath);linenumber=$($_.Line);columnnumber=$($_.Column);]$($_.Message)" }
Write-Output "Static code analysis complete."
}
# run the unit tests with Pester
if ($runTests.IsPresent) {
if ($null -eq $(Get-Module -Name Pester)) {
Install-Module -Name Pester -Repository PSGallery -Force -Scope CurrentUser -AllowClobber -SkipPublisherCheck
}
$pesterArgs = @{
Script = '.\unit'
OutputFile = 'test-results.xml'
OutputFormat = 'NUnitXml'
Show = 'Fails'
Strict = $true
}
if ($codeCoverage.IsPresent) {
$pesterArgs.CodeCoverage = "$outputDir\*.ps1"
$pesterArgs.CodeCoverageOutputFile = "coverage.xml"
$pesterArgs.CodeCoverageOutputFileFormat = 'JaCoCo'
}
else {
$pesterArgs.PassThru = $true
}
if($testName){
$pesterArgs.TestName = $testName
#passthru must be activated according to Pester docs
$pesterArgs.PassThru = $true
}
Invoke-Pester @pesterArgs
}