Skip to content

Commit 3c058a5

Browse files
committed
Allow to install stack hook
1 parent f144b0d commit 3c058a5

File tree

6 files changed

+99
-41
lines changed

6 files changed

+99
-41
lines changed

.github/workflows/test-ghcup.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222

2323
- uses: ./
2424
with:
25+
stack-hook: true
2526
version: ${{ matrix.version }}
2627

2728
- run: ghcup config
@@ -52,6 +53,12 @@ jobs:
5253
- if: runner.os == 'Windows'
5354
run: ghcup run -m sh -- -c 'pacman --version'
5455

56+
- name: Stack hook test
57+
run: |
58+
ghcup install stack latest
59+
cat $(stack path --stack-root)/hooks/ghc-install.sh
60+
shell: bash
61+
5562
vanilla-channel:
5663
strategy:
5764
matrix:

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ jobs:
5656
5757
## Inputs
5858
59-
| Name | Description | Type | Default |
60-
|------------------|--------------------------|------------|------------|
61-
| version | GHCup version to install | `string` | `latest` |
62-
| release-channels | Set the release-channels | `string[]` | `GHCupURL` |
59+
| Name | Description | Type | Default |
60+
|------------------|-----------------------------------------------------------------|------------|------------|
61+
| version | GHCup version to install | `string` | `latest` |
62+
| release-channels | Set the release-channels | `string[]` | `GHCupURL` |
63+
| stack-hook | Install the GHCup stack hook (GHCs are installed through ghcup) | `boolean` | `false` |
6364

6465
## Outputs
6566

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ inputs:
1313
description: Set the release-channels
1414
default: |
1515
GHCupURL
16+
stack-hook:
17+
description: Install the GHCup stack hook (GHCs are installed through ghcup)
18+
default: false
1619

1720
outputs:
1821
path:

ghcup/dist/index.js

Lines changed: 28 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcup/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { main } from "./main.ts"
1+
import { main, parseYAMLBoolean } from "./main.ts"
22
import core from "@actions/core";
33

44
try {
55
main({
66
version: core.getInput("version"),
7-
release_channels: core.getMultilineInput("release-channels")
7+
release_channels: core.getMultilineInput("release-channels"),
8+
stack_hook: parseYAMLBoolean("stack-hook", core.getInput("stack-hook")),
89
})
910
} catch (error) {
1011
core.setFailed((error as Error).message);

ghcup/src/main.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as path from 'path'
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import os from 'os';
24
import { chmod } from 'fs/promises';
35

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

30-
function ghcup_url(version: string, arch: GHCupArch, os: GHCupOS): string {
32+
const hook_url: string = 'https://www.haskell.org/ghcup/sh/hooks/stack/ghc-install.sh';
33+
34+
function ghcup_url(version: string, arch: GHCupArch, gos: GHCupOS): string {
3135
if (version == 'latest') {
32-
return `https://downloads.haskell.org/ghcup/${arch}-${os}-ghcup${ext}`;
36+
return `https://downloads.haskell.org/ghcup/${arch}-${gos}-ghcup${ext}`;
3337
} else {
34-
return `https://downloads.haskell.org/ghcup/${version}/${arch}-${os}-ghcup-${version}${ext}`;
38+
return `https://downloads.haskell.org/ghcup/${version}/${arch}-${gos}-ghcup-${version}${ext}`;
3539
}
3640
}
3741

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

49-
const os = ghcup_os_map.get(platform.platform);
50-
if (os == undefined) {
53+
const gos = ghcup_os_map.get(platform.platform);
54+
if (gos == undefined) {
5155
throw `GHCup does not support platform ${platform.platform}`;
5256
}
5357

54-
const url = ghcup_url(version, arch, os);
58+
const url = ghcup_url(version, arch, gos);
5559

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

76+
function getStackRoot() {
77+
if (platform.isWindows) {
78+
const appdata = process.env['APPDATA'] || '';
79+
return process.env['STACK_ROOT'] ?? path.join(appdata, 'stack');
80+
} else {
81+
const hdir = os.homedir();
82+
return process.env['STACK_ROOT'] ?? path.join(hdir, '.stack');
83+
}
84+
}
85+
86+
async function installStackHook() {
87+
const stack_root = getStackRoot();
88+
const hook_dest = path.join(stack_root, 'hooks', 'ghc-install.sh')
89+
fs.rmSync(hook_dest, {
90+
force: true,
91+
});
92+
// we do not cache, it isn't versioned
93+
const hookPath = await tc.downloadTool(hook_url, hook_dest);
94+
if (!(platform.isWindows)) {
95+
await chmod(hook_dest, "0765");
96+
}
97+
core.debug(`stack ghcup hook is at ${hookPath}`);
98+
}
99+
100+
export function parseYAMLBoolean(name: string, val: string): boolean {
101+
const trueValue = ['true', 'True', 'TRUE'];
102+
const falseValue = ['false', 'False', 'FALSE'];
103+
if (trueValue.includes(val)) return true;
104+
if (falseValue.includes(val)) return false;
105+
throw new TypeError(
106+
`Action input "${name}" does not meet YAML 1.2 "Core Schema" specification: \n` +
107+
`Supported boolean values: \`true | True | TRUE | false | False | FALSE\``
108+
);
109+
}
110+
72111
export type Opts = {
73112
version: string,
74-
release_channels: string[]
113+
release_channels: string[],
114+
stack_hook: boolean
75115
}
76116

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

137+
if (opts.stack_hook) {
138+
installStackHook()
139+
}
140+
97141
await exec.exec(ghcupPath, [
98142
'config', 'set', 'url-source', JSON.stringify(opts.release_channels)
99143
]);
100144
}
145+

0 commit comments

Comments
 (0)