plzplz 0.0.9

A simple cross-platform task runner with helpful defaults
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
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
mod config;
mod hooks;
mod init;
mod runner;
mod settings;
mod templates;
mod utils;

use anyhow::{Result, bail};
use clap::{Parser, Subcommand};
use std::env;
use std::io::IsTerminal;
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "plz")]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    /// Task name, followed by any extra arguments to pass through
    #[arg(trailing_var_arg = true)]
    task: Vec<String>,

    /// Disable interactive prompts (auto-detected in CI)
    #[arg(long)]
    no_interactive: bool,
}

#[derive(Subcommand)]
enum Command {
    /// Manage plz itself: init, add, hooks, schema, and more
    Plz {
        #[command(subcommand)]
        plz_command: Option<PlzCommand>,
    },
}

#[derive(Subcommand)]
enum PlzCommand {
    /// Create a plz.toml
    Init,
    /// Add a new task to plz.toml
    Add {
        /// Name for the new task (prompted if omitted)
        name: Option<String>,
    },
    /// Install or manage git hooks
    Hooks {
        #[command(subcommand)]
        hook_command: Option<HookCommand>,
    },
    /// Print JSON Schema for plz.toml
    Schema,
    /// Print a cheatsheet of plz.toml features
    Cheatsheet,
    /// Update plz to the latest version
    Update,
}

#[derive(Subcommand)]
enum HookCommand {
    /// Install git hooks from plz.toml
    Install,
    /// Uninstall plz-managed git hooks
    Uninstall,
    /// Add a git hook stage to existing tasks
    Add,
    /// Run all tasks for a git hook stage (called by hook scripts)
    Run {
        /// The git hook stage to run (e.g. pre-commit)
        stage: String,
        /// Extra arguments forwarded to tasks (e.g. commit message file for commit-msg)
        #[arg(trailing_var_arg = true)]
        args: Vec<String>,
    },
}

fn is_nested() -> bool {
    env::var_os("PLZ_COMMAND").is_some()
}

fn is_interactive(cli: &Cli) -> bool {
    if cli.no_interactive {
        return false;
    }
    if is_nested() {
        return false;
    }
    if is_ci::cached() {
        eprintln!("Skipping interactive prompts: CI environment detected");
        return false;
    }
    if !std::io::stdin().is_terminal() {
        eprintln!("Skipping interactive prompts: stdin is not a terminal");
        return false;
    }
    true
}

const CONFIG_NAMES: &[&str] = &["plz.toml", ".plz.toml"];

fn find_config() -> Option<PathBuf> {
    let cwd = env::current_dir().ok()?;
    for name in CONFIG_NAMES {
        let path = cwd.join(name);
        if path.exists() {
            return Some(path);
        }
    }
    None
}

struct HelpEntry {
    usage: &'static str,
    description: &'static str,
}

const HELP_COMMANDS: &[HelpEntry] = &[
    HelpEntry {
        usage: "init",
        description: "Create a plz.toml",
    },
    HelpEntry {
        usage: "add [name]",
        description: "Add a new task to plz.toml",
    },
    HelpEntry {
        usage: "hooks",
        description: "Install or manage git hooks",
    },
    HelpEntry {
        usage: "hooks add",
        description: "Add a git hook to existing tasks",
    },
    HelpEntry {
        usage: "schema",
        description: "Print JSON Schema for plz.toml",
    },
    HelpEntry {
        usage: "cheatsheet",
        description: "Print a cheatsheet of plz.toml features",
    },
    HelpEntry {
        usage: "update",
        description: "Update plz to the latest version",
    },
    HelpEntry {
        usage: "plz",
        description: "Manage global defaults",
    },
];

const HELP_OPTIONS: &[HelpEntry] = &[
    HelpEntry {
        usage: "--no-interactive",
        description: "Disable interactive prompts (auto-detected in CI)",
    },
    HelpEntry {
        usage: "-h, --help",
        description: "Print help",
    },
];

enum ResolvedTask {
    Task(String),
    GroupTask(String, String),
}

