galeon-cli 0.4.0

Project scaffolding, code generation, and route inspection CLI for Galeon.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

pub(crate) fn galeon_release_version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

pub(crate) fn galeon_minor_version() -> String {
    let version = galeon_release_version();
    let mut parts = version.split('.');
    let major = parts
        .next()
        .expect("galeon-cli version must include a major semver component");
    let minor = parts
        .next()
        .expect("galeon-cli version must include a minor semver component");
    format!("{major}.{minor}")
}

/// Root `Cargo.toml` for the generated workspace.
pub fn workspace_cargo_toml() -> String {
    r#"[workspace]
resolver = "3"
members = ["crates/*"]

[workspace.package]
edition = "2024"
"#
    .to_owned()
}

/// `galeon.toml` project config.
pub fn galeon_toml(name: &str, preset: &str) -> String {
    format!(
        r#"[project]
name = "{name}"
engine = "{}"
preset = "{preset}"
"#,
        galeon_minor_version(),
    )
}

fn rust_crate_ident(name: &str) -> String {
    name.replace('-', "_")
}

/// `crates/protocol/Cargo.toml`.
pub fn protocol_cargo_toml(name: &str) -> String {
    format!(
        r#"[package]
name = "{name}-protocol"
version = "0.1.0"
edition.workspace = true

[dependencies]
galeon-engine = "{}"
"#,
        galeon_release_version(),
    )
}

/// `crates/protocol/src/lib.rs`.
pub fn protocol_lib_rs(name: &str) -> String {
    format!(
        r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

//! Protocol definitions for {name}.
//!
//! Define your commands, queries, events, and DTOs here using
//! `#[galeon_engine::command]`, `#[galeon_engine::query]`, etc.
"#
    )
}

/// `crates/domain/Cargo.toml`.
pub fn domain_cargo_toml(name: &str) -> String {
    format!(
        r#"[package]
name = "{name}-domain"
version = "0.1.0"
edition.workspace = true

[dependencies]
galeon-engine = "{}"
{name}-protocol = {{ path = "../protocol" }}
"#,
        galeon_release_version(),
    )
}

/// `crates/domain/src/lib.rs`.
pub fn domain_lib_rs(name: &str) -> String {
    format!(
        r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

//! Game systems and handlers for {name}.
"#
    )
}

/// `crates/domain/src/lib.rs` for the local-first starter preset.
pub fn local_first_domain_lib_rs() -> String {
    r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

use galeon_engine::{Engine, MaterialHandle, MeshHandle, Plugin, Transform, Visibility};

/// Minimal starter plugin that guarantees a first renderable entity.
pub struct StarterPlugin;

impl Plugin for StarterPlugin {
    fn build(&self, engine: &mut Engine) {
        engine.set_tick_rate(60.0);
        engine.world_mut().spawn((
            Transform {
                position: [0.0, 0.35, 0.0],
                rotation: [0.0, 0.0, 0.0, 1.0],
                scale: [2.2, 0.7, 1.6],
            },
            Visibility { visible: true },
            MeshHandle { id: 1 },
            MaterialHandle { id: 1 },
        ));
    }
}
"#
    .to_owned()
}

/// `crates/server/Cargo.toml` (server-authoritative and hybrid presets).
pub fn server_cargo_toml(name: &str) -> String {
    format!(
        r#"[package]
name = "{name}-server"
version = "0.1.0"
edition.workspace = true

[dependencies]
galeon-engine = "{}"
{name}-protocol = {{ path = "../protocol" }}
{name}-domain = {{ path = "../domain" }}
axum = "0.8"
tokio = {{ version = "1", features = ["full"] }}
"#,
        galeon_release_version(),
    )
}

/// `crates/server/src/main.rs` (server-authoritative and hybrid presets).
pub fn server_main_rs(name: &str) -> String {
    format!(
        r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

fn main() {{
    println!("TODO: {name} server");
}}
"#
    )
}

/// `crates/db/Cargo.toml` (server-authoritative preset only).
pub fn db_cargo_toml(name: &str) -> String {
    format!(
        r#"[package]
name = "{name}-db"
version = "0.1.0"
edition.workspace = true

[dependencies]
sqlx = {{ version = "0.8", features = ["runtime-tokio-rustls", "postgres"] }}
"#
    )
}

/// `crates/db/src/lib.rs` (server-authoritative preset only).
pub fn db_lib_rs(name: &str) -> String {
    format!(
        r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

//! Database migrations and queries for {name}.
"#
    )
}

