stamp-cli 0.3.2

A cli tool for applying project templates
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
use clap::{Parser, Subcommand};
use console::style;
use dialoguer::{Confirm, Input, MultiSelect, Select, theme::ColorfulTheme};
use directories::ProjectDirs;
use eros::{Context, bail};
use ignore::WalkBuilder;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fs, path::PathBuf, process::exit};
use tera::Tera;

#[derive(Parser)]
#[command(name = "yard", author = "Henry McMahon", version = "0.1", about =  "A cli tool for applying project templates", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Render a template in the registry to a destination directory
    Use {
        /// The template name in the registry
        name: String,
        /// Path to the destination folder
        #[clap(default_value = ".")]
        destination: PathBuf,
        /// Overwrite any conflicting files
        #[clap(long, group = "conflict_strategy")]
        overwrite_conflicts: bool,
        /// Skip any conflicting files
        #[clap(long, group = "conflict_strategy")]
        skip_conflicts: bool,
    },
    /// Render a template from a source directory to a destination directory
    From {
        /// Path to the source folder
        source: PathBuf,
        /// Path to the destination folder
        destination: PathBuf,
        /// Overwrite any conflicting files
        #[clap(long, group = "conflict_strategy")]
        overwrite_conflicts: bool,
        /// Skip any conflicting files
        #[clap(long, group = "conflict_strategy")]
        skip_conflicts: bool,
    },
    /// Register a template source directory. All templates within this directory (recursive) will be available.
    Register {
        /// Path to the source directory to register
        path: PathBuf,
    },
    /// Remove a registered source directory
    Remove {
        /// Path to the source directory to remove
        path: PathBuf,
    },
    /// List registered templates
    List,
}

#[derive(Debug, Deserialize)]
struct TemplateConfig {
    #[serde(default)]
    meta: MetaConfig,
    #[serde(default)]
    questions: Vec<Question>,
}

