Files
flutter-action/src/installer.ts
T

120 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-08-13 17:11:30 +07:00
import * as core from '@actions/core';
import * as io from '@actions/io';
2020-10-08 04:36:23 +00:00
import * as exec from '@actions/exec';
2019-08-13 17:11:30 +07:00
import * as tc from '@actions/tool-cache';
import * as fs from 'fs';
import * as path from 'path';
2020-10-08 01:22:27 +00:00
import * as release from './release';
2019-08-13 17:11:30 +07:00
export async function getFlutter(
version: string,
channel: string
): Promise<void> {
2020-10-08 01:22:27 +00:00
const platform = release.getPlatform();
2020-10-08 04:36:23 +00:00
const useMaster = channel == 'master';
2019-08-18 21:23:37 +07:00
2020-11-23 16:11:57 +03:00
const {
version: selected,
downloadUrl,
channel: validatedChannel
} = await release.determineVersion(
2020-04-14 01:14:42 +00:00
version,
2020-10-08 04:36:23 +00:00
useMaster ? 'dev' : channel,
2020-10-08 01:22:27 +00:00
platform
2020-04-14 01:14:42 +00:00
);
2019-08-16 18:30:17 +07:00
2020-11-23 16:11:57 +03:00
if (channel !== validatedChannel) {
core.debug(`Channel was identifyed as ${validatedChannel}`);
}
2020-10-08 04:36:23 +00:00
let cleanver = useMaster
2020-11-23 16:11:57 +03:00
? validatedChannel
: `${selected.replace('+', '-')}-${validatedChannel}`;
2020-10-08 04:36:23 +00:00
2019-08-15 12:44:24 +07:00
let toolPath = tc.find('flutter', cleanver);
2019-08-13 17:11:30 +07:00
if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`);
} else {
2020-10-08 01:22:27 +00:00
core.debug(`Downloading Flutter from Google storage ${downloadUrl}`);
2019-08-13 17:11:30 +07:00
2020-04-14 01:14:42 +00:00
const sdkFile = await tc.downloadTool(downloadUrl);
2020-10-08 01:22:27 +00:00
const sdkCache = await tmpDir(platform);
const sdkDir = await extract(sdkFile, sdkCache, path.basename(downloadUrl));
2019-08-13 17:11:30 +07:00
2019-08-15 12:44:24 +07:00
toolPath = await tc.cacheDir(sdkDir, 'flutter', cleanver);
2019-08-13 17:11:30 +07:00
}
2020-11-19 20:31:28 +05:30
core.exportVariable('FLUTTER_ROOT', toolPath);
2019-08-13 17:11:30 +07:00
core.addPath(path.join(toolPath, 'bin'));
2020-04-13 14:41:49 -07:00
core.addPath(path.join(toolPath, 'bin', 'cache', 'dart-sdk', 'bin'));
2020-10-08 04:36:23 +00:00
if (useMaster) {
await exec.exec('flutter', ['channel', 'master']);
await exec.exec('flutter', ['upgrade']);
}
2019-08-13 17:11:30 +07:00
}
2020-10-08 01:22:27 +00:00
function tmpBaseDir(platform: string): string {
let tempDirectory = process.env['RUNNER_TEMP'] || '';
2019-08-13 17:11:30 +07:00
2020-10-08 01:22:27 +00:00
if (tempDirectory) {
return tempDirectory;
}
let baseLocation;
switch (platform) {
case 'windows':
baseLocation = process.env['USERPROFILE'] || 'C:\\';
break;
case 'macos':
baseLocation = '/Users';
break;
default:
baseLocation = '/home';
break;
}
return path.join(baseLocation, 'actions', 'temp');
2019-08-13 17:11:30 +07:00
}
2020-10-08 01:22:27 +00:00
async function tmpDir(platform: string): Promise<string> {
const baseDir = tmpBaseDir(platform);
const tempDir = path.join(
baseDir,
2019-08-13 17:11:30 +07:00
'temp_' + Math.floor(Math.random() * 2000000000)
);
2020-10-08 01:22:27 +00:00
await io.mkdirP(tempDir);
return tempDir;
2019-08-13 17:11:30 +07:00
}
2020-10-08 01:22:27 +00:00
async function extract(
2019-08-13 17:11:30 +07:00
sdkFile: string,
2020-10-08 01:22:27 +00:00
sdkCache: string,
originalFilename: string
2019-08-13 17:11:30 +07:00
): Promise<string> {
2020-10-08 01:22:27 +00:00
const fileStats = fs.statSync(path.normalize(sdkFile));
2019-08-13 17:11:30 +07:00
2020-10-08 01:22:27 +00:00
if (fileStats.isFile()) {
const stats = fs.statSync(sdkFile);
2019-08-13 17:11:30 +07:00
2020-10-08 01:22:27 +00:00
if (!stats) {
throw new Error(`Failed to extract ${sdkFile} - it doesn't exist`);
} else if (stats.isDirectory()) {
throw new Error(`Failed to extract ${sdkFile} - it is a directory`);
}
2019-08-13 17:11:30 +07:00
2020-10-08 01:22:27 +00:00
if (originalFilename.endsWith('tar.xz')) {
await tc.extractTar(sdkFile, sdkCache, 'x');
} else {
await tc.extractZip(sdkFile, sdkCache);
}
2019-08-13 17:11:30 +07:00
2020-10-08 01:22:27 +00:00
return path.join(sdkCache, fs.readdirSync(sdkCache)[0]);
2019-08-13 17:11:30 +07:00
} else {
throw new Error(`Flutter sdk argument ${sdkFile} is not a file`);
}
}