Skip to content

Commit

Permalink
Merge pull request #100 from jolicode/gh-pages
Browse files Browse the repository at this point in the history
Setup the demo on GH Pages
  • Loading branch information
lyrixx committed Feb 13, 2024
2 parents 7051d4c + 756d145 commit 5531674
Show file tree
Hide file tree
Showing 16 changed files with 4,295 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/ghpages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

name: Build the public website, and publish to GitHub Pages

on:
push:
branches: [ "main" ]

permissions:
contents: read
pages: write
id-token: write

jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{steps.deployment.outputs.page_url}}
steps:
- name: Setup Pages
uses: actions/configure-pages@v3

- name: Checkout
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
tools: castor

- name: Build
run: |
set -x
castor website:install
castor website:wasm:export --pack --build
working-directory: ./website
env:
BASE_URL: "/${{ github.event.repository.name }}"

- name: Upload Artifact
uses: actions/upload-pages-artifact@v2
with:
path: 'website/public'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.castor.stub.php
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => true, // Symfony(PSR12) override the default value, but we don't want
'blank_line_before_statement' => true, // Symfony(PSR12) override the default value, but we don't want
'array_indentation' => false, // Do not work well with "command builder"
])
->setFinder($finder)
;
10 changes: 10 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

Castor\import(__DIR__ . '/website/castor.php');
4 changes: 4 additions & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.castor.stub.php
/build/
/public/build
/vendor/
16 changes: 16 additions & 0 deletions website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# JoliTypo demo

## Installation

```bash
castor website:install
castor website:wasm:export --build --pack
```

Then you can test it with

```bash
castor website:serve
```

See https://jolicode.com/blog/heberger-un-projet-php-sans-serveur-avec-webassembly for more information.
117 changes: 117 additions & 0 deletions website/castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace website;

use Castor\Attribute\AsTask;
use Symfony\Component\Process\Process;

use function Castor\fs;
use function Castor\io;
use function Castor\run as do_run;

#[AsTask(description: 'Install dependencies')]
function install()
{
io()->note('Installing dependencies');

run(['composer', 'install', '--no-dev', '--optimize-autoloader']);
}

#[AsTask('wasm:build', description: 'Build the wasm-php binary')]
function wasm_build()
{
io()->note('Building wasm-php binary');

$phpWasmDir = __DIR__ . '/vendor/soyuka/php-wasm';
run(['docker', 'buildx', 'bake'], path: $phpWasmDir);
fs()->mirror("{$phpWasmDir}/build", __DIR__ . '/build');
}

#[AsTask('wasm:pack', description: 'Pack custom code')]
function wasm_pack()
{
io()->note('Packing custom code');

run(['docker', 'run',
'-v', __DIR__ . ':/app',
'-v', __DIR__ . '/build:/dist/build',
'-w', '/dist',
'soyuka/php-wasm:8.2.9',
'python3',
'/emsdk/upstream/emscripten/tools/file_packager.py',
'build/php-web.data',
'--use-preload-cache',
'--lz4',
'--preload', '/app',
'--js-output=build/php-web.data.js',
'--no-node',
'--exclude',
'*/.*',
'*/*md',
'*/castor.php',
'*/CHANGELOG',
'*/composer.*',
'*/doc',
'*/docs',
'*/LICENSE',
'*/tests',
'*/Tests',
'*/tools',
'/app/build',
'/app/public',
'/app/vendor/jolicode/jolitypo/src/JoliTypo/Bridge',
'/app/vendor/jolicode/jolitypo/vendor',
'/app/vendor/jolicode/jolitypo/website',
'/app/vendor/soyuka',
'--export-name=createPhpModule',
]);
}

#[AsTask('wasm:export', description: 'Export the wasm-php binary to the public folder with custom code')]
function wasm_export(bool $pack = false, bool $build = false)
{
if ($build) {
wasm_build();
}

if ($pack) {
wasm_pack();
}

io()->note('Exporting wasm-php');

fs()->remove(__DIR__ . '/public/build');

fs()->copy(__DIR__ . '/build/php-web.wasm', __DIR__ . '/public/build/php-web.wasm');
fs()->copy(__DIR__ . '/build/php-web.data', __DIR__ . '/public/build/php-web.data');

$data = file_get_contents(__DIR__ . '/build/php-web.data.js');
$mjs = file_get_contents(__DIR__ . '/build/php-web.mjs');
$mjs = str_replace($p = '// --pre-jses ', "{$data}\n{$p}", $mjs);
$baseUrl = $_SERVER['BASE_URL'] ?? '';
// see: https://github.com/emscripten-core/emscripten/issues/21289
$mjs = str_replace("var REMOTE_PACKAGE_BASE = 'php-web.data';", "var REMOTE_PACKAGE_BASE = '{$baseUrl}/build/php-web.data';", $mjs);
fs()->dumpFile(__DIR__ . '/public/build/php-web.mjs', $mjs);
}

#[AsTask(description: 'Run the server')]
function serve(string $address = 'localhost:9999')
{
io()->note("Serving on http://{$address}");

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

function run(array $command, ?string $path = __DIR__): Process
{
return do_run($command, path: $path, timeout: 0, environment: [
'BUILDKIT_PROGRESS' => 'plain',
]);
}
48 changes: 48 additions & 0 deletions website/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "jolicode/jolitypo-website",
"description": "Demo website for jolicode/jolitypo",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Damien Alexandre",
"email": "dalexandre@jolicode.com"
},
{
"name": "Grégoire Pineau",
"email": "lyrixx@lyrixx.info"
}
],
"repositories": [
{
"type": "package",
"package": {
"name": "soyuka/php-wasm",
"version": "dev-main",
"source": {
"url": "https://github.com/soyuka/php-wasm.git",
"type": "git",
"reference": "main"
}
}
},
{
"type": "path",
"url": "../",
"options": {
"symlink": false
}
}
],
"require": {
"jolicode/jolitypo": "*@dev",
"soyuka/php-wasm": "dev-main@dev"
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
}
}

0 comments on commit 5531674

Please sign in to comment.