Skip to content

Commit

Permalink
Merge pull request #13 from fontist/fix-bin-wrapper
Browse files Browse the repository at this point in the history
[wip]
  • Loading branch information
jcbhmr authored Feb 10, 2024
2 parents 7ef60e7 + 9e4638f commit 19ee675
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
67 changes: 38 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,28 @@ const octokit = token
auth: { reason: "no 'fontist-token' input" },
});

const versionRaw = core.getInput("fontist-version");
const versionRange = versionRaw === "latest" ? "*" : versionRaw;
const tags = await octokit.paginate(octokit.rest.repos.listTags, {
owner: "fontist",
repo: "fontist",
});
const versions = tags.map((tag) => tag.name.slice(1));

const versionRaw = core.getInput("fontist-version");
const versionRange = versionRaw === "latest" ? "*" : versionRaw;
const version = semver.maxSatisfying(versions, versionRange);
assert(
version,
`${versionRange} didn't match any ${JSON.stringify(versions)}}`,
);
core.info(`Resolved version: v${version}`);

const workflowCache = core.getBooleanInput("cache");
const installationKey = `fontist-${version}-installation`;

let found = tc.find("fontist", version);
let cacheHit = !!found;

const workflowCache = core.getBooleanInput("cache");
const keyPrefix = `fontist-${version}-${process.env.RUNNER_OS}`
const installationKey = `${keyPrefix}-installation`;

if (!found) {
core.info(`Fontist v${version} not found in tool cache.`);

Expand All @@ -61,40 +64,46 @@ if (!found) {
const tempDir = join(process.env.RUNNER_TEMP!, Math.random().toString());
await mkdir(tempDir);

const installDir = join(tempDir, "install-dir");
const bindir = join(tempDir, "bindir");

core.info(`Using RubyGems to install Fontist v${version}...`);
core.info(`Installing to ${join(tempDir, "install-dir")}`);
core.info(`Installing binaries to ${join(tempDir, "bindir")}`);
core.info(`Installing to ${installDir}`);
core.info(`Installing binaries to ${bindir}`);
await $({
stdio: "inherit",
})`gem install fontist --version ${version} --no-document --install-dir ${join(tempDir, "install-dir")} --bindir ${join(tempDir, "bindir")}`;

core.info(`Creating wrapper scripts in ${join(tempDir, "bin")}...`);
await mkdir(join(tempDir, "bin"));

const bash = `\
#!/bin/bash
export GEM_PATH='${join(tempDir, "install-dir")}'
export GEM_HOME='${join(tempDir, "install-dir")}'
exec ${join(tempDir, "bindir", "fontist")} "$@"`;
await writeFile(join(tempDir, "bin", "fontist"), bash);
await chmod(join(tempDir, "bin", "fontist"), 0o755);

const cmd = `\
@echo off\r
set GEM_PATH=${join(tempDir, "install-dir")}\r
set GEM_HOME=${join(tempDir, "install-dir")}\r
${join(tempDir, "bindir", "fontist")} %*`;
await writeFile(join(tempDir, "bin", "fontist.cmd"), cmd);
})`gem install fontist --version ${version} --no-document --install-dir ${installDir} --bindir ${bindir}`;

found = await tc.cacheDir(tempDir, "fontist", version);
}
const installDir = join(found, "install-dir")
const bindir = join(found, "bindir");

if (workflowCache) {
core.info(`Caching Fontist installation in workflow cache...`);
await cache.saveCache([found], installationKey);
}

core.addPath(join(found, "bin"));
const wrappers = join(found, "wrappers");
core.info(`Creating wrapper scripts in ${wrappers}...`);
await mkdir(wrappers);

const bash = `\
#!/bin/bash
export GEM_PATH='${installDir}'
export GEM_HOME='${installDir}'
exec '${join(bindir, "fontist")}' "$@"`;
await writeFile(join(wrappers, "fontist"), bash);
await chmod(join(wrappers, "fontist"), 0o755);

const cmd = `\
@echo off\r
set GEM_PATH=${installDir}\r
set GEM_HOME=${installDir}\r
${join(bindir, "fontist")} %*`;
await writeFile(join(wrappers, "fontist.cmd"), cmd);

core.addPath(wrappers);
core.setOutput("fontist-version", version);
core.info(`✅ Fontist v${version} installed!`);

Expand All @@ -103,8 +112,8 @@ if (workflowCache) {
const cacheDependencyPath = core.getInput("cache-dependency-path");
const hash = await glob.hashFiles(cacheDependencyPath);
if (hash) {
const dataKey = `fontist-${version}-data-${hash}`;
core.saveState("cache-data-key", dataKey);
const dataKey = `${keyPrefix}-data-${hash}`;
core.saveState("data-key", dataKey);
core.info(
`Attempting to restore ~/.fontist from workflow cache: ${dataKey}`,
);
Expand Down
4 changes: 2 additions & 2 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { join } from "node:path";

if (core.getBooleanInput("cache")) {
const cacheDir = join(process.env.HOME!, ".fontist");
const dataKey = core.getState("cache-data-key");
const dataKey = core.getState("data-key");
if (dataKey) {
core.info(`Saving ${cacheDir} with key ${dataKey}`);
await cache.saveCache([cacheDir], dataKey);
} else {
core.info(`No cache-data-key. Skipping save.`);
core.info(`No 'data-key' value. Skipping save.`);
}
}

Expand Down

0 comments on commit 19ee675

Please sign in to comment.