-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbuild.ps1
101 lines (83 loc) · 3 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
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 Run-Tests
{
param(
[string]$Path = "$PSScriptRoot\SnipeitPS"
)
$results = Invoke-Pester -PassThru
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
}
}
function Release
{
Write-Output "Setting Variables"
$BuildRoot = $env:CI_PROJECT_DIR
$releasePath = "$BuildRoot\Release"
Write-Output "Build Root : $BuildRoot"
Write-Output "Release Root : $releasePath"
if (-not (Test-Path "$releasePath\SnipeitPS")) {
$null = New-Item -Path "$releasePath\SnipeitPS" -ItemType Directory
}
# Copy module
Copy-Item -Path "$BuildRoot\SnipeitPS\*" -Destination "$releasePath\SnipeitPS" -Recurse -Force
# Copy additional files
$additionalFiles = @(
"$BuildRoot\CHANGELOG.md"
"$BuildRoot\LICENSE"
"$BuildRoot\README.md"
)
Copy-Item -Path $additionalFiles -Destination "$releasePath\SnipeitPS" -Force
$manifestContent = Get-Content -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -Raw
if ($manifestContent -notmatch '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')') {
throw "Module version was not found in manifest file,"
}
$currentVersion = [Version] $Matches.ModuleVersion
if ($env:CI_JOB_ID) {
$newRevision = $env:CI_JOB_ID
}
else {
$newRevision = 0
}
$version = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major,
$currentVersion.Minor,
$newRevision
Write-Output "New version : $version"
Update-Metadata -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -PropertyName ModuleVersion -Value $version
$functionsToExport = Get-ChildItem "$BuildRoot\SnipeitPS\Public" | ForEach-Object {$_.BaseName}
Set-ModuleFunctions -Name "$releasePath\SnipeitPS\SnipeitPS.psd1" -FunctionsToExport $functionsToExport
#Remove-Module SnipeitPS
Import-Module $env:CI_PROJECT_DIR\SnipeitPS\SnipeitPS.psd1 -force -ErrorAction Stop
Publish-Module -Name SnipeitPS -Repository InternalPowerShellModules -NuGetApiKey 123456789
}
foreach($task in $Tasks){
switch($task)
{
"test" {
Install-Dependency -Name "PSScriptAnalyzer"
Install-Dependency -Name "Pester"
Write-Output "Running Pester Tests..."
Run-Tests
}
"release" {
Write-Output "Releasing..."
Release
}
}
}