aboutsummaryrefslogtreecommitdiff
path: root/src/bin/tosin-admin.rs
blob: 3eb57a1cdafce0f111bc2e8241a46bee216679c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::env::set_current_dir;
use std::fs;
use std::io::Write as _;
use std::process::Command;

use structopt::StructOpt;

const TOSIN_DEPENDENCY: &str = concat!(r#"tosin = { path = ""#, env!("CARGO_MANIFEST_DIR"), r#"" }"#);
const PROJECT_MAIN: &str = r#"
use tosin::Settings;
use tosin::contrib::admin;
use tosin::urls::{UrlMap, url_map};

fn urls() -> UrlMap {
    url_map! {
        "admin" / ..admin::site::urls(),
    }
}

fn settings() -> Settings {
    Settings {
        ..Settings::default()
    }
}

tosin::main!(urls(), settings());
"#;

#[derive(StructOpt, Debug)]
enum Opt {
    /// Start a new project/site (can contain multiple apps)
    StartProject {
        name: Option<String>,
    }
}

fn main() {
    let opts = Opt::from_args();
    match opts {
        Opt::StartProject { name } => {
            match name {
                Some(name) => {
                    // there's a name, so we're creating a project.
                    // TODO make this all more robust
                    let cargo_new = Command::new("cargo")
                        .args(&["new", "--bin", &name])
                        .status()
                        .unwrap();
                    if !cargo_new.success() {
                        panic!("cargo new failed");
                    }

                    set_current_dir(name).unwrap();

                    let mut cargo_toml = fs::OpenOptions::new()
                        .append(true)
                        .open("Cargo.toml")
                        .unwrap();
                    writeln!(cargo_toml, "{}", TOSIN_DEPENDENCY).unwrap();
                    drop(cargo_toml);

                    fs::write("src/main.rs", PROJECT_MAIN).unwrap();
                }
                None => {
                    todo!("absorb existing Rust project maybe??")
                }
            }
        }
    }
}