Skip to content

Commit

Permalink
Moved build helper functions
Browse files Browse the repository at this point in the history
Moved build helper functions into separate PS script, which can be easier shared across projects.
  • Loading branch information
dustinmoris committed Aug 21, 2018
1 parent b4202bf commit d4d9ac0
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 144 deletions.
190 changes: 190 additions & 0 deletions .psscripts/build-functions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# ----------------------------------------------
# Generic functions
# ----------------------------------------------

function Test-IsWindows
{
<#
.DESCRIPTION
Checks to see whether the current environment is Windows or not.
.EXAMPLE
if (Test-IsWindows) { Write-Host "Hello Windows" }
#>

[environment]::OSVersion.Platform -ne "Unix"
}

function Invoke-Cmd ($Cmd)
{
<#
.DESCRIPTION
Runs a shell or bash command and throws an error if the command didn't exit with 0.
.PARAMETER cmd
The command to be executed.
.EXAMPLE
Invoke-Cmd -Cmd "dotnet new classlib"
.NOTES
Use this PowerShell command to execute any dotnet CLI commands in order to ensure that they behave the same way in the case of an error across different environments (Windows, OSX and Linux).
#>

Write-Host $Cmd -ForegroundColor DarkCyan
if (Test-IsWindows) { $Cmd = "cmd.exe /C $Cmd" }
Invoke-Expression -Command $Cmd
if ($LastExitCode -ne 0) { Write-Error "An error occured when executing '$Cmd'."; return }
}

