From 6a1287939f2de7f78bba2eb90014fd7686b73cf1 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Thu, 23 May 2024 15:16:12 -0300 Subject: [PATCH] Add flake-dirs input --- action.yml | 6 ++++++ dist/index.js | 28 +++++++++++++++++++++++++--- dist/index.js.map | 2 +- src/index.ts | 38 +++++++++++++++++++++++++++++++++++--- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index 7840ce4..4cc3c3c 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,11 @@ inputs: path-to-flake-dir: description: "The path of the directory containing `flake.nix` file within your repository. Useful when `flake.nix` cannot reside at the root of your repository." required: false + flake-dirs: + description: | + A space-separated list of directories containing `flake.nix` files within your repository. Useful when you have multiple flakes in your repository. + required: false + default: "" pr-title: description: "The title of the PR to be created" required: false @@ -164,6 +169,7 @@ runs: INPUT_INPUTS: ${{ inputs.inputs }} INPUT_NIX-OPTIONS: ${{ inputs.nix-options }} INPUT_PATH-TO-FLAKE-DIR: ${{ inputs.path-to-flake-dir }} + INPUT_FLAKE-DIRS: ${{ inputs.flake-dirs }} INPUT_PR-ASSIGNEES: ${{ inputs.pr-assignees }} INPUT_PR-BODY: ${{ inputs.pr-body }} INPUT_PR-LABELS: ${{ inputs.pr-labels }} diff --git a/dist/index.js b/dist/index.js index 98b15f3..621bc2c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -94778,6 +94778,10 @@ var UpdateFlakeLockAction = class extends DetSysAction { this.flakeInputs = inputs_exports.getArrayOfStrings("inputs", "space"); this.nixOptions = inputs_exports.getArrayOfStrings("nix-options", "space"); this.pathToFlakeDir = inputs_exports.getStringOrNull("path-to-flake-dir"); + this.flakeDirs = inputs_exports.getArrayOfStrings("flake-dirs", "space"); + if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") { + throw new Error("Both path-to-flake-dir and flake-dirs is defined"); + } } async main() { await this.update(); @@ -94786,6 +94790,19 @@ var UpdateFlakeLockAction = class extends DetSysAction { async post() { } async update() { + if (this.flakeDirs !== null && this.flakeDirs.length > 0) { + core.debug( + `Running flake lock update in multiple directories: ${this.flakeDirs}` + ); + for (const directory of this.flakeDirs) { + await this.updateFlake(directory); + } + } else { + await this.updateFlake(this.pathToFlakeDir); + } + } + async updateFlake(directory) { + core.debug(`Running flake lock update in directory ${directory}`); const nixCommandArgs = makeNixCommandArgs( this.nixOptions, this.flakeInputs, @@ -94793,6 +94810,7 @@ var UpdateFlakeLockAction = class extends DetSysAction { ); core.debug( JSON.stringify({ + directory, options: this.nixOptions, inputs: this.flakeInputs, message: this.commitMessage, @@ -94800,16 +94818,20 @@ var UpdateFlakeLockAction = class extends DetSysAction { }) ); const execOptions = { - cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : void 0 + cwd: directory }; const exitCode = await exec.exec("nix", nixCommandArgs, execOptions); if (exitCode !== 0) { this.recordEvent(EVENT_EXECUTION_FAILURE, { exitCode }); - core.setFailed(`non-zero exit code of ${exitCode} detected`); + core.setFailed( + `non-zero exit code of ${exitCode} detected while updating directory ${directory}` + ); } else { - core.info(`flake.lock file was successfully updated`); + core.info( + `flake.lock file in ${directory} was successfully updated` + ); } } }; diff --git a/dist/index.js.map b/dist/index.js.map index 154daa5..10d75b7 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/nix.ts","../src/index.ts"],"sourcesContent":["// Build the Nix args out of inputs from the Actions environment\nexport function makeNixCommandArgs(\n nixOptions: string[],\n flakeInputs: string[],\n commitMessage: string,\n): string[] {\n const flakeInputFlags = flakeInputs.flatMap((input) => [\n \"--update-input\",\n input,\n ]);\n\n const updateLockMechanism = flakeInputFlags.length === 0 ? \"update\" : \"lock\";\n\n return nixOptions\n .concat([\"flake\", updateLockMechanism])\n .concat(flakeInputFlags)\n .concat([\"--commit-lock-file\", \"--commit-lockfile-summary\", commitMessage]);\n}\n","import { makeNixCommandArgs } from \"./nix.js\";\nimport * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { DetSysAction, inputs } from \"detsys-ts\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction extends DetSysAction {\n private commitMessage: string;\n private nixOptions: string[];\n private flakeInputs: string[];\n private pathToFlakeDir: string | null;\n\n constructor() {\n super({\n name: \"update-flake-lock\",\n fetchStyle: \"universal\",\n requireNix: \"fail\",\n });\n\n this.commitMessage = inputs.getString(\"commit-msg\");\n this.flakeInputs = inputs.getArrayOfStrings(\"inputs\", \"space\");\n this.nixOptions = inputs.getArrayOfStrings(\"nix-options\", \"space\");\n this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n }\n\n async main(): Promise {\n await this.update();\n }\n\n // No post phase\n async post(): Promise {}\n\n async update(): Promise {\n // Nix command of this form:\n // nix ${maybe nix options} flake ${\"update\" or \"lock\"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}\n // Example commands:\n // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n // nix flake update --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n const nixCommandArgs: string[] = makeNixCommandArgs(\n this.nixOptions,\n this.flakeInputs,\n this.commitMessage,\n );\n\n actionsCore.debug(\n JSON.stringify({\n options: this.nixOptions,\n inputs: this.flakeInputs,\n message: this.commitMessage,\n args: nixCommandArgs,\n }),\n );\n\n const execOptions: actionsExec.ExecOptions = {\n cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : undefined,\n };\n\n const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs, execOptions);\n\n if (exitCode !== 0) {\n this.recordEvent(EVENT_EXECUTION_FAILURE, {\n exitCode,\n });\n actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);\n } else {\n actionsCore.info(`flake.lock file was successfully updated`);\n }\n }\n}\n\nfunction main(): void {\n new UpdateFlakeLockAction().execute();\n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I,aAAa,GAAG;AAClB,WAAK,YAAY,yBAAyB;AAAA,QACxC;AAAA,MACF,CAAC;AACD,MAAY,sBAAU,yBAAyB,QAAQ,WAAW;AAAA,IACpE,OAAO;AACL,MAAY,iBAAK,0CAA0C;AAAA,IAC7D;AAAA,EACF;AACF;AAEA,SAAS,OAAa;AACpB,MAAI,sBAAsB,EAAE,QAAQ;AACtC;AAEA,KAAK;","names":[]} \ No newline at end of file +{"version":3,"sources":["../src/nix.ts","../src/index.ts"],"sourcesContent":["// Build the Nix args out of inputs from the Actions environment\nexport function makeNixCommandArgs(\n nixOptions: string[],\n flakeInputs: string[],\n commitMessage: string,\n): string[] {\n const flakeInputFlags = flakeInputs.flatMap((input) => [\n \"--update-input\",\n input,\n ]);\n\n const updateLockMechanism = flakeInputFlags.length === 0 ? \"update\" : \"lock\";\n\n return nixOptions\n .concat([\"flake\", updateLockMechanism])\n .concat(flakeInputFlags)\n .concat([\"--commit-lock-file\", \"--commit-lockfile-summary\", commitMessage]);\n}\n","import { makeNixCommandArgs } from \"./nix.js\";\nimport * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { DetSysAction, inputs } from \"detsys-ts\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction extends DetSysAction {\n private commitMessage: string;\n private nixOptions: string[];\n private flakeInputs: string[];\n private pathToFlakeDir: string | null;\n private flakeDirs: string[] | null;\n\n constructor() {\n super({\n name: \"update-flake-lock\",\n fetchStyle: \"universal\",\n requireNix: \"fail\",\n });\n\n this.commitMessage = inputs.getString(\"commit-msg\");\n this.flakeInputs = inputs.getArrayOfStrings(\"inputs\", \"space\");\n this.nixOptions = inputs.getArrayOfStrings(\"nix-options\", \"space\");\n this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n this.flakeDirs = inputs.getArrayOfStrings(\"flake-dirs\", \"space\");\n\n if (\n this.flakeDirs !== null &&\n this.flakeDirs.length > 0 &&\n this.pathToFlakeDir !== \"\"\n ) {\n // TODO: improve this error message\n throw new Error(\"Both path-to-flake-dir and flake-dirs is defined\");\n }\n }\n\n async main(): Promise {\n await this.update();\n }\n\n // No post phase\n async post(): Promise {}\n\n async update(): Promise {\n if (this.flakeDirs !== null && this.flakeDirs.length > 0) {\n actionsCore.debug(\n `Running flake lock update in multiple directories: ${this.flakeDirs}`,\n );\n\n for (const directory of this.flakeDirs) {\n await this.updateFlake(directory);\n }\n } else {\n await this.updateFlake(this.pathToFlakeDir!);\n }\n }\n\n private async updateFlake(directory: string): Promise {\n actionsCore.debug(`Running flake lock update in directory ${directory}`);\n\n // Nix command of this form:\n // nix ${maybe nix options} flake ${\"update\" or \"lock\"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}\n // Example commands:\n // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n // nix flake update --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n const nixCommandArgs: string[] = makeNixCommandArgs(\n this.nixOptions,\n this.flakeInputs,\n this.commitMessage,\n );\n\n actionsCore.debug(\n JSON.stringify({\n directory,\n options: this.nixOptions,\n inputs: this.flakeInputs,\n message: this.commitMessage,\n args: nixCommandArgs,\n }),\n );\n\n const execOptions: actionsExec.ExecOptions = {\n cwd: directory,\n };\n\n const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs, execOptions);\n\n if (exitCode !== 0) {\n this.recordEvent(EVENT_EXECUTION_FAILURE, {\n exitCode,\n });\n actionsCore.setFailed(\n `non-zero exit code of ${exitCode} detected while updating directory ${directory}`,\n );\n } else {\n actionsCore.info(\n `flake.lock file in ${directory} was successfully updated`,\n );\n }\n }\n}\n\nfunction main(): void {\n new UpdateFlakeLockAction().execute();\n}\n\nmain();\n"],"mappings":";AACO,SAAS,mBACd,YACA,aACA,eACU;AACV,QAAM,kBAAkB,YAAY,QAAQ,CAAC,UAAU;AAAA,IACrD;AAAA,IACA;AA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,iBAA2B;AAAA,MAC/B,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,IACP;AAEA,IAAY;AAAA,MACV,KAAK,UAAU;AAAA,QACb;AAAA,QACA,SAAS,KAAK;AAAA,QACd,QAAQ,KAAK;AAAA,QACb,SAAS,KAAK;AAAA,QACd,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAEA,UAAM,cAAuC;AAAA,MAC3C,KAAK;AAAA,IACP;AAEA,UAAM,WAAW,MAAkB,iBAAK,OAAO,gBAAgB,WAAW;AAE1E,QAAI,aAAa,GAAG;AAClB,WAAK,YAAY,yBAAyB;AAAA,QACxC;AAAA,MACF,CAAC;AACD,MAAY;AAAA,QACV,yBAAyB,QAAQ,sCAAsC,SAAS;AAAA,MAClF;AAAA,IACF,OAAO;AACL,MAAY;AAAA,QACV,sBAAsB,SAAS;AAAA,MACjC;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,OAAa;AACpB,MAAI,sBAAsB,EAAE,QAAQ;AACtC;AAEA,KAAK;","names":[]} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 75ce696..665eca9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ class UpdateFlakeLockAction extends DetSysAction { private nixOptions: string[]; private flakeInputs: string[]; private pathToFlakeDir: string | null; + private flakeDirs: string[] | null; constructor() { super({ @@ -22,6 +23,16 @@ class UpdateFlakeLockAction extends DetSysAction { this.flakeInputs = inputs.getArrayOfStrings("inputs", "space"); this.nixOptions = inputs.getArrayOfStrings("nix-options", "space"); this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir"); + this.flakeDirs = inputs.getArrayOfStrings("flake-dirs", "space"); + + if ( + this.flakeDirs !== null && + this.flakeDirs.length > 0 && + this.pathToFlakeDir !== "" + ) { + // TODO: improve this error message + throw new Error("Both path-to-flake-dir and flake-dirs is defined"); + } } async main(): Promise { @@ -32,6 +43,22 @@ class UpdateFlakeLockAction extends DetSysAction { async post(): Promise {} async update(): Promise { + if (this.flakeDirs !== null && this.flakeDirs.length > 0) { + actionsCore.debug( + `Running flake lock update in multiple directories: ${this.flakeDirs}`, + ); + + for (const directory of this.flakeDirs) { + await this.updateFlake(directory); + } + } else { + await this.updateFlake(this.pathToFlakeDir!); + } + } + + private async updateFlake(directory: string): Promise { + actionsCore.debug(`Running flake lock update in directory ${directory}`); + // Nix command of this form: // nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message} // Example commands: @@ -45,6 +72,7 @@ class UpdateFlakeLockAction extends DetSysAction { actionsCore.debug( JSON.stringify({ + directory, options: this.nixOptions, inputs: this.flakeInputs, message: this.commitMessage, @@ -53,7 +81,7 @@ class UpdateFlakeLockAction extends DetSysAction { ); const execOptions: actionsExec.ExecOptions = { - cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : undefined, + cwd: directory, }; const exitCode = await actionsExec.exec("nix", nixCommandArgs, execOptions); @@ -62,9 +90,13 @@ class UpdateFlakeLockAction extends DetSysAction { this.recordEvent(EVENT_EXECUTION_FAILURE, { exitCode, }); - actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`); + actionsCore.setFailed( + `non-zero exit code of ${exitCode} detected while updating directory ${directory}`, + ); } else { - actionsCore.info(`flake.lock file was successfully updated`); + actionsCore.info( + `flake.lock file in ${directory} was successfully updated`, + ); } } }