Skip to main content

solar_core/subcommand/
new.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6use clap::Parser;
7
8use crate::{SolarError, config::ProjConfig, solar_init};
9
10#[derive(Parser, Clone)]
11pub struct New {
12    /// The name of the new project.
13    name: String,
14
15    /// The project configuration to initialize.
16    #[arg(default_value = "cargobinbasic")]
17    project: ProjConfig,
18
19    /// The destination to create the new project.
20    #[arg(short, long, default_value = ".")]
21    destination: PathBuf,
22}
23
24impl New {
25    pub fn run(&mut self) -> Result<(), SolarError> {
26        // Ensure the destination directory exists
27        let project_dir = self.destination.join(&self.name);
28        fs::create_dir_all(&project_dir)?;
29
30        // Initialize the project
31        solar_new(&self.project, &mut self.destination, &self.name)
32    }
33}
34
35pub fn solar_new(
36    config: &ProjConfig,
37    destination: &mut Path,
38    name: &str,
39) -> Result<(), SolarError> {
40    // Ensure the destination directory exists
41    let project_dir = destination.join(name);
42    fs::create_dir_all(&project_dir)?;
43
44    // Initialize the project
45    solar_init(config, destination)
46}