Skip to content

Commit

Permalink
Prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Jan 1, 2025
1 parent bd169cf commit b0bbb20
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test-ghcup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- [self-hosted, FreeBSD, X64]
version:
- latest
- '0.1.30.0'
- "0.1.30.0"
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v4
Expand All @@ -34,17 +34,17 @@ jobs:
- run: ghcup whereis ghc latest
- run: which ghc
- run: ghc --version

- run: ghcup install cabal latest --set
- run: ghcup whereis cabal latest
- run: which cabal
- run: cabal --version

- run: ghcup install stack latest --set
- run: ghcup whereis stack latest
- run: which stack
- run: stack --version

- run: ghcup install hls latest --set
- run: ghcup whereis hls latest
- run: which haskell-language-server-wrapper
Expand Down
4 changes: 2 additions & 2 deletions ghcup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { main, getInputAsBool } from "./main.ts"
import { main, getInputAsBool } from "./main.ts";
import core from "@actions/core";

try {
main({
version: core.getInput("version"),
release_channels: core.getMultilineInput("release-channels"),
stack_hook: getInputAsBool("stack-hook"),
})
});
} catch (error) {
core.setFailed((error as Error).message);
}
119 changes: 67 additions & 52 deletions ghcup/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import * as path from 'path';
import * as fs from 'fs';
import os from 'os';
import { chmod } from 'fs/promises';
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';
import core, { platform } from '@actions/core';
import exec from '@actions/exec';
import tc from "@actions/tool-cache";
import core, { platform } from "@actions/core";
import exec from "@actions/exec";

const ext = platform.isWindows ? ".exe" : "";

type Architecture = typeof platform.arch;
type GHCupArch = 'aarch64' | 'armv7' | 'i386' | 'x86_64';
type GHCupArch = "aarch64" | "armv7" | "i386" | "x86_64";

const ghcup_arch_map: Map<Architecture, GHCupArch> = new Map([
['arm64', 'aarch64'],
['arm', 'armv7'],
['ia32', 'i386'],
['x64', 'x86_64']
["arm64", "aarch64"],
["arm", "armv7"],
["ia32", "i386"],
["x64", "x86_64"],
]);

type Platform = typeof platform.platform;
type GHCupOS = 'apple-darwin' | 'linux' | 'mingw64' | 'portbld-freebsd';
type GHCupOS = "apple-darwin" | "linux" | "mingw64" | "portbld-freebsd";

const ghcup_os_map: Map<Platform, GHCupOS> = new Map([
['darwin', 'apple-darwin'],
['linux', 'linux'],
['win32', 'mingw64'],
['freebsd', 'portbld-freebsd']
["darwin", "apple-darwin"],
["linux", "linux"],
["win32", "mingw64"],
["freebsd", "portbld-freebsd"],
]);

const hook_url: string = 'https://www.haskell.org/ghcup/sh/hooks/stack/ghc-install.sh';
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') {
if (version == "latest") {
return `https://downloads.haskell.org/ghcup/${arch}-${gos}-ghcup${ext}`;
} else {
return `https://downloads.haskell.org/ghcup/${version}/${arch}-${gos}-ghcup-${version}${ext}`;
}
}

async function ghcup(version: string) {
const ghcupDirectory = tc.find('ghcup', version)
const ghcupDirectory = tc.find("ghcup", version);
if (ghcupDirectory) {
return ghcupDirectory
return ghcupDirectory;
} else {

const arch = ghcup_arch_map.get(platform.arch);
if (arch == undefined) {
throw `GHCup does not support architecture ${platform.arch}`;
Expand All @@ -57,63 +57,76 @@ async function ghcup(version: string) {

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

const tempDirectory = process.env['RUNNER_TEMP'] || '';
const tempDirectory = process.env["RUNNER_TEMP"] || "";
const ghcupExeName = `ghcup${ext}`;
const dest = path.join(tempDirectory, ghcupExeName);

const ghcupPath = await tc.downloadTool(url, dest);

if (!(platform.isWindows)) {
if (!platform.isWindows) {
await chmod(ghcupPath, "0765");
}

const ghcupDir = await tc.cacheFile(ghcupPath, ghcupExeName, "ghcup", version);
const ghcupDir = await tc.cacheFile(
ghcupPath,
ghcupExeName,
"ghcup",
version,
);
core.addPath(ghcupDir);
return path.join(ghcupDir, ghcupExeName);
}
}

function getStackRoot() {
const stackXdg = process.env['STACK_XDG'];

const defaultStackRoot = (() => { if (platform.isWindows) {
const appdata = process.env['APPDATA'] || '';
return stackXdg ? path.join(process.env['XDG_DATA_HOME'] ?? appdata, 'stack') : path.join(appdata, 'stack');
} else {
const hdir = os.homedir();
return stackXdg ? path.join(process.env['XDG_DATA_HOME'] ?? path.join(hdir, '.local', 'share'),'stack') : path.join(hdir, '.stack');
}
})()
return process.env['STACK_ROOT'] ?? defaultStackRoot;
const stackXdg = process.env["STACK_XDG"];

const defaultStackRoot = (() => {
if (platform.isWindows) {
const appdata = process.env["APPDATA"] || "";
return stackXdg
? path.join(process.env["XDG_DATA_HOME"] ?? appdata, "stack")
: path.join(appdata, "stack");
} else {
const hdir = os.homedir();
return stackXdg
? path.join(
process.env["XDG_DATA_HOME"] ?? path.join(hdir, ".local", "share"),
"stack",
)
: path.join(hdir, ".stack");
}
})();
return process.env["STACK_ROOT"] ?? defaultStackRoot;
}

async function installStackHook() {
const stack_root = getStackRoot();
const hook_dest = path.join(stack_root, 'hooks', 'ghc-install.sh')
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)) {
if (!platform.isWindows) {
await chmod(hook_dest, "0765");
}
core.debug(`stack ghcup hook is at ${hookPath}`);
}

export function getInputAsBool(
name: string,
options?: core.InputOptions
name: string,
options?: core.InputOptions,
): boolean {
const result = core.getInput(name, options);
return result.toLowerCase() === "true";
const result = core.getInput(name, options);
return result.toLowerCase() === "true";
}

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

export async function main(opts: Opts) {
const ghcupPath = await ghcup(opts.version);
Expand All @@ -130,17 +143,19 @@ export async function main(opts: Opts) {
core.addPath(bindir);

if (platform.isWindows) {
const ghcup_msys2 = process.env['GHCUP_MSYS2'] ?? 'C:\\msys64';
core.exportVariable('GHCUP_MSYS2', ghcup_msys2);
core.debug(`GHCUP_MSYS2 is ${ghcup_msys2}`);
const ghcup_msys2 = process.env["GHCUP_MSYS2"] ?? "C:\\msys64";
core.exportVariable("GHCUP_MSYS2", ghcup_msys2);
core.debug(`GHCUP_MSYS2 is ${ghcup_msys2}`);
}

if (opts.stack_hook) {
installStackHook()
installStackHook();
}

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

0 comments on commit b0bbb20

Please sign in to comment.