#[derive(Debug, Deserialize, Default)]
struct MetaConfig {
    description: Option<String>,
    name: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Question {
    id: String,
    #[serde(rename = "type")]
    kind: QuestionType,
    prompt: String,
    #[serde(default)]
    default: Option<toml::Value>,
    #[serde(default)]
    options: Option<Vec<String>>,
    #[serde(default)]
    choices: Option<Vec<MultiChoice>>,
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
enum QuestionType {
    String,
    Select,
    MultiSelect,
    Bool,
}

#[derive(Debug, Deserialize)]
struct MultiChoice {
    id: String,
    prompt: String,
    #[serde(default)]
    default: bool,
}

#[derive(Debug, Deserialize, Serialize, Default)]
struct Registry {
    sources: Vec<PathBuf>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum ConflictStrategy {
    Fail,
    Overwrite,
    Skip,
}

fn main() -> eros::Result<()> {
    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Use {
            name,
            destination,
            overwrite_conflicts,
            skip_conflicts,
        } => {
            let strategy = if overwrite_conflicts {
                ConflictStrategy::Overwrite
            } else if skip_conflicts {
                ConflictStrategy::Skip
            } else {
                ConflictStrategy::Fail
            };
            render_registered_template(name, destination, strategy)
        }
        Commands::From {
            source,
            destination,
            overwrite_conflicts,
            skip_conflicts,
        } => {
            let strategy = if overwrite_conflicts {
                ConflictStrategy::Overwrite
            } else if skip_conflicts {
                ConflictStrategy::Skip
            } else {
                ConflictStrategy::Fail
            };
            render_template(source, destination, strategy)
        }
        Commands::Register { path } => register_source(path),
        Commands::Remove { path } => remove_source(path),
        Commands::List => list_templates(),
    };

    if let Err(error) = result {
        eprintln!("Oops something went wrong.\n");
        eprintln!("{:?}", error);
        exit(1);
    };

    Ok(())
}

fn render_registered_template(
    template_name: String,
    destination_path: PathBuf,
    conflict_strategy: ConflictStrategy,
) -> eros::Result<()> {
    let registry = load_registry()?;
    let templates = find_templates(&registry.sources);

    let mut matches: Vec<&FoundTemplate> = Vec::new();

    let query_path = PathBuf::from(&template_name);

    for template in &templates {
        if template.name == template_name {
            matches.push(template);
            continue;
        }

        if template.path.ends_with(&query_path) {
            matches.push(template);
            continue;
        }
    }

    matches.sort_by_key(|t| &t.path);
    matches.dedup_by_key(|t| &t.path);

    if matches.is_empty() {
        bail!("Template '{}' not found in registry", template_name)
    } else if matches.len() > 1 {
        eprintln!("Ambiguous template match for '{}':", template_name);
        for m in matches {
            eprintln!(" - {} ({})", m.name, m.path.to_string_lossy());
        }
        bail!("Please provide a more specific path or name.");
    }

    let selected = matches[0];
    render_template(selected.path.clone(), destination_path, conflict_strategy)
}

fn render_template(
    template_path: PathBuf,
    destination_path: PathBuf,
    conflict_strategy: ConflictStrategy,
) -> eros::Result<()> {
    let config_path = template_path.join("stamp.toml");
    let config_contents = fs::read_to_string(&config_path)
        .with_context(|| format!("could not read `{}`", config_path.to_string_lossy()))?;
    let config: TemplateConfig = toml::from_str(&config_contents).with_context(|| {
        format!(
            "Template config from `{}` is not valid",
            config_path.to_string_lossy()
        )
    })?;

    let mut validation_errors = Vec::new();
    for question in &config.questions {
        if question.options.is_some() && question.choices.is_some() {
            validation_errors.push(format!(
                "Question '{}' cannot have both 'options' and 'choices'",
                question.id
            ));
        }

        match question.kind {
            QuestionType::Select => {
                if question.options.is_none() {
                    validation_errors.push(format!(
                        "Question '{}' of type 'select' must have 'options'",
                        question.id
                    ));
                }
                if question.choices.is_some() {
                    validation_errors.push(format!(
                        "Question '{}' of type 'select' cannot have 'choices'",
                        question.id
                    ));
                }
            }
            QuestionType::MultiSelect => {
                if question.choices.is_none() {
                    validation_errors.push(format!(
                        "Question '{}' of type 'multi-select' must have 'choices'",
                        question.id
                    ));
                }
                if question.options.is_some() {
                    validation_errors.push(format!(
                        "Question '{}' of type 'multi-select' cannot have 'options'",
                        question.id
                    ));
                }
            }
            QuestionType::String | QuestionType::Bool => {
                if question.options.is_some() {
                    validation_errors.push(format!(
                        "Question '{}' of type '{:?}' cannot have 'options'",
                        question.id, question.kind
                    ));
                }
                if question.choices.is_some() {
                    validation_errors.push(format!(
                        "Question '{}' of type '{:?}' cannot have 'choices'",
                        question.id, question.kind
                    ));
                }
            }
        }
    }

    if !validation_errors.is_empty() {
        eprintln!("Invalid template configuration:");
        for error in validation_errors {
            eprintln!(" - {}", error);
        }
        bail!("Template configuration validation failed");
    }

    if conflict_strategy == ConflictStrategy::Fail {
        let mut early_conflicts = Vec::new();
        for entry in walkdir::WalkDir::new(&template_path) {
            let entry = entry?;
            let path = entry.path();
            if path.is_file() {
                if path.file_name().is_some_and(|n| n == "stamp.toml") {
                    continue;
                }

                let relative = path.strip_prefix(&template_path)?;
                let relative_str = relative.to_string_lossy();

                if relative_str.contains("{{") {
                    // skip interpolation since these will be replaced and we don't know what the output will look like
                    continue;
                }
                let mut output_path = destination_path.join(relative);
                let file_name = path.file_name().unwrap_or_default().to_string_lossy();
                let is_tera = file_name.ends_with(".tera") || file_name.contains(".tera.");
                if is_tera {
                    let new_name = output_path
                        .file_name()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .replace(".tera", "");
                    output_path.set_file_name(new_name);
                }

                if output_path.exists() {
                    early_conflicts.push(output_path);
                }
            }
        }

        if !early_conflicts.is_empty() {
            eprintln!("Conflicting files found:");
            for conflict in early_conflicts {
                eprintln!(" - {}", conflict.to_string_lossy());
            }
            bail!(
                "Destination files already exist. Use --overwrite-conflicts or --skip-conflicts to resolve."
            );
        }
    }

    let mut context = tera::Context::new();

    let total_questions = config.questions.len();
    for (i, question) in config.questions.iter().enumerate() {
        let step = i + 1;
        let prompt = format!("[{}/{}] {}", step, total_questions, question.prompt);
        let theme = ColorfulTheme::default();

        match question.kind {
            QuestionType::String => {
                let default_val = question
                    .default
                    .as_ref()
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string());

                let mut input = Input::<String>::with_theme(&theme);
                input = input.with_prompt(&prompt);

                if let Some(default) = default_val {
                    input = input.default(default);
                }

                let value = input.interact()?;
                context.insert(&question.id, &value);
            }
            QuestionType::Bool => {
                let default_val = question
                    .default
                    .as_ref()
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);

                let value = Confirm::with_theme(&theme)
                    .with_prompt(&prompt)
                    .default(default_val)
                    .interact()?;
                context.insert(&question.id, &value);
            }
            QuestionType::Select => {
                if let Some(options) = &question.options {
                    let default_idx = question
                        .default
                        .as_ref()
                        .and_then(|v| v.as_str())
                        .and_then(|d| options.iter().position(|r| r == d))
                        .unwrap_or(0);

                    let selection = Select::with_theme(&theme)
                        .with_prompt(&prompt)
                        .default(default_idx)
                        .items(options)
                        .interact()?;

                    context.insert(&question.id, &options[selection]);
                }
            }
            QuestionType::MultiSelect => {
                if let Some(choices) = &question.choices {
                    let defaults: Vec<bool> = choices.iter().map(|c| c.default).collect();
                    let items: Vec<&String> = choices.iter().map(|c| &c.prompt).collect();

                    let selections = MultiSelect::with_theme(&theme)
                        .with_prompt(&prompt)
                        .items(&items)
                        .defaults(&defaults)
                        .interact()?;

                    for (idx, choice) in choices.iter().enumerate() {
                        let is_selected = selections.contains(&idx);
                        context.insert(&choice.id, &is_selected);
                    }

                    let selected_ids: Vec<&String> =
                        selections.iter().map(|&idx| &choices[idx].id).collect();
                    context.insert(&question.id, &selected_ids);
                }
            }
        }
    }

