Skip to content

Commit

Permalink
Testing CI to replace files
Browse files Browse the repository at this point in the history
  • Loading branch information
d-miles committed Aug 20, 2024
1 parent 6a167a9 commit c8bad23
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/test-ci-script.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Test GitHub Script

on: [push]

permissions:
contents: read

jobs:
deploy:
permissions:
id-token: write
pages: write
runs-on: ubuntu-latest

name: Test GitHub Script
steps:
- uses: actions/checkout@v4

- uses: actions/github-script@v7
with:
script: |
core.startGroup('get replacements');
const versions = [8.1, 8.2, 8.3].map(x => x.toString());
core.info(versions);
const placeholders = {
CURRENT_VERSION: '${{ github.ref_name }}',
MIN_PHP_VERSION: versions.slice(0, 1).join(''),
TESTED_PHP_VERSIONS: `${versions.slice(0, versions.length - 1).join(', ')}, and ${versions.slice(-1)}`,
};
core.info(placeholders);
core.endGroup();
core.startGroup('get files');
const patterns = ['**/*.md', '**/*.html'];
const globber = await glob.create(patterns.join('\n'))
const files = await globber.glob()
core.info(files);
const fs = require('fs');
const regex = /<!--\s*([A-Z_-]+)\s*-->.*?<!--\s*END \1-->/gi;
for (const file of files) {
core.info(`${file}:`);
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
core.error(err);
return;
}
let replacements = {};
let match;
while (match = regex.exec(data)) {
const [replace, key] = match;
if (key in replacements) {
if (replace in replacements)
continue;
replacements[replace] = placeholders[key];
core.info(`\t${replace} => ${replacements[key]}`);
} else {
core.warning(`Found ${key} in ${file}, but no matching key in replacements`);
}
}
let newData = data;
for (const [key, value] of Object.entries(replacements)) {
newData = newData.replace(key, value);
}
if (newData !== data) {
fs.writeFile(file, newData, 'utf8', (err) => {
if (err) {
core.error(err);
return;
}
core.info(`\t${replacements.length} replacements made in ${file}`);
});
}
});
}
core.endGroup();

0 comments on commit c8bad23

Please # to comment.