blixt-cli 0.2.0

Blixt CLI — project scaffolding, code generation, dev server, and build tools
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
use std::fs;
use std::path::{Path, PathBuf};

use chrono::Utc;
use console::style;

use crate::validate::{to_pascal_case, to_snake_case};

/// Generates a controller with Askama template views.
///
/// Creates the controller Rust file and corresponding HTML templates
/// for index and show actions under the current working directory.
pub fn generate_controller(name: &str) -> Result<(), String> {
    let base = current_dir()?;
    generate_controller_in(&base, name)
}

/// Generates a model with a database migration.
///
/// Creates the model Rust file with SQLx derive macros and a
/// timestamped SQL migration for creating the table.
pub fn generate_model(name: &str) -> Result<(), String> {
    let base = current_dir()?;
    generate_model_in(&base, name)
}

/// Generates a full CRUD scaffold: controller, model, and list fragment.
///
/// Combines controller and model generation, then adds a Datastar-ready
/// list fragment template for streaming updates.
pub fn generate_scaffold(name: &str) -> Result<(), String> {
    let base = current_dir()?;
    generate_scaffold_in(&base, name)
}

// --- Path-aware implementations (testable without chdir) ---

/// Controller generation rooted at `base`.
fn generate_controller_in(base: &Path, name: &str) -> Result<(), String> {
    let snake = to_snake_case(name);
    let pascal = to_pascal_case(name);

    write_controller_file(base, &snake, &pascal)?;
    write_index_template(base, &snake, &pascal)?;
    write_show_template(base, &snake, &pascal)?;

    print_controller_route_hint(&snake);
    Ok(())
}

/// Model generation rooted at `base`.
fn generate_model_in(base: &Path, name: &str) -> Result<(), String> {
    let snake = to_snake_case(name);
    let pascal = to_pascal_case(name);
    let plural = format!("{snake}s");

    write_model_file(base, &snake, &pascal)?;
    write_migration_file(base, &snake, &plural)?;

    println!(
        "  {} model {} and migration for {plural}",
        style("created").green().bold(),
        snake
    );
    Ok(())
}

/// Scaffold generation rooted at `base`.
fn generate_scaffold_in(base: &Path, name: &str) -> Result<(), String> {
    let snake = to_snake_case(name);
    let pascal = to_pascal_case(name);

    generate_model_in(base, name)?;
    write_scaffold_controller_file(base, &snake, &pascal)?;
    write_index_template(base, &snake, &pascal)?;
    write_show_template(base, &snake, &pascal)?;
    write_list_fragment(base, &snake)?;

    println!("  {} controller {snake}", style("created").green().bold());
    print_scaffold_route_hints(&snake);
    Ok(())
}

// --- File writers (private helpers) ---

