stellar_scaffold_cli/commands/
mod.rs

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