rproj 0.15.0

Guided bootstrap-to-game-dev CLI for Roblox: takes a fresh Windows PC to a working Roblox/Luau setup, then scaffolds projects on it
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Everything provisioning decided, machine-wide. `rproj new` reads this
/// and re-asks only on a machine with nothing recorded, or with
/// `--reconfigure`.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct GlobalConfig {
    #[serde(default)]
    pub roblox_projects_root: Option<PathBuf>,
    #[serde(default)]
    pub selected_system_apps: Vec<String>,
    #[serde(default)]
    pub selected_rokit_tools: Vec<String>,
    #[serde(default)]
    pub selected_studio_plugins: Vec<String>,
    #[serde(default)]
    pub selected_vscode_extensions: Vec<String>,
    #[serde(default)]
    pub last_checked: Option<String>,
}

impl GlobalConfig {
    pub(crate) fn dirs() -> Result<ProjectDirs> {
        ProjectDirs::from("", "", "rproj")
            .context("could not determine a config directory for this platform")
    }

    fn path() -> Result<PathBuf> {
        Ok(Self::dirs()?.config_dir().join("config.toml"))
    }

    pub fn load() -> Result<Self> {
        let path = Self::path()?;
        if !path.exists() {
            return Ok(Self::default());
        }
        let text = fs::read_to_string(&path)
            .with_context(|| format!("failed to read {}", path.display()))?;
        toml::from_str(&text).with_context(|| format!("failed to parse {}", path.display()))
    }

    pub fn save(&self) -> Result<()> {
        let path = Self::path()?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        let text = toml::to_string_pretty(self)?;
        fs::write(&path, text).with_context(|| format!("failed to write {}", path.display()))
    }

    pub fn projects_root(&self) -> Result<PathBuf> {
        if let Some(root) = &self.roblox_projects_root {
            return Ok(root.clone());
        }
        let documents = dirs_documents()?;
        Ok(documents.join("RobloxProjects"))
    }

    /// Whether this machine has been through provisioning at least once.
    ///
    /// Keyed on having recorded *any* selection rather than on a flag, so
    /// a config written by an older version still counts and someone who
    /// deliberately selected nothing isn't asked again every time.
    pub fn machine_configured(&self) -> bool {
        self.last_checked.is_some()
    }

    /// One-line description of what provisioning last set up, for the
    /// summary `rproj new` prints when it skips the questions.
    pub fn machine_summary(&self) -> String {
        let parts = [
            (self.selected_system_apps.len(), "apps"),
            (self.selected_rokit_tools.len(), "tools"),
            (self.selected_studio_plugins.len(), "plugins"),
            (self.selected_vscode_extensions.len(), "extensions"),
        ];
        let listed: Vec<String> = parts
            .iter()
            .filter(|(n, _)| *n > 0)
            .map(|(n, label)| format!("{n} {label}"))
            .collect();
        if listed.is_empty() {
            "nothing selected".to_string()
        } else {
            listed.join(", ")
        }
    }
}

fn dirs_documents() -> Result<PathBuf> {
    let home = std::env::var_os("USERPROFILE").context("USERPROFILE is not set")?;
    Ok(PathBuf::from(home).join("Documents"))
}

/// How a project's packages get pulled in. Wally is the default; git
/// submodules clone each selected package's repo into `modules/submodules/`
/// instead of writing a `wally.toml`. See `steps::modules`.
///
/// `None` is a real answer, not the absence of one. Before it existed, a
/// project with no packages silently became a Wally project - and was then
/// offered a `wally.toml` for dependencies it did not have. A tutorial
/// project, or one that vendors by hand, genuinely has no dependency
/// manager, and saying so is what stops the scaffold pretending otherwise.
///
/// Adding the variant is deliberately *not* modelled as `Option<..>`: it is
/// a third case for the code that mounts package folders, and an `Option`
/// would only move the exhaustiveness check somewhere the compiler cannot
/// help.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PackageWorkflow {
    /// The default in the `Default` sense only - which strategy a *project*
    /// gets is always an explicit answer, never this.
    #[default]
    Wally,
    GitSubmodules,
    None,
}