pub fn format_help() -> String {
    let dim = "\x1b[2m";
    let bold = "\x1b[1m";
    let reset = "\x1b[0m";

    let mut out = String::new();
    out.push_str(&format!(
        "{bold}plz{reset} [task] [args...]          Run a task from plz.toml\n"
    ));
    out.push_str(&format!(
        "{bold}plz{reset} [group] [task] [args...]  Run a task from a task group\n"
    ));
    out.push_str(&format!(
        "{bold}plz{reset}                           Choose a task interactively\n"
    ));
    out.push('\n');

    let max_usage = HELP_COMMANDS
        .iter()
        .map(|e| e.usage.len())
        .max()
        .unwrap_or(0);
    out.push_str(&format!("{dim}Commands:{reset}\n"));
    for entry in HELP_COMMANDS {
        let padding = " ".repeat(max_usage - entry.usage.len() + 2);
        out.push_str(&format!(
            "  {dim}plz{reset} {}{padding}{}\n",
            entry.usage, entry.description
        ));
    }

    out.push('\n');
    let max_opt = HELP_OPTIONS
        .iter()
        .map(|e| e.usage.len())
        .max()
        .unwrap_or(0);
    out.push_str(&format!("{dim}Options:{reset}\n"));
    for entry in HELP_OPTIONS {
        let padding = " ".repeat(max_opt - entry.usage.len() + 2);
        out.push_str(&format!(
            "  {}{padding}{}\n",
            entry.usage, entry.description
        ));
    }

    out
}

fn main() -> Result<()> {
    // Intercept --help/-h at top level before clap parses
    // (clap's help is disabled so subcommands keep their own help)
    {
        let args: Vec<String> = env::args().collect();
        if args.len() == 2 && (args[1] == "--help" || args[1] == "-h" || args[1] == "help") {
            print!("{}", format_help());
            return Ok(());
        }
    }

    let cli = Cli::parse();

    match cli.command {
        Some(Command::Plz { ref plz_command }) => match plz_command {
            Some(PlzCommand::Init) => return init::run(),
            Some(PlzCommand::Add { name }) => return init::add_task(name.clone()),
            Some(PlzCommand::Schema) => {
                let schema = schemars::schema_for!(config::PlzConfig);
                println!("{}", serde_json::to_string_pretty(&schema)?);
                return Ok(());
            }
            Some(PlzCommand::Cheatsheet) => return init::print_cheatsheet(),
            Some(PlzCommand::Update) => return init::self_update(),
            Some(PlzCommand::Hooks { hook_command }) => {
                let config_path =
                    find_config().ok_or_else(|| anyhow::anyhow!("No plz.toml found"))?;
                let config = config::load(&config_path)?;
                let base_dir = config_path.parent().unwrap().to_path_buf();
                let interactive = is_interactive(&cli);
                match hook_command {
                    Some(HookCommand::Install) => return hooks::install(&config, &base_dir),
                    Some(HookCommand::Uninstall) => return hooks::uninstall(&config, &base_dir),
                    Some(HookCommand::Add) => return hooks::add_hook(&config, &config_path),
                    Some(HookCommand::Run { stage, .. }) => {
                        return hooks::run_stage(&config, stage, &base_dir, interactive);
                    }
                    None => {
                        return hooks::interactive_install(&config, &base_dir, interactive);
                    }
                }
            }
            None => return init::setup(),
        },
        None => {}
    }

    let interactive = is_interactive(&cli);

    let config_path = match find_config() {
        Some(path) => path,
        None => {
            if cli.task.is_empty() {
                if interactive {
                    return init::run();
                }
                print!("{}", format_help());
                return Ok(());
            }
            if let Some(result) = try_plz_subcommand(&cli.task) {
                return result;
            }
            bail!("No plz.toml found. Run `plz init` to create one.");
        }
    };
    let config = config::load(&config_path)?;
    let base_dir = config_path.parent().unwrap().to_path_buf();

    if cli.task.is_empty() {
        if !interactive {
            bail!("No task specified (running in non-interactive mode)");
        }

        // Build pick list: top-level tasks + group:task entries
        let mut pick_entries: Vec<(String, ResolvedTask)> = Vec::new();
        let mut names: Vec<&String> = config.tasks.keys().collect();
        names.sort();
        for name in &names {
            pick_entries.push((name.to_string(), ResolvedTask::Task(name.to_string())));
        }
        if let Some(ref groups) = config.taskgroup {
            let mut group_names: Vec<&String> = groups.keys().collect();
            group_names.sort();
            for gname in group_names {
                let group = &groups[gname];
                let mut task_names: Vec<&String> = group.tasks.keys().collect();
                task_names.sort();
                for tname in task_names {
                    pick_entries.push((
                        format!("{gname}:{tname}"),
                        ResolvedTask::GroupTask(gname.clone(), tname.clone()),
                    ));
                }
            }
        }

        if pick_entries.is_empty() {
            bail!("No tasks defined in plz.toml");
        }

        let items: Vec<utils::PickItem> = pick_entries
            .iter()
            .map(|(label, resolved)| {
                let desc = match resolved {
                    ResolvedTask::Task(n) => {
                        config.tasks[n].description.clone().unwrap_or_default()
                    }
                    ResolvedTask::GroupTask(g, t) => config
                        .get_group_task(g, t)
                        .and_then(|task| task.description.clone())
                        .unwrap_or_default(),
                };
                utils::PickItem {
                    label: label.clone(),
                    description: desc,
                    preview: None,
                }
            })
            .collect();
        match utils::pick_from_list(&items, "Enter to run ยท Esc to cancel")? {
            Some(idx) => {
                match &pick_entries[idx].1 {
                    ResolvedTask::Task(name) => {
                        runner::run_task(&config, name, &base_dir, interactive)?;
                    }
                    ResolvedTask::GroupTask(g, t) => {
                        runner::run_group_task(&config, g, t, &base_dir, interactive)?;
                    }
                }
                hooks::hint_uninstalled_hooks(&config, &base_dir);
                return Ok(());
            }
            None => {
                println!("\x1b[2mโœ•  Cancelled\x1b[0m");
                return Ok(());
            }
        }
    }

    let input = &cli.task[0];

    // Fall through to built-in subcommands if no task matches
    if !config.tasks.contains_key(input)
        && let Some(result) = try_plz_subcommand(&cli.task)
    {
        return result;
    }

    let resolved = resolve_task(&config, input, &cli.task[1..], interactive)?;
    match resolved {
        ResolvedTask::Task(task_name) => {
            let extra_args = &cli.task[1..];
            runner::run_task_with_args(&config, &task_name, &base_dir, interactive, extra_args)?;
        }
        ResolvedTask::GroupTask(group, task) => {
            // For group tasks, args start at [2] (task[0]=group, task[1]=task_name)
            let extra_args = if cli.task.len() > 2 {
                &cli.task[2..]
            } else {
                &[]
            };
            runner::run_group_task_with_args(
                &config,
                &group,
                &task,
                &base_dir,
                interactive,
                extra_args,
            )?;
        }
    }
    hooks::hint_uninstalled_hooks(&config, &base_dir);

    Ok(())
}

