Skip to content

Commit

Permalink
(build) Update to use lightweight bootstrapper with dotnet tool (cake…
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete authored Oct 8, 2021
1 parent 1023b18 commit 7ae4e06
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 219 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"cake.tool": {
"version": "1.3.0",
"commands": [
"dotnet-cake"
]
}
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
*.sh text eol=lf

###############################################################################
# Set default behavior for command prompt diff.
Expand Down
13 changes: 10 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#module nuget:?package=Cake.DotNetTool.Module&version=0.4.0
#tool "dotnet:https://api.nuget.org/v3/index.json?package=Wyam.Tool&version=2.2.9"
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Wyam&version=2.2.9"
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Yaml&version=3.1.1"
Expand Down Expand Up @@ -219,7 +218,11 @@ Task("Build")
{ "AssemblyFiles", assemblies },
{ "CakeLatestReleaseName", releaseInfo.LatestReleaseName },
{ "CakeLatestReleaseUrl", releaseInfo.LatestReleaseUrl },
}
},
EnvironmentVariables = new Dictionary<string, string>
{
{ "DOTNET_ROLL_FORWARD", "Major" },
},
});
});

Expand All @@ -243,7 +246,11 @@ Task("Preview")
{ "AssemblyFiles", assemblies },
{ "CakeLatestReleaseName", releaseInfo.LatestReleaseName },
{ "CakeLatestReleaseUrl", releaseInfo.LatestReleaseUrl },
}
},
EnvironmentVariables = new Dictionary<string, string>
{
{ "DOTNET_ROLL_FORWARD", "Major" },
},
});
});

Expand Down
13 changes: 11 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
@echo off
powershell -ExecutionPolicy Unrestricted ./build.ps1 %CAKE_ARGS% %*
@echo on
@cd %~dp0

set DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
set DOTNET_CLI_TELEMETRY_OPTOUT=1
set DOTNET_NOLOGO=1

dotnet tool restore
@if %ERRORLEVEL% neq 0 goto :eof

dotnet cake %*
3 changes: 0 additions & 3 deletions build.config

This file was deleted.

151 changes: 9 additions & 142 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,146 +1,13 @@
$DotNetInstallerUri = 'https://dot.net/v1/dotnet-install.ps1';
$DotNetUnixInstallerUri = 'https://dot.net/v1/dotnet-install.sh'
$DotNetChannel = 'LTS'
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$ErrorActionPreference = 'Stop'

[string] $CakeVersion = ''
[string] $DotNetVersion= ''
foreach($line in Get-Content "$PSScriptRoot\build.config")
{
if ($line -like 'CAKE_VERSION=*') {
$CakeVersion = $line.SubString(13)
}
elseif ($line -like 'DOTNET_VERSION=*') {
$DotNetVersion =$line.SubString(15)
}
}
Set-Location -LiteralPath $PSScriptRoot

$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = '1'
$env:DOTNET_CLI_TELEMETRY_OPTOUT = '1'
$env:DOTNET_NOLOGO = '1'

if ([string]::IsNullOrEmpty($CakeVersion) -or [string]::IsNullOrEmpty($DotNetVersion)) {
'Failed to parse Cake / .NET Core SDK Version'
exit 1
}
dotnet tool restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

# Make sure tools folder exists
$ToolPath = Join-Path $PSScriptRoot "tools"
if (!(Test-Path $ToolPath)) {
Write-Verbose "Creating tools directory..."
New-Item -Path $ToolPath -Type Directory -Force | out-null
}


if ($PSVersionTable.PSEdition -ne 'Core') {
# Attempt to set highest encryption available for SecurityProtocol.
# PowerShell will not set this by default (until maybe .NET 4.6.x). This
# will typically produce a message for PowerShell v2 (just an info
# message though)
try {
# Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
# Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
# exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
# installed (.NET 4.5 is an in-place upgrade).
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
} catch {
Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3'
}
}

###########################################################################
# INSTALL .NET CORE CLI
###########################################################################

Function Remove-PathVariable([string]$VariableToRemove)
{
$SplitChar = ';'
if ($IsMacOS -or $IsLinux) {
$SplitChar = ':'
}

$path = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($path -ne $null)
{
$newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "User")
}

$path = [Environment]::GetEnvironmentVariable("PATH", "Process")
if ($path -ne $null)
{
$newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "Process")
}
}

# Get .NET Core CLI path if installed.
$FoundDotNetCliVersion = $null;
if (Get-Command dotnet -ErrorAction SilentlyContinue) {
$FoundDotNetCliVersion = dotnet --version;
}

if($FoundDotNetCliVersion -ne $DotNetVersion) {
$InstallPath = Join-Path $PSScriptRoot ".dotnet"
if (!(Test-Path $InstallPath)) {
New-Item -Path $InstallPath -ItemType Directory -Force | Out-Null;
}

if ($IsMacOS -or $IsLinux) {
(New-Object System.Net.WebClient).DownloadFile($DotNetUnixInstallerUri, "$InstallPath\dotnet-install.sh");
& bash $InstallPath\dotnet-install.sh --version "$DotNetVersion" --install-dir "$InstallPath" --channel "$DotNetChannel" --no-path
}
else {
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, "$InstallPath\dotnet-install.ps1");
& $InstallPath\dotnet-install.ps1 -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;
}

