Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions .castor/website.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of JoliCode's json-ld project.
*
* (c) jolicode.com <coucou@jolicode.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace website;

use Castor\Attribute\AsOption;
use Castor\Attribute\AsTask;

use function Castor\context;
use function Castor\finder;
use function Castor\fs;
use function Castor\io;
use function Castor\run;
use function PhpWasm\build as build_wasm;
use function PhpWasm\check_stack;

use PhpWasm\Stack;
use PhpWasm\Target;

use function PhpWasm\test_page;

use Symfony\Component\Console\Input\InputOption;

// The demo website: a static page, in website/public, that audits a document
// with the validator, run by PHP built as WebAssembly. See website/README.md.

#[AsTask(description: 'Installs the dependencies of the demo website')]
function install(): void
{
io()->title('Installing the dependencies of the website');

$context = context()->withWorkingDirectory(websiteDir());

run(['composer', 'install', '--no-dev', '--no-progress'], context: $context);
// The library is a copy of this repository, that Composer only makes again
// when the commit changes: the website would miss what is not committed.
run(['composer', 'reinstall', '--no-progress', 'jolicode/structured-data'], context: $context);
run(['npm', 'ci', '--no-audit', '--no-fund'], context: $context);
}

/**
* @param list<string> $set
*/
#[AsTask(description: 'Builds the demo website, in website/public/build: its assets, and PHP as WebAssembly with the validator')]
function build(
#[AsOption(description: 'Overrides a value of the bake file, as "docker buildx bake --set" does (the CI gives the cache this way), can be repeated', mode: InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY)]
array $set = [],
): void {
// What is built comes from vendor/ and node_modules/
install();

io()->title('Building the website');

$website = websiteDir();
$app = "{$website}/build/app";

fs()->remove([$app, "{$website}/public/build"]);

run(['npm', 'run', 'build'], context: context()->withWorkingDirectory($website));

// What is embedded takes some of the memory of the WebAssembly module:
// only what bin/audit.php reads
fs()->mirror($website, $app, finder()
->in(["{$website}/assets/images", "{$website}/bin", "{$website}/src", "{$website}/templates", "{$website}/vendor"])
// Composer copies the directories of the repository that Git ignores too
->notPath(['jolicode/structured-data/vendor', 'jolicode/structured-data/website'])
->notName(['*.md', 'composer.lock']),
);

build_wasm([wasmStack()], $set);
}

#[AsTask(description: 'Checks, in node, that what is built for the demo website boots, and audits a document')]
function check(): void
{
buildIfNeeded();

io()->title('Checking the website');

check_stack(wasmStack());
test_page('website/tests/page.test.mjs', 'website/tests/format.test.mjs');
}

#[AsTask(description: 'Serves the demo website')]
function serve(string $address = 'localhost:9999'): void
{
buildIfNeeded();

io()->note("Serving on http://{$address}");

run(['php', '-S', $address, '-t', websiteDir() . '/public']);
}

function websiteDir(): string
{
return \dirname(__DIR__) . '/website';
}

/**
* For the tasks that need what is built: build() when it is missing.
*/
function buildIfNeeded(): void
{
if (!is_file(wasmStack()->artifact(Target::Web, 'wasm'))) {
build();
}
}

