anvilforge-cli 0.3.6

Smith — Anvilforge's CLI (Artisan equivalent). Scaffolding, migrations, serve, queue:work, schedule:run, test.
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
//! `smith make:auth` — scaffold login/register/logout (Laravel Breeze equivalent).
//!
//! Writes into the standard Laravel locations:
//! - `app/Http/Controllers/AuthController.rs`
//! - `app/Http/Requests/LoginRequest.rs` + `RegisterRequest.rs`
//! - `routes/auth.rs`
//! - `resources/views/auth/login.forge.html` + `register.forge.html`
//! - `database/migrations/<ts>_add_auth_columns_to_users.rs`
//!
//! All sibling `mod.rs` files are auto-updated. Route registration in
//! `bootstrap/app.rs` is best-effort: we splice `.web(routes::auth::register)`
//! into the existing `.web(...)` chain if we can find it; otherwise the user
//! gets a single concrete instruction.

use std::fs;
use std::path::Path;

use anyhow::{Context, Result};

use super::project_root;

const AUTH_MIGRATION_FILE: &str = "2026_01_01_000099_add_auth_columns_to_users.rs";
const AUTH_MIGRATION_PATH: &str =
    "database/migrations/2026_01_01_000099_add_auth_columns_to_users.rs";
const AUTH_MIGRATION_STEM: &str = "2026_01_01_000099_add_auth_columns_to_users";
const AUTH_MIGRATION_MOD: &str = "add_auth_columns_to_users";

pub fn scaffold() -> Result<()> {
    let root = project_root();

    let files: [(&str, &str); 7] = [
        ("app/Http/Controllers/AuthController.rs", AUTH_CONTROLLER),
        ("app/Http/Requests/LoginRequest.rs", LOGIN_REQUEST),
        ("app/Http/Requests/RegisterRequest.rs", REGISTER_REQUEST),
        ("routes/auth.rs", AUTH_ROUTES),
        ("resources/views/auth/login.forge.html", LOGIN_VIEW),
        ("resources/views/auth/register.forge.html", REGISTER_VIEW),
        (AUTH_MIGRATION_PATH, AUTH_MIGRATION),
    ];

    let mut written = Vec::new();
    let mut skipped = Vec::new();
    for (rel, contents) in &files {
        let path = root.join(rel);
        if path.exists() {
            skipped.push(*rel);
            continue;
        }
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).ok();
        }
        fs::write(&path, contents).with_context(|| format!("writing {}", path.display()))?;
        written.push(*rel);
    }

    // Auto-wire mod.rs files + bootstrap + routes.
    let mut wired = Vec::new();
    let mut wiring_notes = Vec::new();
    wire_mod_use(
        &root,
        "app/Http/Controllers/mod.rs",
        "AuthController",
        &mut wired,
    )?;
    wire_mod_use(
        &root,
        "app/Http/Requests/mod.rs",
        "LoginRequest",
        &mut wired,
    )?;
    wire_mod_use(
        &root,
        "app/Http/Requests/mod.rs",
        "RegisterRequest",
        &mut wired,
    )?;
    wire_routes_mod(&root, &mut wired, &mut wiring_notes)?;
    wire_migrations_mod(&root, &mut wired)?;
    wire_bootstrap_routes(&root, &mut wired, &mut wiring_notes)?;

    println!();
    println!("  ✓ scaffolded auth");
    println!();
    for w in &written {
        println!("    + {w}");
    }
    if !skipped.is_empty() {
        println!();
        println!("  skipped (already exist):");
        for s in &skipped {
            println!("    - {s}");
        }
    }
    if !wired.is_empty() {
        println!();
        println!("  wired:");
        for w in &wired {
            println!("    ~ {w}");
        }
    }
    if !wiring_notes.is_empty() {
        println!();
        println!("  manual follow-ups:");
        for n in &wiring_notes {
            println!("    ! {n}");
        }
    }
    println!();
    println!("  next:");
    println!("    smith migrate");
    println!();
    Ok(())
}

