stellar_scaffold_cli/commands/
mod.rs

1use std::str::FromStr;
2
3use clap::{command, CommandFactory, FromArgMatches, Parser};
4
5pub mod build;
6pub mod generate;
7pub mod init;
8pub mod update_env;
9pub mod watch;
10
11const ABOUT: &str = "Build smart contracts with frontend support";
12
13#[derive(Parser, Debug)]
14#[command(
15    name = "stellar-scaffold",
16    about = ABOUT,
17    disable_help_subcommand = true,
18)]
19pub struct Root {
20    #[command(subcommand)]
21    pub cmd: Cmd,
22}
23
24impl Root {
25    pub fn new() -> Result<Self, clap::Error> {
26        let mut matches = Self::command().get_matches();
27        Self::from_arg_matches_mut(&mut matches)
28    }
29
30    pub fn from_arg_matches<I, T>(itr: I) -> Result<Self, clap::Error>
31    where
32        I: IntoIterator<Item = T>,
33        T: Into<std::ffi::OsString> + Clone,
34    {
35        Self::from_arg_matches_mut(&mut Self::command().get_matches_from(itr))
36    }
37    pub async fn run(&mut self) -> Result<(), Error> {
38        match &mut self.cmd {
39            Cmd::Init(init_info) => init_info.run()?,
40            Cmd::Build(build_info) => build_info.run().await?,
41            Cmd::Generate(generate) => match &mut generate.cmd {
42                generate::Command::Contract(contract) => contract.run().await?,
43            },
44            Cmd::UpdateEnv(e) => e.run()?,
45            Cmd::Watch(watch_info) => watch_info.run().await?,
46        }
47        Ok(())
48    }
49}
50
51impl FromStr for Root {
52    type Err = clap::Error;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        Self::from_arg_matches(s.split_whitespace())
56    }
57}
58
59#[derive(Parser, Debug)]
60pub enum Cmd {
61    /// Initialize the project
62    Init(init::Cmd),
63
64    /// Build contracts, resolving dependencies in the correct order. If you have an `environments.toml` file, it will also follow its instructions to configure the environment set by the `STELLAR_SCAFFOLD_ENV` environment variable, turning your contracts into frontend packages (NPM dependencies).
65    Build(build::Command),
66
67    /// generate contracts
68    Generate(generate::Cmd),
69
70    /// Update an environment variable in a .env file
71    UpdateEnv(update_env::Cmd),
72
73    /// Monitor contracts and environments.toml for changes and rebuild as needed
74    Watch(watch::Cmd),
75}
76
77#[derive(thiserror::Error, Debug)]
78pub enum Error {
79    // TODO: stop using Debug for displaying errors
80    #[error(transparent)]
81    Init(#[from] init::Error),
82    #[error(transparent)]
83    BuildContracts(#[from] build::Error),
84    #[error(transparent)]
85    Contract(#[from] generate::contract::Error),
86    #[error(transparent)]
87    UpdateEnv(#[from] update_env::Error),
88    #[error(transparent)]
89    Watch(#[from] watch::Error),
90}