setup-dotnet/src/installer.ts
2019-11-05 11:34:01 -08:00

94 lines
2.4 KiB
TypeScript

// Load tempDirectory before it gets wiped by tool-cache
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import httpClient = require('typed-rest-client/HttpClient');
import {chmodSync} from 'fs';
import * as path from 'path';
import * as semver from 'semver';
const IS_WINDOWS = process.platform === 'win32';
export class DotnetCoreInstaller {
constructor(version: string = "", jsonfile: string = "") {
if (semver.valid(semver.clean(version) || '') == null) {
throw 'Implicit version not permitted';
}
this.version = version;
this.jsonfile = jsonfile;
}
public async installDotnet() {
let output = '';
let resultCode = 0;
if (IS_WINDOWS) {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.ps1')
.replace(/'/g, "''");
let command = `& '${escapedScript}'`;
if (this.version) {
command += ` -Version ${this.version}`;
}
if (this.jsonfile) {
command += ` -jsonfile ${this.jsonfile}`;
}
const powershellPath = await io.which('powershell', true);
resultCode = await exec.exec(
`"${powershellPath}"`,
[
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
],
{
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
}
}
);
} else {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.sh')
.replace(/'/g, "''");
chmodSync(escapedScript, '777');
const scriptPath = await io.which(escapedScript, true);
let scriptArguments: string[] = [];
if (this.version) {
scriptArguments.concat(['--version', this.version]);
}
if (this.jsonfile) {
scriptArguments.concat(['--jsonfile', this.jsonfile]);
}
resultCode = await exec.exec(
`"${scriptPath}"`,
scriptArguments,
{
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
}
}
);
}
if (resultCode != 0) {
throw `Failed to install dotnet ${resultCode}. ${output}`;
}
}
private version: string;
private jsonfile: string;
}