/// Append `#[path = "<Name>.rs"] mod <snake>; pub use <snake>::<Name>;` to `mod.rs` if absent.
fn wire_mod_use(root: &Path, rel_mod_rs: &str, name: &str, wired: &mut Vec<String>) -> Result<()> {
    let mod_rs = root.join(rel_mod_rs);
    let snake = to_snake(name);
    let marker = format!("\"{name}.rs\"");
    let mut current = if mod_rs.exists() {
        fs::read_to_string(&mod_rs).unwrap_or_default()
    } else {
        if let Some(parent) = mod_rs.parent() {
            fs::create_dir_all(parent).ok();
        }
        String::new()
    };
    if current.contains(&marker) {
        return Ok(());
    }
    if !current.is_empty() && !current.ends_with('\n') {
        current.push('\n');
    }
    current.push_str(&format!(
        "\n#[path = \"{name}.rs\"]\nmod {snake};\npub use {snake}::{name};\n"
    ));
    fs::write(&mod_rs, current).with_context(|| format!("write {}", mod_rs.display()))?;
    wired.push(format!("{rel_mod_rs} (+{name})"));
    Ok(())
}

fn wire_routes_mod(root: &Path, wired: &mut Vec<String>, notes: &mut Vec<String>) -> Result<()> {
    let mod_rs = root.join("routes/mod.rs");
    if !mod_rs.exists() {
        notes.push("routes/mod.rs not found — add `pub mod auth;` by hand".to_string());
        return Ok(());
    }
    let mut current = fs::read_to_string(&mod_rs).unwrap_or_default();
    if current.contains("pub mod auth") || current.contains("mod auth ") {
        return Ok(());
    }
    if !current.ends_with('\n') {
        current.push('\n');
    }
    current.push_str("pub mod auth;\n");
    fs::write(&mod_rs, current)?;
    wired.push("routes/mod.rs (+pub mod auth)".to_string());
    Ok(())
}

fn wire_migrations_mod(root: &Path, wired: &mut Vec<String>) -> Result<()> {
    let mod_rs = root.join("database/migrations/mod.rs");
    let marker = format!("\"{AUTH_MIGRATION_FILE}\"");
    let mut current = if mod_rs.exists() {
        fs::read_to_string(&mod_rs).unwrap_or_default()
    } else {
        String::new()
    };
    if current.contains(&marker) {
        return Ok(());
    }
    if !current.is_empty() && !current.ends_with('\n') {
        current.push('\n');
    }
    current.push_str(&format!(
        "\n#[path = \"{AUTH_MIGRATION_FILE}\"]\npub mod {AUTH_MIGRATION_MOD};\n"
    ));
    fs::write(&mod_rs, current)?;
    wired.push(format!(
        "database/migrations/mod.rs (+{AUTH_MIGRATION_STEM})"
    ));
    Ok(())
}

/// Best-effort: insert `.web(routes::auth::register)` into the `.web(...)` chain
/// in `bootstrap/app.rs`. If we can't locate the chain we leave a note instead.
fn wire_bootstrap_routes(
    root: &Path,
    wired: &mut Vec<String>,
    notes: &mut Vec<String>,
) -> Result<()> {
    let path = root.join("bootstrap/app.rs");
    if !path.exists() {
        notes.push(
            "bootstrap/app.rs not found — register routes::auth::register manually".to_string(),
        );
        return Ok(());
    }
    let current = fs::read_to_string(&path).unwrap_or_default();
    if current.contains("routes::auth::register") || current.contains("routes::auth(") {
        return Ok(());
    }

    // Splice in a `.web(routes::auth::register)` right after `.web(routes::web::register)`
    // or `.web(routes::web)`. If we can't find either, leave a note.
    let anchors = [
        ".web(routes::web::register)",
        ".web(routes::web)",
        ".web(crate::routes::web::register)",
    ];
    let mut updated = current.clone();
    let mut found = false;
    for anchor in anchors {
        if let Some(idx) = updated.find(anchor) {
            let insert_at = idx + anchor.len();
            let inject = "\n        .web(routes::auth::register)";
            updated.insert_str(insert_at, inject);
            found = true;
            break;
        }
    }
    if found {
        fs::write(&path, updated)?;
        wired.push("bootstrap/app.rs (+.web(routes::auth::register))".to_string());
    } else {
        notes.push(
            "bootstrap/app.rs: couldn't find a `.web(...)` chain — add `.web(routes::auth::register)` to your builder manually"
                .to_string(),
        );
    }
    Ok(())
}