Remove-PathVariable "$InstallPath"
$env:PATH = "$InstallPath;$env:PATH"
}

$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
$env:DOTNET_CLI_TELEMETRY_OPTOUT=1
$env:DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2


###########################################################################
# INSTALL CAKE
###########################################################################

# Make sure Cake has been installed.
[string] $CakeExePath = ''
[string] $CakeInstalledVersion = Get-Command dotnet-cake -ErrorAction SilentlyContinue | % {&$_.Source --version}

if ($CakeInstalledVersion -eq $CakeVersion) {
# Cake found locally
$CakeExePath = (Get-Command dotnet-cake).Source
}
else {
$CakePath = Join-Path $ToolPath ".store\cake.tool\$CakeVersion"
$CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)


if ((!(Test-Path -Path $CakePath -PathType Container)) -or (!(Test-Path $CakeExePath -PathType Leaf))) {

if ((![string]::IsNullOrEmpty($CakeExePath)) -and (Test-Path $CakeExePath -PathType Leaf))
{
& dotnet tool uninstall --tool-path $ToolPath Cake.Tool
}

& dotnet tool install --tool-path $ToolPath --version $CakeVersion Cake.Tool
if ($LASTEXITCODE -ne 0)
{
'Failed to install cake'
exit 1
}
$CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)
}
}

###########################################################################
# RUN BUILD SCRIPT
###########################################################################
& "$CakeExePath" ./build.cake --bootstrap
if ($LASTEXITCODE -eq 0)
{
& "$CakeExePath" ./build.cake $args
}
exit $LASTEXITCODE
dotnet cake @args
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
72 changes: 5 additions & 67 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,74 +1,12 @@
#!/usr/bin/env bash
# Define varibles
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $SCRIPT_DIR/build.config
TOOLS_DIR=$SCRIPT_DIR/tools
CAKE_EXE=$TOOLS_DIR/dotnet-cake
CAKE_PATH=$TOOLS_DIR/.store/cake.tool/$CAKE_VERSION
set -euox pipefail

if [ "$CAKE_VERSION" = "" ] || [ "$DOTNET_VERSION" = "" ]; then
echo "An error occured while parsing Cake / .NET Core SDK version."
exit 1
fi

# Make sure the tools folder exist.
if [ ! -d "$TOOLS_DIR" ]; then
mkdir "$TOOLS_DIR"
fi

###########################################################################
# INSTALL .NET CORE CLI
###########################################################################

DOTNET_INSTALLED_VERSION=$(dotnet --version 2>&1)

if [ "$DOTNET_VERSION" != "$DOTNET_INSTALLED_VERSION" ]; then
echo "Installing .NET CLI..."
if [ ! -d "$SCRIPT_DIR/.dotnet" ]; then
mkdir "$SCRIPT_DIR/.dotnet"
fi
curl -Lsfo "$SCRIPT_DIR/.dotnet/dotnet-install.sh" https://dot.net/v1/dotnet-install.sh
bash "$SCRIPT_DIR/.dotnet/dotnet-install.sh" --version $DOTNET_VERSION --install-dir .dotnet --no-path
export PATH="$SCRIPT_DIR/.dotnet":$PATH
fi
cd "$(dirname "${BASH_SOURCE[0]}")"

export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER=0
export DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2

###########################################################################
# INSTALL CAKE
###########################################################################

CAKE_INSTALLED_VERSION=$(dotnet-cake --version 2>&1)

if [ "$CAKE_VERSION" != "$CAKE_INSTALLED_VERSION" ]; then
if [ ! -f "$CAKE_EXE" ] || [ ! -d "$CAKE_PATH" ]; then
if [ -f "$CAKE_EXE" ]; then
dotnet tool uninstall --tool-path $TOOLS_DIR Cake.Tool
fi

echo "Installing Cake $CAKE_VERSION..."
dotnet tool install --tool-path $TOOLS_DIR --version $CAKE_VERSION Cake.Tool
if [ $? -ne 0 ]; then
echo "An error occured while installing Cake."
exit 1
fi
fi

# Make sure that Cake has been installed.
if [ ! -f "$CAKE_EXE" ]; then
echo "Could not find Cake.exe at '$CAKE_EXE'."
exit 1
fi
else
CAKE_EXE="dotnet-cake"
fi
export DOTNET_NOLOGO=1

###########################################################################
# RUN BUILD SCRIPT
###########################################################################
dotnet tool restore

# Start Cake
(exec "$CAKE_EXE" build.cake --bootstrap) && (exec "$CAKE_EXE" build.cake "$@")
dotnet cake "$@"
12 changes: 10 additions & 2 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Cake MyGet" value="https://www.myget.org/F/cake/api/v3/index.json" />
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="cake-myget.org" value="https://www.myget.org/F/cake/api/v3/index.json" />
</packageSources>
</configuration>
<disabledPackageSources>
<clear />
</disabledPackageSources>
<fallbackPackageFolders>
<clear />
</fallbackPackageFolders>
</configuration>

0 comments on commit 7ae4e06

Please sign in to comment.