-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved build helper functions into separate PS script, which can be easier shared across projects.
- Loading branch information
1 parent
b4202bf
commit d4d9ac0
Showing
9 changed files
with
217 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.