impl PackageWorkflow {
    pub const ALL: &'static [Self] = &[Self::Wally, Self::GitSubmodules, Self::None];
}

/// Reading and writing `rproj.toml`, the per-project record.
///
/// **The file *is* the project graph** - the decisions the project was built
/// from, rather than the files that came out. That is what lets `rproj
/// upgrade` re-derive from intent, so a changed default reaches a project
/// made months ago instead of stranding it.
///
/// Free functions rather than an `impl`, because the graph type lives in
/// `graph` and owning its persistence here keeps `graph` free of filesystem
/// concerns.
pub mod project_file {
    use super::*;
    use crate::graph::ProjectGraph;

    pub fn path_in(project_dir: &Path) -> PathBuf {
        project_dir.join("rproj.toml")
    }

    pub fn load_from(project_dir: &Path) -> Result<Option<ProjectGraph>> {
        let path = path_in(project_dir);
        if !path.exists() {
            return Ok(None);
        }
        let text = fs::read_to_string(&path)
            .with_context(|| format!("failed to read {}", path.display()))?;
        let graph: ProjectGraph =
            toml::from_str(&text).with_context(|| format!("failed to parse {}", path.display()))?;
        Ok(Some(graph))
    }

    pub fn save_to(graph: &ProjectGraph, project_dir: &Path) -> Result<()> {
        let path = path_in(project_dir);
        let text = toml::to_string_pretty(graph)?;
        fs::write(&path, text).with_context(|| format!("failed to write {}", path.display()))
    }
}

/// A saved project composition, reusable for the next project.
///
/// Deliberately *not* the old hardcoded presets: this is whatever a real
/// project ended up with, saved under a name you chose, so the set of
/// available setups is yours rather than a curated list that goes stale.
/// A saved setup **is a project graph**. It used to be packages plus a
/// workflow, so `--like` replayed half a composition and then asked three
/// more questions - "reuse my setup" that reuses some of it is a promise
/// half kept. Storing the graph means a saved setup replays straight to the
/// summary.
pub type SavedSetup = crate::graph::ProjectGraph;

fn save_new_setup_at(setup: &SavedSetup, path: &Path) -> Result<()> {
    use std::io::Write;
    let parent = path.parent().context("setup has no parent directory")?;
    fs::create_dir_all(parent)?;
    let text = toml::to_string_pretty(setup)?;
    let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
    temporary.write_all(text.as_bytes())?;
    temporary.as_file().sync_all()?;
    // The destination may have been created while scaffolding was running.
    temporary.persist_noclobber(path).with_context(|| {
        format!(
            "could not save new setup {}; existing setups are never replaced",
            path.display()
        )
    })?;
    Ok(())
}

/// Reading and writing `<config>/setups/<name>.toml`.
pub struct Setups;

impl Setups {
    fn dir() -> Result<PathBuf> {
        Ok(GlobalConfig::dirs()?.config_dir().join("setups"))
    }

    fn path_for(name: &str) -> Result<PathBuf> {
        Ok(Self::dir()?.join(format!("{name}.toml")))
    }

    pub fn save(setup: &SavedSetup, name: &str) -> Result<PathBuf> {
        let path = Self::path_for(name)?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        fs::write(&path, toml::to_string_pretty(setup)?)
            .with_context(|| format!("failed to write {}", path.display()))?;
        Ok(path)
    }

    pub fn load(name: &str) -> Result<Option<SavedSetup>> {
        let path = Self::path_for(name)?;
        if !path.exists() {
            return Ok(None);
        }
        let text = fs::read_to_string(&path)
            .with_context(|| format!("failed to read {}", path.display()))?;
        Ok(Some(toml::from_str(&text).with_context(|| {
            format!("failed to parse {}", path.display())
        })?))
    }

