Skip to content

Commit

Permalink
Change build scripts to download .NET Core SDK only if local installa…
Browse files Browse the repository at this point in the history
…tion is missing or doesn't match expected version.
  • Loading branch information
matkoch committed Jun 5, 2018
1 parent 86660de commit 797dcfb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 44 deletions.
30 changes: 21 additions & 9 deletions bootstrapping/build.netcore.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[CmdletBinding()]
Param(
[switch]$Local,
#[switch]$CustomParam,
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
[string[]]$BuildArguments
)
Expand All @@ -17,8 +17,9 @@ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$BuildProjectFile = "$PSScriptRoot\_BUILD_DIRECTORY_NAME_\_BUILD_PROJECT_NAME_.csproj"
$TempDirectory = "$PSScriptRoot\_ROOT_DIRECTORY_\.tmp"

$DotNetChannel = "2.0"
$DotNetScriptUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1"
$DotNetGlobalFile = "$PSScriptRoot\_ROOT_DIRECTORY_\global.json"
$DotNetInstallUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1"
$DotNetReleasesUrl = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json"

$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
$env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
Expand All @@ -33,21 +34,32 @@ function ExecSafe([scriptblock] $cmd) {
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if (-not $Local -and (Get-Command "dotnet" -ErrorAction SilentlyContinue) -ne $null) {
# If global.json exists, load expected version
if (Test-Path $DotNetGlobalFile) {
$DotNetVersion = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json).sdk.version
}

# If dotnet is installed locally, and expected version is not set or installation matches the expected version
if ((Get-Command "dotnet" -ErrorAction SilentlyContinue) -ne $null -and `
(!(Test-Path variable:DotNetVersion) -or $(& dotnet --version) -eq $DotNetVersion)) {
$env:DOTNET_EXE = (Get-Command "dotnet").Path
}
else {
$DotNetDirectory = "$TempDirectory\dotnet-win"
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"

$DotNetScriptFile = "$TempDirectory\dotnet-install.ps1"
if (!(Test-Path $DotNetScriptFile)) {
md -force $TempDirectory > $null
(New-Object System.Net.WebClient).DownloadFile($DotNetScriptUrl, $DotNetScriptFile)
# If expected version is not set, get latest version
if (!(Test-Path variable:DotNetVersion)) {
$DotNetVersion = $(Invoke-WebRequest -UseBasicParsing $DotNetReleasesUrl | ConvertFrom-Json)[0]."version-sdk"
}

ExecSafe { & $DotNetScriptFile -InstallDir $DotNetDirectory -Channel $DotNetChannel -NoPath }
# Download and execute install script
$DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
md -force $TempDirectory > $null
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile)
ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath }
}

Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"

ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile -- $args }
41 changes: 28 additions & 13 deletions bootstrapping/build.netcore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

echo $(bash --version 2>&1 | head -n 1)

LOCAL=0
#CUSTOMPARAM=0
BUILD_ARGUMENTS=()
for i in "$@"; do
case $(echo $1 | awk '{print tolower($0)}') in
-local) LOCAL=1;;
# -custom-param) CUSTOMPARAM=1;;
*) BUILD_ARGUMENTS+=("$1") ;;
esac
shift
Expand All @@ -22,8 +22,9 @@ SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
BUILD_PROJECT_FILE="$SCRIPT_DIR/_BUILD_DIRECTORY_NAME_/_BUILD_PROJECT_NAME_.csproj"
TEMP_DIRECTORY="$SCRIPT_DIR/_ROOT_DIRECTORY_/.tmp"

DOTNET_CHANNEL="2.0"
DOTNET_SCRIPT_URL="https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.sh"
DOTNET_GLOBAL_FILE="$SCRIPT_DIR/_ROOT_DIRECTORY_/global.json"
DOTNET_INSTALL_URL="https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.sh"
DOTNET_RELEASES_URL="https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json"

export DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
Expand All @@ -33,21 +34,35 @@ export NUGET_XMLDOC_MODE="skip"
# EXECUTION
###########################################################################

if ! ((LOCAL)) && [ -x "$(command -v dotnet)" ]; then
function FirstJsonValue {
perl -nle 'print $1 if m{"'$1'": "([^"\-]+)",?}' <<< ${@:2}
}

# If global.json exists, load expected version
if [ -f "$DOTNET_GLOBAL_FILE" ]; then
DOTNET_VERSION=$(FirstJsonValue "version" $(cat "$DOTNET_GLOBAL_FILE"))
fi

# If dotnet is installed locally, and expected version is not set or installation matches the expected version
if [[ -x "$(command -v dotnet)" && (-z ${DOTNET_VERSION+x} || $(dotnet --version) == "$DOTNET_VERSION") ]]; then
export DOTNET_EXE="$(command -v dotnet)"
else
DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix"
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"

DOTNET_SCRIPT_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
if [ ! -f "$DOTNET_SCRIPT_FILE" ]; then
mkdir -p "$TEMP_DIRECTORY"
curl -Lsfo "$DOTNET_SCRIPT_FILE" "$DOTNET_SCRIPT_URL"

# If expected version is not set, get latest version
if [ -z ${DOTNET_VERSION+x} ]; then
DOTNET_VERSION=$(FirstJsonValue "version-sdk" $(curl -s "$DOTNET_RELEASES_URL"))
fi

chmod +x "$DOTNET_SCRIPT_FILE"
"$DOTNET_SCRIPT_FILE" --install-dir "$DOTNET_DIRECTORY" --channel "$DOTNET_CHANNEL" --no-path

# Download and execute install script
DOTNET_INSTALL_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
mkdir -p "$TEMP_DIRECTORY"
curl -Lsfo "$DOTNET_INSTALL_FILE" "$DOTNET_INSTALL_URL"
chmod +x "$DOTNET_INSTALL_FILE"
"$DOTNET_INSTALL_FILE" --install-dir "$DOTNET_DIRECTORY" --version "$DOTNET_VERSION" --no-path
fi

echo "Microsoft (R) .NET Core SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" -- ${BUILD_ARGUMENTS[@]}
31 changes: 21 additions & 10 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[CmdletBinding()]
Param(
[switch]$Local,
[switch]$RefExt,
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
[string[]]$BuildArguments
Expand All @@ -18,8 +17,9 @@ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$BuildProjectFile = "$PSScriptRoot\build\.build.csproj"
$TempDirectory = "$PSScriptRoot\.tmp"

$DotNetChannel = "2.0"
$DotNetScriptUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1"
$DotNetGlobalFile = "$PSScriptRoot\global.json"
$DotNetInstallUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1"
$DotNetReleasesUrl = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json"

$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
$env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
Expand All @@ -34,21 +34,32 @@ function ExecSafe([scriptblock] $cmd) {
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if ((-not $Local) -and (Get-Command "dotnet" -ErrorAction SilentlyContinue) -ne $null) {
# If global.json exists, load expected version
if (Test-Path $DotNetGlobalFile) {
$DotNetVersion = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json).sdk.version
}

# If dotnet is installed locally, and expected version is not set or installation matches the expected version
if ((Get-Command "dotnet" -ErrorAction SilentlyContinue) -ne $null -and `
(!(Test-Path variable:DotNetVersion) -or $(& dotnet --version) -eq $DotNetVersion)) {
$env:DOTNET_EXE = (Get-Command "dotnet").Path
}
else {
$DotNetDirectory = "$TempDirectory\dotnet-win"
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"

$DotNetScriptFile = "$TempDirectory\dotnet-install.ps1"
if (!(Test-Path $DotNetScriptFile)) {
md -force $TempDirectory > $null
(New-Object System.Net.WebClient).DownloadFile($DotNetScriptUrl, $DotNetScriptFile)
# If expected version is not set, get latest version
if (!(Test-Path variable:DotNetVersion)) {
$DotNetVersion = $(Invoke-WebRequest -UseBasicParsing $DotNetReleasesUrl | ConvertFrom-Json)[0]."version-sdk"
}

ExecSafe { & $DotNetScriptFile -InstallDir $DotNetDirectory -Channel $DotNetChannel -NoPath }

# Download and execute install script
$DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
md -force $TempDirectory > $null
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile)
ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath }
}

Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"

ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /p:ReferenceExternal=$RefExt }
Expand Down
35 changes: 24 additions & 11 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

