create-bevy-starter 0.4.0

Builds a new Bevy game from the bevy-starter template, from a few questions
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fmt, fs};

use crate::choices::{
    Choices, Removal, branch_for, project_path, removals_for, remove_marked_lines, remove_section,
    replace_line,
};

// ==== Config =================================================================

/// Where every game is cloned from.
const TEMPLATE_URL: &str = "https://github.com/Heryk13/bevy-starter.git";

/// The message of the game's first commit, in the template's Conventional Commits style.
const FIRST_COMMIT_MESSAGE: &str = "build: start from bevy-starter";

/// The heading of the section about the template's branch tree, the same in `README.md` and in
/// `CONTRIBUTING.md`.
const BRANCHES_HEADING: &str = "## Branches";

// ==== Types ==================================================================

/// Why a game could not be created. Each variant is a failure the CLI can meet and tell apart.
pub(crate) enum ScaffoldError {
    FolderNotEmpty(PathBuf),
    CloneFailed(String),
    RemoveFailed(String),
    TemplateChanged(PathBuf),
    EditFailed(String),
    GitFailed(String),
}

/// Gives the message the user reads, which says what went wrong and what to do next.
impl fmt::Display for ScaffoldError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScaffoldError::FolderNotEmpty(path) => {
                write!(
                    f,
                    "{} already exists and is not empty. Choose another name or an empty folder.",
                    path.display()
                )
            }
            ScaffoldError::CloneFailed(message) => {
                write!(
                    f,
                    "Failed to clone the template: {}. Check your internet connection, or wait a \
                     moment, and try again.",
                    message
                )
            }
            ScaffoldError::RemoveFailed(message) => {
                write!(
                    f,
                    "Could not remove a file the template does not need: {}. Close any program \
                     using the folder, such as an editor or the file explorer, and try again.",
                    message
                )
            }
            ScaffoldError::TemplateChanged(path) => {
                write!(
                    f,
                    "{} is not what this create-bevy-starter expects: the template changed and the \
                     CLI did not follow. Update create-bevy-starter, or report it to its author.",
                    path.display()
                )
            }
            ScaffoldError::EditFailed(message) => {
                write!(
                    f,
                    "Could not edit a file of the new game: {}. Close any program using the \
                     folder, such as an editor or the file explorer, and try again.",
                    message
                )
            }
            ScaffoldError::GitFailed(message) => {
                // a first commit most often fails on a git that was never told who commits
                write!(
                    f,
                    "Could not start the game's git history: {}. If git asks who you are, run \
                     `git config --global user.name \"Your Name\"` and `git config --global \
                     user.email you@example.com`, then try again.",
                    message
                )
            }
        }
    }
}

// ==== Checks =================================================================

/// Tells whether `git` runs on this machine, since the template is cloned with it.
pub(crate) fn is_git_installed() -> bool {
    let output = Command::new("git").arg("--version").output();
    if let Ok(output) = output
        && output.status.success()
    {
        return true;
    }
    false
}

/// Tells whether the game can be created at `path`: nothing there yet, or an empty folder.
/// Anything else, a file or a folder that cannot be read included, is refused, so nothing is
/// ever overwritten.
fn is_folder_free(path: &Path) -> bool {
    !path.exists() || fs::read_dir(path).is_ok_and(|mut entries| entries.next().is_none())
}

// ==== Creating ===============================================================

/// Builds the game the user chose, in `<folder>/<name>`. Stops before writing anything when the
/// folder is taken.
pub(crate) fn create_game(choices: &Choices) -> Result<(), ScaffoldError> {
    let path = project_path(&choices.folder, &choices.name);
    if !is_folder_free(&path) {
        return Err(ScaffoldError::FolderNotEmpty(path));
    }
    if let Err(error) = build_game(&path, choices) {
        // a half-built game is worse than none; a failed cleanup is dropped, since the error
        // the user needs is the one that stopped the build
        let _ = fs::remove_dir_all(&path);
        return Err(error);
    }
    Ok(())
}

