Skip to content

Commit

Permalink
Add --json argument to all commands (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Pruitt authored Apr 14, 2021
1 parent c9d636b commit d00d161
Show file tree
Hide file tree
Showing 23 changed files with 419 additions and 150 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Version 0.3.0

* Add `--json` argument to all commands

# Version 0.2.3

* Fixed dragonruby install from itch.io package
* Fixed issue with add with empty dependencies in config
* Fixed `smaug.rb` generation on Windows
* Fix dragonruby install from itch.io package
* Fix issue with add with empty dependencies in config
* Fix `smaug.rb` generation on Windows
* Add `compile_ruby` flag to enable Bytecode compilation
* Automatically run `smaug install` on `smaug add`
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smaug-cli"
version = "0.2.3"
version = "0.3.0"
authors = ["Matt Pruitt <[email protected]>"]
edition = "2018"

Expand All @@ -21,6 +21,7 @@ question = "0.2.2"
reqwest = { version = "0.11", features = ["blocking", "json"] }
rm_rf = "0.6.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
stderrlog = "0.5"
tinytemplate = "1.1"
zip = "0.5"
Expand Down
20 changes: 18 additions & 2 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use clap::ArgMatches;
use std::error::Error;
use serde::Serialize;
use std::fmt::Display;

pub type CommandResult = Result<Box<dyn Display>, Box<dyn Error>>;
pub type CommandResult = Result<Box<dyn Json>, Box<dyn Json>>;

pub trait Json {
fn to_json(&self) -> String;
fn to_string(&self) -> String;
}

impl<T: Serialize + Display> Json for T {
fn to_json(&self) -> String {
serde_json::to_string(self).expect("Could not convert to json")
}

fn to_string(&self) -> String {
format!("{}", self)
}
}

pub trait Command {
fn run(&self, matches: &ArgMatches) -> CommandResult;
}
55 changes: 41 additions & 14 deletions cli/src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ use derive_more::Display;
use derive_more::Error;
use log::*;
use serde::Deserialize;
use serde::Serialize;
use std::env;
use std::path::Path;
use std::path::PathBuf;
use toml_edit::{value, Document};

#[derive(Debug, Display, Error)]
pub struct Add;

#[derive(Debug, Display, Error, Serialize)]
pub enum Error {
#[display(fmt = "Could not find Smaug.toml at {}", "config_path.display()")]
FileNotFound { config_path: PathBuf },
#[display(fmt = "Could not find Smaug.toml at {}", "path.display()")]
FileNotFound { path: PathBuf },
#[display(fmt = "{} has already been added to this project.", "name")]
AlreadyAdded { name: String },
#[display(fmt = "Could not fetch from registry.")]
RegistryError,
#[display(fmt = "Could not install packages.")]
InstallError,
}

#[derive(Debug)]
pub struct Add;
#[derive(Debug, Display, Serialize)]
#[display(fmt = "Added {} {} to your project.", "package", "version")]
pub struct AddResult {
package: String,
version: String,
}

impl Command for Add {
fn run(&self, matches: &ArgMatches) -> CommandResult {
Expand All @@ -30,22 +41,35 @@ impl Command for Add {
let directory: &str = matches
.value_of("path")
.unwrap_or_else(|| current_directory.to_str().unwrap());

debug!("Directory: {}", directory);
let canonical = std::fs::canonicalize(directory)?;

let canonical = match std::fs::canonicalize(directory) {
Ok(dir) => dir,
Err(..) => {
return Err(Box::new(Error::FileNotFound {
path: Path::new(directory).to_path_buf(),
}))
}
};

let path = Path::new(&canonical);
let path = std::fs::canonicalize(&path).expect("Could not find path");

let config_path = path.join("Smaug.toml");

if !config_path.is_file() {
return Err(Box::new(Error::FileNotFound { config_path }));
return Err(Box::new(Error::FileNotFound { path: config_path }));
}

let config =
std::fs::read_to_string(config_path.clone()).expect("Could not read Smaug.toml");

let package_name = matches.value_of("PACKAGE").expect("No package given");
let latest_version = fetch_from_registry(package_name.to_string())?;
let latest_version = match fetch_from_registry(package_name.to_string()) {
Ok(version) => version,
Err(..) => return Err(Box::new(Error::RegistryError)),
};

trace!("Latest version: {}", latest_version);

Expand All @@ -66,14 +90,17 @@ impl Command for Add {

doc["dependencies"][package_name] = value(latest_version.clone());

std::fs::write(config_path, doc.to_string_in_original_order())?;
std::fs::write(config_path, doc.to_string_in_original_order())
.expect("Couldn't write config file.");

Install.run(matches)?;
if Install.run(matches).is_err() {
return Err(Box::new(Error::InstallError));
}

Ok(Box::new(format!(
"Added {} version {} to your project.\nRun smaug install to install it.",
package_name, latest_version
)))
Ok(Box::new(AddResult {
package: package_name.to_string(),
version: latest_version,
}))
}
}

Expand Down
39 changes: 33 additions & 6 deletions cli/src/commands/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@ use clap::ArgMatches;
use derive_more::Display;
use derive_more::Error;
use log::*;
use serde::Serialize;
use smaug::dragonruby;
use std::env;
use std::path::Path;
use std::path::PathBuf;
use std::process;

#[derive(Debug)]
#[derive(Debug, Display, Serialize)]
#[display(fmt = "Succesfully created bindings.")]
pub struct Bind;

#[derive(Debug, Display, Error)]
#[derive(Debug, Display, Error, Serialize)]
pub enum Error {
#[display(
fmt = "Could not find the configured version of DragonRuby. Install it with `smaug dragonruby install`"
)]
ConfiguredDragonRubyNotFound,
#[display(fmt = "The bind command is only available for DragonRuby pro.")]
DragonRubyNotPro,
#[display(fmt = "Couldn't load Smaug configuration.")]
ConfigError { path: PathBuf },
#[display(fmt = "Could not find file at {}", "path.display()")]
FileNotFound { path: PathBuf },
}

impl Command for Bind {
Expand All @@ -39,12 +46,22 @@ impl Command for Bind {
.value_of("path")
.unwrap_or_else(|| current_directory.to_str().unwrap());
debug!("Directory: {}", directory);
let path = Path::new(directory);
let path = std::fs::canonicalize(&path).expect("Could not find path");
let path = match std::fs::canonicalize(directory) {
Ok(dir) => dir,
Err(..) => {
return Err(Box::new(Error::FileNotFound {
path: Path::new(directory).to_path_buf(),
}))
}
};

let config_path = path.join("Smaug.toml");

let config = smaug::config::load(&config_path)?;
let config = match smaug::config::load(&config_path) {
Ok(config) => config,
Err(..) => return Err(Box::new(Error::ConfigError { path: config_path })),
};

debug!("Smaug config: {:?}", config);

let project_output = path.join(output);
Expand Down Expand Up @@ -77,17 +94,27 @@ impl Command for Bind {
project_input.display(),
dragonruby_options.join(" ")
);

let quiet = matches.is_present("json") || matches.is_present("quiet");

let stdout = if quiet {
process::Stdio::null()
} else {
process::Stdio::inherit()
};

process::Command::new(bin)
.current_dir(bin_dir.to_str().unwrap())
.arg(format!("--output={}", project_output.display()))
.arg(project_input)
.args(dragonruby_options)
.stdout(stdout)
.spawn()
.unwrap()
.wait()
.unwrap();

Ok(Box::new("Successfully created bindings."))
Ok(Box::new(Bind {}))
}
}
}
Expand Down
45 changes: 37 additions & 8 deletions cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@ use clap::ArgMatches;
use derive_more::Display;
use derive_more::Error;
use log::*;
use serde::Serialize;
use smaug::dragonruby;
use smaug::util::dir::copy_directory;
use std::env;
use std::path::Path;
use std::path::PathBuf;
use std::process;

