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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str::FromStr;

use anyhow::{bail, format_err};
use clap::Parser;
use fs_err as fs;
use fs_err::OpenOptions;

use super::resolve_path;

static MODEL_PROJECT: &str =
    include_str!("../../assets/default-model-project/default.project.json");
static MODEL_README: &str = include_str!("../../assets/default-model-project/README.md");
static MODEL_INIT: &str = include_str!("../../assets/default-model-project/src-init.lua");
static MODEL_GIT_IGNORE: &str = include_str!("../../assets/default-model-project/gitignore.txt");

static PLACE_PROJECT: &str =
    include_str!("../../assets/default-place-project/default.project.json");
static PLACE_README: &str = include_str!("../../assets/default-place-project/README.md");
static PLACE_GIT_IGNORE: &str = include_str!("../../assets/default-place-project/gitignore.txt");

static PLUGIN_PROJECT: &str =
    include_str!("../../assets/default-plugin-project/default.project.json");
static PLUGIN_README: &str = include_str!("../../assets/default-plugin-project/README.md");
static PLUGIN_GIT_IGNORE: &str = include_str!("../../assets/default-plugin-project/gitignore.txt");

/// Initializes a new Rojo project.
#[derive(Debug, Parser)]
pub struct InitCommand {
    /// Path to the place to create the project. Defaults to the current directory.
    #[clap(default_value = "")]
    pub path: PathBuf,

    /// The kind of project to create, 'place', 'plugin', or 'model'. Defaults to place.
    #[clap(long, default_value = "place")]
    pub kind: InitKind,
}

impl InitCommand {
    pub fn run(self) -> anyhow::Result<()> {
        let base_path = resolve_path(&self.path);
        fs::create_dir_all(&base_path)?;

        let canonical = fs::canonicalize(&base_path)?;
        let project_name = canonical
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("new-project");

        let project_params = ProjectParams {
            name: project_name.to_owned(),
        };

        match self.kind {
            InitKind::Place => init_place(&base_path, project_params)?,
            InitKind::Model => init_model(&base_path, project_params)?,
            InitKind::Plugin => init_plugin(&base_path, project_params)?,
        }

        println!("Created project successfully.");

        Ok(())
    }
}

/// The templates we support for initializing a Rojo project.
#[derive(Debug, Clone, Copy)]
pub enum InitKind {
    /// A place that contains a baseplate.
    Place,

    /// An empty model, suitable for a library.
    Model,

    /// An empty plugin.
    Plugin,
}

impl FromStr for InitKind {
    type Err = anyhow::Error;

    fn from_str(source: &str) -> Result<Self, Self::Err> {
        match source {
            "place" => Ok(InitKind::Place),
            "model" => Ok(InitKind::Model),
            "plugin" => Ok(InitKind::Plugin),
            _ => Err(format_err!(
                "Invalid init kind '{}'. Valid kinds are: place, model, plugin",
                source
            )),
        }
    }
}

fn init_place(base_path: &Path, project_params: ProjectParams) -> anyhow::Result<()> {
    println!("Creating new place project '{}'", project_params.name);

    let project_file = project_params.render_template(PLACE_PROJECT);
    try_create_project(base_path, &project_file)?;

    let readme = project_params.render_template(PLACE_README);
    write_if_not_exists(&base_path.join("README.md"), &readme)?;

    let src = base_path.join("src");
    fs::create_dir_all(&src)?;

    let src_shared = src.join("shared");
    fs::create_dir_all(src.join(&src_shared))?;

    let src_server = src.join("server");
    fs::create_dir_all(src.join(&src_server))?;

    let src_client = src.join("client");
    fs::create_dir_all(src.join(&src_client))?;

    write_if_not_exists(
        &src_shared.join("Hello.lua"),
        "return function()\n\tprint(\"Hello, world!\")\nend",
    )?;

    write_if_not_exists(
        &src_server.join("init.server.lua"),
        "print(\"Hello world, from server!\")",
    )?;

    write_if_not_exists(
        &src_client.join("init.client.lua"),
        "print(\"Hello world, from client!\")",
    )?;

    let git_ignore = project_params.render_template(PLACE_GIT_IGNORE);
    try_git_init(base_path, &git_ignore)?;

    Ok(())
}