fn try_plz_subcommand(task: &[String]) -> Option<Result<()>> {
    let input = task.first()?.as_str();
    match input {
        "init" => Some(init::run()),
        "add" => {
            let name = task.get(1).cloned();
            Some(init::add_task(name))
        }
        "schema" => {
            let schema = schemars::schema_for!(config::PlzConfig);
            Some(
                serde_json::to_string_pretty(&schema)
                    .map(|s| println!("{}", s))
                    .map_err(Into::into),
            )
        }
        "cheatsheet" => Some(init::print_cheatsheet()),
        "update" => Some(init::self_update()),
        "help" => {
            print!("{}", format_help());
            Some(Ok(()))
        }
        "hooks" => {
            let config_path = match find_config() {
                Some(p) => p,
                None => return Some(Err(anyhow::anyhow!("No plz.toml found"))),
            };
            let config = match config::load(&config_path) {
                Ok(c) => c,
                Err(e) => return Some(Err(e)),
            };
            let base_dir = config_path.parent().unwrap().to_path_buf();
            let sub = task.get(1).map(|s| s.as_str());
            match sub {
                Some("install") => Some(hooks::install(&config, &base_dir)),
                Some("uninstall") => Some(hooks::uninstall(&config, &base_dir)),
                Some("add") => Some(hooks::add_hook(&config, &config_path)),
                _ => {
                    let interactive = !is_ci::cached()
                        && std::io::stdin().is_terminal()
                        && env::var_os("PLZ_COMMAND").is_none();
                    Some(hooks::interactive_install(&config, &base_dir, interactive))
                }
            }
        }
        _ => None,
    }
}

fn resolve_task(
    config: &config::PlzConfig,
    input: &str,
    rest: &[String],
    interactive: bool,
) -> Result<ResolvedTask> {
    // 1. Exact match on top-level task (top-level wins)
    if config.tasks.contains_key(input) {
        return Ok(ResolvedTask::Task(input.to_string()));
    }

    // 2. Check if input matches a taskgroup name
    if let Some(group) = config.get_group(input) {
        if rest.is_empty() {
            // `plz <group>` with no task โ€” interactive picker within group
            if !interactive {
                bail!(
                    "No task specified for group \"{input}\". Available tasks: {}",
                    {
                        let mut names: Vec<&String> = group.tasks.keys().collect();
                        names.sort();
                        names
                            .iter()
                            .map(|n| n.as_str())
                            .collect::<Vec<_>>()
                            .join(", ")
                    }
                );
            }
            let mut names: Vec<&String> = group.tasks.keys().collect();
            names.sort();
            if names.is_empty() {
                bail!("No tasks defined in group \"{input}\"");
            }
            let items: Vec<utils::PickItem> = names
                .iter()
                .map(|name| utils::PickItem {
                    label: name.to_string(),
                    description: group.tasks[*name].description.clone().unwrap_or_default(),
                    preview: None,
                })
                .collect();
            match utils::pick_from_list(&items, "Enter to run ยท Esc to cancel")? {
                Some(idx) => {
                    return Ok(ResolvedTask::GroupTask(
                        input.to_string(),
                        names[idx].clone(),
                    ));
                }
                None => bail!("Cancelled"),
            }
        }

        let task_input = &rest[0];

        // Exact match within group
        if group.tasks.contains_key(task_input.as_str()) {
            return Ok(ResolvedTask::GroupTask(
                input.to_string(),
                task_input.clone(),
            ));
        }

        // Fuzzy match within group
        if !interactive {
            bail!("\"{input}:{task_input}\" isn't a task. Run `plz {input}` to see group tasks.");
        }

        let mut matches: Vec<&String> = group
            .tasks
            .keys()
            .filter(|k| utils::fuzzy_match(task_input, k))
            .collect();
        matches.sort();

        match matches.len() {
            0 => {
                bail!(
                    "\"{input}:{task_input}\" isn't a task. Run `plz {input}` to see group tasks."
                )
            }
            1 => {
                let confirmed: bool =
                    cliclack::confirm(format!("Did you mean \"{input}:{}\"?", matches[0]))
                        .initial_value(true)
                        .interact()?;
                if confirmed {
                    return Ok(ResolvedTask::GroupTask(
                        input.to_string(),
                        matches[0].clone(),
                    ));
                }
                bail!("Cancelled");
            }
            _ => {
                let selected: &&String = cliclack::select("Did you mean...".to_string())
                    .items(
                        &matches
                            .iter()
                            .map(|n| (n, n.as_str(), ""))
                            .collect::<Vec<_>>(),
                    )
                    .interact()?;
                return Ok(ResolvedTask::GroupTask(
                    input.to_string(),
                    selected.to_string(),
                ));
            }
        }
    }

    // 3. Fall through to fuzzy match on top-level tasks
    if !interactive {
        bail!("\"{input}\" isn't a task. Run `plz` to see all commands.");
    }

    let mut matches: Vec<&String> = config
        .tasks
        .keys()
        .filter(|k| utils::fuzzy_match(input, k))
        .collect();
    matches.sort();

    match matches.len() {
        0 => {
            eprintln!("\x1b[2m\"{input}\" isn't a task.\x1b[0m\n");
            let mut names: Vec<&String> = config.tasks.keys().collect();
            names.sort();
            if names.is_empty() {
                bail!("No tasks defined in plz.toml");
            }
            let items: Vec<utils::PickItem> = names
                .iter()
                .map(|name| utils::PickItem {
                    label: name.to_string(),
                    description: config.tasks[*name].description.clone().unwrap_or_default(),
                    preview: None,
                })
                .collect();
            match utils::pick_from_list(&items, "Enter to run ยท Esc to cancel")? {
                Some(idx) => Ok(ResolvedTask::Task(names[idx].clone())),
                None => bail!("Cancelled"),
            }
        }
        1 => {
            let confirmed: bool = cliclack::confirm(format!("Did you mean \"{}\"?", matches[0]))
                .initial_value(true)
                .interact()?;
            if confirmed {
                Ok(ResolvedTask::Task(matches[0].clone()))
            } else {
                bail!("Cancelled");
            }
        }
        _ => {
            let selected: &&String = cliclack::select("Did you mean...".to_string())
                .items(
                    &matches
                        .iter()
                        .map(|n| (n, n.as_str(), ""))
                        .collect::<Vec<_>>(),
                )
                .interact()?;
            Ok(ResolvedTask::Task(selected.to_string()))
        }
    }
}