Skip to content

Commit 802e84c

Browse files
LukeTowersclaude
andauthored
Add scaffold:winter.blog demo-data command (#84)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e357150 commit 802e84c

11 files changed

Lines changed: 803 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Code Quality
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
codeQuality:
11+
runs-on: ubuntu-latest
12+
name: PHP
13+
steps:
14+
- name: Cancel previous incomplete runs
15+
uses: styfle/cancel-workflow-action@0.12.1
16+
with:
17+
access_token: ${{ github.token }}
18+
19+
- name: Checkout changes
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Install PHP and PHP Code Sniffer
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: 8.2
28+
extensions: curl, fileinfo, gd, mbstring, openssl, pdo, pdo_sqlite, sqlite3, xml, zip
29+
tools: phpcs
30+
31+
- name: Run code quality checks (on push)
32+
if: github.event_name == 'push'
33+
run: ./.github/workflows/utilities/phpcs-push ${{ github.sha }}
34+
35+
- name: Run code quality checks (on pull request)
36+
if: github.event_name == 'pull_request'
37+
run: ./.github/workflows/utilities/phpcs-pr ${{ github.base_ref }}

.github/workflows/tests.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
phpUnitTests:
15+
strategy:
16+
matrix:
17+
operatingSystem: [ubuntu-latest]
18+
phpVersion: ['8.1', '8.2', '8.3', '8.4']
19+
winterBranch: ['develop']
20+
fail-fast: false
21+
name: PHP ${{ matrix.phpVersion }} / Winter ${{ matrix.winterBranch }}
22+
runs-on: ${{ matrix.operatingSystem }}
23+
steps:
24+
- name: Setup Winter
25+
uses: wintercms/setup-winter-action@v1
26+
with:
27+
php-version: ${{ matrix.phpVersion }}
28+
winter-ref: ${{ matrix.winterBranch }}
29+
plugin-author: winter
30+
plugin-name: blog
31+
32+
- name: Run linting
33+
run: ./vendor/bin/parallel-lint plugins/winter/blog
34+
35+
- name: Run unit tests (develop branch)
36+
run: php artisan winter:test -p Winter.Blog -- --testdox
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Run PHPCS tests against a PR.
5+
*
6+
* The only argument is for the PR's base branch, which is then compared to the HEAD of the PR to retrieve the list
7+
* of changed files. The PHPCS tests are only run against these changed files, to speed up the tests.
8+
*/
9+
if (empty($argv[1])) {
10+
echo 'You must provide a base branch to check this PR against.';
11+
echo "\n";
12+
exit(1);
13+
}
14+
15+
// Get a changelist of files from Git for this PR.
16+
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
17+
$files = array_filter(explode("\n", $fileList));
18+
19+
foreach ($files as &$file) {
20+
if (strpos($file, ' ') !== false) {
21+
$file = str_replace(' ', '\\ ', $file);
22+
}
23+
}
24+
25+
// Run all changed files through the PHPCS code sniffer and generate a CSV report
26+
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
27+
$lines = array_map(function ($row) {
28+
return array_map(function ($column) {
29+
return trim($column, '"');
30+
}, explode(',', $row));
31+
}, array_filter(explode("\n", $csv)));
32+
33+
// Remove header row
34+
array_shift($lines);
35+
36+
if (!count($lines)) {
37+
echo "\e[0;32mFound no issues with code quality.\e[0m";
38+
echo "\n";
39+
exit(0);
40+
} else {
41+
// Group errors by file
42+
$files = [];
43+
44+
foreach ($lines as $line) {
45+
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);
46+
47+
if (empty($files[$filename])) {
48+
$files[$filename] = [];
49+
}
50+
51+
$files[$filename][] = [
52+
'warning' => ($line[3] === 'warning'),
53+
'message' => $line[4],
54+
'line' => $line[1],
55+
];
56+
}
57+
58+
// Render report
59+
echo "\e[0;31mFound "
60+
. ((count($lines) === 1)
61+
? '1 issue'
62+
: count($lines) . ' issues')
63+
. " with code quality.\e[0m";
64+
echo "\n";
65+
66+
foreach ($files as $file => $errors) {
67+
echo "\n";
68+
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
69+
echo "\n\n";
70+
71+
foreach ($errors as $error) {
72+
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
73+
if ($error['warning'] === false) {
74+
echo "\e[0;31mERR:\e[0m ";
75+
} else {
76+
echo "\e[1;33mWARN:\e[0m ";
77+
}
78+
echo $error['message'];
79+
echo "\n";
80+
}
81+
}
82+
exit(1);
83+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Run PHPCS tests against a push.
5+
*
6+
* The only argument is for the commit, which a list of changed files is retrieved from. The PHPCS tests are only run
7+
* against these changed files, to speed up the tests.
8+
*/
9+
if (empty($argv[1])) {
10+
echo 'You must provide a commit SHA to check.';
11+
echo "\n";
12+
exit(1);
13+
}
14+
15+
// Get a changelist of files from Git for this push.
16+
$fileList = shell_exec('git show --name-only --pretty="" --diff-filter=ACMR ' . $argv[1]);
17+
$files = array_filter(explode("\n", $fileList));
18+
19+
foreach ($files as &$file) {
20+
if (strpos($file, ' ') !== false) {
21+
$file = str_replace(' ', '\\ ', $file);
22+
}
23+
}
24+
25+
// Run all changed files through the PHPCS code sniffer and generate a CSV report
26+
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
27+
$lines = array_map(function ($row) {
28+
return array_map(function ($column) {
29+
return trim($column, '"');
30+
}, explode(',', $row));
31+
}, array_filter(explode("\n", $csv)));
32+
33+
// Remove header row
34+
array_shift($lines);
35+
36+
if (!count($lines)) {
37+
echo "\e[0;32mFound no issues with code quality.\e[0m";
38+
echo "\n";
39+
exit(0);
40+
} else {
41+
// Group errors by file
42+
$files = [];
43+
44+
foreach ($lines as $line) {
45+
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);
46+
47+
if (empty($files[$filename])) {
48+
$files[$filename] = [];
49+
}
50+
51+
$files[$filename][] = [
52+
'warning' => ($line[3] === 'warning'),
53+
'message' => $line[4],
54+
'line' => $line[1],
55+
];
56+
}
57+
58+
// Render report
59+
echo "\e[0;31mFound "
60+
. ((count($lines) === 1)
61+
? '1 issue'
62+
: count($lines) . ' issues')
63+
. " with code quality.\e[0m";
64+
echo "\n";
65+
66+
foreach ($files as $file => $errors) {
67+
echo "\n";
68+
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
69+
echo "\n\n";
70+
71+
foreach ($errors as $error) {
72+
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
73+
if ($error['warning'] === false) {
74+
echo "\e[0;31mERR:\e[0m ";
75+
} else {
76+
echo "\e[1;33mWARN:\e[0m ";
77+
}
78+
echo $error['message'];
79+
echo "\n";
80+
}
81+
}
82+
exit(1);
83+
}

Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Backend\Models\UserRole;
77
use System\Classes\PluginBase;
88
use Winter\Blog\Classes\TagProcessor;
9+
use Winter\Blog\Console\ScaffoldCommand;
910
use Winter\Blog\Models\Category;
1011
use Winter\Blog\Models\Post;
1112
use Winter\Storm\Support\Facades\Event;
@@ -160,6 +161,8 @@ public function register(): void
160161
$input
161162
);
162163
});
164+
165+
$this->registerConsoleCommand('winter.blog.scaffold', ScaffoldCommand::class);
163166
}
164167

165168
/**

0 commit comments

Comments
 (0)