use super::*;
use leo_package::{Package, Workspace};
#[derive(Parser, Debug)]
pub struct LeoNew {
#[clap(name = "NAME", help = "Set package name")]
pub(crate) name: String,
#[clap(long, help = "Create the package as a library instead of a program", conflicts_with = "workspace")]
pub(crate) library: bool,
#[clap(long, help = "Create a workspace skeleton instead of a package", conflicts_with = "library")]
pub(crate) workspace: bool,
}
impl Command for LeoNew {
type Input = ();
type Output = ();
fn log_span(&self) -> Span {
tracing::span!(tracing::Level::INFO, "Leo")
}
fn prelude(&self, _: Context) -> Result<Self::Input> {
Ok(())
}
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
let package_path = context.parent_dir()?;
std::env::set_current_dir(&package_path)
.map_err(|err| crate::errors::failed_to_set_cwd(package_path.display(), err))?;
if self.workspace {
let full_path = Workspace::initialize_skeleton(&self.name, &package_path)?;
println!("Created workspace {} at `{}`.", self.name.bold(), full_path.display());
} else {
let full_path = Package::initialize(&self.name, &package_path, self.library)?;
println!("Created program {} at `{}`.", self.name.bold(), full_path.display());
if Workspace::auto_register_member(&full_path)? {
println!("Added {} to the enclosing workspace.", self.name.bold());
}
}
Ok(())
}
}