rproj 0.13.1

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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use std::io::{self, IsTerminal};
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, TryRecvError};
use std::time::Duration;

use anyhow::{Context, Result};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap};

use super::catalog_browser::{CatalogApp, CatalogExit};
use crate::config::{GlobalConfig, PackageWorkflow, Setups, project_file, project_template};
use crate::graph::ProjectGraph;
use crate::steps::update_check::{self, UpdateStatus};
use crate::tui::{
    ACCENT, InputState, TerminalSession, centered, is_too_small, render_footer, render_input,
    responsive_panes, selected_style, title_style,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HubOutcome {
    Quit,
    New { name: String },
    EditProjectTemplate,
    ConfigureTools,
    SetupMachine,
    Upgrade,
    Watch,
    Test,
    CopySource,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Action {
    New,
    Template,
    Configure,
    Setup,
    Upgrade,
    Watch,
    Test,
    Copy,
    Catalog,
}

const ACTIONS: &[(Action, &str, &str)] = &[
    (
        Action::New,
        "New Project",
        "Scaffold a project under your configured RobloxProjects folder.",
    ),
    (
        Action::Template,
        "Edit Project Template",
        "Customize the validated Rojo tree inherited by future projects.",
    ),
    (
        Action::Configure,
        "Configure Tools",
        "Walk through settings for a tool used by this project.",
    ),
    (
        Action::Setup,
        "Machine Setup",
        "Install or revise machine-wide apps, tools, plugins, and extensions.",
    ),
    (
        Action::Upgrade,
        "Upgrade Project",
        "Re-apply current generated configuration to this project.",
    ),
    (
        Action::Watch,
        "Watch Project",
        "Install missing dependencies and start the Rojo sourcemap watcher.",
    ),
    (
        Action::Test,
        "Test Project",
        "Restore dependencies and run the test runner recorded by this project.",
    ),
    (
        Action::Copy,
        "Copy Source",
        "Copy source files with relative-path headers to the clipboard.",
    ),
    (
        Action::Catalog,
        "Catalog",
        "Browse packages, tools, capabilities, generated files, and topics.",
    ),
];

struct WorkspaceContext {
    cwd: PathBuf,
    machine: String,
    machine_ready: bool,
    setups: Vec<String>,
    template: String,
    has_project_file: bool,
    has_rojo_file: bool,
    has_src: bool,
    graph: Option<ProjectGraph>,
    warnings: Vec<String>,
}

impl WorkspaceContext {
    fn load(cwd: PathBuf) -> Self {
        let mut warnings = Vec::new();
        let mut machine_ready = false;
        let machine = match GlobalConfig::load() {
            Ok(config) if config.machine_configured() => {
                machine_ready = true;
                format!("configured: {}", config.machine_summary())
            }
            Ok(_) => "not configured".into(),
            Err(error) => {
                warnings.push(format!("Machine config: {error:#}"));
                "unavailable".into()
            }
        };
        let project_path = project_file::path_in(&cwd);
        let has_project_file = project_path.is_file();
        let graph = match project_file::load_from(&cwd) {
            Ok(graph) => graph,
            Err(error) => {
                warnings.push(format!("Project config: {error:#}"));
                None
            }
        };
        let template = match project_template::path() {
            Ok(path) if path.is_file() => match project_template::load() {
                Ok(Some(_)) => "custom template".into(),
                Ok(None) => "built-in template".into(),
                Err(error) => {
                    warnings.push(format!("Project template: {error:#}"));
                    "custom template needs repair".into()
                }
            },
            Ok(_) => "built-in template".into(),
            Err(error) => {
                warnings.push(format!("Project template: {error:#}"));
                "unavailable".into()
            }
        };
        Self {
            has_rojo_file: cwd.join("default.project.json").is_file(),
            has_src: cwd.join("src").is_dir(),
            cwd,
            machine,
            machine_ready,
            setups: Setups::list(),
            template,
            has_project_file,
            graph,
            warnings,
        }
    }

    fn recognized_project(&self) -> bool {
        self.has_project_file || self.has_rojo_file
    }

    fn availability(&self, action: Action) -> std::result::Result<(), &'static str> {
        match action {
            Action::New if !self.machine_ready => {
                Err("Run Machine Setup first; New Project does not install machine applications.")
            }
            Action::Configure if !self.recognized_project() => {
                Err("No project here: default.project.json or rproj.toml is required.")
            }
            Action::Upgrade if !self.has_project_file => {
                Err("Upgrade requires rproj.toml in the current directory.")
            }
            Action::Upgrade if self.graph.is_none() => {
                Err("rproj.toml is invalid; repair it before upgrading.")
            }
            Action::Watch if !self.has_rojo_file => {
                Err("Watch requires default.project.json in the current directory.")
            }
            Action::Test if !self.has_project_file => {
                Err("Test requires rproj.toml in the current directory.")
            }
            Action::Test if self.graph.is_none() => {
                Err("rproj.toml is invalid; repair it before testing.")
            }
            Action::Test
                if self
                    .graph
                    .as_ref()
                    .and_then(|graph| graph.test_runner())
                    .is_none() =>
            {
                Err("Testing is not enabled in this project.")
            }
            Action::Test
                if self
                    .graph
                    .as_ref()
                    .is_some_and(|graph| !graph.testing_is_compatible()) =>
            {
                Err("Jest Roblox requires the Wally dependency workflow.")
            }
            Action::Copy if !self.has_src => {
                Err("Copy Source requires a src directory in the current directory.")
            }
            _ => Ok(()),
        }
    }

    fn project_summary(&self) -> String {
        match &self.graph {
            Some(graph) => format!(
                "recognized\nmode: {}\npackages: {} via {}\ncapabilities: {}",
                nonempty(&graph.mode, "unspecified"),
                graph.packages.len(),
                workflow_name(graph.package_workflow),
                if graph.capabilities.is_empty() {
                    "none".into()
                } else {
                    graph.capability_keys().join(", ")
                }
            ),
            None if self.has_project_file => "rproj.toml is invalid".into(),
            None if self.has_rojo_file => "Rojo project without rproj.toml".into(),
            None => "No project detected in this exact directory.".into(),
        }
    }
}

fn nonempty<'a>(value: &'a str, fallback: &'a str) -> &'a str {
    if value.is_empty() { fallback } else { value }
}

fn workflow_name(workflow: PackageWorkflow) -> &'static str {
    match workflow {
        PackageWorkflow::Wally => "Wally",
        PackageWorkflow::GitSubmodules => "git submodules",
        PackageWorkflow::None => "no package manager",
    }
}

