Skip to main content

rl_lang/tooling/
new.rs

1use std::io;
2
3/// Creates a new rl project directory at `name`.
4///
5/// Scaffolds the following structure:
6/// ```text
7/// <name>/
8/// |-- .gitignore
9/// |-- rl.toml
10/// |-- src/
11///     |-- main.rl
12/// ```
13///
14/// `rl.toml` is pre-filled with the project name and current rl version.
15/// `src/main.rl` contains a hello world entry point.
16/// A git repository is initialized automatically.
17///
18/// Prints an error and exits with code `1` on any IO failure.
19pub fn create_project(name: &str, no_git: bool) {
20    if let Err(e) = try_create_project(name, no_git) {
21        eprintln!("error: failed to create project '{}': {}", name, e);
22        std::process::exit(1);
23    }
24    println!("created project '{}'", name);
25}
26
27/// Inner fallible implementation of [`create_project`].
28///
29/// Returns [`io::Error`] if any filesystem operation or `git init` fails.
30fn try_create_project(name: &str, no_git: bool) -> io::Result<()> {
31    let toml = format!(
32        r#"[project]
33name = "{}"
34rl-version = "{}"
35version = "0.0.1"
36entry = "src/main.rl"
37"#,
38        name,
39        env!("CARGO_PKG_VERSION"),
40    );
41    let main = r#"get println from std::io
42fn main() {
43    println("hello world")
44}
45main()
46"#;
47    std::fs::create_dir(name)?;
48    std::fs::create_dir(format!("{}/src", name))?;
49    std::fs::write(format!("{}/rl.toml", name), toml)?;
50    std::fs::write(format!("{}/src/main.rl", name), main)?;
51    std::fs::write(format!("{}/.gitignore", name), "")?;
52    if !no_git {
53        std::process::Command::new("git")
54            .args(["init", name])
55            .output()?;
56    }
57    Ok(())
58}