-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.ps1
133 lines (112 loc) · 4.25 KB
/
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
[CmdletBinding()]
param(
[switch]
$Bootstrap,
[switch]
$Test,
[switch]
$Publish,
# Only works in conjunction with $Test (#TODO)
[switch]
$Coverage
)
# Bootstrap step
if ($Bootstrap.IsPresent) {
Write-Host "Validate and install missing prerequisits for building ..."
# Dependencies of PoshNotify
if (-not (Get-Module -Name MacNotify -ListAvailable)) {
Write-Warning "Module 'MacNotify' is missing. Installing 'MacNotify' ..."
Install-Module -Name MacNotify -Scope CurrentUser -Force
}
if (-not (Get-Module -Name PSNotifySend -ListAvailable)) {
Write-Warning "Module 'PSNotifySend' is missing. Installing 'PSNotifySend' ..."
Install-Module -Name PSNotifySend -Scope CurrentUser -Force
}
# For testing
if (-not (Get-Module -Name Pester -ListAvailable)) {
Write-Warning "Module 'Pester' is missing. Installing 'Pester' ..."
Install-Module -Name Pester -Scope CurrentUser -Force
}
# For coverage
if (-not (Get-Module -Name PSCodeCovIo -ListAvailable) -and $Coverage.IsPresent) {
Write-Warning "Module 'PSCodeCovIo' is missing. Installing 'PSCodeCovIo' ..."
Install-Module -Name PSCodeCovIo -Scope CurrentUser -Force
}
}
# Test step
if($Test.IsPresent) {
if (-not (Get-Module -Name Pester -ListAvailable)) {
throw "Cannot find the 'Pester' module. Please specify '-Bootstrap' to install build dependencies."
}
if (-not (Get-Module -Name PSCodeCovIo -ListAvailable) -and $Coverage.IsPresent) {
throw "Cannot find the 'PSCodeCovIo' module. Please specify '-Bootstrap' to install build dependencies."
}
if($Coverage.IsPresent) {
switch ($true) {
$IsWindows {
# Must be PowerShell Core on Windows
$Exclude = @("*Linux*", "*MacOS*")
break
}
$IsMacOS {
$Exclude = @("*Linux*", "*Windows*")
break
}
$IsLinux {
$Exclude = @("*Windows*", "*MacOS*")
break
}
Default {
# Must be Windows PowerShell
$Exclude = @("*Linux*", "*MacOS*")
break
}
}
$RelevantFiles = (Get-ChildItem $PSScriptRoot/src -Recurse -Include "*.psm1","*.ps1" -Exclude $Exclude).FullName
}
if ($env:TF_BUILD) {
if ($Coverage.IsPresent){
$res = Invoke-Pester "$PSScriptRoot/test" -CodeCoverage $RelevantFiles -OutputFormat NUnitXml -OutputFile TestResults.xml -PassThru
} else {
$res = Invoke-Pester "$PSScriptRoot/test" -OutputFormat NUnitXml -OutputFile TestResults.xml -PassThru
}
if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed." }
} else {
if ($Coverage.IsPresent) {
$res = Invoke-Pester -Path "$PSScriptRoot/test" -CodeCoverage $RelevantFiles -PassThru
} else {
$res = Invoke-Pester -Path "$PSScriptRoot/test" -PassThru
}
}
if ($Coverage.IsPresent) {
Export-CodeCovIoJson -CodeCoverage $res.CodeCoverage -RepoRoot $PSScriptRoot -Path coverage.json
Invoke-WebRequest -Uri 'https://codecov.io/bash' -OutFile codecov.sh
}
}
#Publish step
if($Publish.IsPresent) {
if ((Test-Path .\output)) {
Remove-Item -Path .\Output -Recurse -Force
}
# Copy Module Files to Output Folder
if (-not (Test-Path .\output\PoshNotify)) {
$null = New-Item -Path .\output\PoshNotify -ItemType Directory
}
Copy-Item -Path '.\src\*' -Filter *.* -Recurse -Destination .\output\PoshNotify -Force
# Copy Module README file
Copy-Item -Path '.\README.md' -Destination .\output\PoshNotify -Force
# Publish Module to PowerShell Gallery
Try {
# Build a splat containing the required details and make sure to Stop for errors which will trigger the catch
$params = @{
Path = ('{0}\Output\PoshNotify' -f $PSScriptRoot )
NuGetApiKey = $env:psgallery
ErrorAction = 'Stop'
}
Publish-Module @params
Write-Output -InputObject ('PoshNotify PowerShell Module version published to the PowerShell Gallery')
}
Catch {
throw $_
}
}