#[derive(Debug)]
pub struct Build;

#[derive(Debug, Display, Error)]
#[derive(Debug, Serialize, Display)]
#[display(fmt = "Successfully built {}", "project_name")]
pub struct BuildResult {
project_name: String,
}
#[derive(Debug, Display, Error, Serialize)]
pub enum Error {
#[display(
fmt = "Could not find the configured version of DragonRuby. Install it with `smaug dragonruby install`"
)]
ConfiguredDragonRubyNotFound,
#[display(fmt = "Couldn't load Smaug configuration.")]
ConfigError { path: PathBuf },
#[display(fmt = "Could not find file at {}", "path.display()")]
FileNotFound { path: PathBuf },
}

impl Command for Build {
Expand All @@ -35,12 +46,21 @@ impl Command for Build {
.value_of("path")
.unwrap_or_else(|| current_directory.to_str().unwrap());
debug!("Directory: {}", directory);
let path = Path::new(directory);
let path = std::fs::canonicalize(&path).expect("Could not find path");
let path = match std::fs::canonicalize(directory) {
Ok(dir) => dir,
Err(..) => {
return Err(Box::new(Error::FileNotFound {
path: Path::new(directory).to_path_buf(),
}))
}
};

let config_path = path.join("Smaug.toml");

let config = smaug::config::load(&config_path)?;
let config = match smaug::config::load(&config_path) {
Ok(conf) => conf,
Err(..) => return Err(Box::new(Error::ConfigError { path: config_path })),
};
debug!("Smaug config: {:?}", config);

trace!("Writing game metadata.");
Expand Down Expand Up @@ -75,11 +95,21 @@ impl Command for Build {
path.to_str().unwrap(),
dragonruby_options.join(" "),
);

let quiet = matches.is_present("json") || matches.is_present("quiet");

let stdout = if quiet {
process::Stdio::null()
} else {
process::Stdio::inherit()
};

process::Command::new(bin)
.current_dir(bin_dir.to_str().unwrap())
.arg("--only-package")
.args(dragonruby_options)
.arg(path.file_name().unwrap())
.stdout(stdout)
.spawn()
.unwrap()
.wait()
Expand Down Expand Up @@ -107,10 +137,9 @@ impl Command for Build {
.expect("couldn't copy exceptions");
}

Ok(Box::new(format!(
"Successfully built {} to the builds dir.",
config.project.unwrap().name
)))
Ok(Box::new(BuildResult {
project_name: config.project.unwrap().name,
}))
}
}
}
Expand Down
Loading

0 comments on commit d00d161

Please sign in to comment.