echo $(bash --version 2>&1 | head -n 1)

LOCAL=0
REFEXT="False"
BUILD_ARGUMENTS=()
for i in "$@"; do
case $(echo $1 | awk '{print tolower($0)}') in
-local) LOCAL=1;;
-refext) REFEXT="True";;
*) BUILD_ARGUMENTS+=("$1") ;;
esac
Expand All @@ -24,8 +22,9 @@ SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
BUILD_PROJECT_FILE="$SCRIPT_DIR/build/.build.csproj"
TEMP_DIRECTORY="$SCRIPT_DIR/.tmp"

DOTNET_CHANNEL="2.0"
DOTNET_SCRIPT_URL="https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.sh"
DOTNET_GLOBAL_FILE="$SCRIPT_DIR/global.json"
DOTNET_INSTALL_URL="https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.sh"
DOTNET_RELEASES_URL="https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json"

export DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
Expand All @@ -37,21 +36,35 @@ export FrameworkPathOverride=$(dirname $(which mono))/../lib/mono/4.6.1-api/
# EXECUTION
###########################################################################

if ! ((LOCAL)) && [ -x "$(command -v dotnet)" ]; then
function FirstJsonValue {
perl -nle 'print $1 if m{"'$1'": "([^"\-]+)",?}' <<< ${@:2}
}