enum Screen {
    Hub,
    Catalog(CatalogApp),
}

enum Modal {
    Help,
    ProjectName(InputState),
}

struct HubApp {
    context: WorkspaceContext,
    selected: usize,
    screen: Screen,
    modal: Option<Modal>,
    status: String,
    update: UpdateStatus,
    update_receiver: Option<Receiver<UpdateStatus>>,
}

impl HubApp {
    fn new(context: WorkspaceContext) -> Self {
        let (update, update_receiver) = update_check::background_status();
        Self {
            context,
            selected: 0,
            screen: Screen::Hub,
            modal: None,
            status: "Choose a task. Direct commands remain available for scripts.".into(),
            update,
            update_receiver,
        }
    }

    fn refresh_update(&mut self) {
        let received = self.update_receiver.as_ref().map(Receiver::try_recv);
        match received {
            Some(Ok(status)) => {
                self.update = status;
                self.update_receiver = None;
            }
            Some(Err(TryRecvError::Disconnected)) => {
                self.update = UpdateStatus::Unknown;
                self.update_receiver = None;
            }
            Some(Err(TryRecvError::Empty)) | None => {}
        }
    }

    fn handle_key(&mut self, key: KeyEvent) -> Option<HubOutcome> {
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            return Some(HubOutcome::Quit);
        }
        if let Screen::Catalog(catalog) = &mut self.screen {
            return match catalog.handle_key(key) {
                Some(CatalogExit::Back) => {
                    self.screen = Screen::Hub;
                    None
                }
                Some(CatalogExit::Quit) => Some(HubOutcome::Quit),
                None => None,
            };
        }
        if let Some(modal) = self.modal.take() {
            return self.handle_modal(modal, key);
        }
        match key.code {
            KeyCode::Esc => Some(HubOutcome::Quit),
            KeyCode::Char('?') => {
                self.modal = Some(Modal::Help);
                None
            }
            KeyCode::Up => {
                self.selected = self.selected.saturating_sub(1);
                None
            }
            KeyCode::Down => {
                self.selected = (self.selected + 1).min(ACTIONS.len() - 1);
                None
            }
            KeyCode::Enter => self.activate(),
            _ => None,
        }
    }

    fn handle_modal(&mut self, modal: Modal, key: KeyEvent) -> Option<HubOutcome> {
        match modal {
            Modal::Help => {
                if !matches!(key.code, KeyCode::Esc | KeyCode::Enter | KeyCode::Char('?')) {
                    self.modal = Some(Modal::Help);
                }
                None
            }
            Modal::ProjectName(mut input) => match key.code {
                KeyCode::Esc => None,
                KeyCode::Enter => {
                    let name = input.text().trim();
                    if let Err(error) = super::creation::validate_name(name) {
                        input.error = Some(error.into());
                        self.modal = Some(Modal::ProjectName(input));
                        None
                    } else {
                        Some(HubOutcome::New { name: name.into() })
                    }
                }
                _ => {
                    input.handle_key(key);
                    self.modal = Some(Modal::ProjectName(input));
                    None
                }
            },
        }
    }

    fn activate(&mut self) -> Option<HubOutcome> {
        let action = ACTIONS[self.selected].0;
        crate::diagnostics::event("hub.action", format!("{action:?}"));
        if let Err(reason) = self.context.availability(action) {
            crate::diagnostics::event("hub.unavailable", reason);
            self.status = reason.into();
            return None;
        }
        match action {
            Action::New => {
                self.modal = Some(Modal::ProjectName(InputState::new("")));
                None
            }
            Action::Template => Some(HubOutcome::EditProjectTemplate),
            Action::Configure => Some(HubOutcome::ConfigureTools),
            Action::Setup => Some(HubOutcome::SetupMachine),
            Action::Upgrade => Some(HubOutcome::Upgrade),
            Action::Watch => Some(HubOutcome::Watch),
            Action::Test => Some(HubOutcome::Test),
            Action::Copy => Some(HubOutcome::CopySource),
            Action::Catalog => {
                self.screen = Screen::Catalog(CatalogApp::new());
                None
            }
        }
    }

    fn render(&self, frame: &mut ratatui::Frame<'_>) {
        if let Screen::Catalog(catalog) = &self.screen {
            catalog.render(frame);
            return;
        }
        let area = frame.area();
        if is_too_small(area) {
            frame.render_widget(
                Paragraph::new("rproj workspace\n\nTerminal is too small. Resize to at least 60 x 16.\n\n? help   Esc exit")
                    .alignment(Alignment::Center)
                    .wrap(Wrap { trim: true })
                    .block(Block::default().borders(Borders::ALL).border_style(Style::default().fg(Color::Yellow))),
                area,
            );
            return;
        }
        let outer = Layout::vertical([
            Constraint::Length(3),
            Constraint::Min(8),
            Constraint::Length(3),
        ])
        .split(area);
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled(
                    format!("rproj {}", env!("CARGO_PKG_VERSION")),
                    title_style(),
                ),
                Span::raw(format!("  {}", self.context.cwd.display())),
            ]))
            .block(Block::default().borders(Borders::BOTTOM)),
            outer[0],
        );
        let panes = responsive_panes(outer[1], 40);
        self.render_actions(frame, panes[0]);
        self.render_context(frame, panes[1]);
        render_footer(
            frame,
            outer[2],
            &self.status,
            "Arrows navigate  Enter open  Esc exit  ? help",
            self.status.contains("requires") || self.status.starts_with("No project"),
        );
        match &self.modal {
            Some(Modal::Help) => self.render_help(frame, area),
            Some(Modal::ProjectName(input)) => {
                render_input(frame, area, "New project", "Project folder name", input)
            }
            None => {}
        }
    }

    fn render_actions(&self, frame: &mut ratatui::Frame<'_>, area: Rect) {
        let items = ACTIONS.iter().map(|(action, label, _)| {
            let unavailable = self.context.availability(*action).is_err();
            ListItem::new(format!(
                "{}{}",
                label,
                if unavailable { "  [unavailable]" } else { "" }
            ))
            .style(Style::default().fg(if unavailable {
                Color::DarkGray
            } else {
                Color::White
            }))
        });
        let mut state = ListState::default().with_selected(Some(self.selected));
        frame.render_stateful_widget(
            List::new(items)
                .block(
                    Block::default()
                        .title(" Tasks ")
                        .borders(Borders::ALL)
                        .border_style(Style::default().fg(ACCENT)),
                )
                .highlight_style(selected_style(true)),
            area,
            &mut state,
        );
    }

    fn render_context(&self, frame: &mut ratatui::Frame<'_>, area: Rect) {
        let (action, label, description) = ACTIONS[self.selected];
        let availability = self
            .context
            .availability(action)
            .err()
            .map(|reason| format!("\n\nUnavailable: {reason}"))
            .unwrap_or_default();
        let setups = if self.context.setups.is_empty() {
            "none".into()
        } else {
            self.context.setups.join(", ")
        };
        let warnings = if self.context.warnings.is_empty() {
            String::new()
        } else {
            format!("\n\nWarnings\n{}", self.context.warnings.join("\n"))
        };
        let body = format!(
            "{description}{availability}\n\nWorkspace\n{}\n\nMachine\n{}\n\nTemplate\n{}\n\nSaved setups\n{}\n\nVersion\n{}{}",
            self.context.project_summary(),
            self.context.machine,
            self.context.template,
            setups,
            self.update.label(),
            warnings
        );
        frame.render_widget(
            Paragraph::new(body).wrap(Wrap { trim: false }).block(
                Block::default()
                    .title(format!(" {label} "))
                    .borders(Borders::ALL),
            ),
            area,
        );
    }

    fn render_help(&self, frame: &mut ratatui::Frame<'_>, area: Rect) {
        let popup = centered(area, 68, 12);
        frame.render_widget(Clear, popup);
        frame.render_widget(
            Paragraph::new("Workspace hub\n  Shows the current directory, machine setup, saved setups, template, and version state.\n\nNavigation\n  Up/Down select; Enter opens; Esc exits\n\nCommands\n  Selected commands run after rproj restores the terminal. Direct CLI commands remain scriptable.")
                .wrap(Wrap { trim: false })
                .block(Block::default().title(" Help ").borders(Borders::ALL).border_style(Style::default().fg(ACCENT))),
            popup,
        );
    }
}

