Skip to content

Commit b87a530

Browse files
authored
Merge pull request #258 from codesnippetspro/flat-file
feat: run snippets from files
2 parents 5272b5a + 1680b33 commit b87a530

23 files changed

+1112
-110
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Playwright Test Runner
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
test-mode:
7+
required: true
8+
type: string
9+
description: 'Test mode: default or file-based-execution'
10+
project-name:
11+
required: true
12+
type: string
13+
description: 'Playwright project name to run'
14+
15+
jobs:
16+
playwright-test:
17+
name: Playwright tests (${{ inputs.test-mode == 'default' && 'Default Mode' || 'File-based Execution' }})
18+
runs-on: ubuntu-22.04
19+
steps:
20+
- name: Checkout source code
21+
uses: actions/checkout@v4
22+
23+
- name: Set up PHP
24+
uses: codesnippetspro/setup-php@v2
25+
with:
26+
php-version: "8.1"
27+
28+
- name: Set up Node.js
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version-file: .node-version
32+
cache: 'npm'
33+
34+
- name: Compute dependency hash
35+
id: deps-hash
36+
run: |
37+
set -euo pipefail
38+
tmpfile=$(mktemp)
39+
for f in src/composer.lock package-lock.json; do
40+
if [ -f "$f" ]; then
41+
cat "$f" >> "$tmpfile"
42+
fi
43+
done
44+
if [ -s "$tmpfile" ]; then
45+
deps_hash=$(shasum -a 1 "$tmpfile" | awk '{print $1}' | cut -c1-8)
46+
else
47+
deps_hash=$(echo "${GITHUB_SHA:-unknown}" | cut -c1-8)
48+
fi
49+
echo "deps_hash=$deps_hash" >> "$GITHUB_OUTPUT"
50+
51+
- name: Get build cache
52+
id: deps-cache
53+
uses: actions/cache/restore@v4
54+
with:
55+
path: |
56+
node_modules
57+
src/vendor
58+
key: ${{ runner.os }}-deps-${{ steps.deps-hash.outputs.deps_hash }}
59+
restore-keys: |
60+
${{ runner.os }}-deps-
61+
62+
- name: Install workflow dependencies (wp-env, playwright)
63+
if: steps.deps-cache.outputs.cache-hit != 'true'
64+
run: npm run prepare-environment:ci && npm run bundle
65+
66+
- name: Save vendor and node_modules cache
67+
if: steps.deps-cache.outputs.cache-hit != 'true'
68+
uses: actions/cache/save@v4
69+
with:
70+
path: |
71+
src/vendor
72+
node_modules
73+
key: ${{ runner.os }}-deps-${{ steps.deps-hash.outputs.deps_hash }}
74+
75+
- name: Start WordPress environment
76+
run: |
77+
npx wp-env start
78+
79+
- name: Activate code-snippets plugin
80+
run: npx wp-env run cli wp plugin activate code-snippets
81+
82+
- name: WordPress debug information
83+
run: |
84+
npx wp-env run cli wp core version
85+
npx wp-env run cli wp --info
86+
87+
- name: Install playwright/test
88+
run: |
89+
npx playwright install chromium
90+
91+
- name: Run Playwright tests
92+
run: npm run test:playwright -- --project=${{ inputs.project-name }}
93+
94+
- name: Stop WordPress environment
95+
if: always()
96+
run: npx wp-env stop
97+
98+
- uses: actions/upload-artifact@v4
99+
if: always()
100+
with:
101+
name: playwright-test-results-${{ inputs.test-mode }}
102+
path: test-results/
103+
if-no-files-found: ignore
104+
retention-days: 2

.github/workflows/playwright.yml

Lines changed: 21 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -24,107 +24,31 @@ concurrency:
2424
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
2525

