Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable dotnet core installer to work for any architecture #8721

Merged
merged 8 commits into from
Dec 6, 2018
6 changes: 5 additions & 1 deletion Tasks/DotNetCoreInstallerV0/Tests/InstallWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"C:\\somedir\\powershell.exe -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command & 'C:\\currDir\\externals\\install-dotnet.ps1' -Version 1.0.4 -DryRun -SharedRuntime": {
"code": 0,
"stdout": "dotnet-install: Payload URLs:" + os.EOL + "dotnet-install: Primary - https://primary-runtime-url" + os.EOL + "dotnet-install: Legacy - https://legacy-runtime-url" + os.EOL + "dotnet-install: Repeatable invocation: .\install-dotnet.ps1 -Version 1.1.2 -Channel 1.1 -Architecture x64 -InstallDir <auto>"
},
"C:\\somedir\\powershell.exe -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command & 'C:\\currDir\\externals\\get-os-platform.ps1'": {
"code": 0,
"stdout": "Primary: win-x64" + os.EOL,
}
},
"osType": {
Expand All @@ -37,7 +41,7 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{

var ut = require('../utilities');
tr.registerMock('./utilities', {
getCurrentDir : function() {
getCurrentDir: function () {
return "C:\\currDir";
},
setFileAttribute: ut.setFileAttribute
Expand Down
61 changes: 35 additions & 26 deletions Tasks/DotNetCoreInstallerV0/dotnetcoreinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class DotnetCoreInstaller {
public async install() {
// Check cache
let toolPath: string;
let osSuffixes = this.detectMachineOS();
let parts = osSuffixes[0].split("-");
this.arch = parts.length > 1 ? parts[1] : "x64";
toolPath = this.getLocalTool();

if (!toolPath) {
// download, extract, cache
console.log(tl.loc("InstallingAfresh"));
let osSuffixes = this.detectMachineOS();
console.log(tl.loc("GettingDownloadUrl", this.packageType, this.version));
let downloadUrls = await DotNetCoreReleaseFetcher.getDownloadUrls(osSuffixes, this.version, this.packageType);
toolPath = await this.downloadAndInstall(downloadUrls);
Expand Down Expand Up @@ -57,46 +59,52 @@ class DotnetCoreInstaller {

private getLocalTool(): string {
console.log(tl.loc("CheckingToolCache"));
return toolLib.findLocalTool(this.cachedToolName, this.version);
return toolLib.findLocalTool(this.cachedToolName, this.version, this.arch);
}

private detectMachineOS(): string[] {
let osSuffix = [];
let scriptRunner: trm.ToolRunner;

if (tl.osType().match(/^Win/)) {
let primary = "win-" + os.arch();
osSuffix.push(primary);
console.log(tl.loc("PrimaryPlatform", primary));
let escapedScript = path.join(utilities.getCurrentDir(), 'externals', 'get-os-platform.ps1').replace(/'/g, "''");
let command = `& '${escapedScript}'`

let powershellPath = tl.which('powershell', true);
scriptRunner = tl.tool(powershellPath)
.line('-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command')
.arg(command);
}
else {
let scriptPath = path.join(utilities.getCurrentDir(), 'externals', 'get-os-distro.sh');
utilities.setFileAttribute(scriptPath, "777");

let scriptRunner: trm.ToolRunner = tl.tool(tl.which(scriptPath, true));
let result: trm.IExecSyncResult = scriptRunner.execSync();
scriptRunner = tl.tool(tl.which(scriptPath, true));
}

if (result.code != 0) {
throw tl.loc("getMachinePlatformFailed", result.error ? result.error.message : result.stderr);
}
let result: trm.IExecSyncResult = scriptRunner.execSync();

let output: string = result.stdout;
if (result.code != 0) {
throw tl.loc("getMachinePlatformFailed", result.error ? result.error.message : result.stderr);
}

let index;
if ((index = output.indexOf("Primary:")) >= 0) {
let primary = output.substr(index + "Primary:".length).split(os.EOL)[0];
osSuffix.push(primary);
console.log(tl.loc("PrimaryPlatform", primary));
}
let output: string = result.stdout;

if ((index = output.indexOf("Legacy:")) >= 0) {
let legacy = output.substr(index + "Legacy:".length).split(os.EOL)[0];
osSuffix.push(legacy);
console.log(tl.loc("LegacyPlatform", legacy));
}
let index;
if ((index = output.indexOf("Primary:")) >= 0) {
let primary = output.substr(index + "Primary:".length).split(os.EOL)[0];
osSuffix.push(primary);
console.log(tl.loc("PrimaryPlatform", primary));
}

if (osSuffix.length == 0) {
throw tl.loc("CouldNotDetectPlatform");
}
if ((index = output.indexOf("Legacy:")) >= 0) {
let legacy = output.substr(index + "Legacy:".length).split(os.EOL)[0];
osSuffix.push(legacy);
console.log(tl.loc("LegacyPlatform", legacy));
}

if (osSuffix.length == 0) {
throw tl.loc("CouldNotDetectPlatform");
}

return osSuffix;
Expand Down Expand Up @@ -125,14 +133,15 @@ class DotnetCoreInstaller {

// cache tool
console.log(tl.loc("CachingTool"));
let cachedDir = await toolLib.cacheDir(extPath, this.cachedToolName, this.version);
let cachedDir = await toolLib.cacheDir(extPath, this.cachedToolName, this.version, this.arch);
console.log(tl.loc("SuccessfullyInstalled", this.packageType, this.version));
return cachedDir;
}

private packageType: string;
private version: string;
private cachedToolName: string;
private arch: string;
}

async function run() {
Expand Down
41 changes: 33 additions & 8 deletions Tasks/DotNetCoreInstallerV0/externals/get-os-distro.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ get_current_os_name() {

local uname=$(uname)
if [ "$uname" = "Darwin" ]; then
echo "mac-x64"
echo "mac"
return 0
elif [ "$uname" = "Linux" ]; then
local linux_platform_name
linux_platform_name="$(get_linux_platform_name)" || { echo "linux-x64" && return 0 ; }
linux_platform_name="$(get_linux_platform_name)" || { echo "linux" && return 0 ; }

if [[ $linux_platform_name == "rhel.6" ]]; then
echo "$linux_platform_name-x64"
echo "$linux_platform_name"
return 0
elif [[ $linux_platform_name == alpine* ]]; then
echo "linux-musl-x64"
echo "linux-musl"
return 0
else
echo "linux-x64"
echo "linux"
return 0
fi
fi
Expand Down Expand Up @@ -156,12 +156,37 @@ get_legacy_os_name() {
return 1
}

primaryName=$(get_current_os_name || echo "")
legacyName=$(get_legacy_os_name || echo "")
get_machine_architecture() {

if command -v uname > /dev/null; then
CPUName=$(uname -m)
bishal-pdMSFT marked this conversation as resolved.
Show resolved Hide resolved
case $CPUName in
armv7l)
echo "arm"
return 0
;;
aarch64)
echo "arm64"
return 0
;;
esac
fi

# Always default to 'x64'
echo "x64"
bishal-pdMSFT marked this conversation as resolved.
Show resolved Hide resolved
return 0
}

osName=$(get_current_os_name || echo "")
legacyOsName=$(get_legacy_os_name || echo "")
arch=$(get_machine_architecture || echo "")

primaryName="$osName-$arch"
legacyName="$legacyOsName"

echo "Primary:$primaryName"
echo "Legacy:$legacyName"

if [ -z "$primaryName" ] && [ -z "$legacyName" ];then
if [ -z "$osName" ] && [ -z "$legacyOsName" ];then
exit 1
fi
18 changes: 18 additions & 0 deletions Tasks/DotNetCoreInstallerV0/externals/get-os-platform.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function Get-Machine-Architecture()
{
# possible values: AMD64, IA64, x86
return $ENV:PROCESSOR_ARCHITECTURE
sachinma marked this conversation as resolved.
Show resolved Hide resolved
sachinma marked this conversation as resolved.
Show resolved Hide resolved
bishal-pdMSFT marked this conversation as resolved.
Show resolved Hide resolved
}

function Get-CLIArchitecture-From-Architecture([string]$Architecture)
{
switch ($Architecture.ToLower())
{
{ ($_ -eq "amd64") -or ($_ -eq "x64") } { return "x64" }
{ $_ -eq "x86" } { return "x86" }
default { throw "Architecture not supported. If you think this is a bug, please report it at https://github.com/dotnet/cli/issues" }
}
}

$CLIArchitecture = Get-CLIArchitecture-From-Architecture $(Get-Machine-Architecture)
Write-Output "Primary:win-$CLIArchitecture"
4 changes: 2 additions & 2 deletions Tasks/DotNetCoreInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 20
"Minor": 2,
"Patch": 0
},
"satisfies": [
"DotNetCore"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/DotNetCoreInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 20
"Minor": 2,
"Patch": 0
},
"satisfies": [
"DotNetCore"
Expand Down