/// `docker-compose.yml` (server-authoritative preset only).
pub fn docker_compose_yml(name: &str) -> String {
    format!(
        r#"services:
  postgres:
    image: postgres:17
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: {name}
      POSTGRES_USER: {name}
      POSTGRES_PASSWORD: dev
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
"#
    )
}

/// Root `.gitignore` for generated projects.
pub fn project_gitignore() -> String {
    r#"node_modules/
target/
dist/
client/dist/
client/pkg/
"#
    .to_owned()
}

/// Root `package.json` for the local-first starter preset.
pub fn local_first_package_json(name: &str) -> String {
    r#"{
  "name": "__NAME__",
  "private": true,
  "type": "module",
  "scripts": {
    "wasm": "wasm-pack build crates/client --target web --out-dir ../../client/pkg --out-name starter",
    "dev": "bun run wasm && vite client",
    "build": "bun run wasm && vite build client",
    "preview": "vite preview client",
    "check": "bun run wasm && bunx tsc --project client/tsconfig.json --noEmit"
  },
  "dependencies": {
    "@galeon/engine-ts": "^__GALEON_VERSION__",
    "three": "^0.183.2"
  },
  "devDependencies": {
    "@types/three": "^0.183.1",
    "typescript": "^5",
    "vite": "^7"
  }
}
"#
        .replace("__NAME__", name)
        .replace("__GALEON_VERSION__", galeon_release_version())
}

/// Starter `README.md` for the local-first preset.
pub fn local_first_readme_md(name: &str) -> String {
    r#"# __NAME__

Local-first Galeon starter project.

## Prerequisites

- Rust stable with `wasm32-unknown-unknown`
- `wasm-pack`
- Bun

## Commands

```bash
bun install
bun run dev
```

This builds the Rust WASM client crate into `client/pkg/` and starts a Vite dev
server for the generated web starter.

Production build:

```bash
bun run build
```

If you change Rust code while the dev server is running, rerun:

```bash
bun run wasm
```

## Project Layout

- `crates/domain` — Rust-owned starter plugin and future game logic
- `crates/client` — WASM wrapper exposed to the browser
- `client/` — Three.js web client that consumes the extracted frame data
- `crates/protocol` — protocol types for future commands/queries/events
"#
    .replace("__NAME__", name)
}

/// `client/tsconfig.json` for the local-first starter preset.
pub fn local_first_client_tsconfig_json() -> String {
    r#"{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "strict": true,
    "noEmit": true,
    "types": ["vite/client"]
  },
  "include": ["src"]
}
"#
    .to_owned()
}

/// `client/index.html` for the local-first starter preset.
pub fn local_first_client_index_html(name: &str) -> String {
    r#"<!-- SPDX-License-Identifier: AGPL-3.0-only OR Commercial -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>__NAME__ | Galeon Starter</title>
  </head>
  <body>
    <div class="shell">
      <canvas id="viewport"></canvas>
      <aside class="hud">
        <p class="eyebrow">Galeon starter</p>
        <h1>__NAME__</h1>
        <p class="copy">
          The first renderable entity comes from Rust. Three.js only owns the
          scene graph and camera.
        </p>
        <dl class="commands">
          <div>
            <dt>Dev</dt>
            <dd><code>bun run dev</code></dd>
          </div>
          <div>
            <dt>Build</dt>
            <dd><code>bun run build</code></dd>
          </div>
        </dl>
      </aside>
    </div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>
"#
    .replace("__NAME__", name)
}

/// `client/src/main.ts` for the local-first starter preset.
pub fn local_first_client_main_ts() -> String {
    r##"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

import "./style.css";
import { RendererCache } from "@galeon/engine-ts";
import * as THREE from "three";
import init, { StarterWasmEngine } from "../pkg/starter.js";

const canvas = document.querySelector<HTMLCanvasElement>("#viewport");
if (!canvas) {
  throw new Error("missing #viewport canvas");
}

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 9, 22);

const camera = new THREE.PerspectiveCamera(48, 1, 0.1, 100);
camera.position.set(5.5, 4.2, 6.5);
camera.lookAt(0, 0.5, 0);

const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));

const ambient = new THREE.AmbientLight(0xf5f7ff, 0.65);
scene.add(ambient);

const sun = new THREE.DirectionalLight(0xfff1bf, 1.2);
sun.position.set(6, 8, 4);
scene.add(sun);

const grid = new THREE.GridHelper(18, 18, 0x4f46e5, 0x1e293b);
grid.position.y = -0.01;
scene.add(grid);

