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

@@ -10,11 +10,7 @@ process.env['AGENT_TOOLSDIRECTORY'] = toolDir;
process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir;
import {
findHaskellGHCVersion,
findHaskellCabalVersion,
cacheHaskellTool
} from '../src/installer';
import {findHaskellGHCVersion, findHaskellCabalVersion} from '../src/installer';
describe('find-haskell', () => {
beforeAll(async () => {
@@ -24,12 +20,10 @@ describe('find-haskell', () => {
const ghcDir: string = path.join(tempDir, 'ghc', '8.6.5', 'bin');
await io.mkdirP(ghcDir);
fs.writeFileSync(`${ghcDir}.complete`, 'hello');
await cacheHaskellTool(tempDir, 'ghc');
const cabalDir: string = path.join(tempDir, 'cabal', '2.0', 'bin');
await io.mkdirP(cabalDir);
fs.writeFileSync(`${cabalDir}.complete`, 'hello');
await cacheHaskellTool(tempDir, 'cabal');
});
afterAll(async () => {
@@ -41,40 +35,20 @@ describe('find-haskell', () => {
}
}, 100000);
it('Caches ghc into tool cache', async () => {
const ghcDir: string = path.join(tempDir, 'ghc', '8.6.4', 'bin');
await io.mkdirP(ghcDir);
fs.writeFileSync(`${ghcDir}.complete`, 'hello');
await cacheHaskellTool(tempDir, 'ghc');
const installDir: string = path.join(toolDir, 'ghc', '8.6.4', os.arch());
expect(fs.existsSync(installDir)).toBe(true);
});
it('Caches cabal into tool cache', async () => {
const cabalDir: string = path.join(tempDir, 'cabal', '2.4', 'bin');
await io.mkdirP(cabalDir);
fs.writeFileSync(`${cabalDir}.complete`, 'hello');
await cacheHaskellTool(tempDir, 'cabal');
const installDir: string = path.join(toolDir, 'cabal', '2.4.0', os.arch());
expect(fs.existsSync(installDir)).toBe(true);
});
it('Uses version of ghc installed in cache', async () => {
// This will throw if it doesn't find it in the cache (because no such version exists)
await findHaskellGHCVersion('8.6.5');
await findHaskellGHCVersion(tempDir, '8.6.5');
});
it('Uses version of cabal installed in cache', async () => {
// This will throw if it doesn't find it in the cache (because no such version exists)
await findHaskellCabalVersion('2.0');
await findHaskellCabalVersion(tempDir, '2.0');
});
it('findHaskellGHCVersion throws if cannot find any version of ghc', async () => {
let thrown = false;
try {
await findHaskellGHCVersion('9.9.9');
await findHaskellGHCVersion(tempDir, '9.9.9');
} catch {
thrown = true;
}
@@ -84,7 +58,17 @@ describe('find-haskell', () => {
it('findHaskellCabalVersion throws if cannot find any version of ghc', async () => {
let thrown = false;
try {
await findHaskellCabalVersion('9.9.9');
await findHaskellCabalVersion(tempDir, '9.9.9');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('findHaskellGHCVersion throws without baseInstallDir', async () => {
let thrown = false;
try {
await findHaskellGHCVersion('', '8.6.5');
} catch {
thrown = true;
}

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);
}