/// Runs every step that writes the game to disk. Any failure here leaves a half-built folder,
/// which `create_game` deletes.
fn build_game(path: &Path, choices: &Choices) -> Result<(), ScaffoldError> {
    // ---- the clone ----------------------------------------------------------
    let branch = branch_for(&choices.kind);
    let output = Command::new("git")
        .args(["clone", "--depth", "1", "--branch", branch, TEMPLATE_URL])
        .arg(path)
        .output()
        .map_err(|error| ScaffoldError::CloneFailed(error.to_string()))?;
    if !output.status.success() {
        return Err(ScaffoldError::CloneFailed(
            String::from_utf8_lossy(&output.stderr).trim().to_string(),
        ));
    }
    // the game starts its own history: the template's would show in its log, and its push would
    // reach the template's repository
    let git_dir = path.join(".git");
    fs::remove_dir_all(git_dir).map_err(|error| ScaffoldError::RemoveFailed(error.to_string()))?;

    // ---- the three names ----------------------------------------------------
    edit_file(&path.join("Cargo.toml"), |text| {
        replace_line(text, "name = ", &format!("name = \"{}\"", choices.name))
    })?;
    edit_file(&path.join("justfile"), |text| {
        replace_line(text, "game := ", &format!("game := \"{}\"", choices.name))
    })?;
    edit_file(&path.join("src/main.rs"), |text| {
        replace_line(
            text,
            "pub(crate) const GAME_TITLE",
            &format!("pub(crate) const GAME_TITLE: &str = \"{}\";", choices.title),
        )
    })?;

    // ---- what only the template needs ---------------------------------------
    // the specs are the template's history; the folder stays for the game's own, kept by a
    // `.gitkeep` since git drops empty folders
    let specs_dir = path.join("docs/specs");
    fs::remove_dir_all(&specs_dir)
        .map_err(|error| ScaffoldError::RemoveFailed(error.to_string()))?;
    fs::create_dir(&specs_dir).map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
    fs::write(specs_dir.join(".gitkeep"), "")
        .map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
    // a game has no branch tree: the README's section lists the template's branches, and
    // CONTRIBUTING's holds their rules and the tools that keep them in step
    for file in ["README.md", "CONTRIBUTING.md"] {
        edit_file(&path.join(file), |text| {
            remove_section(text, BRANCHES_HEADING)
        })?;
    }
    remove_tools(path)?;

    // ---- what the game was not given ----------------------------------------
    let removals = removals_for(choices);
    for removal in &removals {
        remove_left_out(path, removal)?;
    }
    if !removals.is_empty() {
        tidy_after_removals(path);
    }

    // ---- a history of its own -----------------------------------------------
    run_git(path, &["init"])?;
    run_git(path, &["add", "--all"])?;
    run_git(path, &["commit", "--message", FIRST_COMMIT_MESSAGE])?;
    Ok(())
}

/// Rewrites `file` on disk with `edit`, one of the pure edits in `choices.rs`. When `edit` finds
/// nothing to change, the template moved what the CLI edits, and that is reported instead of
/// leaving the game half made.
fn edit_file(file: &Path, edit: impl FnOnce(&str) -> Option<String>) -> Result<(), ScaffoldError> {
    let text =
        fs::read_to_string(file).map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
    let new_text = edit(&text).ok_or_else(|| ScaffoldError::TemplateChanged(file.to_path_buf()))?;
    fs::write(file, new_text).map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
    Ok(())
}

/// Removes the template's `tools/` from the game at `path`: they commit and merge down the
/// template's branch tree, which a game does not have. A template without the folder is left
/// as it is, since none of its branches had one before the tools were written.
fn remove_tools(path: &Path) -> Result<(), ScaffoldError> {
    let tools_dir = path.join("tools");
    if !tools_dir.exists() {
        return Ok(());
    }
    fs::remove_dir_all(tools_dir).map_err(|error| ScaffoldError::RemoveFailed(error.to_string()))
}