    pub fn save_new(setup: &SavedSetup, name: &str) -> Result<PathBuf> {
        let path = Self::path_for(name)?;
        save_new_setup_at(setup, &path)?;
        Ok(path)
    }

    /// Names of every saved setup, sorted. Missing directory is not an
    /// error - it just means none have been saved yet.
    ///
    /// The `is_file` filter is load-bearing: `read_dir` yields directories
    /// too, and without it a directory named `something.toml` is listed as
    /// a setup that then fails to load, because the path exists and so the
    /// `None` branch in `load` never fires.
    pub fn list() -> Vec<String> {
        let Ok(dir) = Self::dir() else {
            return Vec::new();
        };
        let Ok(entries) = fs::read_dir(dir) else {
            return Vec::new();
        };
        let mut names: Vec<String> = entries
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
            .filter_map(|e| {
                let path = e.path();
                (path.extension()? == "toml")
                    .then(|| path.file_stem()?.to_str().map(str::to_owned))?
            })
            .collect();
        names.sort();
        names
    }
}

/// Persistence for the machine-wide Rojo project template.
pub mod project_template {
    use super::*;

    pub fn path() -> Result<PathBuf> {
        Ok(GlobalConfig::dirs()?
            .config_dir()
            .join("templates")
            .join("default.project.json"))
    }

    pub fn load() -> Result<Option<Value>> {
        load_from(&path()?)
    }

    pub fn read_text() -> Result<Option<String>> {
        read_text_from(&path()?)
    }

    pub fn save(template: &Value) -> Result<PathBuf> {
        save_to(template, &path()?)
    }

    pub fn reset() -> Result<bool> {
        reset_at(&path()?)
    }

    pub(crate) fn reset_at(path: &Path) -> Result<bool> {
        if !path.exists() {
            return Ok(false);
        }
        fs::remove_file(path).with_context(|| format!("failed to remove {}", path.display()))?;
        Ok(true)
    }

    pub(crate) fn load_from(path: &Path) -> Result<Option<Value>> {
        let Some(text) = read_text_from(path)? else {
            return Ok(None);
        };
        let value = serde_json::from_str(&text).with_context(|| {
            format!(
                "failed to parse {} - run `rproj configure project` to repair or reset it",
                path.display()
            )
        })?;
        Ok(Some(value))
    }

    fn read_text_from(path: &Path) -> Result<Option<String>> {
        if !path.exists() {
            return Ok(None);
        }
        fs::read_to_string(path)
            .map(Some)
            .with_context(|| format!("failed to read {}", path.display()))
    }

