1pub mod make_config;
2pub mod templates;
3
4use std::{fs, path::Path};
5
6use anyhow::{bail, Result};
7use make_config::make_config;
8
9use super::{args::InitCommand, pretty};
10
11pub fn init(cmd: InitCommand) -> Result<()> {
12 let config = make_config(&cmd)?;
13
14 if fs::exists(&cmd.name)? {
15 bail!("directory `{}` is not empty", &cmd.name)
16 }
17
18 let project_path = Path::new(&cmd.name);
19
20 fs::create_dir(project_path)?;
21 fs::write(project_path.join("brick.toml"), config)?;
22
23 fs::write(
24 project_path.join(".gitignore"),
25 templates::gitignore(&cmd.name),
26 )?;
27
28 let src_path = project_path.join("src");
29 fs::create_dir(&src_path)?;
30
31 match cmd.cpp {
32 true => match cmd.lib {
33 true => fs::write(src_path.join("lib.cpp"), templates::lib_cpp(&cmd.name)),
34 false => fs::write(src_path.join("main.cpp"), templates::main_cpp(&cmd.name)),
35 },
36 false => match cmd.lib {
37 true => fs::write(src_path.join("lib.c"), templates::lib_c(&cmd.name)),
38 false => fs::write(src_path.join("main.c"), templates::main_c(&cmd.name)),
39 },
40 }?;
41
42 pretty::msg("init", &cmd.name);
43 pretty::info(format!("run `cd {}` to start developing", &cmd.name));
44 if !cmd.lib {
45 pretty::info("run `bricks run` to run your project");
46 }
47 pretty::info("run `bricks build` to build your project");
48
49 Ok(())
50}