/// Removes from the game at `path` what it was not given: `removal`'s files, and every line
/// carrying its marker in `Cargo.toml` and the Rust sources. A removal none of whose files is
/// there means the template moved them, and the game would keep what was left out.
fn remove_left_out(path: &Path, removal: &Removal) -> Result<(), ScaffoldError> {
    let mut found = false;
    for file in removal.files {
        let file = path.join(file);
        let removed = if file.is_dir() {
            fs::remove_dir_all(&file)
        } else if file.is_file() {
            fs::remove_file(&file)
        } else {
            continue;
        };
        removed.map_err(|error| ScaffoldError::RemoveFailed(error.to_string()))?;
        found = true;
    }
    if !found {
        let first = removal.files.first().copied().unwrap_or_default();
        return Err(ScaffoldError::TemplateChanged(path.join(first)));
    }
    let mut sources = vec![path.join("Cargo.toml")];
    collect_rust_files(&path.join("src"), &mut sources)?;
    for source in sources {
        let text = fs::read_to_string(&source)
            .map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
        let (new_text, dropped) = remove_marked_lines(&text, removal.marker);
        if dropped {
            fs::write(&source, new_text)
                .map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
        }
    }
    Ok(())
}

/// Adds every `.rs` file under `dir`, at any depth, to `files`.
fn collect_rust_files(dir: &Path, files: &mut Vec<PathBuf>) -> Result<(), ScaffoldError> {
    let entries =
        fs::read_dir(dir).map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
    for entry in entries {
        let entry = entry.map_err(|error| ScaffoldError::EditFailed(error.to_string()))?;
        let entry_path = entry.path();
        if entry_path.is_dir() {
            collect_rust_files(&entry_path, files)?;
        } else if entry_path
            .extension()
            .is_some_and(|extension| extension == "rs")
        {
            files.push(entry_path);
        }
    }
    Ok(())
}

/// Leaves the game as its own `just check` wants it once lines are gone: formatted again, since
/// a call whose marked argument went can now fit one line, or end without its comma; and its
/// `Cargo.lock` without the crates only what was left out needed, which asking cargo for the
/// game's metadata drops, updating nothing else. Best effort: the game builds either way, and a
/// cargo that cannot run here is no reason to fail.
fn tidy_after_removals(path: &Path) {
    for args in [&["fmt"][..], &["metadata", "--format-version", "1"]] {
        let _ = Command::new("cargo").args(args).current_dir(path).output();
    }
}

/// Runs `git` with `args` inside the game's folder, and gives back what git said when it fails.
fn run_git(path: &Path, args: &[&str]) -> Result<(), ScaffoldError> {
    let output = Command::new("git")
        .args(args)
        .current_dir(path)
        .output()
        .map_err(|error| ScaffoldError::GitFailed(error.to_string()))?;
    if !output.status.success() {
        return Err(ScaffoldError::GitFailed(
            String::from_utf8_lossy(&output.stderr).trim().to_string(),
        ));
    }
    Ok(())
}

// ==== Tests ==================================================================

#[cfg(test)]
mod tests {
    use super::*;

