kick-rs-cli 0.1.0

`cargo kick` subcommand — scaffold (today), dev / generate / add (planned)
Documentation
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
//! `cargo kick` — companion CLI for the [`kick-rs`](https://crates.io/crates/kick-rs)
//! framework. Currently ships:
//!
//! - `new <name>`                               — scaffold a fresh kick-rs project
//! - `g module <name>`                          — generate a module skeleton
//! - `g service <module>/<service_name>`        — generate a `#[service]`-derived stub
//! - `g contributor <module>/<contributor>`     — generate a `#[contributor]` async fn
//! - `add <feature>`                            — toggle an opt-in `kick-rs` feature in Cargo.toml
//! - `info`                                     — print a snapshot of the current project
//! - `dev`                                      — watch the source tree and restart on save
//! - `check`                                    — lint the project for common misconfigurations
//!
//! Cargo subcommand convention: this binary is named `cargo-kick`, so
//! invoking `cargo kick <args>` runs us with `args[1] == "kick"`. We
//! strip that prefix below before handing off to clap.

use clap::{Parser, Subcommand};
use kick_rs_cli::register::RegisterOutcome;
use kick_rs_cli::{add, check, dev, generate, info, new};
use std::path::PathBuf;
use std::process::ExitCode;

#[derive(Parser)]
#[command(
    name = "cargo-kick",
    bin_name = "cargo kick",
    version,
    about = "Companion CLI for the kick-rs framework"
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Scaffold a new kick-rs project.
    New {
        /// Project name. Used as the Cargo `package.name` and (snake-
        /// cased) as the Rust crate identifier. Must be lowercase ASCII
        /// letters / digits / `-` / `_`, starting with a letter.
        name: String,

        /// Directory to create the project in. Defaults to `./<name>`.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Allow writing into a directory that already exists.
        /// Existing files inside are NOT removed.
        #[arg(long)]
        force: bool,
    },

    /// Generate code into an existing project (`g` is a shortcut).
    #[command(alias = "g")]
    Generate {
        #[command(subcommand)]
        kind: Generate,
    },

    /// Lint a kick-rs project for common misconfigurations:
    /// unmounted modules, stale `pub mod` declarations, services /
    /// contributors that aren't registered with their parent module.
    /// Exits non-zero on any finding.
    Check {
        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,
    },

    /// Watch the source tree and restart `cargo run` on save.
    /// Wraps `cargo run` with a debounced file watcher rooted at
    /// `src/`. Editor saves trigger a kill+respawn; stdout/stderr
    /// from the child stream through unchanged.
    Dev {
        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Extra directories to watch (in addition to `src/`).
        /// Repeatable: `--watch templates --watch fixtures`.
        #[arg(long = "watch")]
        watch: Vec<PathBuf>,

        /// Debounce window in milliseconds. Defaults to 250.
        #[arg(long, default_value_t = 250)]
        debounce_ms: u64,
    },

    /// Print a snapshot of the current project — package version,
    /// kick-rs dep version + features, and every module on disk with
    /// the services and contributors registered on each.
    Info {
        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Dep name to inspect (defaults to `kick-rs`).
        #[arg(long, default_value = "kick-rs")]
        dep_name: String,
    },

    /// Toggle an opt-in `kick-rs` feature on the umbrella dep in
    /// Cargo.toml. Pass `list` to print the known features.
    Add {
        /// Feature to enable. Pass `list` to print known features and exit.
        feature: String,

        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Dependency name to mutate (defaults to `kick-rs`). Useful
        /// when you've renamed the umbrella in a workspace.
        #[arg(long, default_value = "kick-rs")]
        dep_name: String,
    },
}

#[derive(Subcommand)]
enum Generate {
    /// Generate a new module skeleton (`mod.rs` + `handlers.rs`) and
    /// register it in `src/modules/mod.rs`.
    Module {
        /// Module name. Must be a Rust identifier: lowercase letters,
        /// digits, and underscores only (hyphens disallowed).
        name: String,

        /// Override the project root. Defaults to walking up from
        /// the current directory.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Overwrite existing files inside the module directory.
        #[arg(long)]
        force: bool,

        /// Skip the `.module(...)` insertion into `src/main.rs`. Use
        /// when your bootstrap lives outside `main.rs` and you want to
        /// register manually.
        #[arg(long)]
        no_register: bool,
    },

    /// Generate a `#[service]`-derived stub inside an existing module.
    /// Spec is `<module>/<service_name>`, e.g. `users/email_sender`.
    Service {
        /// `<module>/<service_name>` spec (both halves must be valid
        /// snake_case identifiers).
        spec: String,

        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Overwrite the service file if it already exists.
        #[arg(long)]
        force: bool,

        /// Skip the `use` + `.service::<...>()` insertion into the
        /// parent module's `mod.rs`.
        #[arg(long)]
        no_register: bool,
    },

    /// Generate a `#[contributor]` async fn (plus a stub Output struct)
    /// inside an existing module. Spec is `<module>/<contributor_name>`,
    /// e.g. `users/load_current_user`.
    Contributor {
        /// `<module>/<contributor_name>` spec (both halves must be
        /// valid snake_case identifiers).
        spec: String,

        /// Override the project root.
        #[arg(long)]
        path: Option<PathBuf>,

        /// Overwrite the contributor file if it already exists.
        #[arg(long)]
        force: bool,

        /// Skip the `use` + `.contribute(...)` insertion into the
        /// parent module's `mod.rs`.
        #[arg(long)]
        no_register: bool,
    },
}