    pub(crate) fn save_to(template: &Value, path: &Path) -> Result<PathBuf> {
        let parent = path
            .parent()
            .context("project template path has no parent")?;
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
        let text = format!("{}\n", serde_json::to_string_pretty(template)?);
        let mut staged = tempfile::NamedTempFile::new_in(parent)
            .with_context(|| format!("failed to stage {}", path.display()))?;
        staged
            .write_all(text.as_bytes())
            .with_context(|| format!("failed to stage {}", path.display()))?;
        staged
            .as_file()
            .sync_all()
            .with_context(|| format!("failed to sync {}", path.display()))?;
        staged
            .persist(path)
            .with_context(|| format!("failed to replace {}", path.display()))?;
        Ok(path.to_path_buf())
    }
}

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

    #[test]
    fn new_setup_save_is_atomic_and_never_replaces_an_existing_file() {
        let root = tempfile::tempdir().unwrap();
        let path = root.path().join("setups/example.toml");
        let graph = SavedSetup::default();
        save_new_setup_at(&graph, &path).unwrap();
        let before = fs::read(&path).unwrap();
        assert!(save_new_setup_at(&graph, &path).is_err());
        assert_eq!(fs::read(&path).unwrap(), before);
        assert_eq!(fs::read_dir(path.parent().unwrap()).unwrap().count(), 1);
    }

    /// `read_dir` yields directories as well as files, and the extension
    /// filter alone does not tell them apart. Without the `is_file` check
    /// a directory named `something.toml` was listed as a saved setup -
    /// and then failed to load rather than returning `None`, because the
    /// path does exist.
    #[test]
    fn listing_ignores_a_directory_named_like_a_setup() {
        let dir = std::env::temp_dir().join(format!("rproj-list-test-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("trap.toml")).expect("create the trap");
        fs::write(
            dir.join("real.toml"),
            "packages = []\npackage_workflow = \"wally\"\n",
        )
        .expect("write a real one");
        fs::write(dir.join("notes.txt"), "not a setup").expect("write");

        // The same filter chain `list` runs, against a directory the test
        // controls. `list` itself reads the real config location, which a
        // test must not touch.
        let mut names: Vec<String> = fs::read_dir(&dir)
            .expect("read_dir")
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
            .filter_map(|e| {
                let path = e.path();
                (path.extension()? == "toml")
                    .then(|| path.file_stem()?.to_str().map(str::to_owned))?
            })
            .collect();
        names.sort();

        assert_eq!(names, ["real"], "the directory and the .txt are both out");
        let _ = fs::remove_dir_all(&dir);
    }

    /// The on-disk spelling is a compatibility promise; the Rust spelling
    /// is not. A rename in Rust must not silently change the file format.
    #[test]
    fn the_workflow_enum_is_written_in_kebab_case() {
        let text = toml::to_string_pretty(&crate::graph::ProjectGraph {
            mode: "guided".into(),
            package_workflow: PackageWorkflow::GitSubmodules,
            ..Default::default()
        })
        .expect("serialise");
        assert!(
            text.contains("package_workflow = \"git-submodules\""),
            "{text}"
        );
        assert!(!text.contains("GitSubmodules"), "{text}");
    }

    #[test]
    fn project_template_round_trips_in_an_injected_location() {
        let dir =
            std::env::temp_dir().join(format!("rproj-template-config-test-{}", std::process::id()));
        let path = dir.join("templates").join("default.project.json");
        let _ = fs::remove_dir_all(&dir);
        let template = serde_json::json!({"name": "ProjectName", "tree": {}});

        project_template::save_to(&template, &path).expect("save template");
        assert_eq!(
            project_template::load_from(&path).expect("load template"),
            Some(template)
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn corrupt_project_template_names_the_repair_command() {
        let dir = std::env::temp_dir().join(format!(
            "rproj-template-corrupt-test-{}",
            std::process::id()
        ));
        let path = dir.join("default.project.json");
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).expect("create fixture");
        fs::write(&path, "not json").expect("write fixture");

        let error = project_template::load_from(&path).unwrap_err().to_string();
        assert!(error.contains("rproj configure project"), "{error}");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn resetting_removes_only_the_custom_template() {
        let dir =
            std::env::temp_dir().join(format!("rproj-template-reset-test-{}", std::process::id()));
        let path = dir.join("templates").join("default.project.json");
        let sibling = dir.join("config.toml");
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(path.parent().unwrap()).expect("create fixture");
        fs::write(&path, "{}").expect("write template");
        fs::write(&sibling, "last_checked = 'now'").expect("write sibling");

        assert!(project_template::reset_at(&path).expect("reset template"));
        assert!(!path.exists());
        assert!(sibling.exists(), "reset removed unrelated configuration");
        assert!(!project_template::reset_at(&path).expect("repeat reset"));
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn saving_replaces_an_existing_template() {
        let dir = std::env::temp_dir().join(format!(
            "rproj-template-replace-test-{}",
            std::process::id()
        ));
        let path = dir.join("default.project.json");
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).expect("create fixture");
        fs::write(&path, "previous contents").expect("write previous template");

        let template = serde_json::json!({ "name": "ProjectName", "tree": {} });
        project_template::save_to(&template, &path).expect("replace template");

        assert_eq!(
            fs::read_to_string(&path).expect("read replacement"),
            "{\n  \"name\": \"ProjectName\",\n  \"tree\": {}\n}\n"
        );
        let _ = fs::remove_dir_all(&dir);
    }
}