    /// Returns an empty folder of the test's own under the system's temporary one, so tests
    /// running in parallel share no files and an earlier run leaves none behind.
    fn fresh_test_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(name);
        // a folder that is not there yet is the usual case, and nothing to report
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).expect("the test folder can be created");
        dir
    }

    #[test]
    fn a_game_keeps_no_tools_folder() {
        let game = fresh_test_dir("create-bevy-starter-tools-removed");
        fs::create_dir(game.join("tools")).expect("the tools folder can be created");
        fs::write(game.join("tools/sync.sh"), "").expect("a tool can be written");
        assert!(remove_tools(&game).is_ok());
        assert!(!game.join("tools").exists());
    }

    #[test]
    fn a_template_without_tools_still_makes_a_game() {
        let game = fresh_test_dir("create-bevy-starter-no-tools");
        assert!(remove_tools(&game).is_ok());
        assert!(game.exists());
    }

    #[test]
    fn what_is_left_out_goes_with_its_lines() {
        let game = fresh_test_dir("create-bevy-starter-left-out");
        fs::create_dir_all(game.join("src/multiplayer")).expect("a folder can be created");
        fs::write(game.join("src/multiplayer.rs"), "").expect("a file can be written");
        fs::write(
            game.join("src/main.rs"),
            "mod multiplayer; // multiplayer\nmod ui;\n",
        )
        .expect("a file can be written");
        fs::write(
            game.join("Cargo.toml"),
            "[dependencies]\nlightyear = \"0.31\" # multiplayer\n",
        )
        .expect("a file can be written");
        assert!(remove_left_out(&game, &crate::choices::MULTIPLAYER).is_ok());
        assert!(!game.join("src/multiplayer.rs").exists());
        assert!(!game.join("src/multiplayer").exists());
        let main = fs::read_to_string(game.join("src/main.rs")).expect("main.rs is there");
        assert_eq!(main, "mod ui;\n");
        let manifest = fs::read_to_string(game.join("Cargo.toml")).expect("Cargo.toml is there");
        assert_eq!(manifest, "[dependencies]\n");
    }

    #[test]
    fn a_removal_whose_files_moved_is_refused() {
        let game = fresh_test_dir("create-bevy-starter-moved");
        fs::create_dir(game.join("src")).expect("a folder can be created");
        assert!(matches!(
            remove_left_out(&game, &crate::choices::MULTIPLAYER),
            Err(ScaffoldError::TemplateChanged(_))
        ));
    }

    // ---- every combination: `just check-combos` -----------------------------

    /// Returns the choices of every game the CLI can make, each in a folder of its own under
    /// `folder`.
    fn every_combination(folder: &Path) -> Vec<Choices> {
        use crate::choices::{Camera, Kind, Player3d};
        let mut kinds = vec![
            Kind::Empty,
            Kind::ThreeD(None),
            Kind::TwoD(false),
            Kind::TwoD(true),
        ];
        let camera_sets = [
            vec![Camera::FirstPerson],
            vec![Camera::ThirdPerson],
            vec![Camera::FirstPerson, Camera::ThirdPerson],
        ];
        for cameras in camera_sets {
            for split_screen in [false, true] {
                kinds.push(Kind::ThreeD(Some(Player3d {
                    cameras: cameras.clone(),
                    split_screen,
                })));
            }
        }
        let mut every = Vec::new();
        for kind in kinds {
            for multiplayer in [false, true] {
                let name = format!("combo-{}", every.len() + 1);
                every.push(Choices {
                    title: name.clone(),
                    name,
                    folder: folder.to_path_buf(),
                    kind: kind.clone(),
                    multiplayer,
                });
            }
        }
        every
    }

    #[test]
    #[ignore = "clones the template and runs each game's `just check`, for an hour or so: \
                `just check-combos`"]
    fn every_combination_passes_the_games_own_check() {
        let folder = fresh_test_dir("create-bevy-starter-combos");
        // one for every game, so Bevy is built once, not once a game
        let target = std::env::temp_dir().join("create-bevy-starter-combos-target");
        let mut failed = Vec::new();
        for choices in every_combination(&folder) {
            let label = format!(
                "{}: {:?}, multiplayer {}",
                choices.name, choices.kind, choices.multiplayer
            );
            println!("==== {label}");
            if let Err(error) = create_game(&choices) {
                failed.push(format!("{label}: {error}"));
                continue;
            }
            let checked = Command::new("just")
                .arg("check")
                .current_dir(project_path(&choices.folder, &choices.name))
                .env("CARGO_TARGET_DIR", &target)
                .status();
            if !checked.is_ok_and(|status| status.success()) {
                failed.push(label);
            }
        }
        assert!(
            failed.is_empty(),
            "these games fail their check: {failed:#?}"
        );
    }
}