fn main() -> ExitCode {
    // When invoked as `cargo kick new foo`, cargo runs us with argv
    // `["cargo-kick", "kick", "new", "foo"]`. Drop the redundant
    // "kick" so clap sees the real subcommand structure.
    let argv: Vec<String> = std::env::args()
        .enumerate()
        .filter(|(i, a)| !(*i == 1 && a == "kick"))
        .map(|(_, a)| a)
        .collect();

    let cli = match Cli::try_parse_from(argv) {
        Ok(c) => c,
        Err(e) => {
            // `e.print()` writes to the right stream (stdout for help,
            // stderr for errors) and `exit_code()` distinguishes them.
            let _ = e.print();
            return ExitCode::from(if e.exit_code() == 0 { 0 } else { 2 });
        }
    };

    match cli.command {
        Command::New { name, path, force } => {
            let args = new::NewArgs { name, path, force };
            match new::run(&args) {
                Ok(dest) => {
                    println!("✓ created kick-rs project at {}", dest.display());
                    println!("  next: cd {} && cargo run", dest.display());
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Generate {
            kind:
                Generate::Module {
                    name,
                    path,
                    force,
                    no_register,
                },
        } => {
            let args = generate::GenerateModuleArgs {
                name: name.clone(),
                project_root: path,
                force,
                auto_register: !no_register,
            };
            match generate::generate_module(&args) {
                Ok(res) => {
                    println!("✓ generated module at {}", res.module_dir.display());
                    print_register_outcome(
                        &res.register,
                        "main.rs",
                        &format!(".module(modules::{name}::define())"),
                    );
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Generate {
            kind:
                Generate::Service {
                    spec,
                    path,
                    force,
                    no_register,
                },
        } => {
            let args = generate::GenerateServiceArgs {
                spec: spec.clone(),
                project_root: path,
                force,
                auto_register: !no_register,
            };
            match generate::generate_service(&args) {
                Ok(res) => {
                    let (module, service_snake) = spec.split_once('/').unwrap();
                    let pascal = generate::to_pascal_case(service_snake);
                    println!("✓ generated service at {}", res.file.display());
                    print_register_outcome(
                        &res.register,
                        &format!("src/modules/{module}/mod.rs"),
                        &format!(
                            "use {service_snake}::{pascal};\n        ...\n        .service::<{pascal}>()"
                        ),
                    );
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Generate {
            kind:
                Generate::Contributor {
                    spec,
                    path,
                    force,
                    no_register,
                },
        } => {
            let args = generate::GenerateContributorArgs {
                spec: spec.clone(),
                project_root: path,
                force,
                auto_register: !no_register,
            };
            match generate::generate_contributor(&args) {
                Ok(res) => {
                    let (module, snake) = spec.split_once('/').unwrap();
                    let pascal = generate::to_pascal_case(snake);
                    println!("✓ generated contributor at {}", res.file.display());
                    print_register_outcome(
                        &res.register,
                        &format!("src/modules/{module}/mod.rs"),
                        &format!(
                            "use {snake}::{pascal};\n        ...\n        .contribute({pascal})"
                        ),
                    );
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Check { path } => {
            let args = check::CheckArgs { project_root: path };
            match check::run(&args) {
                Ok(report) => {
                    print!("{}", check::render(&report));
                    if report.is_clean() {
                        ExitCode::SUCCESS
                    } else {
                        // Use exit code 1 — make this a useful CI gate.
                        ExitCode::FAILURE
                    }
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Dev {
            path,
            watch,
            debounce_ms,
        } => {
            let args = dev::DevArgs {
                project_root: path,
                watch_paths: watch,
                debounce_ms,
            };
            match dev::run(&args) {
                Ok(()) => ExitCode::SUCCESS,
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Info { path, dep_name } => {
            let args = info::InfoArgs {
                project_root: path,
                dep_name,
            };
            match info::collect_info(&args) {
                Ok(snapshot) => {
                    print!("{}", info::render_info(&snapshot));
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
        Command::Add {
            feature,
            path,
            dep_name,
        } => {
            // `add list` is a help shortcut — print the catalog and exit.
            if feature == "list" {
                println!("kick-rs features that `cargo kick add` knows about:");
                for (name, desc) in add::KNOWN_FEATURES {
                    println!("  {name:10} — {desc}");
                }
                return ExitCode::SUCCESS;
            }

            let args = add::AddArgs {
                feature: feature.clone(),
                project_root: path,
                dep_name: dep_name.clone(),
            };
            match add::add_feature(&args) {
                Ok(add::AddOutcome::Added) => {
                    println!("✓ added `{feature}` to {dep_name} features in Cargo.toml");
                    ExitCode::SUCCESS
                }
                Ok(add::AddOutcome::AlreadyEnabled) => {
                    println!("· `{feature}` already enabled on {dep_name}");
                    ExitCode::SUCCESS
                }
                Err(e) => {
                    eprintln!("error: {e}");
                    ExitCode::FAILURE
                }
            }
        }
    }
}

/// Surface the auto-register outcome to the user as a short hint.
/// Inserted/AlreadyRegistered get a one-liner; otherwise we print the
/// manual snippet so adopters can paste it themselves.
fn print_register_outcome(outcome: &RegisterOutcome, target_path: &str, manual_snippet: &str) {
    match outcome {
        RegisterOutcome::Inserted => {
            println!("  ✓ registered in {target_path}");
        }
        RegisterOutcome::AlreadyRegistered => {
            println!("  · {target_path} already had the registration — no edit needed");
        }
        RegisterOutcome::TargetMissing => {
            println!("  ! {target_path} not found — add manually:");
            println!("        {manual_snippet}");
        }
        RegisterOutcome::AnchorNotFound => {
            println!("  ! could not find a known builder pattern in {target_path}; add manually:");
            println!("        {manual_snippet}");
        }
        RegisterOutcome::Skipped => {
            println!("  · skipped per --no-register — add manually:");
            println!("        {manual_snippet}");
        }
    }
}