Skip to main content

leo_lang/cli/commands/
new.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::*;
18
19use leo_package::{Package, Workspace};
20
21/// Create new Leo project
22#[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        // Derive the location of the parent directory to the project.
46        let package_path = context.parent_dir()?;
47
48        // Change the cwd to the parent directory so subsequent commands resolve sensibly.
49        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}