fn to_snake(name: &str) -> String {
    let mut out = String::new();
    for (i, c) in name.chars().enumerate() {
        if c.is_uppercase() && i > 0 {
            out.push('_');
        }
        out.push(c.to_ascii_lowercase());
    }
    out
}

const AUTH_CONTROLLER: &str = r##"//! Auth controllers — login, register, logout.

use anvilforge::prelude::*;
use anvilforge::auth;
use anvilforge::session::Session;

use crate::app::Models::User;
use crate::app::Http::Requests::{LoginRequest, RegisterRequest};

pub struct AuthController;

impl AuthController {
    /// GET /login
    pub async fn show_login() -> Result<ViewResponse> {
        Ok(ViewResponse::new(
            r#"<!DOCTYPE html><html><head><title>Log in</title></head><body>
<h1>Log in</h1>
<form method="POST" action="/login">
    <label>Email <input type="email" name="email" required></label><br>
    <label>Password <input type="password" name="password" required></label><br>
    <button type="submit">Log in</button>
</form>
<p>No account? <a href="/register">Register</a></p>
</body></html>"#.to_string(),
        ))
    }

    /// POST /login
    pub async fn login(
        State(c): State<Container>,
        session: Session,
        payload: LoginRequest,
    ) -> Result<Redirect> {
        let user = auth::attempt::<User>(&c, &payload.email, &payload.password)
            .await?
            .ok_or(Error::Unauthenticated)?;
        auth::login(&session, &user).await?;
        Ok(Redirect::to("/"))
    }

    /// GET /register
    pub async fn show_register() -> Result<ViewResponse> {
        Ok(ViewResponse::new(
            r#"<!DOCTYPE html><html><head><title>Register</title></head><body>
<h1>Register</h1>
<form method="POST" action="/register">
    <label>Name <input type="text" name="name" required></label><br>
    <label>Email <input type="email" name="email" required></label><br>
    <label>Password <input type="password" name="password" required minlength="8"></label><br>
    <button type="submit">Register</button>
</form>
<p>Already have an account? <a href="/login">Log in</a></p>
</body></html>"#.to_string(),
        ))
    }

    /// POST /register
    pub async fn register(
        State(c): State<Container>,
        session: Session,
        payload: RegisterRequest,
    ) -> Result<Redirect> {
        let hashed = auth::hash_password(&payload.password)?;
        let row: (i64,) = sqlx::query_as(
            "INSERT INTO users (name, email, password) VALUES ($1, $2, $3) RETURNING id",
        )
        .bind(&payload.name)
        .bind(&payload.email)
        .bind(&hashed)
        .fetch_one(c.pool())
        .await
        .map_err(Error::Database)?;
        let user = anvilforge::cast::Model::find(c.pool(), row.0)
            .await
            .map(|opt: Option<User>| opt.ok_or(Error::NotFound))
            .map_err(Error::from)??;
        auth::login(&session, &user).await?;
        Ok(Redirect::to("/"))
    }

    /// POST /logout
    pub async fn logout(session: Session) -> Result<Redirect> {
        auth::logout(&session).await?;
        Ok(Redirect::to("/"))
    }
}
"##;

const LOGIN_REQUEST: &str = r#"//! Login request.

use anvilforge::prelude::*;
use garde::Validate;

#[derive(Debug, Deserialize, Validate, FormRequest)]
pub struct LoginRequest {
    #[garde(email)]
    pub email: String,

    #[garde(length(min = 1))]
    pub password: String,
}
"#;

const REGISTER_REQUEST: &str = r#"//! Register request.

