Repository navigation
Verify bundled - add internal generator script #25229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mvorisek
wants to merge
7
commits into
php:master
Choose a base branch
from
mvorisek:verify_bundled_generator_script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67f684d
add "download-bundled" generator
mvorisek cda670f
add manual
mvorisek 0022a7a
address feedback - YAML nowdoc
mvorisek c456688
address feedback - better method name for "dorny filters"
mvorisek 4ddc25e
address feedback - describe what generates the workflow file
mvorisek 298995c
address feedback - allow to update header and describy what generates it
mvorisek 8177d26
address feedback - add comment about path being generated
mvorisek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
232 changes: 232 additions & 0 deletions
232
.github/scripts/download-bundled/make-workflow-file.php
This file contains hidden or 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,232 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Phpsrc\Ci\DownloadBundled; | ||
|
|
||
| // How to add a new bundled source? | ||
| // 1. add a new entry for the source into $bundles variable below (name and monitored directories/files, path is generated from the name) | ||
| // 2. run this make-workflow-file.php generator | ||
| // 3. edit the generated .github/scripts/download-bundled/<name>.sh file, see and be consistent with other files | ||
|
|
||
| // How to run this make-workflow-file.php generator? | ||
| // ``` | ||
| // docker run --rm -v "$(git rev-parse --show-toplevel)":/h php:latest /h/.github/scripts/download-bundled/make-workflow-file.php | ||
| // ``` | ||
|
|
||
| // How to update/download specific bundled source? | ||
| // 1. edit the bundled version inside relevant .github/scripts/download-bundled/<name>.sh file | ||
| // 2. run: | ||
| // ``` | ||
| // docker run --rm -v "$(git rev-parse --show-toplevel)":/f alpine:latest sh -c "apk add git && /f/.github/scripts/download-bundled/<name>.sh" | ||
| // ``` | ||
| // 3. review git changes and commit the changes | ||
|
|
||
| $bundles = [ | ||
| new Bundle('boost.context', ['Zend/asm']), | ||
| new Bundle('XSSE', [], ['Zend/zend_simd.h']), | ||
| new Bundle('JIT IR', ['ext/opcache/jit/ir']), | ||
| new Bundle('xxHash', ['ext/hash/xxhash']), | ||
| new Bundle('PCRE2', ['ext/pcre/pcre2lib']), | ||
| new Bundle('uriparser', ['ext/uri/uriparser']), | ||
| ]; | ||
|
|
||
| class Bundle | ||
| { | ||
| /** | ||
| * @param list<string> $directories | ||
| * @param list<string> $files | ||
| */ | ||
| public function __construct( | ||
| public string $name, | ||
| public array $directories, | ||
| public array $files = [] | ||
| ) {} | ||
|
|
||
| public function getNameForPath(): string | ||
| { | ||
| return preg_replace('~\W+~', '-', strtolower($this->name)); | ||
| } | ||
| } | ||
|
|
||
| class Generator | ||
| { | ||
| /** | ||
| * @param list<Bundle> $bundles | ||
| */ | ||
| public function __construct( | ||
| public array $bundles | ||
| ) {} | ||
|
|
||
| protected function getRepoDirectory(): string | ||
| { | ||
| return dirname(__DIR__, 3); | ||
| } | ||
|
|
||
| protected function indentString(string $value, int $levels, bool $inclFirstLine): string | ||
| { | ||
| return preg_replace( | ||
| '~' . ($inclFirstLine ? '^|' : '') . '(?<=\n)~', | ||
| str_repeat(' ', $levels), | ||
| $value | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $data | ||
| */ | ||
| protected function encodeYml($data): string | ||
| { | ||
| if (is_array($data)) { | ||
| $isList = array_is_list($data); | ||
| $resParts = []; | ||
| foreach ($data as $k => $v) { | ||
| $kEncoded = $isList | ||
| ? '-' | ||
| : $this->encodeYml($k) . ':'; | ||
| $vEncoded = $this->encodeYml($v); | ||
|
|
||
| $resParts[] = $kEncoded | ||
| . (!$isList && is_array($v) && $v !== [] ? "\n " : ' ') | ||
| . (is_array($v) ? $this->indentString($vEncoded, 1, false) : $vEncoded); | ||
| } | ||
|
|
||
| return implode("\n", $resParts); | ||
| } | ||
|
|
||
| if (preg_match('~^(\w+|\$\{\{[^\}]+\}\})$~', $data)) { | ||
| return $data; | ||
| } | ||
|
|
||
| return strpos($data, "\n") !== false | ||
| ? '|' . "\n" . $this->indentString($data, 1, true) | ||
| : '\'' . str_replace('\'', '\'\'', $data) . '\''; | ||
| } | ||
|
|
||
| public function makeWorkflowFile(): void | ||
| { | ||
| $content = <<<'YAML' | ||
| # | ||
| # whole file is generated by .github/scripts/download-bundled/make-workflow-file.php | ||
| # | ||
| name: Verify Bundled Files | ||
|
mvorisek marked this conversation as resolved.
Resolved
|
||
|
|
||
| on: | ||
| push: | ||
| paths: &paths | ||
| %paths% | ||
| pull_request: | ||
| paths: *paths | ||
| schedule: | ||
| - cron: "0 1 * * *" | ||
| workflow_dispatch: ~ | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| VERIFY_BUNDLED_FILES: | ||
| if: github.repository == 'php/php-src' || github.event_name == 'workflow_dispatch' | ||
| name: Verify Bundled Files | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - name: git checkout | ||
| uses: actions/checkout@v7 | ||
|
|
||
| - name: Detect changed files | ||
| if: ${{ github.event_name == 'push' || github.event_name == 'pull_request' }} | ||
| uses: dorny/paths-filter@v3 | ||
| id: changes | ||
| with: | ||
| filters: %filters% | ||
|
|
||
| %steps% | ||
|
|
||
| YAML; | ||
|
|
||
| $paths = [ | ||
| '.github/scripts/download-bundled/**', | ||
| ]; | ||
| foreach ($this->bundles as $bundle) { | ||
| foreach ($this->makeFiltersForDornyPathsFilterAction($bundle) as $p) { | ||
| if (str_starts_with($p, '.github/scripts/download-bundled/')) { | ||
| continue; | ||
| } | ||
|
|
||
| $paths[] = $p; | ||
| } | ||
| } | ||
| $content = str_replace('%paths%', $this->indentString($this->encodeYml($paths), 3, false), $content); | ||
|
|
||
| $filters = []; | ||
| foreach ($this->bundles as $bundle) { | ||
| $filters[$bundle->getNameForPath()] = $this->makeFiltersForDornyPathsFilterAction($bundle); | ||
| } | ||
| $content = str_replace('%filters%', $this->indentString($this->encodeYml($this->encodeYml($filters)), 5, false), $content); | ||
|
|
||
| $steps = []; | ||
| foreach ($this->bundles as $bundle) { | ||
| $steps[] = [ | ||
| 'name' => $bundle->name, | ||
| 'if' => '${{ !cancelled() && (steps.changes.outputs.' . $bundle->getNameForPath() . ' == \'true\' || github.event_name == \'schedule\' || github.event_name == \'workflow_dispatch\') }}', | ||
| 'run' => implode("\n", [ | ||
| 'echo "::group::Download"', | ||
| '.github/scripts/download-bundled/' . $bundle->getNameForPath() . '.sh', | ||
| 'echo "::endgroup::"', | ||
| 'echo "::group::Verify files"', | ||
| ...array_map(static fn ($v) => '.github/scripts/test-directory-unchanged.sh \'' . $v . '\'', array_merge($bundle->directories, $bundle->files)), | ||
| 'echo "::endgroup::"', | ||
| ]), | ||
| ]; | ||
| } | ||
| $content = str_replace('%steps%', $this->indentString($this->encodeYml($steps), 3, false), $content); | ||
|
|
||
| file_put_contents($this->getRepoDirectory() . '/.github/workflows/verify-bundled-files.yml', $content); | ||
| } | ||
|
|
||
| protected function makeFiltersForDornyPathsFilterAction(Bundle $bundle): array | ||
| { | ||
| return [ | ||
| '.github/scripts/download-bundled/' . $bundle->getNameForPath() . '.*', | ||
| ...array_map(static fn ($v) => $v . '/**', $bundle->directories), | ||
| ...$bundle->files, | ||
| ]; | ||
| } | ||
|
|
||
| public function makeDownloadScriptHeaders(): void | ||
| { | ||
| foreach ($this->bundles as $bundle) { | ||
| $this->makeDownloadScriptHeader($bundle); | ||
| } | ||
| } | ||
|
|
||
| protected function makeDownloadScriptHeader(Bundle $bundle): void | ||
|
mvorisek marked this conversation as resolved.
Resolved
|
||
| { | ||
| $scriptPath = $this->getRepoDirectory() . '/.github/scripts/download-bundled/' . $bundle->getNameForPath() . '.sh'; | ||
|
|
||
| $contentWithoutHeader = !file_exists($scriptPath) | ||
| ? "# TODO\n" | ||
| : preg_replace('~^.*\n# header above this line is generated by make-workflow-file.php\n+~s', '', file_get_contents($scriptPath)); | ||
|
|
||
| $header = <<<'YAML' | ||
| #!/bin/sh | ||
| set -ex | ||
| cd "$(dirname "$0")/../../.." | ||
|
|
||
| tmp_dir=%tmp_dir% | ||
| rm -rf "$tmp_dir" | ||
| # header above this line is generated by make-workflow-file.php | ||
|
|
||
|
|
||
| YAML; | ||
|
|
||
| $header = str_replace('%tmp_dir%', '/tmp/php-src-download-bundled/' . $bundle->getNameForPath(), $header); | ||
|
|
||
| file_put_contents($scriptPath, $header . $contentWithoutHeader); | ||
| } | ||
| } | ||
|
|
||
| $generator = new Generator($bundles); | ||
| $generator->makeWorkflowFile(); | ||
| $generator->makeDownloadScriptHeaders(); | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.