use std::str::FromStr;
use clap::{CommandFactory, FromArgMatches, Parser, command};
use stellar_cli;
pub mod build;
pub mod clean;
pub mod ext;
pub mod generate;
pub mod init;
pub mod update_env;
pub mod upgrade;
pub mod version;
pub mod watch;
const ABOUT: &str = "Build smart contracts with frontend support";
#[derive(Parser, Debug)]
#[command(
name = "stellar-scaffold",
about = ABOUT,
disable_help_subcommand = true,
)]
pub struct Root {
#[clap(flatten)]
pub global_args: stellar_cli::commands::global::Args,
#[command(subcommand)]
pub cmd: Cmd,
}
impl Root {
pub fn new() -> Result<Self, clap::Error> {
let mut matches = Self::command().get_matches();
Self::from_arg_matches_mut(&mut matches)
}
pub fn from_arg_matches<I, T>(itr: I) -> Result<Self, clap::Error>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
Self::from_arg_matches_mut(&mut Self::command().get_matches_from(itr))
}
pub async fn run(&mut self) -> Result<(), Error> {
match &mut self.cmd {
Cmd::Init(init_info) => init_info.run(&self.global_args).await?,
Cmd::Version(version_info) => version_info.run(),
Cmd::Build(build_info) => build_info.run(&self.global_args).await?,
Cmd::Generate(generate) => match &mut generate.cmd {
generate::Command::Contract(contract) => contract.run(&self.global_args).await?,
},
Cmd::Ext(ext_cmd) => match &ext_cmd.cmd {
ext::Command::Ls(ls) => ls.run(&self.global_args).map_err(ext::Error::from)?,
},
Cmd::Upgrade(upgrade_info) => upgrade_info.run(&self.global_args).await?,
Cmd::UpdateEnv(e) => e.run()?,
Cmd::Watch(watch_info) => watch_info.run(&self.global_args).await?,
Cmd::Clean(clean) => clean.run(&self.global_args)?,
}
Ok(())
}
}
impl FromStr for Root {
type Err = clap::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_arg_matches(s.split_whitespace())
}
}
#[derive(Parser, Debug)]
pub enum Cmd {
Init(init::Cmd),
Version(version::Cmd),
Build(build::Command),
Generate(generate::Cmd),
Ext(ext::Cmd),
Upgrade(upgrade::Cmd),
UpdateEnv(update_env::Cmd),
Watch(watch::Cmd),
Clean(clean::Cmd),
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Init(#[from] init::Error),
#[error(transparent)]
BuildContracts(#[from] build::Error),
#[error(transparent)]
Contract(#[from] generate::contract::Error),
#[error(transparent)]
Ext(#[from] ext::Error),
#[error(transparent)]
Upgrade(#[from] upgrade::Error),
#[error(transparent)]
UpdateEnv(#[from] update_env::Error),
#[error(transparent)]
Watch(#[from] watch::Error),
#[error(transparent)]
Clean(#[from] clean::Error),
}
pub fn npm_cmd() -> &'static str {
if cfg!(target_os = "windows") {
"npm.cmd"
} else {
"npm"
}
}