use anvilforge::prelude::*;
use garde::Validate;

#[derive(Debug, Deserialize, Validate, FormRequest)]
pub struct RegisterRequest {
    #[garde(length(min = 1, max = 80))]
    pub name: String,

    #[garde(email)]
    pub email: String,

    #[garde(length(min = 8))]
    pub password: String,
}
"#;

const AUTH_ROUTES: &str = r#"//! Auth routes.

use anvilforge::prelude::*;

use crate::app::Http::Controllers::AuthController;

pub fn register(r: Router) -> Router {
    r.get("/login", AuthController::show_login)
        .post("/login", AuthController::login)
        .get("/register", AuthController::show_register)
        .post("/register", AuthController::register)
        .post("/logout", AuthController::logout)
}
"#;

const LOGIN_VIEW: &str = r#"@extends("layouts.app")
@section("title", "Log in")
@section("content")
    <h1>Log in</h1>
    <form method="POST" action="/login">
        @csrf
        <label>Email <input type="email" name="email" value="@old('email')" required></label>
        @error('email')<p class="error">{{ message }}</p>@enderror

        <label>Password <input type="password" name="password" required></label>
        @error('password')<p class="error">{{ message }}</p>@enderror

        <button type="submit">Log in</button>
    </form>
    <p>No account? <a href="/register">Register</a></p>
@endsection
"#;

const REGISTER_VIEW: &str = r#"@extends("layouts.app")
@section("title", "Register")
@section("content")
    <h1>Register</h1>
    <form method="POST" action="/register">
        @csrf
        <label>Name <input type="text" name="name" value="@old('name')" required></label>
        @error('name')<p class="error">{{ message }}</p>@enderror

        <label>Email <input type="email" name="email" value="@old('email')" required></label>
        @error('email')<p class="error">{{ message }}</p>@enderror

        <label>Password <input type="password" name="password" required minlength="8"></label>
        @error('password')<p class="error">{{ message }}</p>@enderror

        <button type="submit">Register</button>
    </form>
    <p>Already have an account? <a href="/login">Log in</a></p>
@endsection
"#;

const AUTH_MIGRATION: &str = r#"//! Add auth columns to the users table.
//!
//! Idempotent on Postgres/MySQL via `ADD COLUMN IF NOT EXISTS`. SQLite has no
//! IF-NOT-EXISTS form for ALTER, so on SQLite this migration assumes the
//! columns are absent; if you already added them by hand, edit this file or
//! delete it before running `migrate`.

use anvilforge::prelude::*;
use anvilforge::cast::{Driver, Schema};

pub struct AddAuthColumnsToUsersTable;

impl CastMigration for AddAuthColumnsToUsersTable {
    fn name(&self) -> &'static str {
        "2026_01_01_000099_add_auth_columns_to_users"
    }

    fn up(&self, s: &mut Schema) {
        match s.driver() {
            Driver::Sqlite => {
                s.raw("ALTER TABLE users ADD COLUMN password TEXT NOT NULL DEFAULT ''");
                s.raw("ALTER TABLE users ADD COLUMN remember_token TEXT");
            }
            _ => {
                s.raw("ALTER TABLE users ADD COLUMN IF NOT EXISTS password VARCHAR(255) NOT NULL DEFAULT ''");
                s.raw("ALTER TABLE users ADD COLUMN IF NOT EXISTS remember_token VARCHAR(100)");
            }
        }
    }

    fn down(&self, s: &mut Schema) {
        match s.driver() {
            Driver::Sqlite => {
                // SQLite doesn't support `DROP COLUMN IF EXISTS`. Either of these
                // raw statements will error if the column is already missing —
                // that's tolerable for a rollback.
                s.raw("ALTER TABLE users DROP COLUMN password");
                s.raw("ALTER TABLE users DROP COLUMN remember_token");
            }
            _ => {
                s.raw("ALTER TABLE users DROP COLUMN IF EXISTS password");
                s.raw("ALTER TABLE users DROP COLUMN IF EXISTS remember_token");
            }
        }
    }
}
"#;