forked from cdgco/php-rest-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |