From df1aced95d65f3cd0f11e5d32ef74226b669a4e3 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 20 May 2021 15:41:08 -0400 Subject: [PATCH] Add fmt-project subcommand --- CHANGELOG.md | 1 + src/bin.rs | 1 + src/cli/fmt_project.rs | 29 +++++++++++++++++++++++++++++ src/cli/mod.rs | 4 ++++ 4 files changed, 35 insertions(+) create mode 100644 src/cli/fmt_project.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index dd00074d..4a50adb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Added the `gameId` and `placeId` optional properties to project files. * When connecting from the Rojo Roblox Studio plugin, Rojo will set the game and place ID of the current place to these values, if set. * This is equivalent to running `game:SetUniverseId(...)` and `game:SetPlaceId(...)` from the command bar in Studio. +* Added the `fmt-project` subcommand for formatting Rojo project files. * Added "EXPERIMENTAL!" label to two-way sync toggle in Rojo's Roblox Studio plugin. * Fixed `Name` and `Parent` properties being allowed in Rojo projects. ([#413][pr-413]) * Fixed "Open Scripts Externally" feature crashing Studio. ([#369][issue-369]) diff --git a/src/bin.rs b/src/bin.rs index ca4b878d..0b67c214 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -11,6 +11,7 @@ fn run(global: GlobalOptions, subcommand: Subcommand) -> anyhow::Result<()> { Subcommand::Serve(serve_options) => cli::serve(global, serve_options)?, Subcommand::Build(build_options) => cli::build(build_options)?, Subcommand::Upload(upload_options) => cli::upload(upload_options)?, + Subcommand::FmtProject(fmt_options) => fmt_options.run()?, Subcommand::Doc => cli::doc()?, Subcommand::Plugin(plugin_options) => cli::plugin(plugin_options)?, } diff --git a/src/cli/fmt_project.rs b/src/cli/fmt_project.rs new file mode 100644 index 00000000..e227fa44 --- /dev/null +++ b/src/cli/fmt_project.rs @@ -0,0 +1,29 @@ +use std::path::PathBuf; + +use anyhow::Context; +use structopt::StructOpt; + +use crate::project::Project; + +/// Reformat a Rojo project using the standard JSON formatting rules. +#[derive(Debug, StructOpt)] +pub struct FmtProjectCommand { + /// Path to the project to format. Defaults to the current directory. + #[structopt(default_value = "")] + pub project: PathBuf, +} + +impl FmtProjectCommand { + pub fn run(self) -> anyhow::Result<()> { + let project = Project::load_fuzzy(&self.project)? + .context("A project file is required to run 'rojo fmt-project'")?; + + let serialized = serde_json::to_string_pretty(&project) + .context("could not re-encode project file as JSON")?; + + fs_err::write(&project.file_location, &serialized) + .context("could not write back to project file")?; + + Ok(()) + } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 330ac46d..81cc5087 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -2,6 +2,7 @@ mod build; mod doc; +mod fmt_project; mod init; mod plugin; mod serve; @@ -22,6 +23,7 @@ use thiserror::Error; pub use self::build::*; pub use self::doc::*; +pub use self::fmt_project::FmtProjectCommand; pub use self::init::*; pub use self::plugin::*; pub use self::serve::*; @@ -112,6 +114,8 @@ pub enum Subcommand { /// Generates a place or model file out of the project and uploads it to Roblox. Upload(UploadCommand), + FmtProject(FmtProjectCommand), + /// Open Rojo's documentation in your browser. Doc,