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

Users/hiyada/use dot net caching #11274

Merged
merged 5 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,28 @@ mockery.registerMock('azure-pipelines-task-lib/task', {

import { DotNetCoreVersionFetcher } from "../versionfetcher";
import { VersionInfo } from '../models';
let versionFetcher = new DotNetCoreVersionFetcher();
try {
let versionInfo = new VersionInfo(JSON.parse(`{"version":"2.2.104", "files": [{"name": "linux.tar.gz", "rid":"linux-x64", "url": "https://path.to/file.tar.gz"}, {"name": "osx.pkg", "rid":"osx-x64", "url": "https://path.to/file.pkg"}, {"name": "osx.tar.gz", "rid":"osx-x64", "url": "https://path.toMac/file.tar.gz"}, {"name": "win.exe", "rid":"win-x64", "url": "https://path.to/file.exe"}, {"name": "win.zip", "rid":"win-x64", "url": "https://path.to/file.zip"}]}`), "sdk");

// Test for windows
osType = "win";
let versionFetcher = new DotNetCoreVersionFetcher();
let downloadUrl = versionFetcher.getDownloadUrl(versionInfo);
if (downloadUrl != "https://path.to/file.zip") {
throw "";
}

// Test for linux
osType = "linux";
versionFetcher = new DotNetCoreVersionFetcher();
downloadUrl = versionFetcher.getDownloadUrl(versionInfo);
if (downloadUrl != "https://path.to/file.tar.gz") {
throw "";
}

// Test for mac os
osType = "osx";
versionFetcher = new DotNetCoreVersionFetcher();
downloadUrl = versionFetcher.getDownloadUrl(versionInfo);
if (downloadUrl != "https://path.toMac/file.tar.gz") {
throw "";
Expand Down
100 changes: 51 additions & 49 deletions Tasks/UseDotNetV2/versionfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import * as utils from "./versionutilities";

export class DotNetCoreVersionFetcher {
private explicitVersioning: boolean = false;
private channels: Channel[];
private httpCallbackClient: httpClient.HttpClient;
private machineOsSuffixes: string[];
constructor(explicitVersioning: boolean = false) {
this.explicitVersioning = explicitVersioning;
let proxyUrl: string = tl.getVariable("agent.proxyurl");
Expand Down Expand Up @@ -58,9 +61,9 @@ export class DotNetCoreVersionFetcher {
public getDownloadUrl(versionInfo: VersionInfo): string {
console.log(tl.loc("GettingDownloadUrl", versionInfo.getPackageType(), versionInfo.getVersion()));

let osSuffixes = this.detectMachineOS();
this.detectMachineOS();
let downloadPackageInfoObject: VersionFilesData = null;
osSuffixes.find((osSuffix) => {
this.machineOsSuffixes.find((osSuffix) => {
downloadPackageInfoObject = versionInfo.getFiles().find((downloadPackageInfo: VersionFilesData) => {
if (downloadPackageInfo.rid && osSuffix && downloadPackageInfo.rid.toLowerCase() == osSuffix.toLowerCase()) {
if ((osSuffix.split("-")[0] == "win" && downloadPackageInfo.name.endsWith(".zip")) || (osSuffix.split("-")[0] != "win" && downloadPackageInfo.name.endsWith("tar.gz"))) {
Expand All @@ -79,7 +82,7 @@ export class DotNetCoreVersionFetcher {
return downloadPackageInfoObject.url;
}

throw tl.loc("DownloadUrlForMatchingOsNotFound", versionInfo.getPackageType(), versionInfo.getVersion(), osSuffixes.toString());
throw tl.loc("DownloadUrlForMatchingOsNotFound", versionInfo.getPackageType(), versionInfo.getVersion(), this.machineOsSuffixes.toString());
}

private setReleasesIndex(): Promise<void> {
Expand Down Expand Up @@ -162,7 +165,7 @@ export class DotNetCoreVersionFetcher {
tl.debug(tl.loc("VersionInformationNotComplete", release[packageType].version, err));
}
}
if (release && release[packageType] && release[packageType].version && !versionInfoList.find((versionInfo) => { return versionInfo.getVersion() === release[packageType].version }) ) {
if (release && release[packageType] && release[packageType].version && !versionInfoList.find((versionInfo) => { return versionInfo.getVersion() === release[packageType].version })) {
try {
let versionInfo: VersionInfo = new VersionInfo(release[packageType], packageType);
versionInfoList.push(versionInfo);
Expand Down Expand Up @@ -223,58 +226,60 @@ export class DotNetCoreVersionFetcher {
return adjacentChannels;
}

private detectMachineOS(): string[] {
let osSuffix = [];
let scriptRunner: trm.ToolRunner;
private detectMachineOS(): void {
if (!this.machineOsSuffixes) {
let osSuffix = [];
let scriptRunner: trm.ToolRunner;

try {
console.log(tl.loc("DetectingPlatform"));
if (tl.osType().match(/^Win/)) {
let escapedScript = path.join(this.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(this.getCurrentDir(), 'externals', 'get-os-distro.sh');
this.setFileAttribute(scriptPath, "777");

try {
console.log(tl.loc("DetectingPlatform"));
if (tl.osType().match(/^Win/)) {
let escapedScript = path.join(this.getCurrentDir(), 'externals', 'get-os-platform.ps1').replace(/'/g, "''");
let command = `& '${escapedScript}'`
scriptRunner = tl.tool(tl.which(scriptPath, true));
}

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(this.getCurrentDir(), 'externals', 'get-os-distro.sh');
this.setFileAttribute(scriptPath, "777");
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));
catch (ex) {
throw tl.loc("FailedInDetectingMachineArch", JSON.stringify(ex));
}

if (osSuffix.length == 0) {
throw tl.loc("CouldNotDetectPlatform");
}
}
catch (ex) {
throw tl.loc("FailedInDetectingMachineArch", JSON.stringify(ex));
this.machineOsSuffixes = osSuffix;
}

return osSuffix;
}

private setFileAttribute(file: string, mode: string): void {
Expand All @@ -284,9 +289,6 @@ export class DotNetCoreVersionFetcher {
private getCurrentDir(): string {
return __dirname;
}

private channels: Channel[];
private httpCallbackClient: httpClient.HttpClient;
}

const DotNetCoreReleasesIndexUrl: string = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases-index.json";
const DotNetCoreReleasesIndexUrl: string = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases-index.json";