1
0
Fork 0
mirror of https://github.com/subosito/flutter-action.git synced 2024-08-16 10:19:50 +02:00
flutter/lib/installer.js

165 lines
5.9 KiB
JavaScript
Raw Normal View History

2019-08-13 12:24:25 +02:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
2019-08-13 12:46:10 +02:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2019-08-13 12:24:25 +02:00
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const io = __importStar(require("@actions/io"));
const tc = __importStar(require("@actions/tool-cache"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
2019-08-13 12:46:10 +02:00
const v4_1 = __importDefault(require("uuid/v4"));
const exec_1 = require("@actions/exec/lib/exec");
2019-08-13 12:24:25 +02:00
const IS_WINDOWS = process.platform === 'win32';
const IS_DARWIN = process.platform === 'darwin';
const IS_LINUX = process.platform === 'linux';
let tempDirectory = process.env['RUNNER_TEMP'] || '';
if (!tempDirectory) {
let baseLocation;
if (IS_WINDOWS) {
baseLocation = process.env['USERPROFILE'] || 'C:\\';
}
else {
if (process.platform === 'darwin') {
baseLocation = '/Users';
}
else {
baseLocation = '/home';
}
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
}
function getFlutter(version, channel) {
return __awaiter(this, void 0, void 0, function* () {
2019-08-14 03:04:15 +02:00
// make semver compatible, eg: 1.7.8+hotfix.4 -> 1.7.8-hotfix.4
const semver = version.replace('+', '-');
const cleanver = `${semver}-${channel}`;
let toolPath = tc.find('Flutter', cleanver);
2019-08-13 12:24:25 +02:00
if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`);
}
else {
core.debug('Downloading Flutter from Google storage');
const downloadInfo = getDownloadInfo(version, channel);
const sdkFile = yield tc.downloadTool(downloadInfo.url);
let tempDir = generateTempDir();
const sdkDir = yield extractDownload(sdkFile, tempDir);
core.debug(`Flutter sdk extracted to ${sdkDir}`);
2019-08-14 03:04:15 +02:00
toolPath = yield tc.cacheDir(sdkDir, 'Flutter', cleanver);
2019-08-13 12:24:25 +02:00
}
core.exportVariable('FLUTTER_HOME', toolPath);
core.addPath(path.join(toolPath, 'bin'));
});
}
exports.getFlutter = getFlutter;
function osName() {
if (IS_DARWIN)
return 'macos';
if (IS_WINDOWS)
return 'windows';
return process.platform;
}
function extName() {
if (IS_LINUX)
return 'tar.xz';
return 'zip';
}
function getDownloadInfo(version, channel) {
const os = osName();
const ext = extName();
2019-08-13 14:01:25 +02:00
const url = `https://storage.googleapis.com/flutter_infra/releases/${channel}/${os}/flutter_${os}_v${version}-${channel}.${ext}`;
2019-08-13 12:24:25 +02:00
return {
version,
url
};
}
function generateTempDir() {
return path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000));
}
function extractDownload(sdkFile, destDir) {
return __awaiter(this, void 0, void 0, function* () {
yield io.mkdirP(destDir);
const sdkPath = path.normalize(sdkFile);
const stats = fs.statSync(sdkPath);
if (stats.isFile()) {
yield extractFile(sdkFile, destDir);
const sdkDir = path.join(destDir, fs.readdirSync(destDir)[0]);
return sdkDir;
}
else {
throw new Error(`Flutter sdk argument ${sdkFile} is not a file`);
}
});
}
function extractFile(file, destDir) {
return __awaiter(this, void 0, void 0, function* () {
const stats = fs.statSync(file);
if (!stats) {
throw new Error(`Failed to extract ${file} - it doesn't exist`);
}
else if (stats.isDirectory()) {
throw new Error(`Failed to extract ${file} - it is a directory`);
}
2019-08-13 12:29:45 +02:00
if ('tar.xz' === extName()) {
2019-08-13 12:46:10 +02:00
yield extractTarXz(file, destDir);
2019-08-13 12:24:25 +02:00
}
else {
2019-08-13 13:52:19 +02:00
if (IS_DARWIN) {
yield extractZipDarwin(file, destDir);
}
else {
yield tc.extractZip(file, destDir);
}
2019-08-13 12:24:25 +02:00
}
});
}
2019-08-13 12:46:10 +02:00
/**
* Extract a tar.xz
*
* @param file path to the tar.xz
* @param dest destination directory. Optional.
* @returns path to the destination directory
*/
function extractTarXz(file, dest) {
return __awaiter(this, void 0, void 0, function* () {
if (!file) {
throw new Error("parameter 'file' is required");
}
dest = dest || (yield _createExtractFolder(dest));
const tarPath = yield io.which('tar', true);
yield exec_1.exec(`"${tarPath}"`, ['xC', dest, '-f', file]);
return dest;
});
}
exports.extractTarXz = extractTarXz;
function _createExtractFolder(dest) {
return __awaiter(this, void 0, void 0, function* () {
if (!dest) {
dest = path.join(tempDirectory, v4_1.default());
}
yield io.mkdirP(dest);
return dest;
});
}
2019-08-13 13:52:19 +02:00
function extractZipDarwin(file, dest) {
return __awaiter(this, void 0, void 0, function* () {
const unzipPath = path.join(__dirname, '..', 'scripts', 'externals', 'unzip-darwin');
yield exec_1.exec(`"${unzipPath}"`, [file], { cwd: dest });
});
}