/// Writes the controller Rust source file.
fn write_controller_file(base: &Path, snake: &str, pascal: &str) -> Result<(), String> {
    let dir = base.join("src/controllers");
    let path = dir.join(format!("{snake}.rs"));
    let content = format!(
        r#"use blixt::prelude::*;

#[derive(Template)]
#[template(path = "pages/{snake}/index.html")]
pub struct {pascal}Index {{
    pub items: Vec<String>,
}}

pub async fn index() -> Result<{pascal}Index> {{
    Ok({pascal}Index {{
        items: vec![],
    }})
}}

#[derive(Template)]
#[template(path = "pages/{snake}/show.html")]
pub struct {pascal}Show {{
    pub id: String,
}}

pub async fn show(Path(id): Path<String>) -> Result<{pascal}Show> {{
    Ok({pascal}Show {{ id }})
}}
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes a scaffold controller with database-backed CRUD actions.
fn write_scaffold_controller_file(base: &Path, snake: &str, pascal: &str) -> Result<(), String> {
    let dir = base.join("src/controllers");
    let path = dir.join(format!("{snake}.rs"));
    let content = format!(
        r#"use blixt::prelude::*;
use crate::models::{snake}::{pascal};

#[derive(Template)]
#[template(path = "pages/{snake}/index.html")]
pub struct {pascal}Index {{
    pub items: Vec<{pascal}>,
}}

pub async fn index(State(ctx): State<AppContext>) -> Result<impl IntoResponse> {{
    let items = {pascal}::find_all(&ctx.db).await?;
    Ok(Html({pascal}Index {{ items }}.render().map_err(|e| Error::Internal(e.to_string()))?))
}}

#[derive(Template)]
#[template(path = "pages/{snake}/show.html")]
pub struct {pascal}Show {{
    pub item: {pascal},
}}

pub async fn show(State(ctx): State<AppContext>, Path(id): Path<i64>) -> Result<impl IntoResponse> {{
    let item = {pascal}::find_by_id(&ctx.db, id).await?;
    Ok(Html({pascal}Show {{ item }}.render().map_err(|e| Error::Internal(e.to_string()))?))
}}

pub async fn destroy(State(ctx): State<AppContext>, Path(id): Path<i64>) -> Result<impl IntoResponse> {{
    {pascal}::delete(&ctx.db, id).await?;
    let items = {pascal}::find_all(&ctx.db).await?;
    Ok(SseFragment::new({pascal}Index {{ items }})?)
}}
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes the index HTML template for a controller.
fn write_index_template(base: &Path, snake: &str, pascal: &str) -> Result<(), String> {
    let dir = base.join(format!("templates/pages/{snake}"));
    let path = dir.join("index.html");
    let content = format!(
        r#"{{% extends "layouts/app.html" %}}
{{% block title %}}{pascal} List{{% endblock %}}
{{% block content %}}
<h1>{pascal} List</h1>
{{% endblock %}}
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes the show HTML template for a controller.
fn write_show_template(base: &Path, snake: &str, pascal: &str) -> Result<(), String> {
    let dir = base.join(format!("templates/pages/{snake}"));
    let path = dir.join("show.html");
    let content = format!(
        r#"{{% extends "layouts/app.html" %}}
{{% block title %}}{pascal} Detail{{% endblock %}}
{{% block content %}}
<h1>{pascal} #{{{{ id }}}}</h1>
{{% endblock %}}
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes the model Rust source file with SQLx derives and CRUD methods.
fn write_model_file(base: &Path, snake: &str, pascal: &str) -> Result<(), String> {
    let dir = base.join("src/models");
    let path = dir.join(format!("{snake}.rs"));
    let plural = format!("{snake}s");
    let content = format!(
        r#"use blixt::prelude::*;
use sqlx::types::chrono::{{DateTime, Utc}};

#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct {pascal} {{
    pub id: i64,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}}

impl {pascal} {{
    /// Find a single record by primary key.
    pub async fn find_by_id(pool: &DbPool, id: i64) -> Result<Self> {{
        Ok(query_as!(Self, "SELECT id, created_at, updated_at FROM {plural} WHERE id = ?")
            .bind(id)
            .fetch_one(pool)
            .await?)
    }}

    /// Fetch all records ordered by most recent first.
    pub async fn find_all(pool: &DbPool) -> Result<Vec<Self>> {{
        Ok(query_as!(Self, "SELECT id, created_at, updated_at FROM {plural} ORDER BY id DESC")
            .fetch_all(pool)
            .await?)
    }}

    /// Delete a record by primary key.
    pub async fn delete(pool: &DbPool, id: i64) -> Result<()> {{
        query!("DELETE FROM {plural} WHERE id = ?")
            .bind(id)
            .execute(pool)
            .await?;
        Ok(())
    }}
}}
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes a timestamped SQL migration file.
fn write_migration_file(base: &Path, snake: &str, plural: &str) -> Result<(), String> {
    let timestamp = Utc::now().format("%Y%m%d%H%M%S");
    let dir = base.join("migrations");
    let path = dir.join(format!("{timestamp}_create_{snake}s.sql"));
    let content = format!(
        r#"CREATE TABLE IF NOT EXISTS {plural} (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

/// Writes a Datastar-ready list fragment template.
fn write_list_fragment(base: &Path, snake: &str) -> Result<(), String> {
    let dir = base.join(format!("templates/fragments/{snake}"));
    let path = dir.join("list.html");
    let content = format!(
        r#"<div id="{snake}-list">
    {{% for item in items %}}
    <div>{{{{ item.id }}}}</div>
    {{% endfor %}}
</div>
"#
    );

    ensure_dir_exists(&dir)?;
    write_file(&path, &content)
}

// --- Output helpers ---

/// Prints route registration hint after controller generation.
fn print_controller_route_hint(snake: &str) {
    println!("  {} controller {snake}", style("created").green().bold());
    println!(
        "\n  {} Add to src/main.rs routes:",
        style("next:").cyan().bold()
    );
    println!("    .route(\"/{snake}\", get(controllers::{snake}::index))");
    println!("    .route(\"/{snake}/{{id}}\", get(controllers::{snake}::show))");
}

/// Prints full CRUD route registration hints after scaffold generation.
fn print_scaffold_route_hints(snake: &str) {
    println!(
        "\n  {} Add CRUD routes to src/main.rs:",
        style("next:").cyan().bold()
    );
    println!("    .route(\"/{snake}\", get(controllers::{snake}::index))");
    println!("    .route(\"/{snake}\", post(controllers::{snake}::create))");
    println!("    .route(\"/{snake}/{{id}}\", get(controllers::{snake}::show))");
    println!("    .route(\"/{snake}/{{id}}\", put(controllers::{snake}::update))");
    println!("    .route(\"/{snake}/{{id}}\", delete(controllers::{snake}::destroy))");
}

// --- Filesystem utilities ---

/// Returns the current working directory as a `PathBuf`.
fn current_dir() -> Result<PathBuf, String> {
    std::env::current_dir().map_err(|err| format!("Failed to determine current directory: {err}"))
}

/// Creates a directory and all parents, returning an error on failure.
fn ensure_dir_exists(dir: &Path) -> Result<(), String> {
    fs::create_dir_all(dir)
        .map_err(|err| format!("Failed to create directory '{}': {err}", dir.display()))
}

/// Writes content to a file, failing if the file already exists.
fn write_file(path: &Path, content: &str) -> Result<(), String> {
    if path.exists() {
        return Err(format!("File already exists: {}", path.display()));
    }
    fs::write(path, content).map_err(|err| format!("Failed to write '{}': {err}", path.display()))
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;

    #[test]
    fn controller_creates_files_with_expected_content() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let base = tmp.path();

        generate_controller_in(base, "blog_post").expect("generate_controller_in failed");

        let controller = fs::read_to_string(base.join("src/controllers/blog_post.rs"))
            .expect("controller file missing");
        assert!(controller.contains("pub struct BlogPostIndex"));
        assert!(controller.contains("pub struct BlogPostShow"));
        assert!(controller.contains("pub async fn index()"));
        assert!(controller.contains("pub async fn show("));

        let index = fs::read_to_string(base.join("templates/pages/blog_post/index.html"))
            .expect("index template missing");
        assert!(index.contains("BlogPost List"));
        assert!(index.contains("extends \"layouts/app.html\""));

        let show = fs::read_to_string(base.join("templates/pages/blog_post/show.html"))
            .expect("show template missing");
        assert!(show.contains("BlogPost Detail"));
        assert!(show.contains("{{ id }}"));
    }

    #[test]
    fn model_creates_files_with_valid_structure() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let base = tmp.path();

        generate_model_in(base, "User").expect("generate_model_in failed");

        let model =
            fs::read_to_string(base.join("src/models/user.rs")).expect("model file missing");
        assert!(model.contains("pub struct User"));
        assert!(model.contains("pub id: i64"));
        assert!(model.contains("DateTime<Utc>"));
        assert!(model.contains("FromRow"));
        assert!(model.contains("find_by_id"));
        assert!(model.contains("find_all"));
        assert!(model.contains("delete"));
        assert!(model.contains("query_as!"));
        assert!(model.contains("query!"));

        let entries: Vec<_> = fs::read_dir(base.join("migrations"))
            .expect("migrations dir missing")
            .filter_map(|entry| entry.ok())
            .collect();
        assert_eq!(entries.len(), 1);

        let migration_path = entries[0].path();
        let filename = migration_path
            .file_name()
            .expect("no filename")
            .to_string_lossy();
        assert!(filename.ends_with("_create_users.sql"));

        let sql = fs::read_to_string(&migration_path).expect("migration file missing");
        assert!(sql.contains("CREATE TABLE IF NOT EXISTS users"));
        assert!(sql.contains("BIGSERIAL PRIMARY KEY"));
        assert!(sql.contains("created_at TIMESTAMPTZ"));
        assert!(sql.contains("updated_at TIMESTAMPTZ"));
    }

    #[test]
    fn scaffold_creates_controller_model_and_fragment() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let base = tmp.path();

        generate_scaffold_in(base, "Product").expect("generate_scaffold_in failed");

        assert!(base.join("src/controllers/product.rs").exists());
        assert!(base.join("src/models/product.rs").exists());
        assert!(base.join("templates/pages/product/index.html").exists());
        assert!(base.join("templates/pages/product/show.html").exists());

        let controller = fs::read_to_string(base.join("src/controllers/product.rs"))
            .expect("scaffold controller missing");
        assert!(controller.contains("find_all"));
        assert!(controller.contains("find_by_id"));
        assert!(controller.contains("State(ctx)"));

        let fragment = fs::read_to_string(base.join("templates/fragments/product/list.html"))
            .expect("list fragment missing");
        assert!(fragment.contains("product-list"));
        assert!(fragment.contains("item.id"));

        let entries: Vec<_> = fs::read_dir(base.join("migrations"))
            .expect("migrations dir missing")
            .filter_map(|entry| entry.ok())
            .collect();
        assert_eq!(entries.len(), 1);
    }

    #[test]
    fn duplicate_generation_returns_error() {
        let tmp = TempDir::new().expect("failed to create temp dir");
        let base = tmp.path();

        generate_controller_in(base, "Item").expect("first generation failed");

        let result = generate_controller_in(base, "Item");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("already exists"));
    }
}