-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ps1
91 lines (81 loc) · 2.66 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
param(
[string[]]$Tasks
)
function Install-Dependency([string] $Name)
{
$policy = Get-PSRepository -Name "PSGallery" | Select-Object -ExpandProperty "InstallationPolicy"
if($policy -ne "Trusted") {
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
}
if (!(Get-Module -Name $Name -ListAvailable)) {
Install-Module -Name $Name -Scope CurrentUser
}
}
function Analyze-Scripts
{
param(
[string]$Path = "$PSScriptRoot\src\"
)
$result = Invoke-ScriptAnalyzer -Path $Path -Severity @('Error', 'Warning') -Recurse
if ($result) {
$result | Format-Table
Write-Error -Message "$($result.SuggestedCorrections.Count) linting errors or warnings were found. The build cannot continue."
EXIT 1
}
}
function Run-Tests
{
param(
[string]$Path = "$PSScriptRoot\src"
)
$results = Invoke-Pester -Path $Path -CodeCoverage $Path\*\*\*\*.ps1 -PassThru -Quiet
if($results.FailedCount -gt 0) {
Write-Output " > $($results.FailedCount) tests failed. The build cannot continue."
foreach($result in $($results.TestResult | Where {$_.Passed -eq $false} | Select-Object -Property Describe,Context,Name,Passed,Time)){
Write-Output " > $result"
}
EXIT 1
}
$coverage = [math]::Round($(100 - (($results.CodeCoverage.NumberOfCommandsMissed / $results.CodeCoverage.NumberOfCommandsAnalyzed) * 100)), 2);
Write-Output " > Code Coverage: $coverage%"
}
function Deploy-Modules
{
param(
[string]$Path = "$PSScriptRoot\src"
)
try {
foreach($module in $(Get-ChildItem -Path $Path | Where-Object {$_.Name.ToLower().Contains("psdeploy")})){
$name = $module.Name.Split(".")[0]
$localManifest = Import-PowerShellDataFile -Path $(Join-Path -Path $module.DirectoryName -ChildPath "$name\$name.psd1")
if([Version]$localManifest.ModuleVersion -gt $(Find-Module -Name $name).Version) {
Write-Output " > Deploying $name"
Invoke-PSDeploy -Path $module.FullName -Force
}
}
}
catch {
Write-Output $_.Exception.Message
EXIT 1
}
}
foreach($task in $Tasks){
switch($task)
{
"analyze" {
Install-Dependency -Name "PSScriptAnalyzer"
Write-Output "Analyzing Scripts..."
Analyze-Scripts
}
"test" {
Install-Dependency -Name "Pester"
Write-Output "Running Pester Tests..."
Run-Tests
}
"deploy" {
Install-Dependency -Name "PSDeploy"
Write-Output "Deploying Modules..."
Deploy-Modules
}
}
}