    let mut tera = Tera::default();
    tera.autoescape_on(vec![]);
    tera.set_escape_fn(|e| e.to_string());

    struct FileAction {
        source: PathBuf,
        destination: PathBuf,
        is_tera: bool,
    }

    let mut actions: Vec<FileAction> = Vec::new();

    for entry in walkdir::WalkDir::new(&template_path) {
        let entry = entry?;
        let path_in_template = entry.path();

        if path_in_template.is_file() {
            if path_in_template
                .file_name()
                .is_some_and(|name| name == "stamp.toml")
            {
                continue;
            }

            let relative_path_in_template = path_in_template.strip_prefix(&template_path)?;
            let output_path_original = destination_path.join(relative_path_in_template);
            let output_path: Result<PathBuf, String> = output_path_original
                .components()
                .map(|e| {
                    let str_part = e.as_os_str().to_string_lossy();
                    let processed_part = tera.render_str(&str_part, &context);
                    processed_part.map_err(|_| str_part.to_string())
                })
                .try_fold(PathBuf::new(), |acc, part| Ok(acc.join(&part?)));
            let output_path = output_path.map_err(|component_failed| {
                let output_path = output_path_original.to_string_lossy();
                eros::error!(
                    "Failed to render path component `{}` of `{}`",
                    component_failed,
                    output_path
                )
            })?;

            let file_name = path_in_template
                .file_name()
                .unwrap_or_default()
                .to_string_lossy();
            let is_tera = file_name.ends_with(".tera") || file_name.contains(".tera.");

            let mut final_output_path = output_path;
            if is_tera {
                let new_name = final_output_path
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .replace(".tera", "");
                final_output_path.set_file_name(new_name);
            }

            actions.push(FileAction {
                source: path_in_template.to_path_buf(),
                destination: final_output_path,
                is_tera,
            });
        }
    }

    match conflict_strategy {
        ConflictStrategy::Fail => {
            let mut conflicts = Vec::new();
            for action in &actions {
                if action.destination.exists() {
                    conflicts.push(action.destination.clone());
                }
            }
            if !conflicts.is_empty() {
                eprintln!("Conflicting files found:");
                for conflict in conflicts {
                    eprintln!(" - {}", conflict.to_string_lossy());
                }
                bail!(
                    "Destination files already exist. Use --overwrite-conflicts or --skip-conflicts to resolve."
                );
            }
        }
        ConflictStrategy::Skip => {
            actions.retain(|action| !action.destination.exists());
        }
        ConflictStrategy::Overwrite => {
            // Do nothing
        }
    }

    for action in actions {
        if let Some(parent) = action.destination.parent() {
            fs::create_dir_all(parent)?;
        }

        if action.is_tera {
            let tera_template = fs::read_to_string(&action.source)?;
            let rendered = tera.render_str(&tera_template, &context)?;
            fs::write(action.destination, rendered)?;
        } else {
            fs::copy(&action.source, &action.destination)?;
        }
    }

    println!("Template rendered successfully to {:?}", destination_path);
    Ok(())
}

