|
| 1 | +name: Build and deploy Pixel2Polygon to GitHub Pages |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: ["main"] |
| 6 | + push: |
| 7 | + branches: ["main"] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pages: write |
| 13 | + id-token: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: "pages" |
| 17 | + cancel-in-progress: false |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + name: Compile Multilingual sources to WebAssembly |
| 22 | + runs-on: ubuntu-latest |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Check out repository |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Set up Python 3.12 |
| 29 | + uses: actions/setup-python@v5 |
| 30 | + with: |
| 31 | + python-version: "3.12" |
| 32 | + |
| 33 | + - name: Set up Node.js |
| 34 | + uses: actions/setup-node@v4 |
| 35 | + with: |
| 36 | + node-version: "20" |
| 37 | + |
| 38 | + - name: Install build dependencies |
| 39 | + run: | |
| 40 | + python -m pip install --upgrade pip |
| 41 | + python -m pip install -r requirements-build.txt |
| 42 | +
|
| 43 | + - name: Compile Multilingual sources to WebAssembly |
| 44 | + run: python -m multilingualprogramming scripts/compile_wasm.ml |
| 45 | + |
| 46 | + - name: Verify build artifacts |
| 47 | + run: | |
| 48 | + ls -la public |
| 49 | + test -f public/index.html |
| 50 | + test -f public/style.css |
| 51 | + test -f public/app.js |
| 52 | + test -f public/hexagonify.wasm |
| 53 | +
|
| 54 | + - name: Check JavaScript syntax |
| 55 | + run: node --check public/app.js |
| 56 | + |
| 57 | + - name: Validate WASM or fall back to JavaScript-only publish |
| 58 | + run: | |
| 59 | + node -e " |
| 60 | + const fs = require('fs'); |
| 61 | + const wasmPath = 'public/hexagonify.wasm'; |
| 62 | + const watPath = 'public/hexagonify.wat'; |
| 63 | + const fallback = (message, detail) => { |
| 64 | + console.warn(message); |
| 65 | + if (detail) { |
| 66 | + console.warn(detail); |
| 67 | + } |
| 68 | + if (fs.existsSync(wasmPath)) fs.unlinkSync(wasmPath); |
| 69 | + if (fs.existsSync(watPath)) fs.unlinkSync(watPath); |
| 70 | + console.log('Publishing JavaScript-only fallback.'); |
| 71 | + process.exit(0); |
| 72 | + }; |
| 73 | + if (!fs.existsSync(wasmPath)) { |
| 74 | + fallback('WASM artifact missing after compilation.'); |
| 75 | + } |
| 76 | + const bytes = fs.readFileSync(wasmPath); |
| 77 | + const module = new WebAssembly.Module(bytes); |
| 78 | + const importObject = {}; |
| 79 | + for (const entry of WebAssembly.Module.imports(module)) { |
| 80 | + if (!importObject[entry.module]) { |
| 81 | + importObject[entry.module] = {}; |
| 82 | + } |
| 83 | + if (entry.kind === 'function') { |
| 84 | + importObject[entry.module][entry.name] = () => 0; |
| 85 | + } else if (entry.kind === 'memory') { |
| 86 | + importObject[entry.module][entry.name] = new WebAssembly.Memory({ initial: 16 }); |
| 87 | + } else if (entry.kind === 'table') { |
| 88 | + importObject[entry.module][entry.name] = new WebAssembly.Table({ initial: 0, element: 'anyfunc' }); |
| 89 | + } else if (entry.kind === 'global') { |
| 90 | + importObject[entry.module][entry.name] = new WebAssembly.Global({ value: 'i32', mutable: true }, 0); |
| 91 | + } |
| 92 | + } |
| 93 | + if (!importObject.env) { |
| 94 | + importObject.env = {}; |
| 95 | + } |
| 96 | + WebAssembly.instantiate(module, importObject).then((instance) => { |
| 97 | + const exports = instance.exports; |
| 98 | + const required = [ |
| 99 | + 'hex_vertex_x', 'hex_vertex_y', |
| 100 | + 'horiz_spacing', 'vert_spacing', |
| 101 | + 'tri_h', 'tri_vertex_x', 'tri_vertex_y', |
| 102 | + 'color_mean', |
| 103 | + 'method_hex', 'method_square', 'method_triangle', |
| 104 | + ]; |
| 105 | + const missing = required.filter((name) => typeof exports[name] !== 'function'); |
| 106 | + if (missing.length) { |
| 107 | + fallback('WASM exports missing.', missing.join(', ')); |
| 108 | + } |
| 109 | + const checks = [ |
| 110 | + ['hex_vertex_x', Number(exports.hex_vertex_x(0, 0, 10, 0)), 0, 0.001], |
| 111 | + ['hex_vertex_y', Number(exports.hex_vertex_y(0, 0, 10, 0)), -10, 0.001], |
| 112 | + ['horiz_spacing', Number(exports.horiz_spacing(10)), 17.320508, 0.01], |
| 113 | + ['vert_spacing', Number(exports.vert_spacing(10)), 15, 0.001], |
| 114 | + ['tri_h', Number(exports.tri_h(10)), 8.660254, 0.01], |
| 115 | + ['tri_vertex_x', Number(exports.tri_vertex_x(5, 10, 1, 1)), 10, 0.001], |
| 116 | + ['tri_vertex_y', Number(exports.tri_vertex_y(5, 10, 2, 0)), 13.660254, 0.01], |
| 117 | + ['color_mean', Number(exports.color_mean(11, 2)), 6, 0.001], |
| 118 | + ['method_hex', Number(exports.method_hex()), 0, 0.001], |
| 119 | + ['method_square', Number(exports.method_square()), 1, 0.001], |
| 120 | + ['method_triangle', Number(exports.method_triangle()), 2, 0.001], |
| 121 | + ]; |
| 122 | + for (const [name, actual, expected, tolerance] of checks) { |
| 123 | + if (Math.abs(actual - expected) > tolerance) { |
| 124 | + fallback('WASM smoke test failed.', JSON.stringify({ name, expected, actual, tolerance })); |
| 125 | + } |
| 126 | + } |
| 127 | + console.log('WASM smoke test OK'); |
| 128 | + }).catch((err) => { |
| 129 | + fallback('Could not instantiate WASM.', err && err.stack ? err.stack : String(err)); |
| 130 | + }); |
| 131 | + " |
| 132 | +
|
| 133 | + - name: Configure GitHub Pages |
| 134 | + uses: actions/configure-pages@v5 |
| 135 | + |
| 136 | + - name: Upload Pages artifact |
| 137 | + uses: actions/upload-pages-artifact@v3 |
| 138 | + with: |
| 139 | + path: "public" |
| 140 | + |
| 141 | + deploy: |
| 142 | + name: Deploy to GitHub Pages |
| 143 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 144 | + runs-on: ubuntu-latest |
| 145 | + needs: build |
| 146 | + environment: |
| 147 | + name: github-pages |
| 148 | + url: ${{ steps.deployment.outputs.page_url }} |
| 149 | + |
| 150 | + steps: |
| 151 | + - name: Deploy to GitHub Pages |
| 152 | + id: deployment |
| 153 | + uses: actions/deploy-pages@v4 |
0 commit comments