SnipeitPS/build.ps1

102 lines
3 KiB
PowerShell
Raw Normal View History

2017-12-31 19:36:51 +00:00
param(
[string[]]$Tasks
)
2017-12-31 21:16:55 +00:00
2017-12-31 19:36:51 +00:00
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"
)
2017-12-31 19:42:27 +00:00
$results = Invoke-Pester -PassThru
2017-12-31 19:36:51 +00:00
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
}
}
2017-12-31 21:16:55 +00:00
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
2017-12-31 21:21:21 +00:00
$functionsToExport = Get-ChildItem "$BuildRoot\SnipeitPS\Public" | ForEach-Object {$_.BaseName}
2017-12-31 21:16:55 +00:00
Set-ModuleFunctions -Name "$releasePath\SnipeitPS\SnipeitPS.psd1" -FunctionsToExport $functionsToExport
2017-12-31 21:23:55 +00:00
#Remove-Module SnipeitPS
2017-12-31 21:27:15 +00:00
Import-Module $env:CI_PROJECT_DIR\SnipeitPS\SnipeitPS.psd1 -force -ErrorAction Stop
2017-12-31 21:16:55 +00:00
Publish-Module -Name SnipeitPS -Repository InternalPowerShellModules -NuGetApiKey 123456789
}
2017-12-31 19:36:51 +00:00
foreach($task in $Tasks){
switch($task)
{
"test" {
2017-12-31 19:42:27 +00:00
Install-Dependency -Name "PSScriptAnalyzer"
2017-12-31 19:36:51 +00:00
Install-Dependency -Name "Pester"
Write-Output "Running Pester Tests..."
Run-Tests
}
2017-12-31 20:07:11 +00:00
"release" {
2017-12-31 21:16:55 +00:00
Write-Output "Releasing..."
Release
2017-12-31 20:07:11 +00:00
}
2017-12-31 19:36:51 +00:00
}
}