|
| 1 | +#requires -Module VirtualEngine.Build, VirtualEngine.Compression; |
| 2 | +#requires -Version 3; |
| 3 | + |
| 4 | +Properties { |
| 5 | + $currentDir = Resolve-Path -Path .; |
| 6 | + $basePath = $psake.build_script_dir; |
| 7 | + $buildDir = 'Build'; |
| 8 | + $releaseDir = 'Release'; |
| 9 | + $invocation = (Get-Variable MyInvocation -Scope 1).Value; |
| 10 | + $thumbprint = 'D10BB31E5CE3048A7D4DA0A4DD681F05A85504D3'; |
| 11 | + $timeStampServer = 'http://timestamp.verisign.com/scripts/timestamp.dll'; |
| 12 | + $company = 'Virtual Engine'; |
| 13 | + $author = 'Iain Brighton'; |
| 14 | + $githubOwner = 'VirtualEngine'; |
| 15 | + $githubTokenPath = '~\Github.apitoken'; |
| 16 | + $chocolateyTokenPath = '~\Chocolatey.apitoken'; |
| 17 | +} |
| 18 | + |
| 19 | +Task Default -Depends Build; |
| 20 | +Task Build -Depends Clean, Setup, Deploy; |
| 21 | +Task Stage -Depends Build, Version, Sign, Zip; |
| 22 | +Task Publish -Depends Stage, Release, Chocolatey; |
| 23 | + |
| 24 | +Task Clean { |
| 25 | + ## Remove build directory |
| 26 | + $baseBuildPath = Join-Path -Path $psake.build_script_dir -ChildPath $buildDir; |
| 27 | + if (Test-Path -Path $baseBuildPath) { |
| 28 | + Write-Host (' Removing build base directory "{0}".' -f $baseBuildPath) -ForegroundColor Yellow; |
| 29 | + Remove-Item $baseBuildPath -Recurse -Force -ErrorAction Stop; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +Task Setup { |
| 34 | + # Properties are not available in the script scope. |
| 35 | + Set-Variable manifest -Value (Get-ModuleManifest) -Scope Script; |
| 36 | + Set-Variable buildPath -Value (Join-Path -Path $psake.build_script_dir -ChildPath "$buildDir\$($manifest.Name)") -Scope Script; |
| 37 | + Set-Variable releasePath -Value (Join-Path -Path $psake.build_script_dir -ChildPath $releaseDir) -Scope Script; |
| 38 | + Set-Variable version -Value (Get-GitVersionString) -Scope Script; |
| 39 | + |
| 40 | + Write-Host (' Building module "{0}".' -f $manifest.Name) -ForegroundColor Yellow; |
| 41 | + Write-Host (' Using Git version "{0}".' -f $version) -ForegroundColor Yellow; |
| 42 | + |
| 43 | + ## Create the build directory |
| 44 | + Write-Host (' Creating build directory "{0}".' -f $buildPath) -ForegroundColor Yellow; |
| 45 | + [Ref] $null = New-Item $buildPath -ItemType Directory -Force -ErrorAction Stop; |
| 46 | + |
| 47 | + ## Create the release directory |
| 48 | + if (!(Test-Path -Path $releasePath)) { |
| 49 | + Write-Host (' Creating release directory "{0}".' -f $releasePath) -ForegroundColor Yellow; |
| 50 | + [Ref] $null = New-Item $releasePath -ItemType Directory -Force -ErrorAction Stop; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +Task Test { |
| 55 | + $testResultsPath = Join-Path $buildPath -ChildPath 'NUnit.xml'; |
| 56 | + $testResults = Invoke-Pester -Path $basePath -OutputFile $testResultsPath -OutputFormat NUnitXml -PassThru -Strict; |
| 57 | + if ($testResults.FailedCount -gt 0) { |
| 58 | + Write-Error ('{0} unit tests failed.' -f $testResults.FailedCount); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +Task Deploy { |
| 63 | + ## Copy release files |
| 64 | + Write-Host (' Copying release files to build directory "{0}".' -f $buildPath) -ForegroundColor Yellow; |
| 65 | + $excludedFiles = @( '*.Tests.ps1','Build.PSake.ps1','.git*','*.png','Build','Release','readme.md' ); |
| 66 | + Get-ModuleFile -Exclude $excludedFiles | % { |
| 67 | + $destinationPath = '{0}{1}' -f $buildPath, $PSItem.FullName.Replace($basePath, ''); |
| 68 | + [Ref] $null = New-Item -ItemType File -Path $destinationPath -Force; |
| 69 | + Copy-Item -Path $PSItem.FullName -Destination $destinationPath -Force; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +Task Version { |
| 74 | + ## Version module manifest prior to build |
| 75 | + $manifestPath = Join-Path $buildPath -ChildPath "$($manifest.Name).psd1"; |
| 76 | + Write-Host (' Versioning module manifest "{0}".' -f $manifestPath) -ForegroundColor Yellow; |
| 77 | + Set-ModuleManifestProperty -Path $manifestPath -Version $version -CompanyName $company -Author $author; |
| 78 | + ## Reload module manifest to ensure the version number is picked back up |
| 79 | + Set-Variable manifest -Value (Get-ModuleManifest -Path $manifestPath) -Scope Script -Force; |
| 80 | +} |
| 81 | + |
| 82 | +Task Sign { |
| 83 | + Get-ChildItem -Path $buildPath -Include *.ps* -Recurse -File | % { |
| 84 | + Write-Host (' Signing file "{0}":' -f $PSItem.FullName) -ForegroundColor Yellow -NoNewline; |
| 85 | + $signResult = Set-ScriptSignature -Path $PSItem.FullName -Thumbprint $thumbprint -TimeStampServer $timeStampServer -ErrorAction Stop; |
| 86 | + Write-Host (' {0}.' -f $signResult.Status) -ForegroundColor Green; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +Task Zip { |
| 91 | + ## Creates the release files in the $releaseDir |
| 92 | + $zipReleaseName = '{0}-v{1}.zip' -f $manifest.Name, $version; |
| 93 | + $zipPath = Join-Path -Path $releasePath -ChildPath $zipReleaseName; |
| 94 | + Write-Host (' Creating zip file "{0}".' -f $zipPath) -ForegroundColor Yellow; |
| 95 | + ## Zip the parent directory |
| 96 | + $zipSourcePath = Split-Path -Path $buildPath -Parent; |
| 97 | + $zipFile = New-ZipArchive -Path $zipSourcePath -DestinationPath $zipPath; |
| 98 | + Write-Host (' Zip file "{0}" created.' -f $zipFile.Fullname) -ForegroundColor Yellow; |
| 99 | +} |
| 100 | + |
| 101 | +Task Release { |
| 102 | + ## Create a Github release |
| 103 | + $githubApiKey = (New-Object System.Management.Automation.PSCredential 'OAUTH', (Get-Content -Path $githubTokenPath | ConvertTo-SecureString)).GetNetworkCredential().Password; |
| 104 | + Write-Host (' Creating new Github "{0}" release in repository "{1}/{2}".' -f $version, $githubOwner, $manifest.Name) -ForegroundColor Yellow; |
| 105 | + #$release = New-GitHubRelease -Version $version -Repository $manifest.Name -Owner $githubOwner -ApiKey $githubApiKey; |
| 106 | + if ($release) { |
| 107 | + ## Creates the release files in the $releaseDir |
| 108 | + $zipReleaseName = '{0}-v{1}.zip' -f $manifest.Name, $version; |
| 109 | + $zipPath = Join-Path -Path $releasePath -ChildPath $zipReleaseName; |
| 110 | + Write-Host (' Uploading asset "{0}".' -f $zipPath) -ForegroundColor Yellow; |
| 111 | + $asset = Invoke-GitHubAssetUpload -Release $release -ApiKey $githubApiKey -Path $zipPath; |
| 112 | + Set-Variable -Name assetUri -Value $asset.Browser_Download_Url -Scope Script -Force; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +Task Chocolatey { |
| 117 | + Set-Variable chocolateyBuildPath -Value (Join-Path -Path $psake.build_script_dir -ChildPath "$buildDir\Chocolatey") -Scope Script; |
| 118 | + ## Create the Chocolatey folder |
| 119 | + Write-Host (' Creating Chocolatey directory "{0}".' -f $chocolateyBuildPath) -ForegroundColor Yellow; |
| 120 | + [Ref] $null = New-Item $chocolateyBuildPath -ItemType Directory -Force -ErrorAction Stop; |
| 121 | + $chocolateyToolsPath = New-Item "$chocolateyBuildPath\tools" -ItemType Directory -Force -ErrorAction Stop; |
| 122 | + |
| 123 | + ## Create the Chocolatey package |
| 124 | + $nuspecFilename = '{0}.nuspec' -f $manifest.Name; |
| 125 | + $nuspecPath = Join-Path -Path $chocolateyBuildPath -ChildPath $nuspecFilename; |
| 126 | + Write-Host (' Creating Nuget specification "{0}".' -f $nuspecPath) -ForegroundColor Yellow; |
| 127 | + (New-NuGetNuspec -InputObject $manifest).Save($nuspecPath); |
| 128 | + |
| 129 | + Write-Host ' Creating Chocolatey install files.' -ForegroundColor Yellow; |
| 130 | + New-ChocolateyInstallZipModule -Path $chocolateyToolsPath.FullName -PackageName $manifest.Name -Uri $assetUri; |
| 131 | + Write-Host (' Creating Nuget package from "{0}".' -f $nuspecPath) -ForegroundColor Yellow; |
| 132 | + $nugetOutput = Invoke-NuGetPack -Path $nuspecPath -DestinationPath $releasePath; |
| 133 | + if ($nugetOutput) { |
| 134 | + $nugetPackagePath = Join-Path -Path $releasePath -ChildPath ('{0}.{1}.nupkg' -f $manifest.Name.ToLower(), $version); |
| 135 | + Write-Host (' Chocolatey package "{0}" created.' -f $nugetPackagePath) -ForegroundColor Yellow; |
| 136 | + $chocolateyApiKey = (New-Object System.Management.Automation.PSCredential 'OAUTH', (Get-Content -Path $chocolateyTokenPath | ConvertTo-SecureString)).GetNetworkCredential().Password; |
| 137 | + Write-Host (' Pushing Chocolatey package "{0}".' -f $nugetPackagePath) -ForegroundColor Yellow; |
| 138 | + } |
| 139 | +} |
0 commit comments