clawless-cli 0.4.0

Generate and develop CLIs with Clawless
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
use std::fs::{create_dir_all, read_to_string, write};
use std::path::{Path, PathBuf};

use anyhow::{Result, anyhow};
use clawless::prelude::*;
use convert_case::{Case, Casing};
use indoc::indoc;

use crate::input::CommandName;

/// Arguments for the `generate command` command
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Args)]
pub struct GenerateCommandArgs {
    /// Name of the command to generate.
    ///
    /// Use slash notation for nested commands (e.g., "db/migrate").
    name: String,
}

/// Generate a new command in a Clawless project
///
/// This command creates a new command file with boilerplate code and automatically
/// adds the necessary `mod` statement to the parent module. It supports nested
/// command hierarchies using slash notation.
///
/// The command must be run from within a Clawless project directory (or any of
/// its subdirectories). The project is identified by the presence of a `main.rs`
/// file containing the `clawless::main!` macro.
///
/// # Examples
///
/// Generate a simple command:
/// ```shell
/// clawless generate command deploy
/// ```
///
/// Generate a nested command:
/// ```shell
/// clawless generate command db/migrate
/// ```
#[command(alias = "c")]
pub async fn command(args: GenerateCommandArgs, context: Context) -> CommandResult {
    // Check is command is running inside a Clawless project
    let project = find_clawless_project(context.current_working_directory())?;

    // Parse command name to support nested commands (e.g. `clawless generate command generate/context`)
    let command_name = CommandName::try_from(&args.name)?;

    // Create `src/commands/<parent commands>/` directories if they do not exist
    create_parent_directory(&project, &command_name)?;

    // Create `src/commands/<parent commands>/<command>.rs` file with boilerplate code
    create_command_file(&project, &command_name)?;

    // Add mod <command> to parent module
    insert_mod_statement(&project, &command_name)?;

    // Print a success message to the user

    Ok(())
}

fn find_clawless_project(current_working_directory: &CurrentWorkingDirectory) -> Result<PathBuf> {
    let main_rs_path = find_main_rs(current_working_directory)
        .ok_or_else(|| anyhow!("failed to find a main.rs file in the current directory or any of its parent directories"))?;

    check_main_rs(&main_rs_path)?;

    let project_path = main_rs_path
        .parent() // src
        .and_then(Path::parent) // crate root
        .ok_or_else(|| anyhow!("failed to determine the project directory from main.rs path"))?
        .to_path_buf();

    Ok(project_path)
}

fn find_main_rs(current_working_directory: &CurrentWorkingDirectory) -> Option<PathBuf> {
    let mut dir = current_working_directory.get().to_path_buf();

    loop {
        // Check for src/main.rs first, then main.rs in the current directory
        let src_main_rs = dir.join("src").join("main.rs");
        if src_main_rs.exists() {
            return Some(src_main_rs);
        }

        let main_rs = dir.join("main.rs");
        if main_rs.exists() {
            return Some(main_rs);
        }

        // Move up to parent directory, or return None if at filesystem root
        dir = dir.parent()?.to_path_buf();
    }
}

fn check_main_rs(path: &Path) -> Result<()> {
    let content =
        read_to_string(path).context(format!("failed to read main.rs at {}", path.display()))?;

    if !content.contains("clawless::main!") {
        anyhow::bail!(
            "the main.rs file at '{}' does not contain the 'clawless::main!' macro, indicating that this is not a Clawless project",
            path.display()
        );
    }

    Ok(())
}

fn create_parent_directory(project: &Path, command_name: &CommandName) -> Result<()> {
    let mut dir_path = project.join("src").join("commands");

    for module in command_name.parent_modules() {
        dir_path = dir_path.join(module.to_case(Case::Snake));
    }

    create_dir_all(&dir_path).context(format!(
        "failed to create parent directories for command at {}",
        dir_path.display()
    ))?;

    Ok(())
}

fn create_command_file(project_path: &Path, command_name: &CommandName) -> Result<()> {
    let struct_prefix = command_name.name().to_case(Case::Pascal);

    let boilerplate = format!(
        indoc! {
            r#"use clawless::prelude::*;

            #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Args)]
            pub struct {}Args {{
                // Define command arguments here
            }}

            #[command]
            pub async fn {}(args: {}Args, context: Context) -> CommandResult {{
                // Command implementation goes here
                Ok(())
            }}
            "#
        },
        struct_prefix,
        command_name.name(),
        struct_prefix
    );

    let command_file_path = command_name.path_from_project_root(project_path);

    write(&command_file_path, boilerplate).context(format!(
        "failed to create file for new command at {}",
        command_file_path.display()
    ))?;

    Ok(())
}