2626
jobs:
27-
Playwright:
28-
name: Playwright test on PHP 8.1
29-
runs-on: ubuntu-22.04
27+
playwright-default:
3028
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
31-
steps:
32-
- name: Checkout source code
33-
uses: actions/checkout@v4
34-
35-
- name: Set up PHP
36-
uses: codesnippetspro/setup-php@v2
37-
with:
38-
php-version: "8.1"
39-
40-
- name: Set up Node.js
41-
uses: actions/setup-node@v4
42-
with:
43-
node-version-file: .node-version
44-
cache: 'npm'
45-
46-
- name: Compute dependency hash
47-
id: deps-hash
48-
run: |
49-
set -euo pipefail
50-
# concatenate existing lock files (src/composer.lock and package-lock.json)
51-
tmpfile=$(mktemp)
52-
for f in src/composer.lock package-lock.json; do
53-
if [ -f "$f" ]; then
54-
cat "$f" >> "$tmpfile"
55-
fi
56-
done
57-
if [ -s "$tmpfile" ]; then
58-
deps_hash=$(shasum -a 1 "$tmpfile" | awk '{print $1}' | cut -c1-8)
59-
else
60-
# no lock files found, fall back to short commit sha
61-
deps_hash=$(echo "${GITHUB_SHA:-unknown}" | cut -c1-8)
62-
fi
63-
echo "deps_hash=$deps_hash" >> "$GITHUB_OUTPUT"
64-
65-
- name: Get build cache
66-
id: deps-cache
67-
uses: actions/cache/restore@v4
68-
with:
69-
path: |
70-
node_modules
71-
src/vendor
72-
key: ${{ runner.os }}-deps-${{ steps.deps-hash.outputs.deps_hash }}
73-
restore-keys: |
74-
${{ runner.os }}-deps-
75-
76-
- name: Install workflow dependencies (wp-env, playwright)
77-
if: steps.deps-cache.outputs.cache-hit != 'true'
78-
run: npm run prepare-environment:ci && npm run bundle
79-
80-
- name: Save vendor and node_modules cache
81-
if: steps.deps-cache.outputs.cache-hit != 'true'
82-
uses: actions/cache/save@v4
83-
with:
84-
path: |
85-
src/vendor
86-
node_modules
87-
key: ${{ runner.os }}-deps-${{ steps.deps-hash.outputs.deps_hash }}
88-
89-
- name: Start WordPress environment
90-
run: |
91-
npx wp-env start
29+
uses: ./.github/workflows/playwright-test.yml
30+
with:
31+
test-mode: 'default'
32+
project-name: 'chromium-db-snippets'
9233

93-
- name: Activate code-snippets plugin
94-
run: npx wp-env run cli wp plugin activate code-snippets
95-
96-
- name: WordPress debug information
97-
run: |
98-
npx wp-env run cli wp core version
99-
npx wp-env run cli wp --info
100-
101-
- name: Install playwright/test
102-
run: |
103-
npx playwright install chromium
104-
105-
- name: Run Playwright tests
106-
run: npm run test:playwright
107-
108-
- name: Stop WordPress environment
109-
if: always()
110-
run: npx wp-env stop
111-
112-
- uses: actions/upload-artifact@v4
113-
if: always()
114-
with:
115-
name: playwright-test-results
116-
path: test-results/
117-
if-no-files-found: ignore
118-
retention-days: 2
34+
playwright-file-based-execution:
35+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
36+
uses: ./.github/workflows/playwright-test.yml
37+
with:
38+
test-mode: 'file-based-execution'
39+
project-name: 'chromium-file-based-snippets'
11940

12041
test-result:
121-
needs: [Playwright]
122-
if: always() && (needs.Playwright.result != 'skipped')
42+
needs: [playwright-default, playwright-file-based-execution]
43+
if: always() && (needs.playwright-default.result != 'skipped' || needs.playwright-file-based-execution.result != 'skipped')
12344
runs-on: ubuntu-22.04
124-
name: Playwright - Test Results
45+
name: Playwright - Test Results Summary
12546
steps:
126-
- name: Test status
127-
run: echo "Test status is - ${{ needs.Playwright.result }}"
128-
- name: Check Playwright status
129-
if: ${{ needs.Playwright.result != 'success' && needs.Playwright.result != 'skipped' }}
47+
- name: Test status summary
48+
run: |
49+
echo "Default Mode: ${{ needs.playwright-default.result }}"
50+
echo "File-based Execution: ${{ needs.playwright-file-based-execution.result }}"
51+
52+
- name: Check overall status
53+
if: ${{ (needs.playwright-default.result != 'success' && needs.playwright-default.result != 'skipped') || (needs.playwright-file-based-execution.result != 'success' && needs.playwright-file-based-execution.result != 'skipped') }}
13054
run: exit 1

