leo_lang/cli/commands/
new.rs1use super::*;
18
19use leo_package::{Package, Workspace};
20
21#[derive(Parser, Debug)]
23pub struct LeoNew {
24 #[clap(name = "NAME", help = "Set package name")]
25 pub(crate) name: String,
26 #[clap(long, help = "Create the package as a library instead of a program", conflicts_with = "workspace")]
27 pub(crate) library: bool,
28 #[clap(long, help = "Create a workspace skeleton instead of a package", conflicts_with = "library")]
29 pub(crate) workspace: bool,
30}
31
32impl Command for LeoNew {
33 type Input = ();
34 type Output = ();
35
36 fn log_span(&self) -> Span {
37 tracing::span!(tracing::Level::INFO, "Leo")
38 }
39
40 fn prelude(&self, _: Context) -> Result<Self::Input> {
41 Ok(())
42 }
43
44 fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
45 let package_path = context.parent_dir()?;
47
48 std::env::set_current_dir(&package_path)
50 .map_err(|err| crate::errors::failed_to_set_cwd(package_path.display(), err))?;
51
52 if self.workspace {
53 let full_path = Workspace::initialize_skeleton(&self.name, &package_path)?;
54 println!("Created workspace {} at `{}`.", self.name.bold(), full_path.display());
55 } else {
56 let full_path = Package::initialize(&self.name, &package_path, self.library)?;
57 println!("Created program {} at `{}`.", self.name.bold(), full_path.display());
58
59 if Workspace::auto_register_member(&full_path)? {
60 println!("Added {} to the enclosing workspace.", self.name.bold());
61 }
62 }
63
64 Ok(())
65 }
66}