fn init_model(base_path: &Path, project_params: ProjectParams) -> anyhow::Result<()> {
    println!("Creating new model project '{}'", project_params.name);

    let project_file = project_params.render_template(MODEL_PROJECT);
    try_create_project(base_path, &project_file)?;

    let readme = project_params.render_template(MODEL_README);
    write_if_not_exists(&base_path.join("README.md"), &readme)?;

    let src = base_path.join("src");
    fs::create_dir_all(&src)?;

    let init = project_params.render_template(MODEL_INIT);
    write_if_not_exists(&src.join("init.lua"), &init)?;

    let git_ignore = project_params.render_template(MODEL_GIT_IGNORE);
    try_git_init(base_path, &git_ignore)?;

    Ok(())
}

fn init_plugin(base_path: &Path, project_params: ProjectParams) -> anyhow::Result<()> {
    println!("Creating new plugin project '{}'", project_params.name);

    let project_file = project_params.render_template(PLUGIN_PROJECT);
    try_create_project(base_path, &project_file)?;

    let readme = project_params.render_template(PLUGIN_README);
    write_if_not_exists(&base_path.join("README.md"), &readme)?;

    let src = base_path.join("src");
    fs::create_dir_all(&src)?;

    write_if_not_exists(
        &src.join("init.server.lua"),
        "print(\"Hello world, from plugin!\")\n",
    )?;

    let git_ignore = project_params.render_template(PLUGIN_GIT_IGNORE);
    try_git_init(base_path, &git_ignore)?;

    Ok(())
}

/// Contains parameters used in templates to create a project.
struct ProjectParams {
    name: String,
}

impl ProjectParams {
    /// Render a template by replacing variables with project parameters.
    fn render_template(&self, template: &str) -> String {
        template
            .replace("{project_name}", &self.name)
            .replace("{rojo_version}", env!("CARGO_PKG_VERSION"))
    }
}

/// Attempt to initialize a Git repository if necessary, and create .gitignore.
fn try_git_init(path: &Path, git_ignore: &str) -> Result<(), anyhow::Error> {
    if should_git_init(path) {
        log::debug!("Initializing Git repository...");

        let status = Command::new("git").arg("init").current_dir(path).status()?;

        if !status.success() {
            bail!("git init failed: status code {:?}", status.code());
        }
    }

    write_if_not_exists(&path.join(".gitignore"), git_ignore)?;

    Ok(())
}

/// Tells whether we should initialize a Git repository inside the given path.
///
/// Will return false if the user doesn't have Git installed or if the path is
/// already inside a Git repository.
fn should_git_init(path: &Path) -> bool {
    let result = Command::new("git")
        .args(["rev-parse", "--is-inside-work-tree"])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .current_dir(path)
        .status();

    match result {
        // If the command ran, but returned a non-zero exit code, we are not in
        // a Git repo and we should initialize one.
        Ok(status) => !status.success(),

        // If the command failed to run, we probably don't have Git installed.
        Err(_) => false,
    }
}

/// Write a file if it does not exist yet, otherwise, leave it alone.
fn write_if_not_exists(path: &Path, contents: &str) -> Result<(), anyhow::Error> {
    let file_res = OpenOptions::new().write(true).create_new(true).open(path);

    let mut file = match file_res {
        Ok(file) => file,
        Err(err) => {
            return match err.kind() {
                io::ErrorKind::AlreadyExists => return Ok(()),
                _ => Err(err.into()),
            }
        }
    };

    file.write_all(contents.as_bytes())?;

    Ok(())
}

/// Try to create a project file and fail if it already exists.
fn try_create_project(base_path: &Path, contents: &str) -> Result<(), anyhow::Error> {
    let project_path = base_path.join("default.project.json");

    let file_res = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&project_path);

    let mut file = match file_res {
        Ok(file) => file,
        Err(err) => {
            return match err.kind() {
                io::ErrorKind::AlreadyExists => {
                    bail!("Project file already exists: {}", project_path.display())
                }
                _ => Err(err.into()),
            }
        }
    };

    file.write_all(contents.as_bytes())?;

    Ok(())
}