fn register_source(path: PathBuf) -> eros::Result<()> {
    let mut registry = load_registry()?;
    let canon_path = fs::canonicalize(&path)
        .with_context(|| format!("Could not find path `{}`", path.to_string_lossy()))?;

    if !canon_path.is_dir() {
        bail!("Path must be a directory");
    }

    if registry.sources.contains(&canon_path) {
        println!(
            "Source `{}` already registered",
            canon_path.to_string_lossy()
        );
    } else {
        registry.sources.push(canon_path.clone());
        save_registry(&registry)?;
        println!(
            "Source `{}` registered successfully",
            canon_path.to_string_lossy()
        );
    }

    Ok(())
}

fn list_templates() -> eros::Result<()> {
    let registry = load_registry()?;

    if registry.sources.is_empty() {
        println!("No sources registered");
        return Ok(());
    }

    let templates = find_templates(&registry.sources);

    if templates.is_empty() {
        println!("No templates found in registered sources");
        return Ok(());
    }

    for template in templates {
        print!("{}", style(&template.name).bold().cyan());

        if let Some(desc) = template.description {
            print!(" - {}", style(desc).italic());
        }
        println!();

        println!("  {}", style(template.path.to_string_lossy()).dim());
        println!();
    }

    Ok(())
}

fn load_registry() -> eros::Result<Registry> {
    let registry_path = get_registry_path()?;
    if let Ok(contents) = fs::read_to_string(&registry_path) {
        let registry: Registry = serde_json::from_str(&contents).with_context(|| {
            format!(
                "Registry from `{}` is not valid",
                registry_path.to_string_lossy()
            )
        })?;
        Ok(registry)
    } else {
        Ok(Registry::default())
    }
}

fn get_registry_path() -> eros::Result<PathBuf> {
    if let Some(proj_dirs) = ProjectDirs::from("com", "mcmah309", "stamp") {
        let config_dir = proj_dirs.config_dir();
        fs::create_dir_all(config_dir)?;
        Ok(config_dir.join("template_registry.json"))
    } else {
        bail!("Could not determine configuration directory")
    }
}

fn save_registry(registry: &Registry) -> eros::Result<()> {
    let registry_path = get_registry_path()?;
    let contents = serde_json::to_string_pretty(registry)?;
    fs::write(registry_path, contents)?;
    Ok(())
}

fn remove_source(path: PathBuf) -> eros::Result<()> {
    let mut registry = load_registry()?;
    let canon_path = if path.exists() {
        fs::canonicalize(&path)?
    } else {
        path
    };

    if let Some(index) = registry.sources.iter().position(|r| *r == canon_path) {
        registry.sources.remove(index);
        save_registry(&registry)?;
        println!(
            "Source `{}` removed successfully",
            canon_path.to_string_lossy()
        );
    } else {
        bail!(
            "Source `{}` not found in registry",
            canon_path.to_string_lossy()
        );
    }

    Ok(())
}

struct FoundTemplate {
    path: PathBuf,
    name: String,
    description: Option<String>,
}

fn find_templates(sources: &[PathBuf]) -> Vec<FoundTemplate> {
    let mut templates = Vec::new();
    // Used to prevent recursing into already found templates
    let mut excluded_paths: HashSet<PathBuf> = HashSet::new();

    for source in sources {
        let walker = WalkBuilder::new(source)
            .max_depth(Some(4))
            .standard_filters(true)
            .build();

        for result in walker {
            match result {
                Ok(entry) => {
                    if entry.file_type().is_some_and(|ft| ft.is_dir()) {
                        let path = entry.path().to_path_buf();

                        // Skip if ancestor is already a template
                        if excluded_paths
                            .iter()
                            .any(|excluded| path.starts_with(excluded) && path != *excluded)
                        {
                            continue;
                        }

                        let config_path = path.join("stamp.toml");
                        if config_path.exists() {
                            // Found a template
                            excluded_paths.insert(path.clone());

                            let name = path
                                .file_name()
                                .unwrap_or_default()
                                .to_string_lossy()
                                .to_string();
                            let mut description = None;
                            let mut display_name = name.clone();

                            if let Ok(contents) = fs::read_to_string(&config_path)
                                && let Ok(config) = toml::from_str::<TemplateConfig>(&contents)
                            {
                                if let Some(n) = config.meta.name {
                                    display_name = n;
                                }
                                description = config.meta.description;
                            }

                            templates.push(FoundTemplate {
                                path,
                                name: display_name,
                                description,
                            });
                        }
                    }
                }
                Err(err) => {
                    eros::error!("Error walking directory: {}", err);
                }
            }
        }
    }

    templates
}