# If global.json exists, load expected version
if [ -f "$DOTNET_GLOBAL_FILE" ]; then
DOTNET_VERSION=$(FirstJsonValue "version" $(cat "$DOTNET_GLOBAL_FILE"))
fi

# If dotnet is installed locally, and expected version is not set or installation matches the expected version
if [[ -x "$(command -v dotnet)" && (-z ${DOTNET_VERSION+x} || $(dotnet --version) == "$DOTNET_VERSION") ]]; then
export DOTNET_EXE="$(command -v dotnet)"
else
DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix"
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"

DOTNET_SCRIPT_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
if [ ! -f "$DOTNET_SCRIPT_FILE" ]; then
mkdir -p "$TEMP_DIRECTORY"
curl -Lsfo "$DOTNET_SCRIPT_FILE" "$DOTNET_SCRIPT_URL"
# If expected version is not set, get latest version
if [ -z ${DOTNET_VERSION+x} ]; then
DOTNET_VERSION=$(FirstJsonValue "version-sdk" $(curl -s "$DOTNET_RELEASES_URL"))
fi

chmod +x "$DOTNET_SCRIPT_FILE"
"$DOTNET_SCRIPT_FILE" --install-dir "$DOTNET_DIRECTORY" --channel "$DOTNET_CHANNEL" --no-path
# Download and execute install script
DOTNET_INSTALL_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
mkdir -p "$TEMP_DIRECTORY"
curl -Lsfo "$DOTNET_INSTALL_FILE" "$DOTNET_INSTALL_URL"
chmod +x "$DOTNET_INSTALL_FILE"
"$DOTNET_INSTALL_FILE" --install-dir "$DOTNET_DIRECTORY" --version "$DOTNET_VERSION" --no-path
fi

echo "Microsoft (R) .NET Core SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /p:"ReferenceExternal=$REFEXT"
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.1.200"
"version": "2.1.300"
}
}

0 comments on commit 797dcfb

Please sign in to comment.