Skip to content

Commit

Permalink
Tweak the interface while testing with install-nix-action
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Mar 13, 2024
1 parent d4808a5 commit 9938b1b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 36 deletions.
44 changes: 29 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 51 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from "node:path";
import got from "got";
import { v4 as uuidV4 } from "uuid";
import { createWriteStream } from "node:fs";
import { copyFile, mkdir } from "node:fs/promises";
import fs, { chmod, copyFile, mkdir } from "node:fs/promises";
import { pipeline } from "node:stream/promises";
import { tmpdir } from "node:os";
// eslint-disable-next-line import/no-unresolved
Expand Down Expand Up @@ -33,34 +33,58 @@ const gotClient = got.extend({

export type FetchSuffixStyle = "nix-style" | "gh-env-style" | "universal";

export type Options = {
name: string;
idsProjectName?: string;
fetchStyle: FetchSuffixStyle;
legacySourcePrefix?: string;
};

// A confident version of Options, where defaults have been processed
type ConfidentOptions = {
name: string;
idsProjectName: string;
fetchStyle: FetchSuffixStyle;
legacySourcePrefix?: string;
};

function makeOptionsConfident(options: Options): ConfidentOptions {
return {
name: options.name,
idsProjectName: options.idsProjectName || options.name,
fetchStyle: options.fetchStyle,
legacySourcePrefix: options.legacySourcePrefix,
};
}

export class IdsToolbox {
projectName: string;
identity: correlation.AnonymizedCorrelationHashes;
options: ConfidentOptions;
archOs: string;
nixSystem: string;
architectureFetchSuffix: string;
sourceParameters: SourceDef;

constructor(
projectName: string,
fetchStyle: FetchSuffixStyle,
legacySourcePrefix?: string,
) {
this.projectName = projectName;
constructor(options: Options) {
this.options = makeOptionsConfident(options);

this.identity = correlation.identify();
this.archOs = platform.getArchOs();
this.nixSystem = platform.getNixPlatform(this.archOs);

if (fetchStyle === "gh-env-style") {
if (options.fetchStyle === "gh-env-style") {
this.architectureFetchSuffix = this.archOs;
} else if (fetchStyle === "nix-style") {
} else if (options.fetchStyle === "nix-style") {
this.architectureFetchSuffix = this.nixSystem;
} else if (fetchStyle === "universal") {
} else if (options.fetchStyle === "universal") {
this.architectureFetchSuffix = "universal";
} else {
throw new Error(`fetchStyle ${fetchStyle} is not a valid style`);
throw new Error(`fetchStyle ${options.fetchStyle} is not a valid style`);
}

this.sourceParameters = constructSourceParameters(legacySourcePrefix);
this.sourceParameters = constructSourceParameters(
options.legacySourcePrefix,
);
}

private getUrl(): URL {
Expand All @@ -71,7 +95,7 @@ export class IdsToolbox {
}

const fetchUrl = new URL("https://install.determinate.systems/");
fetchUrl.pathname += this.projectName;
fetchUrl.pathname += this.options.idsProjectName;

if (p.tag) {
fetchUrl.pathname += `/tag/${p.tag}`;
Expand All @@ -92,7 +116,7 @@ export class IdsToolbox {

private cacheKey(version: string): string {
const cleanedVersion = version.replace(/[^a-zA-Z0-9-+.]/g, "");
return `determinatesystem-${this.projectName}-${this.architectureFetchSuffix}-${cleanedVersion}`;
return `determinatesystem-${this.options.name}-${this.architectureFetchSuffix}-${cleanedVersion}`;
}

private async getCachedVersion(version: string): Promise<undefined | string> {
Expand All @@ -105,14 +129,14 @@ export class IdsToolbox {

if (
await actionsCache.restoreCache(
[this.projectName],
[this.options.name],
this.cacheKey(version),
[],
undefined,
true,
)
) {
return `${tempDir}/${this.projectName}`;
return `${tempDir}/${this.options.name}`;
}

return undefined;
Expand All @@ -131,10 +155,10 @@ export class IdsToolbox {
const tempDir = this.getTemporaryName();
await mkdir(tempDir);
process.chdir(tempDir);
await copyFile(toolPath, `${tempDir}/${this.projectName}`);
await copyFile(toolPath, `${tempDir}/${this.options.name}`);

await actionsCache.saveCache(
[this.projectName],
[this.options.name],
this.cacheKey(version),
undefined,
true,
Expand All @@ -151,7 +175,7 @@ export class IdsToolbox {
correlatedUrl.searchParams.set("ci", "github");
correlatedUrl.searchParams.set(
"correlation",
JSON.stringify(correlation.identify()),
JSON.stringify(this.identity),
);

const versionCheckup = await gotClient.head(correlatedUrl);
Expand Down Expand Up @@ -192,8 +216,14 @@ export class IdsToolbox {
return destFile;
}

async fetchExecutable(): Promise<string> {
const binaryPath = await this.fetch();
await chmod(binaryPath, fs.constants.S_IXUSR | fs.constants.S_IXGRP);
return binaryPath;
}

private getTemporaryName(): string {
const _tmpdir = process.env["RUNNER_TEMP"] || tmpdir();
return path.join(_tmpdir, `${this.projectName}-${uuidV4()}`);
return path.join(_tmpdir, `${this.options.name}-${uuidV4()}`);
}
}

0 comments on commit 9938b1b

Please sign in to comment.