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
use std::path::Path;

use anyhow::Result;
use inquire::Confirm;
use tracing::instrument;

/// Creates a directory if it doesn't exist and the provided `--dry-run` flag is not set.
#[macro_export]
macro_rules! create_dir_gracefully {
    ($dir:expr, $dry_run:expr) => {
        if !$dry_run {
            tracing::debug!("Creating directory {:?}", $dir);
            std::fs::create_dir_all($dir)?;
        }
    };
}

pub use create_dir_gracefully;

/// Checks if rust artifacts are present in the given directory.
/// If `dry_run` is enabled, this method will not error if rust
/// artifacts are found.
#[instrument(name = "utils", skip(dir, ci, dry_run))]
pub fn check_artifacts(dir: &Path, ci: bool, dry_run: bool) -> Result<()> {
    if dry_run {
        return Ok(());
    }
    let mut prompted = false;
    if dir.join("Cargo.toml").exists() {
        tracing::warn!("Rust artifacts detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            anyhow::bail!("User aborted after detecting rust artifacts in the project directory");
        }
        prompted = true;
    }
    if !prompted && dir.join("LICENSE").exists() {
        tracing::warn!("LICENSE detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            anyhow::bail!("User aborted after detecting existing license in the project directory");
        }
        prompted = true;
    }
    if !prompted && dir.join("README.md").exists() {
        tracing::warn!("README detected in the project directory");
        if !Confirm::new("[WARNING] Found README.md in the project directory. Proceeding will overwrite this file. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            anyhow::bail!("User aborted after detecting existing readme in the project directory");
        }
        prompted = true;
    }
    if !prompted && ci && dir.join(".github").join("workflows").join("ci.yml").exists() {
        tracing::warn!("Rust artifacts detected in the project directory");
        if !Confirm::new("[WARNING] Found conflicting files. Are you sure you wish to proceed?")
            .prompt()?
        {
            println!("Phew, close call... aborting");
            anyhow::bail!("User aborted after detecting rust artifacts in the project directory");
        }
    }
    Ok(())
}