/**
* The PHP that runs the validator in the browser, see https://github.com/lyrixx/php-wasm-builder.
*/
function wasmStack(): Stack
{
return Stack::named('website')
->php('8.5.12')
// What the library, Twig and league/commonmark require
->extensions('ctype', 'dom', 'filter', 'libxml', 'mbstring')
// Filled by build(), assets/php.js runs /app/bin/audit.php
->embed('website/build/app', at: '/app')
->targets(Target::Web)
->outputDir('website/public/build')
;
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/.php-cs-fixer.php export-ignore
/CHANGELOG.md export-ignore
/CONTRIBUTING.md export-ignore
/castor.composer.json export-ignore
/castor.composer.lock export-ignore
/castor.php export-ignore
/generators export-ignore
/infection.json5 export-ignore
Expand All @@ -18,6 +20,7 @@
/tests export-ignore
/tools export-ignore
/var export-ignore
/website export-ignore

# Only the resources needed at runtime (the schema.org contexts and the
# standards data) stay in the archive: the vocabulary definitions used for
Expand Down
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ updates:
- "/tools/phpunit"
- "/tools/phpbench"
- "/tools/infection"
- "/website"
schedule:
interval: "weekly"
groups:
Expand All @@ -31,3 +32,15 @@ updates:
- "*"
cooldown:
default-days: 7

# The page of the demo website is built with these.
- package-ecosystem: "npm"
directory: "/website"
schedule:
interval: "weekly"
groups:
website:
patterns:
- "*"
cooldown:
default-days: 7
75 changes: 75 additions & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Demo website

on:
push:
branches: [main]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
build:
name: Build and check the website
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Setup PHP
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
# The version PHP is built with as WebAssembly, see .castor/website.php
php-version: "8.5"
# Dependabot does not track tools installed by setup-php: bump these
# versions and their SHA-256 checksums by hand, in every job.
tools: >-
composer:2.11.1@sha256:6f1b891e1d4927d62740276c540fb490e47689e19e102c49e8da849794882f8c,
castor:1.9.1@sha256:3312568da9a593ad7d3ef6548f05c12d994dbc7140634e401531dc8d7de41764
coverage: none

# It builds the assets of the page, and runs the checks
- name: Setup node
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24

# The cache of the PHP build needs the docker-container driver of buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

# And the runtime token and URLs of GitHub Actions, that only actions get
- name: Expose the GitHub runtime
uses: crazy-max/ghaction-github-runtime@04d248b84655b509d8c44dc1d6f990c879747487 # v4.0.0

- name: Build
run: castor website:build --set '*.cache-from=type=gha' --set '*.cache-to=type=gha,mode=max,ignore-error=true'

- name: Check
run: castor website:check

- name: Upload the website
if: github.event_name == 'push'
uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
path: website/public

# Only what passes the checks is deployed
deploy:
name: Deploy to GitHub Pages
if: github.event_name == 'push'
needs: build
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@368f82528645a54fb793d4d04e342629a3f51346 # v5.0.1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ phpunit.xml

# Castor stubs
.castor.stub.php

# Demo website: the bake file of php-wasm-builder (what is built is ignored in website/)
/build/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ Command | Description
`castor qa:all` | Runs all QA tasks
`castor qa:earl` | Runs the W3C test suites and writes the EARL conformance report (see [Conformance](#conformance))

The following commands build the [demo website](website/README.md), that runs the validator in the browser:

Command | Description
---- | -----
`castor website:build` | Builds the demo website: its assets, and PHP as WebAssembly with the validator
`castor website:check` | Checks, in node, that what is built boots, and audits a document
`castor website:serve` | Serves the demo website

The following commands are available to run the tests:

Command | Description
Expand Down
5 changes: 5 additions & 0 deletions castor.composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"lyrixx/php-wasm-builder": "^0.12.2"
}
}
65 changes: 65 additions & 0 deletions castor.composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ parameters:
- tests
- .castor
- castor.php
excludePaths:
analyse:
# The packages of castor.composer.json, that the tasks of the website use
- .castor/vendor
scanFiles:
- .castor.stub.php
scanDirectories:
Expand Down
4 changes: 4 additions & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/build/
/node_modules/
/public/build/
/vendor/
36 changes: 36 additions & 0 deletions website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Demo website

A static page that audits a document with the validator, in the browser: PHP is built as
WebAssembly, with [lyrixx/php-wasm-builder](https://github.com/lyrixx/php-wasm-builder), and the
library of this repository embedded in it. GitHub Pages serves `public/`, deployed from `main` by
`.github/workflows/website.yml`.

It comes from
[jolicode/structured-data-validator-demo](https://github.com/jolicode/structured-data-validator-demo),
without its server: the editor and the tree of the properties are its ones, with another look.
Fetching a URL needs a server, and is not here: a page can not read another website. Pretty-printing
the document is the "Format" button of the result: `assets/format.js` runs Prettier in the browser,
as its `bin/format-document.js` does in node.

## How it works

- `public/index.html` is the homepage. `assets/app.js` gives the document of its form, or one of
`assets/examples.js`, to PHP, and shows what it answers in place of the form.
- `bin/audit.php` is what PHP runs: it audits the document, and renders `templates/result.html.twig`,
the editor and the result, with Twig.
- `assets/php.js` loads PHP, and runs `bin/audit.php`.

## Build it

Docker, node and [Castor](https://github.com/jolicode/castor) are needed. The tasks are in
`.castor/website.php`:

Command | Description
---- | -----
`castor website:build` | Installs the dependencies, and builds the assets and PHP in `public/build/`
`castor website:check` | Checks, in node, that PHP boots, and runs the tests of `tests/`
`castor website:serve` | Serves the website on http://localhost:9999

`composer.json` declares the parent directory as a path repository: the website is built with the
code of the working directory, not with a release. Run `castor website:build` again after a change:
only the last step of the build of PHP runs again.
Loading
Loading