Skip to content

Commit 7a32f9f

Browse files
committed
Add build system
1 parent c2edbf5 commit 7a32f9f

13 files changed

Lines changed: 437 additions & 0 deletions

.nuke/parameters.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "build.schema.json",
3+
"Solution": "src/Scarlet.System.Text.Json.DateTimeConverter.sln"
4+
}

build.cmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:; set -eo pipefail
2+
:; SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
3+
:; ${SCRIPT_DIR}/build.sh "$@"
4+
:; exit $?
5+
6+
@ECHO OFF
7+
powershell -ExecutionPolicy ByPass -NoProfile -File "%~dp0build.ps1" %*

build.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
4+
[string[]]$BuildArguments
5+
)
6+
7+
Write-Output "PowerShell $($PSVersionTable.PSEdition) version $($PSVersionTable.PSVersion)"
8+
9+
Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { Write-Error $_ -ErrorAction Continue; exit 1 }
10+
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
11+
12+
###########################################################################
13+
# CONFIGURATION
14+
###########################################################################
15+
16+
$BuildProjectFile = "$PSScriptRoot\build\_build.csproj"
17+
$TempDirectory = "$PSScriptRoot\\.nuke\temp"
18+
19+
$DotNetGlobalFile = "$PSScriptRoot\\global.json"
20+
$DotNetInstallUrl = "https://dot.net/v1/dotnet-install.ps1"
21+
$DotNetChannel = "STS"
22+
23+
$env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
24+
$env:DOTNET_NOLOGO = 1
25+
26+
###########################################################################
27+
# EXECUTION
28+
###########################################################################
29+
30+
function ExecSafe([scriptblock] $cmd) {
31+
& $cmd
32+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
33+
}
34+
35+
# If dotnet CLI is installed globally and it matches requested version, use for execution
36+
if ($null -ne (Get-Command "dotnet" -ErrorAction SilentlyContinue) -and `
37+
$(dotnet --version) -and $LASTEXITCODE -eq 0) {
38+
$env:DOTNET_EXE = (Get-Command "dotnet").Path
39+
}
40+
else {
41+
# Download install script
42+
$DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
43+
New-Item -ItemType Directory -Path $TempDirectory -Force | Out-Null
44+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
45+
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile)
46+
47+
# If global.json exists, load expected version
48+
if (Test-Path $DotNetGlobalFile) {
49+
$DotNetGlobal = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json)
50+
if ($DotNetGlobal.PSObject.Properties["sdk"] -and $DotNetGlobal.sdk.PSObject.Properties["version"]) {
51+
$DotNetVersion = $DotNetGlobal.sdk.version
52+
}
53+
}
54+
55+
# Install by channel or version
56+
$DotNetDirectory = "$TempDirectory\dotnet-win"
57+
if (!(Test-Path variable:DotNetVersion)) {
58+
ExecSafe { & powershell $DotNetInstallFile -InstallDir $DotNetDirectory -Channel $DotNetChannel -NoPath }
59+
} else {
60+
ExecSafe { & powershell $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath }
61+
}
62+
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
63+
$env:PATH = "$DotNetDirectory;$env:PATH"
64+
}
65+
66+
Write-Output "Microsoft (R) .NET SDK version $(& $env:DOTNET_EXE --version)"
67+
68+
if (Test-Path env:NUKE_ENTERPRISE_TOKEN) {
69+
& $env:DOTNET_EXE nuget remove source "nuke-enterprise" > $null
70+
& $env:DOTNET_EXE nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password $env:NUKE_ENTERPRISE_TOKEN > $null
71+
}
72+
73+
ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet }
74+
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }

build.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
3+
bash --version 2>&1 | head -n 1
4+
5+
set -eo pipefail
6+
SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
7+
8+
###########################################################################
9+
# CONFIGURATION
10+
###########################################################################
11+
12+
BUILD_PROJECT_FILE="$SCRIPT_DIR/build/_build.csproj"
13+
TEMP_DIRECTORY="$SCRIPT_DIR//.nuke/temp"
14+
15+
DOTNET_GLOBAL_FILE="$SCRIPT_DIR//global.json"
16+
DOTNET_INSTALL_URL="https://dot.net/v1/dotnet-install.sh"
17+
DOTNET_CHANNEL="STS"
18+
19+
export DOTNET_CLI_TELEMETRY_OPTOUT=1
20+
export DOTNET_NOLOGO=1
21+
22+
###########################################################################
23+
# EXECUTION
24+
###########################################################################
25+
26+
function FirstJsonValue {
27+
perl -nle 'print $1 if m{"'"$1"'": "([^"]+)",?}' <<< "${@:2}"
28+
}
29+
30+
# If dotnet CLI is installed globally and it matches requested version, use for execution
31+
if [ -x "$(command -v dotnet)" ] && dotnet --version &>/dev/null; then
32+
export DOTNET_EXE="$(command -v dotnet)"
33+
else
34+
# Download install script
35+
DOTNET_INSTALL_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
36+
mkdir -p "$TEMP_DIRECTORY"
37+
curl -Lsfo "$DOTNET_INSTALL_FILE" "$DOTNET_INSTALL_URL"
38+
chmod +x "$DOTNET_INSTALL_FILE"
39+
40+
# If global.json exists, load expected version
41+
if [[ -f "$DOTNET_GLOBAL_FILE" ]]; then
42+
DOTNET_VERSION=$(FirstJsonValue "version" "$(cat "$DOTNET_GLOBAL_FILE")")
43+
if [[ "$DOTNET_VERSION" == "" ]]; then
44+
unset DOTNET_VERSION
45+
fi
46+
fi
47+
48+
# Install by channel or version
49+
DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix"
50+
if [[ -z ${DOTNET_VERSION+x} ]]; then
51+
"$DOTNET_INSTALL_FILE" --install-dir "$DOTNET_DIRECTORY" --channel "$DOTNET_CHANNEL" --no-path
52+
else
53+
"$DOTNET_INSTALL_FILE" --install-dir "$DOTNET_DIRECTORY" --version "$DOTNET_VERSION" --no-path
54+
fi
55+
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"
56+
export PATH="$DOTNET_DIRECTORY:$PATH"
57+
fi
58+
59+
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"
60+
61+
if [[ ! -z ${NUKE_ENTERPRISE_TOKEN+x} && "$NUKE_ENTERPRISE_TOKEN" != "" ]]; then
62+
"$DOTNET_EXE" nuget remove source "nuke-enterprise" &>/dev/null || true
63+
"$DOTNET_EXE" nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password "$NUKE_ENTERPRISE_TOKEN" --store-password-in-clear-text &>/dev/null || true
64+
fi
65+
66+
"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
67+
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"

build/.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[*.cs]
2+
dotnet_style_qualification_for_field = false:warning
3+
dotnet_style_qualification_for_property = false:warning
4+
dotnet_style_qualification_for_method = false:warning
5+
dotnet_style_qualification_for_event = false:warning
6+
dotnet_style_require_accessibility_modifiers = never:warning
7+
8+
csharp_style_expression_bodied_methods = true:silent
9+
csharp_style_expression_bodied_properties = true:warning
10+
csharp_style_expression_bodied_indexers = true:warning
11+
csharp_style_expression_bodied_accessors = true:warning

build/Build.cs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Linq;
3+
using Nuke.Common;
4+
using Nuke.Common.CI;
5+
using Nuke.Common.CI.GitHubActions;
6+
using Nuke.Common.Execution;
7+
using Nuke.Common.Git;
8+
using Nuke.Common.IO;
9+
using Nuke.Common.ProjectModel;
10+
using Nuke.Common.Tooling;
11+
using Nuke.Common.Tools.DotNet;
12+
using Nuke.Common.Tools.GitHub;
13+
using Nuke.Common.Utilities.Collections;
14+
using Serilog;
15+
using static Nuke.Common.EnvironmentInfo;
16+
using static Nuke.Common.IO.FileSystemTasks;
17+
using static Nuke.Common.IO.PathConstruction;
18+
using static Nuke.Common.Tools.DotNet.DotNetTasks;
19+
20+
[GitHubActions(
21+
"continuous",
22+
GitHubActionsImage.UbuntuLatest,
23+
FetchDepth = 0,
24+
On = new[] { GitHubActionsTrigger.Push },
25+
PublishArtifacts = true,
26+
InvokedTargets = new[] { nameof(Compile), nameof(Pack) })]
27+
[GitHubActions(
28+
"release",
29+
GitHubActionsImage.UbuntuLatest,
30+
FetchDepth = 0,
31+
OnPushTags = new[] { @"\d+\.\d+\.\d+" },
32+
PublishArtifacts = true,
33+
InvokedTargets = new[] { nameof(Push), nameof(PushGithubNuget) },
34+
ImportSecrets = new[] { nameof(NuGetApiKey), nameof(PersonalAccessToken) })]
35+
class Build : NukeBuild
36+
{
37+
/// Support plugins are available for:
38+
/// - JetBrains ReSharper https://nuke.build/resharper
39+
/// - JetBrains Rider https://nuke.build/rider
40+
/// - Microsoft VisualStudio https://nuke.build/visualstudio
41+
/// - Microsoft VSCode https://nuke.build/vscode
42+
43+
public static int Main () => Execute<Build>(x => x.Compile);
44+
45+
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
46+
readonly Configuration Configuration = Configuration.Release;
47+
48+
49+
[Parameter] string NugetApiUrl = "https://api.nuget.org/v3/index.json"; //default
50+
51+
bool IsTag => GitHubActions.Instance?.Ref?.StartsWith("refs/tags/") ?? false;
52+
53+
[Parameter][Secret] readonly string NuGetApiKey;
54+
[Parameter][Secret] readonly string PersonalAccessToken;
55+
56+
[Solution] readonly Solution Solution;
57+
[GitRepository] readonly GitRepository GitRepository;
58+
59+
AbsolutePath SourceDirectory => RootDirectory / "src";
60+
AbsolutePath TestsDirectory => RootDirectory / "tests";
61+
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
62+
AbsolutePath PackagesDirectory => ArtifactsDirectory / "packages";
63+
64+
Target Clean => _ => _
65+
.Before(Restore)
66+
.Executes(() =>
67+
{
68+
SourceDirectory.GlobDirectories("*/bin", "*/obj").DeleteDirectories();
69+
TestsDirectory.GlobDirectories("*/bin", "*/obj").DeleteDirectories();
70+
ArtifactsDirectory.CreateOrCleanDirectory();
71+
});
72+
73+
Target Restore => _ => _
74+
.DependsOn(Clean)
75+
.Executes(() =>
76+
{
77+
DotNetRestore(s => s
78+
.SetProjectFile(Solution));
79+
});
80+
81+
Target Compile => _ => _
82+
.DependsOn(Restore)
83+
.Executes(() =>
84+
{
85+
DotNetBuild(s => s
86+
.SetProjectFile(Solution)
87+
.SetConfiguration(Configuration)
88+
.EnableNoRestore());
89+
});
90+
91+
Target Pack => _ => _
92+
.After(Compile)
93+
.Produces(PackagesDirectory / "*.nupkg")
94+
.Produces(PackagesDirectory / "*.snupkg")
95+
.Requires(() => Configuration.Equals(Configuration.Release))
96+
.Executes(() => {
97+
DotNetPack(_ => _
98+
.Apply(PackSettings));
99+
100+
ReportSummary(_ => _
101+
.AddPair("Packages", PackagesDirectory.GlobFiles("*.nupkg").Count.ToString()));
102+
});
103+
104+
Target Push => _ => _
105+
.DependsOn(Pack)
106+
.OnlyWhenStatic(() => IsTag && IsServerBuild)
107+
.Requires(() => NuGetApiKey)
108+
.Requires(() => Configuration.Equals(Configuration.Release))
109+
.Executes(() =>
110+
{
111+
Log.Information("Running push to packages directory.");
112+
113+
Assert.True(!string.IsNullOrEmpty(NuGetApiKey));
114+
115+
PackagesDirectory.GlobFiles("*.nupkg")
116+
.ForEach(x =>
117+
{
118+
x.NotNull();
119+
120+
DotNetNuGetPush(s => s
121+
.SetTargetPath(x)
122+
.SetSource(NugetApiUrl)
123+
.SetApiKey(NuGetApiKey)
124+
.EnableSkipDuplicate()
125+
);
126+
});
127+
128+
});
129+
130+
Target PushGithubNuget => _ => _
131+
.DependsOn(Pack)
132+
.OnlyWhenStatic(() => IsTag && IsServerBuild)
133+
.Requires(() => PersonalAccessToken)
134+
.Requires(() => Configuration.Equals(Configuration.Release))
135+
.Executes(() =>
136+
{
137+
Log.Information("Running push to packages directory.");
138+
139+
PackagesDirectory.GlobFiles("*.nupkg")
140+
.ForEach(x =>
141+
{
142+
x.NotNull();
143+
144+
Assert.True(!string.IsNullOrEmpty(PersonalAccessToken));
145+
146+
DotNetNuGetPush(s => s
147+
.SetTargetPath(x)
148+
.SetSource($"https://nuget.pkg.github.com/{GitHubActions.Instance.RepositoryOwner}/index.json")
149+
.SetApiKey(PersonalAccessToken)
150+
.EnableSkipDuplicate()
151+
);
152+
});
153+
});
154+
155+
Configure<DotNetPackSettings> PackSettings => _ => _
156+
.SetProject(Solution)
157+
.SetConfiguration(Configuration)
158+
.SetNoBuild(SucceededTargets.Contains(Compile))
159+
.SetOutputDirectory(PackagesDirectory);
160+
161+
}

build/Configuration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using Nuke.Common.Tooling;
5+
6+
[TypeConverter(typeof(TypeConverter<Configuration>))]
7+
public class Configuration : Enumeration
8+
{
9+
public static Configuration Debug = new Configuration { Value = nameof(Debug) };
10+
public static Configuration Release = new Configuration { Value = nameof(Release) };
11+
12+
public static implicit operator string(Configuration configuration)
13+
{
14+
return configuration.Value;
15+
}
16+
}

build/Directory.Build.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<!-- This file prevents unintended imports of unrelated MSBuild files -->
5+
<!-- Uncomment to include parent Directory.Build.props file -->
6+
<!--<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />-->
7+
8+
</Project>

build/Directory.Build.targets

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<!-- This file prevents unintended imports of unrelated MSBuild files -->
5+
<!-- Uncomment to include parent Directory.Build.targets file -->
6+
<!--<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />-->
7+
8+
</Project>

build/_build.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace></RootNamespace>
7+
<NoWarn>CS0649;CS0169;CA1050;CA1822;CA2211;IDE1006</NoWarn>
8+
<NukeRootDirectory>..</NukeRootDirectory>
9+
<NukeScriptDirectory>..</NukeScriptDirectory>
10+
<NukeTelemetryVersion>1</NukeTelemetryVersion>
11+
<IsPackable>false</IsPackable>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Nuke.Common" Version="8.1.2" />
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)