function Remove-OldBuildArtifacts
{
<#
.DESCRIPTION
Deletes all the bin and obj folders from the current- and all sub directories.
#>

Write-Host "Deleting old build artifacts..." -ForegroundColor Magenta

Get-ChildItem -Include "bin", "obj" -Recurse -Directory `
| ForEach-Object {
Write-Host "Removing folder $_" -ForegroundColor DarkGray
Remove-Item $_ -Recurse -Force }
}

function Test-CompareVersions ($projFile, [string]$gitTag)
{
Write-Host "Matching version against git tag..." -ForegroundColor Magenta

[xml]$xml = Get-Content $projFile
[string]$version = $xml.Project.PropertyGroup.Version

Write-Host "Project version: $version" -ForegroundColor Cyan
Write-Host "Git tag version: $gitTag" -ForegroundColor Cyan

if (!$gitTag.EndsWith($version))
{
Write-Error "Version and Git tag do not match."
}
}

# ----------------------------------------------
# .NET Core functions
# ----------------------------------------------

function dotnet-info { Invoke-Cmd "dotnet --info" }
function dotnet-version { Invoke-Cmd "dotnet --version" }
function dotnet-build ($project, $argv) { Invoke-Cmd "dotnet build $project $argv" }
function dotnet-run ($project, $argv) { Invoke-Cmd "dotnet run --project $project $argv" }
function dotnet-pack ($project, $argv) { Invoke-Cmd "dotnet pack $project $argv" }
function dotnet-publish ($project, $argv) { Invoke-Cmd "dotnet publish $project $argv" }

function Get-DotNetRuntimeVersion
{
<#
.DESCRIPTION
Runs the dotnet --info command and extracts the .NET Core Runtime version number.
.NOTES
The .NET Core Runtime version can sometimes be useful for other dotnet CLI commands (e.g. dotnet xunit -fxversion ".NET Core Runtime version").
#>

$info = dotnet-info
[System.Array]::Reverse($info)
$version = $info | Where-Object { $_.Contains("Version") } | Select-Object -First 1
$version.Split(":")[1].Trim()
}

function Get-TargetFrameworks ($projFile)
{
<#
.DESCRIPTION
Returns all target frameworks set up inside a specific .NET Core project file.
.PARAMETER projFile
The full or relative path to a .NET Core project file (*.csproj, *.fsproj, *.vbproj).
.EXAMPLE
Get-TargetFrameworks "MyProject.csproj"
.NOTES
This function will always return an array of target frameworks, even if only a single target framework was found in the project file.
#>

[xml]$proj = Get-Content $projFile

if ($null -ne $proj.Project.PropertyGroup.TargetFrameworks) {
($proj.Project.PropertyGroup.TargetFrameworks).Split(";")
}
else { @($proj.Project.PropertyGroup.TargetFramework) }
}

function Get-NetCoreTargetFramework ($projFile)
{
<#
.DESCRIPTION
Returns a single .NET Core framework which could be found among all configured target frameworks of a given .NET Core project file.
.PARAMETER projFile
The full or relative path to a .NET Core project file (*.csproj, *.fsproj, *.vbproj).
.EXAMPLE
Get-NetCoreTargetFramework "MyProject.csproj"
.NOTES
This function will always return the only netstandard*/netcoreapp* target framework which is set up as a target framework.
#>

Get-TargetFrameworks $projFile | Where-Object { $_ -like "netstandard*" -or $_ -like "netcoreapp*" }
}

function dotnet-test ($project, $argv)
{
# Currently dotnet test does not work for net461 on Linux/Mac
# See: https://github.com/Microsoft/vstest/issues/1318
#
# Previously dotnet-xunit was a working alternative, however
# after issues with the maintenance of dotnet xunit it has been
# discontinued since xunit 2.4: https://xunit.github.io/releases/2.4
if(!(Test-IsWindows))
{
$fw = Get-NetCoreTargetFramework $project;
$argv = "-f $fw " + $argv
}
Invoke-Cmd "dotnet test $project $argv"
}

function Write-DotnetCoreVersions
{
<#
.DESCRIPTION
Writes the .NET Core SDK and Runtime version to the current host.
#>

$sdkVersion = dotnet-version
$runtimeVersion = Get-DotNetRuntimeVersion
Write-Host ".NET Core SDK version: $sdkVersion" -ForegroundColor Cyan
Write-Host ".NET Core Runtime version: $runtimeVersion" -ForegroundColor Cyan
}

# ----------------------------------------------
# AppVeyor functions
# ----------------------------------------------

function Test-IsAppVeyorBuild { return ($env:APPVEYOR -eq $true) }
function Test-IsAppVeyorBuildTriggeredByGitTag { return ($env:APPVEYOR_REPO_TAG -eq $true) }
function Get-AppVeyorGitTag { return $env:APPVEYOR_REPO_TAG_NAME }

function Update-AppVeyorBuildVersion ($projFile)
{
if (Test-IsAppVeyorBuild)
{
Write-Host "Updating AppVeyor build version..." -ForegroundColor Magenta

[xml]$xml = Get-Content $projFile
$version = $xml.Project.PropertyGroup.Version
$buildVersion = "$version-$env:APPVEYOR_BUILD_NUMBER"
Write-Host "Setting AppVeyor build version to $buildVersion."
Update-AppveyorBuild -Version $buildVersion
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ before_install:
- curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
- sudo apt-get update
- sudo apt-get install -y powershell
- sudo pwsh ./install-dotnet.ps1
- sudo pwsh ./.psscripts/install-dotnet.ps1

script:
- export FrameworkPathOverride=$(dirname $(which mono))/../lib/mono/4.5/
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ The build script supports the following flags:
- `-All` will build and test all projects
- `-Release` will build Giraffe with the `Release` configuration
- `-Pack` will create a NuGet package for Giraffe and giraffe-template.
- `-OnlyNetStandard` will build Giraffe only targeting the NETStandard1.6 framework

Examples:

Expand Down
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Release Notes

## 2.0.1

Changed the `task {}` CE to load from `FSharp.Control.Tasks.V2.ContextInsensitive` instead of `FSharp.Control.Tasks.ContextInsensitive`
Changed the `task {}` CE to load from `FSharp.Control.Tasks.V2.ContextInsensitive` instead of `FSharp.Control.Tasks.ContextInsensitive`.

## 2.0.0

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:
init:
- git config --global core.autocrlf true
install:
- ps: .\install-dotnet.ps1
- ps: .\.psscripts\install-dotnet.ps1
build: off
build_script:
- ps: .\build.ps1 -Release -Pack
Expand Down
Loading

0 comments on commit d4d9ac0

Please sign in to comment.