cargo_tangle/
create.rs

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
use clap::Args;
use std::path::PathBuf;
use std::process::Command;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Failed to generate blueprint: {0}")]
    GenerationFailed(anyhow::Error),
    #[error("Failed to initialize submodules, see .gitmodules to add them manually")]
    SubmoduleInit,
    #[error("{0}")]
    Io(#[from] std::io::Error),
}

#[derive(Args, Debug, Clone, Default)]
#[group(id = "source", required = false, multiple = false)]
pub struct Source {
    #[command(flatten)]
    repo: Option<RepoArgs>,

    #[arg(short, long, group = "source")]
    path: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "repo")]
pub struct RepoArgs {
    #[arg(short, long, env, required = false, group = "source")]
    repo: String,
    #[arg(short, long, env)]
    branch: Option<String>,
    #[arg(short, long, env, conflicts_with = "branch")]
    tag: Option<String>,
}

impl From<Source> for Option<cargo_generate::TemplatePath> {
    fn from(value: Source) -> Self {
        let mut template_path = cargo_generate::TemplatePath::default();

        match value {
            Source {
                repo: Some(repo_args),
                ..
            } => {
                template_path.git = Some(repo_args.repo);
                template_path.branch = repo_args.branch;
                template_path.tag = repo_args.tag;
                Some(template_path)
            }
            Source {
                path: Some(path), ..
            } => {
                template_path.path = Some(path.to_string_lossy().into());
                Some(template_path)
            }
            Source {
                repo: None,
                path: None,
            } => None,
        }
    }
}

pub fn new_blueprint(name: String, source: Option<Source>) -> Result<(), Error> {
    println!("Generating blueprint with name: {}", name);

    let source = source.unwrap_or_default();
    let template_path_opt: Option<cargo_generate::TemplatePath> = source.into();

    let template_path = template_path_opt.unwrap_or_else(|| {
        // TODO: Interactive selection (#352)
        cargo_generate::TemplatePath {
            git: Some(String::from(
                "https://github.com/tangle-network/blueprint-template/",
            )),
            branch: Some(String::from("main")),
            ..Default::default()
        }
    });

    let path = cargo_generate::generate(cargo_generate::GenerateArgs {
        template_path,
        list_favorites: false,
        name: Some(name.to_string()),
        force: false,
        verbose: false,
        template_values_file: None,
        silent: false,
        config: None,
        vcs: Some(cargo_generate::Vcs::Git),
        lib: false,
        bin: true,
        ssh_identity: None,
        define: Default::default(),
        init: false,
        destination: None,
        force_git_init: false,
        allow_commands: false,
        overwrite: false,
        skip_submodules: false,
        other_args: Default::default(),
    })
    .map_err(Error::GenerationFailed)?;

    println!("Blueprint generated at: {}", path.display());

    // TODO: Hack, we have to initialize submodules ourselves, cargo-generate just copies
    //       them as normal directories: https://github.com/cargo-generate/cargo-generate/issues/1317
    std::env::set_current_dir(path)?;
    std::fs::remove_dir_all("./contracts/lib/tnt-core")?;

    let output = Command::new("git")
        .args([
            "submodule",
            "add",
            "https://github.com/tangle-network/tnt-core",
            "contracts/lib/tnt-core",
        ])
        .output()?;

    if !output.status.success() {
        eprintln!(
            "Failed to add tnt-core submodule: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    Ok(())
}