Use ghc/cabal where they are installed (no toolcache)

This commit is contained in:
Timothy Clem
2019-09-30 10:01:13 -07:00
parent d0615c7f8e
commit 11998570ec
3 changed files with 59 additions and 87 deletions

View File

@@ -1,54 +1,46 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
export async function cacheHaskellTool(installDir: string, tool: string) {
const baseGHCDir: string = path.join(installDir, tool);
const versions: string[] = fs.readdirSync(baseGHCDir);
for (const v of versions) {
const version = normalizeVersion(v);
core.debug(`found ${tool} version: ${v} normalized to ${version}`);
const dir: string = path.join(baseGHCDir, v);
await tc.cacheDir(dir, tool, version);
export async function findHaskellGHCVersion(
baseInstallDir: string,
version: string
) {
return _findHaskellToolVersion(baseInstallDir, 'ghc', version);
}
export async function findHaskellCabalVersion(
baseInstallDir: string,
version: string
) {
return _findHaskellToolVersion(baseInstallDir, 'cabal', version);
}
export async function _findHaskellToolVersion(
baseInstallDir: string,
tool: string,
version: string
) {
if (!baseInstallDir) {
throw new Error('baseInstallDir parameter is required');
}
if (!tool) {
throw new Error('toolName parameter is required');
}
if (!version) {
throw new Error('versionSpec parameter is required');
}
}
export async function findHaskellGHCVersion(version: string) {
return _findHaskellToolVersion('ghc', version);
}
export async function findHaskellCabalVersion(version: string) {
return _findHaskellToolVersion('cabal', version);
}
export async function _findHaskellToolVersion(tool: string, version: string) {
version = normalizeVersion(version);
const installDir: string | null = tc.find(tool, version);
if (!installDir) {
const toolPath: string | null = path.join(
baseInstallDir,
tool,
version,
'bin'
);
if (fs.existsSync(toolPath) && fs.existsSync(`${toolPath}.complete`)) {
core.debug(`Found tool in cache ${tool} ${version}`);
} else {
throw new Error(`Version ${version} of ${tool} not found`);
}
const toolPath: string = path.join(installDir, 'bin');
core.addPath(toolPath);
}
// This function is required to convert the version 1.10 to 1.10.0.
// Because caching utility accept only sementic version,
// which have patch number as well.
function normalizeVersion(version: string): string {
const versionPart = version.split('.');
if (versionPart[1] == null) {
//append minor and patch version if not available
return version.concat('.0.0');
}
if (versionPart[2] == null) {
//append patch version if not available
return version.concat('.0');
}
return version;
}

View File

@@ -1,29 +1,25 @@
import * as core from '@actions/core';
import {
findHaskellGHCVersion,
findHaskellCabalVersion,
cacheHaskellTool
} from './installer';
import {findHaskellGHCVersion, findHaskellCabalVersion} from './installer';
// ghc and cabal are installed directly to /opt so use that directlly instead of
// copying over to the toolcache dir.
const baseInstallDir = '/opt';
const defaultGHCVersion = '8.6.5';
const defualtCabalVersion = '3.0';
const defaultCabalVersion = '3.0';
async function run() {
try {
await cacheHaskellTool('/opt', 'ghc');
await cacheHaskellTool('/opt', 'cabal');
let ghcVersion = core.getInput('ghc-version');
if (!ghcVersion) {
ghcVersion = defaultGHCVersion;
}
await findHaskellGHCVersion(ghcVersion);
await findHaskellGHCVersion(baseInstallDir, ghcVersion);
let cabalVersion = core.getInput('cabal-version');
if (!cabalVersion) {
cabalVersion = defualtCabalVersion;
cabalVersion = defaultCabalVersion;
}
await findHaskellCabalVersion(cabalVersion);
await findHaskellCabalVersion(baseInstallDir, cabalVersion);
} catch (error) {
core.setFailed(error.message);
}