fn insert_mod_statement(project: &Path, command_name: &CommandName) -> Result<()> {
    // Find the parent module file where the mod statement should be inserted
    let parent = find_parent_module(project, command_name)?;

    let mod_statement = format!("mod {};", command_name.name());

    // Read current content and ensure we don't duplicate the mod statement
    let mut content = read_to_string(&parent)?;
    if content.contains(&mod_statement) {
        return Ok(());
    }

    // Read the file into memory so that we can iterate over it
    let mut lines: Vec<&str> = content.lines().collect();

    // Find insertion point: after last mod, or last use, or first non-doc-comment line, or at start
    let index =
        lines
            .iter()
            .enumerate()
            .rev()
            .find_map(|(index, line)| line.trim_start().starts_with("mod ").then_some(index))
            .or_else(|| {
                lines.iter().enumerate().rev().find_map(|(index, line)| {
                    line.trim_start().starts_with("use ").then_some(index)
                })
            })
            .or_else(|| {
                lines.iter().enumerate().find_map(|(index, line)| {
                    (!line.trim_start().starts_with("//!")).then_some(index)
                })
            })
            .unwrap_or(0);

    // Insert the mod statement after the found index
    lines.insert(index + 1, &mod_statement);
    content = lines.join("\n");

    // Insert a trailing newline
    content.push('\n');

    // Write the updated content back to the parent module file
    write(&parent, content)?;

    Ok(())
}

fn find_parent_module(project: &Path, command_name: &CommandName) -> Result<PathBuf> {
    let parent_modules = command_name.parent_modules();

    // The parent module is either "commands" (if no nesting) or the last element in parent_modules
    let commands_default = "commands".to_string();
    let parent_name = parent_modules.last().unwrap_or(&commands_default);

    // Determine the path to the parent module file (either as a file or mod.rs)
    let mut base = project.join("src").join("commands");

    // Navigate down to the parent's directory, excluding the parent itself
    if parent_modules.len() > 1 {
        for module in &parent_modules[..parent_modules.len() - 1] {
            base = base.join(module.to_case(Case::Snake));
        }
    } else if parent_modules.is_empty() {
        // Parent is src/commands itself
        base = project.join("src");
    }

    let candidate_file = base.join(format!("{}.rs", parent_name.to_case(Case::Snake)));
    let candidate_dir_mod = base.join(parent_name.to_case(Case::Snake)).join("mod.rs");

    if candidate_file.exists() {
        Ok(candidate_file)
    } else if candidate_dir_mod.exists() {
        Ok(candidate_dir_mod)
    } else {
        Err(anyhow!(
            "parent module `{}` does not exist under `src/commands`; refusing to create it",
            parent_name
        ))
    }
}

#[cfg(test)]
mod tests {
    use std::fs::{create_dir, create_dir_all, write};

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn check_clawless_project_finds_main_in_src_directory() {
        let cwd = TempDir::new().unwrap();

        let src_directory = cwd.path().join("src");
        create_dir(&src_directory).unwrap();

        let main_rs_path = src_directory.join("main.rs");
        write(&main_rs_path, "clawless::main!();").unwrap();

        let check = find_clawless_project(&cwd.path().into()).unwrap();

        assert_eq!(cwd.path(), &check);
    }

    #[test]
    fn check_clawless_project_finds_main_in_directory() {
        let cwd = TempDir::new().unwrap();

        let src_directory = cwd.path().join("src");
        create_dir(&src_directory).unwrap();

        let main_rs_path = src_directory.join("main.rs");
        write(&main_rs_path, "clawless::main!();").unwrap();

        let check = find_clawless_project(&src_directory.as_path().into()).unwrap();

        assert_eq!(cwd.path(), &check);
    }

    #[test]
    fn check_clawless_project_finds_main_in_parent_directory() {
        let cwd = TempDir::new().unwrap();

        let src_directory = cwd.path().join("src");

        // Create a subdirectory in which to call `check_clawless_project`
        let sub_dir = src_directory.join("subdir");
        create_dir_all(&sub_dir).unwrap();

        let main_rs_path = src_directory.join("main.rs");
        write(&main_rs_path, "clawless::main!();").unwrap();

        let check = find_clawless_project(&sub_dir.as_path().into()).unwrap();

        assert_eq!(cwd.path(), &check);
    }

    #[test]
    fn check_clawless_project_fails_without_main_rs() {
        let cwd = TempDir::new().unwrap();

        let check = find_clawless_project(&cwd.path().into());

        assert!(check.is_err());
    }

    #[test]
    fn check_clawless_project_fails_without_clawless_macro() {
        let cwd = TempDir::new().unwrap();

        let main_rs_path = cwd.path().join("main.rs");
        write(&main_rs_path, "fn main() {}").unwrap();

        let check = find_clawless_project(&cwd.path().into());

        assert!(check.is_err());
    }

