Skip to content

Commit

Permalink
Allow to install stack hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Dec 30, 2024
1 parent f144b0d commit db0dac8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/test-ghcup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

- uses: ./
with:
stack_hook: true
version: ${{ matrix.version }}

- run: ghcup config
Expand Down Expand Up @@ -52,6 +53,12 @@ jobs:
- if: runner.os == 'Windows'
run: ghcup run -m sh -- -c 'pacman --version'

- name: Stack hook test
run: |
ghcup install stack latest
cat $(stack path --stack-root)/hooks/ghc-install.sh
shell: bash

vanilla-channel:
strategy:
matrix:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
description: Set the release-channels
default: |
GHCupURL
stack_hook:
description: Install the GHCup stack hook
default: false

outputs:
path:
Expand Down
55 changes: 28 additions & 27 deletions ghcup/dist/index.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions ghcup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { main } from "./main.ts"
import { main, parseYAMLBoolean } from "./main.ts"
import core from "@actions/core";

try {
main({
version: core.getInput("version"),
release_channels: core.getMultilineInput("release-channels")
release_channels: core.getMultilineInput("release-channels"),
stack_hook: parseYAMLBoolean("stack_hook", core.getInput("stack_hook")),
})
} catch (error) {
core.setFailed((error as Error).message);
Expand Down
61 changes: 53 additions & 8 deletions ghcup/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as path from 'path'
import * as path from 'path';
import * as fs from 'fs';
import os from 'os';
import { chmod } from 'fs/promises';

import tc from '@actions/tool-cache';
Expand Down Expand Up @@ -27,11 +29,13 @@ const ghcup_os_map: Map<Platform, GHCupOS> = new Map([
['freebsd', 'portbld-freebsd']
]);

function ghcup_url(version: string, arch: GHCupArch, os: GHCupOS): string {
const hook_url: string = 'https://www.haskell.org/ghcup/sh/hooks/stack/ghc-install.sh';

function ghcup_url(version: string, arch: GHCupArch, gos: GHCupOS): string {
if (version == 'latest') {
return `https://downloads.haskell.org/ghcup/${arch}-${os}-ghcup${ext}`;
return `https://downloads.haskell.org/ghcup/${arch}-${gos}-ghcup${ext}`;
} else {
return `https://downloads.haskell.org/ghcup/${version}/${arch}-${os}-ghcup-${version}${ext}`;
return `https://downloads.haskell.org/ghcup/${version}/${arch}-${gos}-ghcup-${version}${ext}`;
}
}

Expand All @@ -46,12 +50,12 @@ async function ghcup(version: string) {
throw `GHCup does not support architecture ${platform.arch}`;
}

const os = ghcup_os_map.get(platform.platform);
if (os == undefined) {
const gos = ghcup_os_map.get(platform.platform);
if (gos == undefined) {
throw `GHCup does not support platform ${platform.platform}`;
}

const url = ghcup_url(version, arch, os);
const url = ghcup_url(version, arch, gos);

const tempDirectory = process.env['RUNNER_TEMP'] || '';
const ghcupExeName = `ghcup${ext}`;
Expand All @@ -69,9 +73,45 @@ async function ghcup(version: string) {
}
}

function getStackRoot() {
if (platform.isWindows) {
const appdata = process.env['APPDATA'] || '';
return process.env['STACK_ROOT'] ?? path.join(appdata, 'stack');
} else {
const hdir = os.homedir();
return process.env['STACK_ROOT'] ?? path.join(hdir, '.stack');
}
}

async function installStackHook() {
const stack_root = getStackRoot();
const hook_dest = path.join(stack_root, 'hooks', 'ghc-install.sh')
fs.rmSync(hook_dest, {
force: true,
});
// we do not cache, it isn't versioned
const hookPath = await tc.downloadTool(hook_url, hook_dest);
if (!(platform.isWindows)) {
await chmod(hook_dest, "0765");
}
core.debug(`stack ghcup hook is at ${hookPath}`);
}

export function parseYAMLBoolean(name: string, val: string): boolean {
const trueValue = ['true', 'True', 'TRUE'];
const falseValue = ['false', 'False', 'FALSE'];
if (trueValue.includes(val)) return true;
if (falseValue.includes(val)) return false;
throw new TypeError(
`Action input "${name}" does not meet YAML 1.2 "Core Schema" specification: \n` +
`Supported boolean values: \`true | True | TRUE | false | False | FALSE\``
);
}

export type Opts = {
version: string,
release_channels: string[]
release_channels: string[],
stack_hook: boolean
}

export async function main(opts: Opts) {
Expand All @@ -94,7 +134,12 @@ export async function main(opts: Opts) {
core.debug(`GHCUP_MSYS2 is ${ghcup_msys2}`);
}

if (opts.stack_hook) {
installStackHook()
}

await exec.exec(ghcupPath, [
'config', 'set', 'url-source', JSON.stringify(opts.release_channels)
]);
}

0 comments on commit db0dac8

Please sign in to comment.