From cbbdcb7f1ae614ce31630e118570d265614356e1 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 10 Nov 2026 22:08:16 -0300 Subject: [PATCH 1/6] Add generate-pot: the POT file from WP-CLI, and whether a string changed --- .github/actions/generate-pot/README.md | 100 ++++++ .github/actions/generate-pot/action.yml | 81 +++++ .github/actions/generate-pot/generate-pot.js | 269 +++++++++++++++ .../actions/generate-pot/generate-pot.test.js | 326 ++++++++++++++++++ 4 files changed, 776 insertions(+) create mode 100644 .github/actions/generate-pot/README.md create mode 100644 .github/actions/generate-pot/action.yml create mode 100644 .github/actions/generate-pot/generate-pot.js create mode 100644 tests/actions/generate-pot/generate-pot.test.js diff --git a/.github/actions/generate-pot/README.md b/.github/actions/generate-pot/README.md new file mode 100644 index 0000000..20567e0 --- /dev/null +++ b/.github/actions/generate-pot/README.md @@ -0,0 +1,100 @@ +# generate-pot + +Generates a plugin's POT file with `wp i18n make-pot`, and says whether its strings changed. + +```yaml +- uses: actions/checkout@v6 + with: + persist-credentials: false + +# PHP has to be on the runner: wp-cli runs on it. +- uses: shivammathur/setup-php@v2 + with: + php-version: '8.2' + coverage: none + +- id: pot + uses: stellarwp/plugin-toolbox/.github/actions/generate-pot@v1 + with: + pot-path: lang/my-plugin.pot + +- if: steps.pot.outputs.changed == 'yes' + run: echo "Commit ${{ steps.pot.outputs.pot-path }} and push it to translation here." +``` + +Build the assets first when the plugin ships compiled JavaScript with strings in it. The action +writes the POT and commits nothing. + +## What it runs + +``` +wp i18n make-pot --skip-audit [--domain=…] [--headers=…] [--exclude=…] +``` + +wp-cli is release 2.12.0, pinned in [`generate-pot.js`](generate-pot.js) with its sha512. The phar +is downloaded into the runner's temp directory, never the checkout, and checked before it runs. A +phar that does not match is deleted and the step fails. A later step of the same job reuses the +download, and checks it again. + +## What is left out + +Three lists are joined into `--exclude`, each entry once: + +- The lines of `.distignore`, when the checkout has one: what the zip leaves out is not translated. + Empty lines and `#` comments are skipped. +- The path of each git submodule, read from `.gitmodules`. A submodule has its own text domain and + its own POT, and its strings do not belong in this one. +- The lines of the `exclude` input. + +With none of them, make-pot uses its own defaults. + +```yaml +- uses: stellarwp/plugin-toolbox/.github/actions/generate-pot@v1 + with: + pot-path: languages/my-plugin.pot + domain: my-plugin + headers: '{"Report-Msgid-Bugs-To": "https://example.com/support", "Last-Translator": "", "Language-Team": ""}' + exclude: | + *.js.map + src/**/*.js +``` + +## The changed check + +make-pot stamps the time (`POT-Creation-Date`), the plugin version (`Project-Id-Version`) and its +own version (`X-Generator`) on every run, so the file differs each time even with the same strings. + +| The POT | `changed` | The file | +|---|---|---| +| is not tracked by git: a new POT | `yes` | kept | +| differs from the tracked one in anything else | `yes` | kept | +| differs in those three header lines only, or not at all | `no` | put back with `git checkout` | + +A second run therefore leaves nothing to commit. The check needs a git checkout, at any depth. + +## Inputs + +| Input | Default | | +|---|---|---| +| `pot-path` | required | Where the POT is written, relative to the checkout, e.g. `lang/my-plugin.pot`. | +| `source-path` | `.` | The directory scanned for strings, relative to the checkout. | +| `domain` | | The text domain, passed as `--domain`. Empty leaves it to make-pot. | +| `headers` | | POT headers to set, as a JSON object of strings in a string. Empty sets none. | +| `exclude` | | Extra exclusions, one per line, added to `.distignore` and the submodule paths. | +| `working-directory` | | Where the repository is checked out. Empty is the workspace. | + +## Outputs + +| Output | | +|---|---| +| `pot-path` | The POT, relative to the checkout, as given. | +| `changed` | `yes` when the POT is new or its strings changed, `no` otherwise. | + +## How it runs + +The logic is in [`generate-pot.js`](generate-pot.js), run by `actions/github-script`, so there is +no dependency to install and nothing to build. It needs `php`, `curl` and `git` on the runner. + +Run its tests locally: `npm install` once in the toolbox, then +`node --test tests/actions/generate-pot/generate-pot.test.js`. They stand in for the download and +for wp-cli, and run the changed check against a real git repository. diff --git a/.github/actions/generate-pot/action.yml b/.github/actions/generate-pot/action.yml new file mode 100644 index 0000000..d83b5f8 --- /dev/null +++ b/.github/actions/generate-pot/action.yml @@ -0,0 +1,81 @@ +# Generates a plugin's POT file with `wp i18n make-pot`, and says whether its strings changed, so a +# caller commits and pushes a POT only when there is something new to translate. It writes the POT +# and commits nothing. +# +# The logic lives in generate-pot.js, run by actions/github-script, so there is no dependency to +# install and nothing to build. wp-cli is a pinned release, downloaded into the runner's temp +# directory and checked against its sha512 before it runs on the runner's PHP. +name: Generate POT +description: Generates the POT file of a plugin with wp-cli and says whether its strings changed + +inputs: + pot-path: + description: >- + Where the POT is written, relative to the checkout, e.g. `lang/the-events-calendar.pot` or + `languages/give.pot`. Empty, absolute or outside the checkout fails before anything runs. + required: true + source-path: + description: 'The directory scanned for strings, relative to the checkout.' + required: false + default: '.' + domain: + description: >- + The text domain to extract, passed as `--domain`. Empty leaves it to make-pot, which reads the + `Text Domain` header of the plugin, or uses the directory name. + required: false + default: '' + headers: + description: >- + POT headers to set, as a JSON object of strings in a string, passed as `--headers`, e.g. + `'{"Report-Msgid-Bugs-To": "https://example.com/support"}'`. Anything else fails before + anything runs. Empty sets none. + required: false + default: '' + exclude: + description: >- + Extra files and directories to leave out, one per line, as `--exclude` takes them, e.g. + `*.js.map`. They are added to the lines of `.distignore` and the git submodule paths, which + are always left out. + required: false + default: '' + working-directory: + description: >- + Where the repository is checked out, relative to the workspace or absolute. Empty is the + workspace. A directory that is not there fails before anything runs. + required: false + default: '' + +outputs: + pot-path: + description: 'The POT, relative to the checkout, as given.' + value: ${{ steps.pot.outputs.pot-path }} + changed: + description: >- + `yes` when the POT is new or its strings changed. `no` when the POT git tracks differs only in + the `POT-Creation-Date`, `Project-Id-Version` and `X-Generator` headers, or not at all; the + file is then put back as it was. + value: ${{ steps.pot.outputs.changed }} + +runs: + using: 'composite' + steps: + - name: Generate the POT and check whether the strings changed + id: pot + uses: actions/github-script@v9 + env: + # github-script's require() resolves against the workspace of the repository being built, + # not this action's directory, so the path has to be passed in. github.action_path is the + # documented expression for it. + ACTION_PATH: ${{ github.action_path }} + # Read from process.env rather than interpolated into the script, so an input is data and + # never part of the program. + INPUT_POT_PATH: ${{ inputs.pot-path }} + INPUT_SOURCE_PATH: ${{ inputs.source-path }} + INPUT_DOMAIN: ${{ inputs.domain }} + INPUT_HEADERS: ${{ inputs.headers }} + INPUT_EXCLUDE: ${{ inputs.exclude }} + INPUT_WORKING_DIRECTORY: ${{ inputs.working-directory }} + with: + script: | + const { runGeneratePot } = require(`${process.env.ACTION_PATH}/generate-pot.js`) + await runGeneratePot({ core, exec, env: process.env }) diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js new file mode 100644 index 0000000..c5ebbda --- /dev/null +++ b/.github/actions/generate-pot/generate-pot.js @@ -0,0 +1,269 @@ +'use strict' + +/** + * Generates a plugin's POT file with `wp i18n make-pot`, and says whether its strings changed. + * + * runGeneratePot validates the inputs, downloads a pinned wp-cli phar into the runner's temp + * directory and checks its sha512, runs make-pot on the runner's PHP, then compares the POT with + * the one git tracks. The commands run through `exec`, so a test can pass stand-ins and assert the + * calls. + */ + +const crypto = require('node:crypto') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') + +const { runOrFail } = require('../../lib/exec.js') +const { pharExists } = require('../../lib/pup.js') +const { readWorkingDirectory, readCheckoutFile } = require('../../lib/workspace.js') + +/** What was not done, to close every error message. */ +const WHAT = 'No POT was generated.' + +/** + * The wp-cli release that runs make-pot, and the sha512 of its phar. A release with its checksum, + * not the newest build: the phar runs in jobs that hold a token. Bump both together. + */ +const WP_CLI = { + version: '2.12.0', + sha512: + 'be928f6b8ca1e8dfb9d2f4b75a13aa4aee0896f8a9a0a1c45cd5d2c98605e6172e6d014dda2e27f88c98befc16c040cbb2bd1bfa121510ea5cdf5f6a30fe8832', +} + +/** + * The header lines make-pot stamps on every run: the time, the plugin version and its own version. + * A POT that differs in these only holds the same strings. + */ +const STAMPED_HEADERS = ['POT-Creation-Date:', 'Project-Id-Version:', 'X-Generator:'] + +/** + * Splits a multi-line value into its lines, trimmed, without the empty ones and the `#` comments. + * + * @param {string|null} text The value: an input, or a file's contents. + * + * @returns {string[]} The lines. + */ +function readLines(text) { + return String(text ?? '') + .split('\n') + .map((line) => line.trim()) + .filter((line) => line !== '' && !line.startsWith('#')) +} + +/** + * Reads the `pot-path` input: where the POT is written, relative to the checkout. + * + * @param {string} value The input value. + * @param {string} cwd The checkout. + * + * @throws {Error} When it is empty, absolute, or leads outside the checkout. + * + * @returns {string} The path, as given. + */ +function readPotPath(value, cwd) { + const trimmed = String(value ?? '').trim() + + if (trimmed === '') { + throw new Error(`pot-path is empty. Pass where the POT is written, such as lang/my-plugin.pot. ${WHAT}`) + } + + const relative = path.relative(cwd, path.resolve(cwd, trimmed)) + if (path.isAbsolute(trimmed) || relative === '' || relative.startsWith('..') || path.isAbsolute(relative)) { + throw new Error( + `pot-path '${trimmed}' is not a path inside the checkout. Pass it relative to the checkout, such as ` + + `lang/my-plugin.pot. ${WHAT}` + ) + } + + return trimmed +} + +/** + * Reads the `headers` input: the POT headers to set, as a JSON object of strings. + * + * @param {string} value The input value. Empty sets no header. + * + * @throws {Error} When it is not JSON, or not an object whose values are all strings. + * + * @returns {string|null} The object as compact JSON, or null for an empty value. + */ +function readHeaders(value) { + const trimmed = String(value ?? '').trim() + + if (trimmed === '') { + return null + } + + let headers + try { + headers = JSON.parse(trimmed) + } catch (error) { + throw new Error(`headers is not valid JSON (${error.message}). ${WHAT}`) + } + + if ( + headers === null || + typeof headers !== 'object' || + Array.isArray(headers) || + Object.values(headers).some((header) => typeof header !== 'string') + ) { + throw new Error( + `headers is not a JSON object of strings, such as {"Report-Msgid-Bugs-To": "https://example.com"}. ${WHAT}` + ) + } + + return JSON.stringify(headers) +} + +/** + * What make-pot leaves out: the `.distignore` lines, the git submodule paths and the `exclude` + * input, in that order, each once. + * + * A submodule has its own text domain and its own POT, and a checkout with submodules would fold + * its strings in. + * + * @param {string} cwd The checkout. + * @param {string} extra The `exclude` input. + * + * @returns {string[]} The exclusions. + */ +function readExcludes(cwd, extra) { + const submodules = [...(readCheckoutFile(cwd, '.gitmodules') ?? '').matchAll(/^\s*path\s*=\s*(.+?)\s*$/gm)].map( + (match) => match[1] + ) + + return [...new Set([...readLines(readCheckoutFile(cwd, '.distignore')), ...submodules, ...readLines(extra)])] +} + +/** + * Puts the pinned wp-cli phar in the runner's temp directory, so it never ends up in the checkout, + * and checks its sha512. A phar an earlier step of the job downloaded is reused, and checked again. + * A failed download or a wrong checksum leaves no file behind. + * + * @param {object} options + * @param {object} options.exec The @actions/exec toolkit. + * @param {object} options.env The step environment, for RUNNER_TEMP. + * @param {object} options.wpCli The release and its checksum. + * + * @throws {Error} When the download fails or the checksum does not match. + * + * @returns {Promise} The path of the phar. + */ +async function setupWpCli({ exec, env, wpCli }) { + const { version, sha512 } = wpCli + const phar = path.join(env.RUNNER_TEMP || os.tmpdir(), `wp-cli-${version}.phar`) + + try { + if (!pharExists(phar)) { + const url = `https://github.com/wp-cli/wp-cli/releases/download/v${version}/wp-cli-${version}.phar` + + await runOrFail(exec, 'curl', ['-fsSL', '--retry', '3', '-o', phar, url], {}, `Downloading wp-cli ${version}`) + } + + const actual = crypto.createHash('sha512').update(fs.readFileSync(phar)).digest('hex') + if (actual !== sha512) { + throw new Error(`The wp-cli ${version} phar does not match its sha512: expected ${sha512}, got ${actual}. ${WHAT}`) + } + } catch (error) { + fs.rmSync(phar, { force: true }) + + throw error + } + + return phar +} + +/** + * Whether the POT's strings changed, against the file git tracks. A tracked POT that differs in + * the stamped headers only is put back as it was, so a second run leaves nothing to commit. + * + * @param {object} exec The @actions/exec toolkit. + * @param {string} cwd The checkout. + * @param {string} potPath The POT, relative to the checkout. + * + * @throws {Error} When git fails: the directory is not a git checkout. + * + * @returns {Promise} True when the POT is new or its strings changed. + */ +async function stringsChanged(exec, cwd, potPath) { + const options = { cwd, silent: true } + const tracked = await runOrFail(exec, 'git', ['ls-files', '--', potPath], options, 'git ls-files') + + if (tracked.trim() === '') { + return true + } + + const diff = await exec.getExecOutput( + 'git', + ['diff', '--quiet', ...STAMPED_HEADERS.map((header) => `-I${header}`), '--', potPath], + { ...options, ignoreReturnCode: true } + ) + + if (diff.exitCode === 1) { + return true + } + + if (diff.exitCode !== 0) { + throw new Error(`git diff failed (exit code ${diff.exitCode}):\n${diff.stderr.trim()}`) + } + + await runOrFail(exec, 'git', ['checkout', '--', potPath], options, 'git checkout') + + return false +} + +/** + * Validates the inputs, runs make-pot and sets the outputs. + * + * Every input is checked before the phar is downloaded, and the phar is checked before it runs. + * + * @param {object} options + * @param {object} options.core The @actions/core toolkit, for the log and the outputs. + * @param {object} options.exec The @actions/exec toolkit. + * @param {object} options.env The environment the action manifest put its inputs in. + * @param {string} options.cwd The workspace, the process's working directory by default; + * `working-directory` is resolved against it. + * @param {object} options.wpCli The wp-cli release and its checksum; a test passes a stand-in. + * + * @throws {Error} When an input is wrong, the phar cannot be downloaded or fails its checksum, + * make-pot fails or writes no file, or git fails. + * + * @returns {Promise} Resolves once the outputs are set. + */ +async function runGeneratePot({ core, exec, env, cwd: workspace = process.cwd(), wpCli = WP_CLI }) { + const cwd = readWorkingDirectory(env.INPUT_WORKING_DIRECTORY, { base: workspace, what: WHAT }) + const potPath = readPotPath(env.INPUT_POT_PATH, cwd) + const sourcePath = String(env.INPUT_SOURCE_PATH ?? '').trim() || '.' + const domain = String(env.INPUT_DOMAIN ?? '').trim() + const headers = readHeaders(env.INPUT_HEADERS) + const excludes = readExcludes(cwd, env.INPUT_EXCLUDE) + + const phar = await setupWpCli({ exec, env, wpCli }) + + const args = ['i18n', 'make-pot', sourcePath, potPath, '--skip-audit'] + if (domain !== '') { + args.push(`--domain=${domain}`) + } + if (headers !== null) { + args.push(`--headers=${headers}`) + } + if (excludes.length > 0) { + args.push(`--exclude=${excludes.join(',')}`) + } + + // The arguments are a list and there is no shell, so an input is never part of a command line. + await runOrFail(exec, 'php', ['-d', 'xdebug.mode=off', phar, ...args], { cwd }, 'wp i18n make-pot') + + if (!fs.existsSync(path.join(cwd, potPath))) { + throw new Error(`wp i18n make-pot wrote no file at ${potPath}. Check source-path and pot-path. ${WHAT}`) + } + + const changed = await stringsChanged(exec, cwd, potPath) + + core.info(changed ? `The strings of ${potPath} changed.` : `No string changed since the last ${potPath}.`) + core.setOutput('pot-path', potPath) + core.setOutput('changed', changed ? 'yes' : 'no') +} + +module.exports = { runGeneratePot, WP_CLI } diff --git a/tests/actions/generate-pot/generate-pot.test.js b/tests/actions/generate-pot/generate-pot.test.js new file mode 100644 index 0000000..13c68fc --- /dev/null +++ b/tests/actions/generate-pot/generate-pot.test.js @@ -0,0 +1,326 @@ +'use strict' + +const { describe, it, beforeEach, afterEach } = require('node:test') +const assert = require('node:assert') +const { execFileSync } = require('node:child_process') +const crypto = require('node:crypto') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') + +const { runGeneratePot, WP_CLI } = require('../../../.github/actions/generate-pot/generate-pot.js') +const { coreWithOutputs, recordingExec, recordedRealExec } = require('../../support/actions.js') + +/** A git that signs nothing, whatever the machine it runs on configures. */ +const GIT_ENV = { + ...process.env, + GIT_AUTHOR_NAME: 't', + GIT_AUTHOR_EMAIL: 't@example.com', + GIT_COMMITTER_NAME: 't', + GIT_COMMITTER_EMAIL: 't@example.com', + GIT_CONFIG_COUNT: '2', + GIT_CONFIG_KEY_0: 'tag.gpgsign', + GIT_CONFIG_VALUE_0: 'false', + GIT_CONFIG_KEY_1: 'commit.gpgsign', + GIT_CONFIG_VALUE_1: 'false', +} + +/** What the stand-in for curl writes as the phar, and the release whose checksum it matches. */ +const PHAR = ' `msgid "" +msgstr "" +"Project-Id-Version: Plugin ${version}\\n" +"Report-Msgid-Bugs-To: https://example.com\\n" +"POT-Creation-Date: ${date}\\n" +"X-Generator: WP-CLI ${wp}\\n" +"X-Domain: plugin\\n" + +#: plugin.php:3 +msgid "${msgid}" +msgstr "" +` + +describe('generate-pot', () => { + let repo + let runnerTemp + + const git = (...args) => execFileSync('git', args, { cwd: repo, env: GIT_ENV, encoding: 'utf8', stdio: 'pipe' }) + + const write = (name, body) => { + fs.mkdirSync(path.dirname(path.join(repo, name)), { recursive: true }) + fs.writeFileSync(path.join(repo, name), body) + } + + const read = (name) => fs.readFileSync(path.join(repo, name), 'utf8') + + const commit = () => { + git('add', '--all') + git('commit', '-q', '--allow-empty', '-m', 'commit') + } + + /** + * Runs the action with real git, and stand-ins for curl and php. + * + * @param {object} t The node:test context. + * @param {object} options + * @param {object} options.inputs The INPUT_* variables, over a `pot-path` of `lang/plugin.pot`. + * @param {string} options.generated What the stand-in for make-pot writes; undefined writes nothing. + * @param {string} options.phar What the stand-in for curl downloads. + * @param {object} options.wpCli The release expected; the stand-in's by default. + * @param {string} options.cwd The workspace. + * + * @returns {Promise<{outputs: object, calls: object[], php: object}>} The outputs, the curl and + * php calls, and the php call. + */ + const run = async (t, { inputs = {}, generated, phar = PHAR, wpCli = FAKE_WP_CLI, cwd = repo } = {}) => { + const { core, outputs } = await coreWithOutputs(t) + const real = await recordedRealExec() + const fake = recordingExec([ + { command: 'curl', effect: (args) => fs.writeFileSync(args[args.indexOf('-o') + 1], phar) }, + { + command: 'php', + effect: (args, options) => { + if (generated !== undefined) { + const target = path.join(options.cwd, args[args.indexOf('make-pot') + 2]) + fs.mkdirSync(path.dirname(target), { recursive: true }) + fs.writeFileSync(target, generated) + } + }, + }, + ]) + const exec = { + getExecOutput: (command, args, options) => + (command === 'git' ? real.exec : fake.exec).getExecOutput(command, args, { ...options, env: GIT_ENV }), + } + + await runGeneratePot({ + core, + exec, + env: { RUNNER_TEMP: runnerTemp, INPUT_POT_PATH: 'lang/plugin.pot', ...inputs }, + cwd, + wpCli, + }) + + return { outputs: outputs(), calls: fake.calls, php: fake.calls.find((call) => call.command === 'php') } + } + + beforeEach(() => { + repo = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'generate-pot-'))) + runnerTemp = fs.mkdtempSync(path.join(os.tmpdir(), 'generate-pot-temp-')) + git('init', '-q') + write('plugin.php', ' { + fs.rmSync(repo, { recursive: true, force: true }) + fs.rmSync(runnerTemp, { recursive: true, force: true }) + }) + + describe('the wp-cli phar', () => { + it('pins the release TEC runs', () => { + assert.deepStrictEqual(WP_CLI, { + version: '2.12.0', + sha512: + 'be928f6b8ca1e8dfb9d2f4b75a13aa4aee0896f8a9a0a1c45cd5d2c98605e6172e6d014dda2e27f88c98befc16c040cbb2bd1bfa121510ea5cdf5f6a30fe8832', + }) + }) + + it('downloads the pinned release into the runner temp directory and runs it', async (t) => { + const { calls, php } = await run(t, { generated: pot() }) + const phar = path.join(runnerTemp, 'wp-cli-9.9.9.phar') + + assert.deepStrictEqual(calls[0].args.slice(-3), [ + '-o', + phar, + 'https://github.com/wp-cli/wp-cli/releases/download/v9.9.9/wp-cli-9.9.9.phar', + ]) + assert.deepStrictEqual(php.args.slice(0, 5), ['-d', 'xdebug.mode=off', phar, 'i18n', 'make-pot']) + assert.strictEqual(php.options.cwd, repo) + }) + + it('fails on a checksum mismatch, runs nothing and leaves no file behind', async (t) => { + await assert.rejects(run(t, { wpCli: WP_CLI }), /wp-cli 2\.12\.0.*sha512.*No POT was generated/s) + + assert.deepStrictEqual(fs.readdirSync(runnerTemp), []) + assert.ok(!fs.existsSync(path.join(repo, 'lang/plugin.pot'))) + }) + + it('checks a phar an earlier step left before it is reused', async (t) => { + fs.writeFileSync(path.join(runnerTemp, 'wp-cli-9.9.9.phar'), 'tampered') + + await assert.rejects(run(t), /sha512/) + assert.deepStrictEqual(fs.readdirSync(runnerTemp), []) + }) + + it('reuses a phar that matches, without a download', async (t) => { + fs.writeFileSync(path.join(runnerTemp, 'wp-cli-9.9.9.phar'), PHAR) + + const { calls } = await run(t, { generated: pot() }) + + assert.deepStrictEqual(calls.map((call) => call.command), ['php']) + }) + + it('leaves no file behind when the download fails', async (t) => { + const { core } = await coreWithOutputs(t) + const { exec } = recordingExec([ + { command: 'curl', exitCode: 56, stderr: 'Connection reset', effect: (args) => fs.writeFileSync(args[args.indexOf('-o') + 1], 'half') }, + ]) + + await assert.rejects( + runGeneratePot({ core, exec, env: { RUNNER_TEMP: runnerTemp, INPUT_POT_PATH: 'lang/plugin.pot' }, cwd: repo }), + /Downloading wp-cli 2\.12\.0 failed.*Connection reset/s + ) + assert.deepStrictEqual(fs.readdirSync(runnerTemp), []) + }) + }) + + describe('the make-pot command', () => { + it('runs with the defaults: the checkout as the source, the audit skipped, nothing else', async (t) => { + const { php } = await run(t, { generated: pot() }) + + assert.deepStrictEqual(php.args.slice(3), ['i18n', 'make-pot', '.', 'lang/plugin.pot', '--skip-audit']) + }) + + it('passes the source, the domain and the headers', async (t) => { + const { php } = await run(t, { + generated: pot(), + inputs: { + INPUT_SOURCE_PATH: 'src', + INPUT_DOMAIN: 'kadence-blocks', + INPUT_HEADERS: '{ "Report-Msgid-Bugs-To": "https://example.com/bugs", "Last-Translator": "" }', + }, + }) + + assert.deepStrictEqual(php.args.slice(5), [ + 'src', + 'lang/plugin.pot', + '--skip-audit', + '--domain=kadence-blocks', + '--headers={"Report-Msgid-Bugs-To":"https://example.com/bugs","Last-Translator":""}', + ]) + }) + + it('excludes the .distignore lines, the submodule paths and the exclude input', async (t) => { + write('.distignore', '# Development files\n/tests\n\nnode_modules\r\n') + write('.gitmodules', '[submodule "common"]\n\tpath = common\n\turl = https://example.com/common.git\n[submodule "x"]\n path=libs/x\n') + + const { php } = await run(t, { generated: pot(), inputs: { INPUT_EXCLUDE: '*.js.map\n\n src/**/*.js \nnode_modules\n' } }) + + assert.strictEqual(php.args.at(-1), '--exclude=/tests,node_modules,common,libs/x,*.js.map,src/**/*.js') + }) + + it('reads .distignore in the working directory', async (t) => { + const workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'generate-pot-workspace-')) + t.after(() => fs.rmSync(workspace, { recursive: true, force: true })) + fs.renameSync(repo, path.join(workspace, 'source')) + repo = path.join(workspace, 'source') + write('.distignore', 'vendor\n') + + const { php, outputs } = await run(t, { generated: pot(), cwd: workspace, inputs: { INPUT_WORKING_DIRECTORY: 'source' } }) + + assert.strictEqual(php.options.cwd, repo) + assert.strictEqual(php.args.at(-1), '--exclude=vendor') + assert.strictEqual(outputs['pot-path'], 'lang/plugin.pot') + }) + + it('fails when make-pot fails', async (t) => { + const { core } = await coreWithOutputs(t) + fs.writeFileSync(path.join(runnerTemp, 'wp-cli-9.9.9.phar'), PHAR) + const { exec } = recordingExec([{ command: 'php', exitCode: 1, stderr: 'Error: Not a valid source directory.' }]) + + await assert.rejects( + runGeneratePot({ core, exec, env: { RUNNER_TEMP: runnerTemp, INPUT_POT_PATH: 'lang/plugin.pot' }, cwd: repo, wpCli: FAKE_WP_CLI }), + /wp i18n make-pot failed.*Not a valid source directory/s + ) + }) + }) + + describe('the inputs', () => { + const refused = [ + ['an empty pot-path', { INPUT_POT_PATH: ' ' }, /pot-path is empty/], + ['an absolute pot-path', { INPUT_POT_PATH: '/tmp/plugin.pot' }, /pot-path '\/tmp\/plugin\.pot' is not a path inside the checkout/], + ['a pot-path outside the checkout', { INPUT_POT_PATH: '../plugin.pot' }, /not a path inside the checkout/], + ['headers that are not JSON', { INPUT_HEADERS: '{nope' }, /headers is not valid JSON/], + ['headers that are not an object', { INPUT_HEADERS: '["a"]' }, /headers is not a JSON object of strings/], + ['a header that is not a string', { INPUT_HEADERS: '{"a": 1}' }, /headers is not a JSON object of strings/], + ['a working-directory that is not there', { INPUT_WORKING_DIRECTORY: 'nowhere' }, /working-directory 'nowhere' is not a directory/], + ] + + for (const [name, inputs, message] of refused) { + it(`refuses ${name} before anything is downloaded`, async (t) => { + const { core } = await coreWithOutputs(t) + const { exec, calls } = recordingExec() + + await assert.rejects(runGeneratePot({ core, exec, env: { RUNNER_TEMP: runnerTemp, INPUT_POT_PATH: 'lang/plugin.pot', ...inputs }, cwd: repo }), message) + assert.deepStrictEqual(calls, []) + }) + } + }) + + describe('the changed check', () => { + it('reports a new POT as changed', async (t) => { + const { outputs } = await run(t, { generated: pot() }) + + assert.deepStrictEqual(outputs, { 'pot-path': 'lang/plugin.pot', changed: 'yes' }) + assert.strictEqual(read('lang/plugin.pot'), pot()) + }) + + it('reports a POT with a changed string as changed and keeps it', async (t) => { + write('lang/plugin.pot', pot()) + commit() + const generated = pot({ date: '2026-02-02T00:00:00+00:00', msgid: 'Goodbye' }) + + const { outputs } = await run(t, { generated }) + + assert.strictEqual(outputs.changed, 'yes') + assert.strictEqual(read('lang/plugin.pot'), generated) + }) + + it('puts back a POT that differs in the three stamped headers only', async (t) => { + write('lang/plugin.pot', pot()) + commit() + + const { outputs } = await run(t, { generated: pot({ date: '2026-02-02T00:00:00+00:00', version: '1.1.0', wp: '2.13.0' }) }) + + assert.strictEqual(outputs.changed, 'no') + assert.strictEqual(read('lang/plugin.pot'), pot()) + assert.strictEqual(git('status', '--porcelain'), '') + }) + + it('reports a POT that another header changed as changed', async (t) => { + write('lang/plugin.pot', pot()) + commit() + + const { outputs } = await run(t, { generated: pot().replace('X-Domain: plugin', 'X-Domain: other') }) + + assert.strictEqual(outputs.changed, 'yes') + }) + + it('reports an identical POT as unchanged', async (t) => { + write('lang/plugin.pot', pot()) + commit() + + const { outputs } = await run(t, { generated: pot() }) + + assert.strictEqual(outputs.changed, 'no') + }) + + it('fails when make-pot wrote no POT', async (t) => { + await assert.rejects(run(t), /wrote no file at lang\/plugin\.pot/) + }) + }) +}) From f4e7cdeeb8d449b1e382716a58b388b2d6431721 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:18:49 -0300 Subject: [PATCH 2/6] generate-pot downloads and checks wp-cli with the shared phar helper --- .github/actions/generate-pot/generate-pot.js | 36 +++++++------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js index c5ebbda..0b95038 100644 --- a/.github/actions/generate-pot/generate-pot.js +++ b/.github/actions/generate-pot/generate-pot.js @@ -9,13 +9,12 @@ * calls. */ -const crypto = require('node:crypto') const fs = require('node:fs') const os = require('node:os') const path = require('node:path') const { runOrFail } = require('../../lib/exec.js') -const { pharExists } = require('../../lib/pup.js') +const { downloadPhar } = require('../../lib/phar.js') const { readWorkingDirectory, readCheckoutFile } = require('../../lib/workspace.js') /** What was not done, to close every error message. */ @@ -142,6 +141,7 @@ function readExcludes(cwd, extra) { * A failed download or a wrong checksum leaves no file behind. * * @param {object} options + * @param {object} options.core The @actions/core toolkit, to say when the phar is reused. * @param {object} options.exec The @actions/exec toolkit. * @param {object} options.env The step environment, for RUNNER_TEMP. * @param {object} options.wpCli The release and its checksum. @@ -150,28 +150,18 @@ function readExcludes(cwd, extra) { * * @returns {Promise} The path of the phar. */ -async function setupWpCli({ exec, env, wpCli }) { +function setupWpCli({ core, exec, env, wpCli }) { const { version, sha512 } = wpCli - const phar = path.join(env.RUNNER_TEMP || os.tmpdir(), `wp-cli-${version}.phar`) - try { - if (!pharExists(phar)) { - const url = `https://github.com/wp-cli/wp-cli/releases/download/v${version}/wp-cli-${version}.phar` - - await runOrFail(exec, 'curl', ['-fsSL', '--retry', '3', '-o', phar, url], {}, `Downloading wp-cli ${version}`) - } - - const actual = crypto.createHash('sha512').update(fs.readFileSync(phar)).digest('hex') - if (actual !== sha512) { - throw new Error(`The wp-cli ${version} phar does not match its sha512: expected ${sha512}, got ${actual}. ${WHAT}`) - } - } catch (error) { - fs.rmSync(phar, { force: true }) - - throw error - } - - return phar + return downloadPhar({ + exec, + phar: path.join(env.RUNNER_TEMP || os.tmpdir(), `wp-cli-${version}.phar`), + url: `https://github.com/wp-cli/wp-cli/releases/download/v${version}/wp-cli-${version}.phar`, + label: `wp-cli ${version}`, + sha512, + what: WHAT, + core, + }) } /** @@ -239,7 +229,7 @@ async function runGeneratePot({ core, exec, env, cwd: workspace = process.cwd(), const headers = readHeaders(env.INPUT_HEADERS) const excludes = readExcludes(cwd, env.INPUT_EXCLUDE) - const phar = await setupWpCli({ exec, env, wpCli }) + const phar = await setupWpCli({ core, exec, env, wpCli }) const args = ['i18n', 'make-pot', sourcePath, potPath, '--skip-audit'] if (domain !== '') { From 70e75a63308456efd15a64f7f9f76753b4aa36cf Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:20:41 -0300 Subject: [PATCH 3/6] generate-pot ignores the stamped fields in the POT header entry only --- .github/actions/generate-pot/README.md | 2 ++ .github/actions/generate-pot/generate-pot.js | 36 ++++++++++++------- .../actions/generate-pot/generate-pot.test.js | 11 ++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/.github/actions/generate-pot/README.md b/.github/actions/generate-pot/README.md index 20567e0..61abde9 100644 --- a/.github/actions/generate-pot/README.md +++ b/.github/actions/generate-pot/README.md @@ -63,6 +63,8 @@ With none of them, make-pot uses its own defaults. make-pot stamps the time (`POT-Creation-Date`), the plugin version (`Project-Id-Version`) and its own version (`X-Generator`) on every run, so the file differs each time even with the same strings. +The check leaves out these three lines of the header entry only: a translation entry that holds one +of the names is compared like any other. | The POT | `changed` | The file | |---|---|---| diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js index 0b95038..3c7cc45 100644 --- a/.github/actions/generate-pot/generate-pot.js +++ b/.github/actions/generate-pot/generate-pot.js @@ -31,10 +31,10 @@ const WP_CLI = { } /** - * The header lines make-pot stamps on every run: the time, the plugin version and its own version. + * The header fields make-pot stamps on every run: the time, the plugin version and its own version. * A POT that differs in these only holds the same strings. */ -const STAMPED_HEADERS = ['POT-Creation-Date:', 'Project-Id-Version:', 'X-Generator:'] +const STAMPED_HEADERS = ['POT-Creation-Date', 'Project-Id-Version', 'X-Generator'] /** * Splits a multi-line value into its lines, trimmed, without the empty ones and the `#` comments. @@ -164,9 +164,26 @@ function setupWpCli({ core, exec, env, wpCli }) { }) } +/** + * A POT without the stamped header fields. Only the header entry, the first block before a blank + * line, is read for them, so a translation entry that holds a header name keeps it. + * + * @param {string} pot The POT's contents. + * + * @returns {string} The contents without the stamped header lines. + */ +function withoutStampedHeaders(pot) { + const normalized = pot.replace(/\r\n/g, '\n') + const end = normalized.indexOf('\n\n') + const header = end === -1 ? normalized : normalized.slice(0, end) + const kept = header.split('\n').filter((line) => !STAMPED_HEADERS.some((field) => line.startsWith(`"${field}:`))) + + return kept.join('\n') + normalized.slice(header.length) +} + /** * Whether the POT's strings changed, against the file git tracks. A tracked POT that differs in - * the stamped headers only is put back as it was, so a second run leaves nothing to commit. + * the stamped header fields only is put back as it was, so a second run leaves nothing to commit. * * @param {object} exec The @actions/exec toolkit. * @param {string} cwd The checkout. @@ -184,20 +201,13 @@ async function stringsChanged(exec, cwd, potPath) { return true } - const diff = await exec.getExecOutput( - 'git', - ['diff', '--quiet', ...STAMPED_HEADERS.map((header) => `-I${header}`), '--', potPath], - { ...options, ignoreReturnCode: true } - ) + const before = await runOrFail(exec, 'git', ['show', `:./${potPath}`], options, 'git show') + const after = fs.readFileSync(path.join(cwd, potPath), 'utf8') - if (diff.exitCode === 1) { + if (withoutStampedHeaders(before) !== withoutStampedHeaders(after)) { return true } - if (diff.exitCode !== 0) { - throw new Error(`git diff failed (exit code ${diff.exitCode}):\n${diff.stderr.trim()}`) - } - await runOrFail(exec, 'git', ['checkout', '--', potPath], options, 'git checkout') return false diff --git a/tests/actions/generate-pot/generate-pot.test.js b/tests/actions/generate-pot/generate-pot.test.js index 13c68fc..65c4e33 100644 --- a/tests/actions/generate-pot/generate-pot.test.js +++ b/tests/actions/generate-pot/generate-pot.test.js @@ -301,6 +301,17 @@ describe('generate-pot', () => { assert.strictEqual(git('status', '--porcelain'), '') }) + it('reports a changed string that holds a stamped header name as changed', async (t) => { + write('lang/plugin.pot', pot({ msgid: 'X-Generator: old' })) + commit() + const generated = pot({ msgid: 'X-Generator: new' }) + + const { outputs } = await run(t, { generated }) + + assert.strictEqual(outputs.changed, 'yes') + assert.strictEqual(read('lang/plugin.pot'), generated) + }) + it('reports a POT that another header changed as changed', async (t) => { write('lang/plugin.pot', pot()) commit() From 9dd8c134bc0f9b7ca46c03eae2d5991cc6118ea8 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:21:10 -0300 Subject: [PATCH 4/6] generate-pot makes the .distignore and submodule exclusions relative to source-path --- .github/actions/generate-pot/README.md | 9 ++++- .github/actions/generate-pot/action.yml | 7 ++-- .github/actions/generate-pot/generate-pot.js | 39 +++++++++++++++---- .../actions/generate-pot/generate-pot.test.js | 9 +++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/.github/actions/generate-pot/README.md b/.github/actions/generate-pot/README.md index 61abde9..9d27fa2 100644 --- a/.github/actions/generate-pot/README.md +++ b/.github/actions/generate-pot/README.md @@ -44,7 +44,12 @@ Three lists are joined into `--exclude`, each entry once: Empty lines and `#` comments are skipped. - The path of each git submodule, read from `.gitmodules`. A submodule has its own text domain and its own POT, and its strings do not belong in this one. -- The lines of the `exclude` input. +- The lines of the `exclude` input, passed as given. + +make-pot matches an exclusion against paths relative to `source-path`, so the `.distignore` and +submodule paths are made relative to it first: with `source-path: src`, `src/admin` becomes +`admin`, and a path outside `src`, such as `/tests`, is dropped. A `.distignore` line with no `/` +in it, such as `node_modules`, is a name that matches at any depth, and is kept as it is. With none of them, make-pot uses its own defaults. @@ -82,7 +87,7 @@ A second run therefore leaves nothing to commit. The check needs a git checkout, | `source-path` | `.` | The directory scanned for strings, relative to the checkout. | | `domain` | | The text domain, passed as `--domain`. Empty leaves it to make-pot. | | `headers` | | POT headers to set, as a JSON object of strings in a string. Empty sets none. | -| `exclude` | | Extra exclusions, one per line, added to `.distignore` and the submodule paths. | +| `exclude` | | Extra exclusions, one per line, relative to `source-path`, added to `.distignore` and the submodule paths. | | `working-directory` | | Where the repository is checked out. Empty is the workspace. | ## Outputs diff --git a/.github/actions/generate-pot/action.yml b/.github/actions/generate-pot/action.yml index d83b5f8..7351542 100644 --- a/.github/actions/generate-pot/action.yml +++ b/.github/actions/generate-pot/action.yml @@ -33,9 +33,10 @@ inputs: default: '' exclude: description: >- - Extra files and directories to leave out, one per line, as `--exclude` takes them, e.g. - `*.js.map`. They are added to the lines of `.distignore` and the git submodule paths, which - are always left out. + Extra files and directories to leave out, one per line, as `--exclude` takes them: relative to + `source-path`, e.g. `*.js.map`. They are added to the lines of `.distignore` and the git + submodule paths, which are always left out once made relative to `source-path`; the ones + outside it are dropped. required: false default: '' working-directory: diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js index 3c7cc45..20f601a 100644 --- a/.github/actions/generate-pot/generate-pot.js +++ b/.github/actions/generate-pot/generate-pot.js @@ -115,24 +115,49 @@ function readHeaders(value) { return JSON.stringify(headers) } +/** + * Makes a checkout path relative to the scanned directory: make-pot matches `--exclude` against + * paths relative to `source-path`. A line without a `/` other than a trailing one is a name that + * matches at any depth, and is kept as it is. A line with one is a path from the checkout root. + * + * @param {string} line The `.distignore` line or submodule path. + * @param {string} base The scanned directory, relative to the checkout; empty for the checkout. + * @param {boolean} anchored True for a submodule path, which is always from the checkout root. + * + * @returns {string|null} The exclusion, or null for a path outside the scanned directory. + */ +function rebaseExclude(line, base, anchored) { + const trimmed = line.replace(/^\/+|\/+$/g, '') + + if (base === '' || (!anchored && !trimmed.includes('/') && !line.startsWith('/'))) { + return line + } + + return trimmed.startsWith(`${base}/`) ? trimmed.slice(base.length + 1) : null +} + /** * What make-pot leaves out: the `.distignore` lines, the git submodule paths and the `exclude` - * input, in that order, each once. + * input, in that order, each once. The first two are paths from the checkout, made relative to + * `source-path`; the ones outside it are dropped. The `exclude` input is passed as given. * * A submodule has its own text domain and its own POT, and a checkout with submodules would fold * its strings in. * - * @param {string} cwd The checkout. - * @param {string} extra The `exclude` input. + * @param {string} cwd The checkout. + * @param {string} sourcePath The `source-path` input. + * @param {string} extra The `exclude` input. * * @returns {string[]} The exclusions. */ -function readExcludes(cwd, extra) { +function readExcludes(cwd, sourcePath, extra) { + const base = path.relative(cwd, path.resolve(cwd, sourcePath)).split(path.sep).join('/') const submodules = [...(readCheckoutFile(cwd, '.gitmodules') ?? '').matchAll(/^\s*path\s*=\s*(.+?)\s*$/gm)].map( - (match) => match[1] + (match) => rebaseExclude(match[1], base, true) ) + const distignore = readLines(readCheckoutFile(cwd, '.distignore')).map((line) => rebaseExclude(line, base, false)) - return [...new Set([...readLines(readCheckoutFile(cwd, '.distignore')), ...submodules, ...readLines(extra)])] + return [...new Set([...distignore, ...submodules, ...readLines(extra)].filter((exclude) => exclude !== null))] } /** @@ -237,7 +262,7 @@ async function runGeneratePot({ core, exec, env, cwd: workspace = process.cwd(), const sourcePath = String(env.INPUT_SOURCE_PATH ?? '').trim() || '.' const domain = String(env.INPUT_DOMAIN ?? '').trim() const headers = readHeaders(env.INPUT_HEADERS) - const excludes = readExcludes(cwd, env.INPUT_EXCLUDE) + const excludes = readExcludes(cwd, sourcePath, env.INPUT_EXCLUDE) const phar = await setupWpCli({ core, exec, env, wpCli }) diff --git a/tests/actions/generate-pot/generate-pot.test.js b/tests/actions/generate-pot/generate-pot.test.js index 65c4e33..6c8ef5a 100644 --- a/tests/actions/generate-pot/generate-pot.test.js +++ b/tests/actions/generate-pot/generate-pot.test.js @@ -223,6 +223,15 @@ describe('generate-pot', () => { assert.strictEqual(php.args.at(-1), '--exclude=/tests,node_modules,common,libs/x,*.js.map,src/**/*.js') }) + it('makes the .distignore and submodule paths relative to source-path, and drops the ones outside it', async (t) => { + write('.distignore', '/tests\nnode_modules\nsrc/internal.php\n/src/admin/\nassets/js\n') + write('.gitmodules', '[submodule "lib"]\n\tpath = src/vendor/lib\n[submodule "common"]\n\tpath = common\n') + + const { php } = await run(t, { generated: pot(), inputs: { INPUT_SOURCE_PATH: './src/', INPUT_EXCLUDE: 'src/*.map' } }) + + assert.strictEqual(php.args.at(-1), '--exclude=node_modules,internal.php,admin,vendor/lib,src/*.map') + }) + it('reads .distignore in the working directory', async (t) => { const workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'generate-pot-workspace-')) t.after(() => fs.rmSync(workspace, { recursive: true, force: true })) From 256c64b7eaa8cedce9c1955496f8e38e6ccdd0ef Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 21:29:22 -0300 Subject: [PATCH 5/6] generate-pot keeps a .distignore rule that starts with a wildcard as it is when source-path is set --- .github/actions/generate-pot/README.md | 3 ++- .github/actions/generate-pot/generate-pot.js | 7 +++++-- tests/actions/generate-pot/generate-pot.test.js | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/actions/generate-pot/README.md b/.github/actions/generate-pot/README.md index 9d27fa2..3b330db 100644 --- a/.github/actions/generate-pot/README.md +++ b/.github/actions/generate-pot/README.md @@ -49,7 +49,8 @@ Three lists are joined into `--exclude`, each entry once: make-pot matches an exclusion against paths relative to `source-path`, so the `.distignore` and submodule paths are made relative to it first: with `source-path: src`, `src/admin` becomes `admin`, and a path outside `src`, such as `/tests`, is dropped. A `.distignore` line with no `/` -in it, such as `node_modules`, is a name that matches at any depth, and is kept as it is. +in it, such as `node_modules`, is a name that matches at any depth, and is kept as it is, and so is +a line whose first part holds a `*`, such as `**/private/*.php`. With none of them, make-pot uses its own defaults. diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js index 20f601a..c894232 100644 --- a/.github/actions/generate-pot/generate-pot.js +++ b/.github/actions/generate-pot/generate-pot.js @@ -118,7 +118,8 @@ function readHeaders(value) { /** * Makes a checkout path relative to the scanned directory: make-pot matches `--exclude` against * paths relative to `source-path`. A line without a `/` other than a trailing one is a name that - * matches at any depth, and is kept as it is. A line with one is a path from the checkout root. + * matches at any depth, and so is one whose first part holds a `*`, such as a leading `**`: both + * are kept as they are. Any other line with a `/` is a path from the checkout root. * * @param {string} line The `.distignore` line or submodule path. * @param {string} base The scanned directory, relative to the checkout; empty for the checkout. @@ -129,7 +130,9 @@ function readHeaders(value) { function rebaseExclude(line, base, anchored) { const trimmed = line.replace(/^\/+|\/+$/g, '') - if (base === '' || (!anchored && !trimmed.includes('/') && !line.startsWith('/'))) { + const anyDepth = !line.startsWith('/') && (!trimmed.includes('/') || trimmed.split('/')[0].includes('*')) + + if (base === '' || (!anchored && anyDepth)) { return line } diff --git a/tests/actions/generate-pot/generate-pot.test.js b/tests/actions/generate-pot/generate-pot.test.js index 6c8ef5a..f8c75bf 100644 --- a/tests/actions/generate-pot/generate-pot.test.js +++ b/tests/actions/generate-pot/generate-pot.test.js @@ -224,12 +224,12 @@ describe('generate-pot', () => { }) it('makes the .distignore and submodule paths relative to source-path, and drops the ones outside it', async (t) => { - write('.distignore', '/tests\nnode_modules\nsrc/internal.php\n/src/admin/\nassets/js\n') + write('.distignore', '/tests\nnode_modules\nsrc/internal.php\n/src/admin/\nassets/js\n**/private/*.php\n*/fixtures\n') write('.gitmodules', '[submodule "lib"]\n\tpath = src/vendor/lib\n[submodule "common"]\n\tpath = common\n') const { php } = await run(t, { generated: pot(), inputs: { INPUT_SOURCE_PATH: './src/', INPUT_EXCLUDE: 'src/*.map' } }) - assert.strictEqual(php.args.at(-1), '--exclude=node_modules,internal.php,admin,vendor/lib,src/*.map') + assert.strictEqual(php.args.at(-1), '--exclude=node_modules,internal.php,admin,**/private/*.php,*/fixtures,vendor/lib,src/*.map') }) it('reads .distignore in the working directory', async (t) => { From f064f63383ae888fb33416d7373eb17e8d93e121 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 22:18:23 -0300 Subject: [PATCH 6/6] generate-pot says make-pot matches every exclusion at any depth, a leading / included --- .github/actions/generate-pot/README.md | 6 ++++++ .github/actions/generate-pot/generate-pot.js | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/actions/generate-pot/README.md b/.github/actions/generate-pot/README.md index 3b330db..c8cc3e3 100644 --- a/.github/actions/generate-pot/README.md +++ b/.github/actions/generate-pot/README.md @@ -52,6 +52,12 @@ submodule paths are made relative to it first: with `source-path: src`, `src/adm in it, such as `node_modules`, is a name that matches at any depth, and is kept as it is, and so is a line whose first part holds a `*`, such as `**/private/*.php`. +make-pot matches every exclusion at any depth, and drops a leading `/`: `/src/admin/` with +`source-path: src` becomes `admin`, which also leaves out `src/modules/admin/`, and `/tests` with +`source-path: .` also leaves out `src/tests/`. No `--exclude` value matches only at the top of the +scanned directory. When a directory deeper in the source has the same name as one `.distignore` +leaves out, rename one of them or narrow `source-path`. + With none of them, make-pot uses its own defaults. ```yaml diff --git a/.github/actions/generate-pot/generate-pot.js b/.github/actions/generate-pot/generate-pot.js index c894232..bb04d74 100644 --- a/.github/actions/generate-pot/generate-pot.js +++ b/.github/actions/generate-pot/generate-pot.js @@ -117,9 +117,13 @@ function readHeaders(value) { /** * Makes a checkout path relative to the scanned directory: make-pot matches `--exclude` against - * paths relative to `source-path`. A line without a `/` other than a trailing one is a name that - * matches at any depth, and so is one whose first part holds a `*`, such as a leading `**`: both - * are kept as they are. Any other line with a `/` is a path from the checkout root. + * paths relative to `source-path`. A line without a `/` other than a trailing one, or one whose + * first part holds a `*`, such as a leading `**`, is kept as it is. Any other line is read as a path + * from the checkout root and made relative to the scanned directory. + * + * make-pot drops a leading `/` and matches an exclusion at any depth, so the result is not anchored + * either: `admin` also leaves out `modules/admin`. There is no `--exclude` form that matches only at + * the top of the scanned directory. * * @param {string} line The `.distignore` line or submodule path. * @param {string} base The scanned directory, relative to the checkout; empty for the checkout.