import {Box} from 'zena:core'; import { HashSet } from 'zena:collections'; import { JsonArray, JsonObject, JsonValue, parseJson, } from 'zena:json'; import {compareStrings, sortBy} from 'workflow/canonical'; import {joinWorkspace} from './fs-util.zena'; /** Rule `type` identifier for native Zena `*_test.zena` test suites. */ export let ZENA_TEST_RULE_TYPE = 'zena:test'; /** Raised for malformed or unsupported build configuration. */ export class ConfigError extends Error { new(message: String) : super(message); } /** A script in a package, both repo-relative ('' is the root package). */ export class ScriptRef(packageDir: String, script: String) /** The key steps derived from this script use, 'dir:script'. */ export function refKey(ref: ScriptRef): String { return `${ref.packageDir}:${ref.script}`; } /** A resolved dependency edge, with wireit's `cascade` flag (default true). */ export class DependencySpec(ref: ScriptRef, cascade: boolean) /** * Service configuration: `readyWhenLineMatches` is null for `service: true` * (ready on spawn) or a regex pattern string for `readyWhen.lineMatches`. */ export class ServiceConfig(readyWhenLineMatches: String?) /** One entry in a script's `env` map. */ export sealed class EnvBinding { case EnvLiteral, EnvExternal } /** Sets the environment variable to a fixed string value. */ export final class EnvLiteral(value: String) extends EnvBinding /** * Fingerprints an environment variable from the caller's environment, * optionally falling back to `defaultValue` when unset. */ export final class EnvExternal(defaultValue: String?) extends EnvBinding /** * One script's configuration. For a wireit script the fields come from * the wireit block; a bare npm script has only a command and always * runs. */ export class ScriptConfig { ref: ScriptRef; isWireit: boolean; command: String?; /** Globs relative to the package directory. */ files: Array; output: Array; /** * Whether the config declared the key at all. Omitting `files` or * `output` on a task script means the inputs or outputs are unknown * (wireit semantics): the script is never fresh, never cached, and * its cascading dependents cannot be either. An explicit empty array * means "declared none" and tracks fully. Services forbid `output` * and track their identity whenever `files` is declared. */ filesDeclared: boolean; outputDeclared: boolean; dependencies: Array; /** 'true', 'false', or 'if-file-deleted'. */ clean: String; service: ServiceConfig?; /** Sorted by variable name. */ env: Array<(String, EnvBinding)>; /** * Lockfile filenames searched upward from `ref.packageDir` to the * workspace root (wireit defaults to `['package-lock.json']` when * omitted; `[]` disables lockfile tracking). */ packageLocks: Array; /** * Explicit opt-out from freshness and caching (`"cache": false` in * `zebra.json` or `zena-package.json`). Defaults to `true`. */ cacheEnabled: boolean; /** * True when this step is a pure fileset rule (`"filesets"` or the * synthesized `"source"` fileset of a `zena-package.json`) rather than * a runnable script. */ isFileset: boolean; /** * Zena test file patterns (relative to `ref.packageDir`) for a * `"type": "zena:test"` step, or `null` for command/group/service/fileset steps. */ tests: Array?; /** Run `"type": "zena:test"` modules with debug names and inlining disabled (`-g`). */ testDebug: boolean; /** Extra CLI arguments after `--` passed to an entry `"type": "zena:test"` step. */ testExtraArgs: Array?; /** Explicit rule discriminator (such as `'zena:test'`), or `null` for standard scripts. */ ruleType: String?; /** * True when freshness and caching apply to this script at all. A * command-less script, `"type": "zena:test"` step, or fileset always tracks * (unless `cacheEnabled` is false); a service tracks when `files` (or a * fileset dependency) is declared; a command task needs both `files` and * `output` declared. */ fingerprintable: boolean { get { if (!this.isWireit || !this.cacheEnabled) { return false; } if (this.service != null) { return this.filesDeclared; } if (this.command == null) { return true; } return this.filesDeclared && this.outputDeclared; } } new( this.ref, this.isWireit, this.command, this.files, this.output, this.filesDeclared, this.outputDeclared, this.dependencies, this.clean, this.service, this.env, this.packageLocks, this.cacheEnabled = true, this.isFileset = false, this.tests = null, this.testDebug = false, this.testExtraArgs = null, this.ruleType = null, ); } export function isCommentKey(key: String): boolean { return key == '//' || key.startsWith('#'); } function isRecognizedKey(key: String) { return key == 'command' || key == 'files' || key == 'output' || key == 'dependencies' || key == 'clean' || key == 'service' || key == 'env' || key == 'packageLocks' || isCommentKey(key); } export function stringList(value: JsonValue, what: String): Array { if (value == null) { return new GrowableArray(); } if (value is JsonArray) { let out = new GrowableArray(); for (var i = 0; i < value.length; i += 1) { let item = value[i]; if (item is String) { out.push(item); } else { throw new ConfigError(what + ' must contain only strings'); } } return out; } throw new ConfigError(what + ' must be an array'); } /** * Resolves a wireit dependency string. A dependency starting with '.' * is ':