pub fn run() -> Result<HubOutcome> {
    if !io::stdin().is_terminal() || !io::stdout().is_terminal() {
        anyhow::bail!("the rproj workspace hub requires an interactive terminal");
    }
    let cwd = std::env::current_dir().context("could not read the current directory")?;
    let mut app = HubApp::new(WorkspaceContext::load(cwd));
    let outcome = {
        let mut terminal = TerminalSession::enter()?;
        loop {
            app.refresh_update();
            terminal.draw(|frame| app.render(frame))?;
            let event = if app.update_receiver.is_some() {
                terminal.poll_event(Duration::from_millis(150))?
            } else {
                Some(terminal.read_event()?)
            };
            if let Some(Event::Paste(text)) = &event
                && let Some(Modal::ProjectName(input)) = &mut app.modal
            {
                for ch in text.chars().filter(|ch| !ch.is_control()) {
                    input.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE));
                }
            }
            if let Some(Event::Key(key)) = event
                && key.kind != crossterm::event::KeyEventKind::Release
                && let Some(outcome) = app.handle_key(key)
            {
                break outcome;
            }
        }
    };
    Ok(outcome)
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use std::path::Path;
    use tempfile::TempDir;

    fn app_at(path: &Path) -> HubApp {
        let mut app = HubApp::new(WorkspaceContext::load(path.to_path_buf()));
        app.update_receiver = None;
        app.update = UpdateStatus::Current;
        app
    }

    #[test]
    fn creation_requires_setup_and_rejects_invalid_names_without_leaving() {
        let temp = TempDir::new().unwrap();
        let mut app = app_at(temp.path());
        app.context.machine_ready = false;
        assert!(
            app.context
                .availability(Action::New)
                .unwrap_err()
                .contains("Machine Setup")
        );
        assert!(app.context.availability(Action::Setup).is_ok());
        app.context.machine_ready = true;
        app.modal = Some(Modal::ProjectName(InputState::new("../outside")));
        assert!(
            app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
                .is_none()
        );
        assert!(matches!(app.modal, Some(Modal::ProjectName(ref input)) if input.error.is_some()));
    }

    fn rendered(app: &HubApp, width: u16, height: u16) -> String {
        let backend = TestBackend::new(width, height);
        let mut terminal = Terminal::new(backend).unwrap();
        terminal.draw(|frame| app.render(frame)).unwrap();
        terminal
            .backend()
            .buffer()
            .content()
            .iter()
            .map(|cell| cell.symbol())
            .collect()
    }

    #[test]
    fn wide_narrow_help_and_tiny_states_render() {
        let temp = TempDir::new().unwrap();
        let mut app = app_at(temp.path());
        assert!(rendered(&app, 120, 30).contains("Tasks"));
        assert!(rendered(&app, 80, 24).contains("Workspace"));
        assert!(rendered(&app, 40, 10).contains("too small"));
        app.modal = Some(Modal::Help);
        assert!(rendered(&app, 120, 30).contains("Workspace hub"));
    }

    #[test]
    fn project_actions_stay_visible_and_explain_why_they_are_disabled() {
        let temp = TempDir::new().unwrap();
        let mut app = app_at(temp.path());
        app.selected = ACTIONS
            .iter()
            .position(|(action, _, _)| *action == Action::Watch)
            .unwrap();
        assert!(rendered(&app, 120, 30).contains("[unavailable]"));
        assert!(
            app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
                .is_none()
        );
        assert!(app.status.contains("default.project.json"));
    }

    #[test]
    fn test_action_tracks_the_recorded_runner() {
        let temp = TempDir::new().unwrap();
        let mut graph = ProjectGraph::default();
        graph.choose("test", Some("testez"));
        crate::config::project_file::save_to(&graph, temp.path()).unwrap();
        let mut app = app_at(temp.path());
        app.selected = ACTIONS
            .iter()
            .position(|(action, _, _)| *action == Action::Test)
            .unwrap();
        assert_eq!(app.activate(), Some(HubOutcome::Test));

        let empty = TempDir::new().unwrap();
        crate::config::project_file::save_to(&ProjectGraph::default(), empty.path()).unwrap();
        let context = WorkspaceContext::load(empty.path().to_path_buf());
        assert_eq!(
            context.availability(Action::Test),
            Err("Testing is not enabled in this project.")
        );
    }

    #[test]
    fn new_project_requires_a_nonempty_unicode_name() {
        let temp = TempDir::new().unwrap();
        let mut app = app_at(temp.path());
        app.context.machine_ready = true;
        app.activate();
        assert!(
            app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
                .is_none()
        );
        for character in "café".chars() {
            app.handle_key(KeyEvent::new(KeyCode::Char(character), KeyModifiers::NONE));
        }
        assert_eq!(
            app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
            Some(HubOutcome::New {
                name: "café".into()
            })
        );
    }

    #[test]
    fn exact_directory_project_detection_does_not_walk_upward() {
        let temp = TempDir::new().unwrap();
        std::fs::write(temp.path().join("default.project.json"), "{}").unwrap();
        let child = temp.path().join("src");
        std::fs::create_dir(&child).unwrap();
        let parent = WorkspaceContext::load(temp.path().to_path_buf());
        let nested = WorkspaceContext::load(child);
        assert!(parent.recognized_project());
        assert!(!nested.recognized_project());
    }
}