src/php/class-plugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class Plugin {
7777
*/
7878
public Licensing $licensing;
7979

80+
/**
81+
* Handles snippet handler registration.
82+
*
83+
* @var Snippet_Handler_Registry
84+
*/
85+
public Snippet_Handler_Registry $snippet_handler_registry;
86+
8087
/**
8188
* Class constructor
8289
*
@@ -130,6 +137,18 @@ public function load_plugin() {
130137
// Cloud List Table shared functions.
131138
require_once $includes_path . '/cloud/list-table-shared-ops.php';
132139

140+
// Snippet files.
141+
$this->snippet_handler_registry = new Snippet_Handler_Registry( [
142+
'php' => new Php_Snippet_Handler(),
143+
'html' => new Html_Snippet_Handler(),
144+
] );
145+
146+
$fs = new WordPress_File_System_Adapter();
147+
148+
$config_repo = new Snippet_Config_Repository( $fs );
149+
150+
( new Snippet_Files( $this->snippet_handler_registry, $fs, $config_repo ) )->register_hooks();
151+
133152
$this->front_end = new Front_End();
134153
$this->cloud_api = new Cloud_API();
135154

src/php/class-snippet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static function get_type_from_scope( string $scope ): string {
189189
*
190190
* @return string The snippet type – will be a filename extension.
191191
*/
192-
protected function get_type(): string {
192+
public function get_type(): string {
193193
return self::get_type_from_scope( $this->scope );
194194
}
195195

src/php/evaluation/class-evaluate-content.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Code_Snippets\DB;
66
use Code_Snippets\Snippet;
7+
use Code_Snippets\Settings;
8+
use Code_Snippets\Snippet_Files;
9+
use function Code_Snippets\code_snippets;
710

811
/**
912
* Class for evaluating content snippets.
@@ -40,8 +43,13 @@ public function __construct( DB $db ) {
4043
* Initialise class functions.
4144
*/
4245
public function init() {
43-
add_action( 'wp_head', [ $this, 'load_head_content' ] );
44-
add_action( 'wp_footer', [ $this, 'load_footer_content' ] );
46+
if ( Snippet_Files::is_active() ) {
47+
add_action( 'wp_head', [ $this, 'load_head_content_from_flat_files' ] );
48+
add_action( 'wp_footer', [ $this, 'load_footer_content_from_flat_files' ] );
49+
} else {
50+
add_action( 'wp_head', [ $this, 'load_head_content' ] );
51+
add_action( 'wp_footer', [ $this, 'load_footer_content' ] );
52+
}
4553
}
4654

4755
/**
@@ -77,4 +85,46 @@ public function load_head_content() {
7785
public function load_footer_content() {
7886
$this->print_content_snippets( 'footer-content' );
7987
}
88+
89+
public function load_head_content_from_flat_files() {
90+
$this->load_content_snippets_from_flat_files( 'head-content' );
91+
}
92+
93+
public function load_footer_content_from_flat_files() {
94+
$this->load_content_snippets_from_flat_files( 'footer-content' );
95+
}
96+
97+
private function populate_active_snippets_from_flat_files() {
98+
$handler = code_snippets()->snippet_handler_registry->get_handler( 'html' );
99+
$dir_name = $handler->get_dir_name();
100+
$ext = $handler->get_file_extension();
101+
102+
$scopes = [ 'head-content', 'footer-content' ];
103+
$all_snippets = Snippet_Files::get_active_snippets_from_flat_files( $scopes, $dir_name );
104+
105+
foreach ( $all_snippets as $snippet ) {
106+
$scope = $snippet['scope'];
107+
108+
// Add file path information to the snippet for later use
109+
$table_name = Snippet_Files::get_hashed_table_name( $snippet['table'] );
110+
$base_path = Snippet_Files::get_base_dir( $table_name, $dir_name );
111+
$snippet['file_path'] = $base_path . '/' . $snippet['id'] . '.' . $ext;
112+
113+
$this->active_snippets[ $scope ][] = $snippet;
114+
}
115+
}
116+
117+
private function load_content_snippets_from_flat_files( string $scope ) {
118+
if ( is_null( $this->active_snippets ) ) {
119+
$this->populate_active_snippets_from_flat_files();
120+
}
121+
122+
if ( ! isset( $this->active_snippets[ $scope ] ) ) {
123+
return;
124+
}
125+
126+
foreach ( $this->active_snippets[ $scope ] as $snippet ) {
127+
require_once $snippet['file_path'];
128+
}
129+
}
80130
}

0 commit comments

Comments
 (0)