env.mjs RawHistoryFolder
1// SPDX-License-Identifier: MIT
2//
3// Shared by the scripts of this directory: finds the runner driver, and checks
4// that the clone it was built from is the one the recorded data is about and
5// that the .NET runtime is the 9.0 line (number formatting and case folding come
6// from the runtime, not from the SDK).
7//
8// RUNNER_CLONE=/path/to/actions/runner node expr/gen/runner/<script>.mjs
9
10import { execFileSync } from "node:child_process";
11import { readFileSync } from "node:fs";
12import { join, dirname } from "node:path";
13import { fileURLToPath } from "node:url";
14
15export const here = dirname(fileURLToPath(import.meta.url));
16
17/** The commit of actions/runner the data in testdata/ is about (runner-commit.txt). */
18export const runnerCommit = readFileSync(join(here, "runner-commit.txt"), "utf8").trim();
19
20/** The driver built by `dotnet build -c Release expr/gen/runner -p:RunnerSrc=<clone>/src/Sdk`. */
21export const driver = join(here, "bin/Release/net9.0/runner.dll");
22
23/**
24 * Checks the environment and returns { clone, commit, runtime }. The clone is
25 * $RUNNER_CLONE (or the first argument); its HEAD must be runner-commit.txt and
26 * its tree clean in src/Sdk, and `dotnet --list-runtimes` must list a
27 * Microsoft.NETCore.App 9.0.x.
28 */
29export function checkEnvironment(arg) {
30 const clone = process.env.RUNNER_CLONE || arg;
31 if (!clone) throw new Error("set RUNNER_CLONE to a clone of https://github.com/actions/runner");
32 const git = (...a) => execFileSync("git", ["-C", clone, ...a]).toString().trim();
33 const commit = git("rev-parse", "HEAD");
34 if (commit !== runnerCommit) throw new Error(`the clone is at ${commit}, the data is about ${runnerCommit} (runner-commit.txt)`);
35 if (git("status", "--porcelain", "--", "src/Sdk") !== "") throw new Error("the clone has local changes in src/Sdk");
36 const rt = execFileSync("dotnet", ["--list-runtimes"]).toString().split("\n").filter(l => /^Microsoft\.NETCore\.App 9\.0\.\d+ /.test(l));
37 if (rt.length === 0) throw new Error("no Microsoft.NETCore.App 9.0.x runtime installed (dotnet --list-runtimes)");
38 return { clone, commit, runtime: rt[rt.length - 1].replace(/ \[.*$/, "") };
39}
40
41/** Runs the driver with args; the environment is checked first. */
42export function runDriver(args, opts = {}) {
43 checkEnvironment();
44 return execFileSync("dotnet", [driver, ...args], { maxBuffer: 1 << 30, ...opts }).toString("utf8");
45}