Skip to content

Instantly share code, notes, and snippets.

@Mark-Broadhurst
Last active August 6, 2018 15:16
Show Gist options
  • Save Mark-Broadhurst/1975730fa5802188d5056da67b8c9d5c to your computer and use it in GitHub Desktop.
Save Mark-Broadhurst/1975730fa5802188d5056da67b8c9d5c to your computer and use it in GitHub Desktop.
invokeBuild Script MSBuild 14.0
cls
Write-Host "Checking NuGet"
$nugetPath = "$env:LOCALAPPDATA\NuGet\NuGet.exe"
if (!(Get-Command NuGet -ErrorAction SilentlyContinue) -and !(Test-Path $nugetPath)) {
if(! (Test-Path "$env:LOCALAPPDATA\NuGet"))
{
Write-Host "Creating directory"
New-Item -ItemType Directory "$env:LOCALAPPDATA\NuGet"
}
Write-Host 'Downloading NuGet.exe'
(New-Object System.Net.WebClient).DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", $nugetPath)
}
if (Test-Path $nugetPath) {
Set-Alias NuGet (Resolve-Path $nugetPath)
}
Write-Host 'Restoring NuGet packages'
NuGet restore
. '.\functions.ps1'
$invokeBuild = (Get-ChildItem('.\packages\Invoke-Build*\tools\Invoke-Build.ps1')).FullName | Sort-Object $_ | Select -Last 1
& $invokeBuild $args -File tasks.ps1
function Get-SolutionProjects
{
Add-Type -Path (${env:ProgramFiles(x86)} + '\Reference Assemblies\Microsoft\MSBuild\v14.0\Microsoft.Build.dll')
$solutionFile = (Get-ChildItem('*.sln')).FullName | Select -First 1
$solution = [Microsoft.Build.Construction.SolutionFile] $solutionFile
return $solution.ProjectsInOrder |
Where-Object {$_.ProjectType -eq 'KnownToBeMSBuildFormat'} |
ForEach-Object {
$isWebProject = (Select-String -pattern "<UseIISExpress>.+</UseIISExpress>" -path $_.AbsolutePath) -ne $null
@{
Path = $_.AbsolutePath;
Name = $_.ProjectName;
Directory = "$(Split-Path -Path $_.AbsolutePath -Resolve)";
IsWebProject = $isWebProject;
PackageId = $_.ProjectName -replace "\.", "-";
}
}
}
function Get-PackagePath($packageId, $projectPath)
{
if (!(Test-Path "$projectPath\packages.config")) {
throw "Could not find a packages.config file at $project"
}
[xml]$packagesXml = Get-Content "$projectPath\packages.config"
$package = $packagesXml.packages.package | Where { $_.id -eq $packageId }
if (!$package) {
return $null
}
return "packages\$($package.id).$($package.version)"
}
$preRelease = $(Get-Date).ToString("yyMMddHHmmss")
function Get-Version($projectPath)
{
$line = Get-Content "$projectPath\Properties\AssemblyInfo.cs" | Where { $_.Contains("AssemblyVersion") }
if (!$line) {
throw "Couldn't find an AssemblyVersion attribute"
}
$version = $line.Split('"')[1]
$isLocal = [String]::IsNullOrEmpty($env:BUILD_SERVER)
if($isLocal){
$version = "$($version.Replace("*", 0))-pre$preRelease"
} else{
$version = $version.Replace("*", $env:BUILD_NUMBER)
}
return $version
}
param(
$outputDirectory = (property outputDirectory "artifacts"),
$configuration = (property configuration "Release")
)
$absoluteOutputDirectory= "$((Get-Location).Path)\$outputDirectory"
$projects = Get-SolutionProjects
task Clean {
if((Test-Path $absoluteOutputDirectory))
{
Write-Host "Cleaning artifacts directory $absoluteOutputDirectory"
Remove-Item "$absoluteOutputDirectory" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
}
New-Item $absoluteOutputDirectory -ItemType Directory | Out-Null
$projects |
ForEach-Object {
Write-Host "Cleaning bin and obj folders for $($_.Directory)"
Remove-Item "$($_.Directory)\bin" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
Remove-Item "$($_.Directory)\obj" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
}
}
task Compile{
use "14.0" MSBuild
$projects |
ForEach-Object {
if($_.IsWebProject)
{
$webOutDir = "$absoluteOutputDirectory\$($_.Name)"
$outDir = "$absoluteOutputDirectory\$($_.Name)\bin"
Write-Host "Compiling $($_.Name) to $outDir"
exec {MSBuild $($_.Path) /p:Configuration=$configuration /p:OutDir=$outDir /p:WebProjectOutputDir=$webOutDir `
/nologo /p:DebugType=None /p:Platform=AnyCpu /verbosity:quiet }
} else
{
$outDir = "$absoluteOutputDirectory\$($_.Name)"
Write-Host "Compiling $($_.Name) to $webOutDir"
exec {MSBuild $($_.Path) /p:Configuration=$configuration /p:OutDir=$outDir `
/nologo /p:DebugType=None /p:Platform=AnyCpu /verbosity:quiet }
}
}
}
task Test {
$projects |
ForEach-Object {
$xunitPath = Get-PackagePath "xunit.runner.console" $($_.Directory)
if($xunitPath -eq $null){
return
}
$xunitRunner = "$xunitPath\tools\xunit.console.exe"
exec { & $xunitRunner $absoluteOutputDirectory\$($_.Name)\$($_.Name).dll `
-xml "$absoluteOutputDirectory\xunit_$($_.Name).xml" `
-html "$absoluteOutputDirectory\xunit_$($_.Name).html" `
-nologo }
}
}
task Pack {
$projects |
ForEach-Object {
$octopusToolsPath = Get-PackagePath "OctopusTools" $($_.Directory)
if($octopusToolsPath -eq $null){
return
}
$version = Get-Version $_.Directory
exec { & $octopusToolsPath\tools\Octo.exe pack `
--basePath=$absoluteOutputDirectory\$($_.Name) `
--outFolder=$absoluteOutputDirectory --id=$($_.PackageId) `
--overwrite `
--version=$version}
}
}
task Push{
$projects |
ForEach-Object {
$octopusToolsPath = Get-PackagePath "OctopusTools" $($_.Directory)
if($octopusToolsPath -eq $null){
return
}
$package = @(Get-ChildItem $absoluteOutputDirectory\$($_.PackageId)*.nupkg)
exec { NuGet push $package -Source "$env:octopusDeployServer/nuget/packages" `
-ApiKey $env:octopusDeployApiKey }
}
}
task Release {
$projects |
ForEach-Object {
$octopusToolsPath = Get-PackagePath "OctopusTools" $($_.Directory)
if($octopusToolsPath -eq $null){
return
}
$version = Get-Version $_.Directory
exec { & $octopusToolsPath\tools\Octo.exe create-release `
--server="$env:octopusDeployServer" `
--apiKey="$env:octopusDeployApiKey" `
--project="$($_.PackageId)" `
--version="$version" `
--packageVersion="$version" `
--ignoreexisting }
}
}
task Deploy{
$projects |
ForEach-Object {
$octopusToolsPath = Get-PackagePath "OctopusTools" $($_.Directory)
if($octopusToolsPath -eq $null){
return
}
$version = Get-Version $_.Directory
exec { & $octopusToolsPath\tools\Octo.exe deploy-release `
--server="$env:octopusDeployServer" `
--apiKey="$env:octopusDeployApiKey" `
--project="$($_.PackageId)" `
--version="$version" `
--deployto="$env:environment" }
}
}
task dev clean, compile, test
task ci dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment