cargo_run/commands/
init.rs

1//! This module provides the functionality to initialize a `Scripts.toml` file.
2
3use std::{fs, io};
4use colored::*;
5use emoji::symbols;
6
7/// Initialize a `Scripts.toml` file in the current directory.
8///
9/// If the file already exists, it prompts the user for confirmation to replace it.
10/// The function creates a default `Scripts.toml` file if the user agrees.
11///
12/// # Arguments
13///
14/// * `file_path` - Optional path to the Scripts.toml file. Defaults to "Scripts.toml" if None.
15///
16/// # Panics
17///
18/// This function will panic if it fails to read user input or write to the `Scripts.toml` file.
19pub fn init_script_file(file_path: Option<&str>) {
20    let file_path = file_path.unwrap_or("Scripts.toml");
21    if fs::metadata(file_path).is_ok() {
22        println!("{}  [ {} ] already exists. Do you want to replace it? ({}/{})", symbols::warning::WARNING.glyph, file_path.yellow(), "y".green(), "n".red());
23        let mut input = String::new();
24        io::stdin().read_line(&mut input).expect("Failed to read input");
25        if input.trim().to_lowercase() != "y" {
26            println!("Operation cancelled.");
27            return;
28        }
29    }
30    let default_content = r#"
31[global_env]
32
33[scripts]
34dev = "cargo run"
35build = { command = "cargo build", env = { RUST_LOG = "info" } }
36release = "cargo build --release"
37test = { command = "cargo test", env = { RUST_LOG = "warn" } }
38doc = "cargo doc --no-deps --open"
39"#;
40    fs::write(file_path, default_content).expect("Failed to write Scripts.toml");
41    println!("{}  [ {} ] has been created.", symbols::other_symbol::CHECK_MARK.glyph, "Scripts.toml".green());
42}