const cache = new RendererCache(scene);
cache.registerGeometry(1, new THREE.BoxGeometry(1, 1, 1));
cache.registerMaterial(
  1,
  new THREE.MeshStandardMaterial({
    color: 0x38bdf8,
    roughness: 0.45,
    metalness: 0.08,
  }),
);

function resizeRenderer(): void {
  const width = window.innerWidth;
  const height = window.innerHeight;
  renderer.setSize(width, height, false);
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.render(scene, camera);
}

window.addEventListener("resize", resizeRenderer);

await init();
const engine = new StarterWasmEngine();

cache.applyFrame(engine.extract_frame());
resizeRenderer();

let lastFrame = performance.now();
function frame(now: number): void {
  const elapsed = Math.min((now - lastFrame) / 1000, 0.25);
  lastFrame = now;

  engine.tick(elapsed);
  cache.applyFrame(engine.extract_frame());

  if (cache.needsRender) {
    renderer.render(scene, camera);
  }

  requestAnimationFrame(frame);
}

requestAnimationFrame(frame);
"##
    .to_owned()
}

/// `client/src/style.css` for the local-first starter preset.
pub fn local_first_client_style_css() -> String {
    r#"/* SPDX-License-Identifier: AGPL-3.0-only OR Commercial */

:root {
  color-scheme: dark;
  --bg: #050b15;
  --panel: rgba(8, 17, 31, 0.84);
  --line: rgba(148, 163, 184, 0.18);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top, rgba(56, 189, 248, 0.18), transparent 36%),
    linear-gradient(180deg, #07101d 0%, var(--bg) 100%);
  color: var(--text);
  font-family: "Segoe UI", "Inter", sans-serif;
}

body {
  min-height: 100vh;
}

.shell {
  position: relative;
  min-height: 100vh;
}

#viewport {
  display: block;
  width: 100vw;
  height: 100vh;
}

.hud {
  position: absolute;
  top: 24px;
  left: 24px;
  width: min(360px, calc(100vw - 48px));
  padding: 20px 22px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  backdrop-filter: blur(18px);
  box-shadow: 0 24px 60px rgba(3, 8, 18, 0.45);
}

.eyebrow {
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.72rem;
  color: var(--accent);
}

.hud h1 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 2.4rem);
}

.copy {
  margin: 12px 0 18px;
  line-height: 1.5;
  color: var(--muted);
}

.commands {
  display: grid;
  gap: 12px;
  margin: 0;
}

.commands div {
  display: grid;
  gap: 4px;
}

.commands dt {
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.commands dd {
  margin: 0;
}

code {
  font-family: "Cascadia Code", "JetBrains Mono", monospace;
  font-size: 0.95rem;
  color: #f8fafc;
}

@media (max-width: 640px) {
  .hud {
    top: auto;
    bottom: 18px;
    left: 18px;
    width: calc(100vw - 36px);
    padding: 18px;
  }
}
"#
    .to_owned()
}

/// `crates/client/Cargo.toml` for the local-first starter preset.
pub fn local_first_client_cargo_toml(name: &str) -> String {
    format!(
        r#"[package]
name = "{name}-client"
version = "0.1.0"
edition.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
galeon-engine = "{version}"
galeon-engine-three-sync = "{version}"
wasm-bindgen = "0.2"
{name}-domain = {{ path = "../domain" }}
"#,
        version = galeon_release_version(),
    )
}

/// `crates/client/src/lib.rs` for the local-first starter preset.
pub fn local_first_client_lib_rs(name: &str) -> String {
    r#"// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

use __DOMAIN_CRATE__::StarterPlugin;
use galeon_engine::Engine;
use galeon_engine_three_sync::{WasmEngine, WasmFramePacket};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct StarterWasmEngine {
    inner: WasmEngine,
}

#[wasm_bindgen]
impl StarterWasmEngine {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        let mut engine = Engine::new();
        engine.add_plugin(StarterPlugin);
        Self {
            inner: WasmEngine::from_engine(engine),
        }
    }

    pub fn tick(&mut self, elapsed: f64) -> u32 {
        self.inner.tick(elapsed)
    }

    pub fn extract_frame(&self) -> WasmFramePacket {
        self.inner.extract_frame()
    }

    pub fn debug_snapshot(&self) -> String {
        self.inner.debug_snapshot()
    }
}
"#
    .replace(
        "__DOMAIN_CRATE__",
        &rust_crate_ident(&format!("{name}-domain")),
    )
}