    #[test]
    fn create_command_file_writes_boilerplate() {
        let cwd = TempDir::new().unwrap();
        create_dir_all(cwd.path().join("src").join("commands").join("parent")).unwrap();

        let command_name = CommandName::builder()
            .name("command".to_string())
            .parent_modules(vec!["parent".into()])
            .build();

        create_command_file(cwd.path(), &command_name).unwrap();

        let command_file_path = command_name.path_from_project_root(cwd.path());
        let content = read_to_string(command_file_path).unwrap();

        assert!(content.contains("pub struct CommandArgs"));
        assert!(content.contains("pub async fn command(args: CommandArgs, context: Context)"));
    }

    #[test]
    fn find_parent_module_locates_file_module() {
        let cwd = TempDir::new().unwrap();
        create_dir_all(cwd.path().join("src").join("commands")).unwrap();

        let commands_rs_path = cwd.path().join("src").join("commands.rs");
        write(&commands_rs_path, "").unwrap();

        let command_name = CommandName::builder()
            .name("test".to_string())
            .parent_modules(vec![])
            .build();

        let parent_module_path = find_parent_module(cwd.path(), &command_name).unwrap();

        assert_eq!(parent_module_path, commands_rs_path);
    }

    #[test]
    fn find_parent_module_locates_mod_rs_module() {
        let cwd = TempDir::new().unwrap();
        create_dir_all(cwd.path().join("src").join("commands")).unwrap();

        let commands_mod_rs_path = cwd.path().join("src").join("commands").join("mod.rs");
        write(&commands_mod_rs_path, "").unwrap();

        let command_name = CommandName::builder()
            .name("test".to_string())
            .parent_modules(vec![])
            .build();

        let parent_module_path = find_parent_module(cwd.path(), &command_name).unwrap();

        assert_eq!(parent_module_path, commands_mod_rs_path);
    }

    #[test]
    fn insert_mod_statement_inserts_after_use_in_commands_rs() {
        let cwd = TempDir::new().unwrap();
        create_dir_all(cwd.path().join("src")).unwrap();

        let commands_rs_path = cwd.path().join("src").join("commands.rs");
        // file with use statements but no mod statements
        write(
            &commands_rs_path,
            "use crate::foo;\nuse crate::bar;\n\nfn helper() {}\n",
        )
        .unwrap();

        let command_name = CommandName::builder()
            .name("mycmd".to_string())
            .parent_modules(vec![])
            .build();

        insert_mod_statement(cwd.path(), &command_name).unwrap();

        let content = read_to_string(&commands_rs_path).unwrap();
        assert!(content.contains("mod mycmd;"));

        // Ensure the inserted `mod` appears after at last `use` and before the function
        let idx_first_use = content.find("use crate::bar;").unwrap();
        let idx_mod = content.find("mod mycmd;").unwrap();
        let idx_fn = content.find("fn helper()").unwrap();
        assert!(idx_first_use < idx_mod && idx_mod < idx_fn);
    }

    #[test]
    fn insert_mod_statement_does_not_duplicate_existing_mod() {
        let cwd = TempDir::new().unwrap();
        create_dir_all(cwd.path().join("src")).unwrap();

        let commands_rs_path = cwd.path().join("src").join("commands.rs");
        write(&commands_rs_path, "mod existing;\nmod mycmd;\n").unwrap();

        let command_name = CommandName::builder()
            .name("mycmd".to_string())
            .parent_modules(vec![])
            .build();

        // Should be a no-op and not produce a second `mod mycmd;`
        insert_mod_statement(cwd.path(), &command_name).unwrap();

        let content = read_to_string(&commands_rs_path).unwrap();
        // There should be exactly one occurrence
        let occurrences = content.matches("mod mycmd;").count();
        assert_eq!(occurrences, 1);
    }

    #[test]
    fn insert_mod_statement_inserts_after_existing_use_and_mod() {
        let cwd = TempDir::new().unwrap();
        let base = cwd.path().join("src").join("commands").join("parent");
        create_dir_all(&base).unwrap();

        let parent_mod_rs = base.join("mod.rs");
        write(
            &parent_mod_rs,
            "//! module docs\nuse crate::common;\n\nmod another_command;\n\n// implementation\n",
        )
        .unwrap();

        let command_name = CommandName::builder()
            .name("command".to_string())
            .parent_modules(vec!["parent".into()])
            .build();

        insert_mod_statement(cwd.path(), &command_name).unwrap();

        let content = read_to_string(&parent_mod_rs).unwrap();
        assert!(content.contains("mod command;"));

        // Ensure the mod was inserted before the implementation comment and after at least one use
        let idx_existing_mod = content.find("mod another_command;").unwrap();
        let idx_mod = content.find("mod command;").unwrap();
        let idx_impl = content.find("// implementation").unwrap();
        assert!(idx_existing_mod < idx_mod && idx_mod < idx_impl);
    }
}