# PSake makes variables declared here available in other scriptblocks # Init some things Properties { # Find the build folder based on build system $ProjectRoot = $ENV:BHProjectPath if (-not $ProjectRoot) { $ProjectRoot = $PSScriptRoot } $Timestamp = Get-date -uformat "%Y%m%d-%H%M%S" $PSVersion = $PSVersionTable.PSVersion.Major $TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml" $lines = '----------------------------------------------------------------------' $Verbose = @{} if ( $ENV:BHCommitMessage -match "!verbose" ) { $Verbose = @{Verbose = $True} } } Task Default -Depends Deploy Task Init { $lines Set-Location $ProjectRoot "Build System Details:" Get-Item ENV:BH* | Format-List "`n" } Task UnitTests -Depends Init { $lines 'Running quick unit tests to fail early if there is an error' $TestResults = Invoke-Pester -Path $ProjectRoot\Tests\*unit* -PassThru -Tag Build if ( $TestResults.FailedCount -gt 0 ) { Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed" } "`n" } Task Test -Depends UnitTests { $lines "`n`tSTATUS: Testing with PowerShell $PSVersion" # Gather test results. Store them in a variable and file $TestResults = Invoke-Pester -Path $ProjectRoot\Tests -PassThru -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile" -Tag Build # In Appveyor? Upload our tests! #Abstract this into a function? If ( $ENV:BHBuildSystem -eq 'AppVeyor' ) { [xml]$content = Get-Content "$ProjectRoot\$TestFile" $content.'test-results'.'test-suite'.type = "Powershell" $content.Save( "$ProjectRoot\$TestFile" ) "Uploading $ProjectRoot\$TestFile to AppVeyor" "JobID: $env:APPVEYOR_JOB_ID" (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path "$ProjectRoot\$TestFile")) } Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction SilentlyContinue # Failed tests? # Need to tell psake or it will proceed to the deployment. Danger! if ( $TestResults.FailedCount -gt 0 ) { Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed" } "`n" } Task Build -Depends Test { $lines $functions = Get-ChildItem "$PSScriptRoot\$env:BHProjectName\Public\*.ps1" | Where-Object { $_.name -notmatch 'Tests'} | Select-Object -ExpandProperty basename # Load the module, read the exported functions, update the psd1 FunctionsToExport Set-ModuleFunctions -Name $env:BHPSModuleManifest -FunctionsToExport $functions # Bump the module version $version = [version] (Step-Version (Get-Metadata -Path $env:BHPSModuleManifest)) $galleryVersion = Get-NextPSGalleryVersion -Name $env:BHProjectName if ( $version -lt $galleryVersion ) { $version = $galleryVersion } $version = [version]::New($version.Major, $version.Minor, $version.Build, $env:BHBuildNumber) Write-Host "Using version: $version" Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $version $psm1 = "$PSScriptRoot\$env:BHProjectName\$env:BHProjectName.psm1" Remove-Item $psm1 # keep the first line in the psm1 file and clear the rest Get-Content -Path $psm1 -TotalCount 1 | Set-Content -Path $psm1 foreach ( $folder in ('Classes', 'Public', 'Private') ) { Add-Content -Path $psm1 -Value "Write-Verbose 'Importing from [$folder]'" $imports = Get-ChildItem "$PSScriptRoot\$env:BHProjectName\$folder\*.ps1" -Exclude *.Tests.ps1 foreach ( $file in $imports ) { Add-Content -Path $psm1 -Value '' Add-Content -Path $psm1 -Value "Write-Verbose ' Source [\$folder\$($file.name)]'" [System.IO.File]::ReadAllText($file.fullname) | Add-Content -Path $psm1 } # Remove folders after import Remove-Item "$PSScriptRoot\$env:BHProjectName\$folder" -Recurse } # Add exports Add-Content -Path $psm1 -Value "Export-ModuleMember -Function $($functions -join ', ')" Import-Module $psm1 -Force } Task Deploy -Depends Build { $lines # Gate deployment if ( $ENV:BHBuildSystem -ne 'Unknown' -and $ENV:BHBranchName -eq "master" -and $ENV:BHCommitMessage -match '!deploy' ) { $Params = @{ Path = $ProjectRoot Force = $true } Invoke-PSDeploy @Verbose @Params } else { "Skipping deployment: To deploy, ensure that...`n" + "`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" + "`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" + "`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)" } }