Skip to content

Commit

Permalink
Make the tests pass, remove versioning from Abscissa
Browse files Browse the repository at this point in the history
This removes versioning support from Abscissa, and trusts clap
completely to deal with that. This also means what the `version`
subcommand is gone in favour of `--version`.
  • Loading branch information
rsdy authored and rsdy committed May 24, 2021
1 parent d972d4d commit 7b2484e
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 167 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands.rs
Expand Up @@ -2,7 +2,6 @@

pub mod gen;
pub mod new;
pub mod version;

use self::{gen::GenCommand, new::NewCommand};
use super::config::CliConfig;
Expand All @@ -20,6 +19,7 @@ enum SubCommands {

/// Abscissa CLI Subcommands
#[derive(Command, Debug, Clap)]
#[clap(author, about, version)]
pub struct CliCommand {
#[clap(subcommand)]
subcmd: SubCommands,
Expand Down
17 changes: 0 additions & 17 deletions cli/src/commands/version.rs

This file was deleted.

1 change: 0 additions & 1 deletion cli/src/template/collection.rs
Expand Up @@ -30,7 +30,6 @@ const DEFAULT_TEMPLATE_FILES: &[(&str, &str)] = &[
template!("src/bin/app/main.rs.hbs"),
template!("src/commands.rs.hbs"),
template!("src/commands/start.rs.hbs"),
template!("src/commands/version.rs.hbs"),
template!("src/config.rs.hbs"),
template!("src/error.rs.hbs"),
template!("src/lib.rs.hbs"),
Expand Down
2 changes: 1 addition & 1 deletion cli/template/Cargo.toml.hbs
Expand Up @@ -5,7 +5,7 @@ version = "{{version}}"
edition = "{{edition}}"

[dependencies]
gumdrop = "0.7"
clap = "3.0.0-beta.2"
serde = { version = "1", features = ["serde_derive"] }
thiserror = "1"

Expand Down
8 changes: 4 additions & 4 deletions cli/template/src/application.rs.hbs
@@ -1,10 +1,10 @@
//! {{title}} Abscissa Application

use crate::{commands::{{~command_type~}}, config::{{~config_type~}}};
use crate::{commands::EntryPoint, config::{{~config_type~}}};
use abscissa_core::{
application::{self, AppCell},
config::{self, CfgCell},
trace, Application, EntryPoint, FrameworkError, StandardPaths,
trace, Application, FrameworkError, StandardPaths,
};

/// Application state
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Default for {{application_type}} {

impl Application for {{application_type}} {
/// Entrypoint command for this application.
type Cmd = EntryPoint<{{~command_type~}}>;
type Cmd = EntryPoint;

/// Application configuration.
type Cfg = {{config_type~}};
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Application for {{application_type}} {
}

/// Get tracing configuration from command-line options
fn tracing_config(&self, command: &EntryPoint<{{~command_type~}}>) -> trace::Config {
fn tracing_config(&self, command: &EntryPoint) -> trace::Config {
if command.verbose {
trace::Config::verbose()
} else {
Expand Down
56 changes: 37 additions & 19 deletions cli/template/src/commands.rs.hbs
Expand Up @@ -5,48 +5,63 @@
//! The default application comes with two subcommands:
//!
//! - `start`: launches the application
//! - `version`: print application version
//! - `--version`: print application version
//!
//! See the `impl Configurable` below for how to specify the path to the
//! application's configuration file.

mod start;
mod version;

use self::{start::StartCmd, version::VersionCmd};
use self::start::StartCmd;
use crate::config::{{~config_type~}};
use abscissa_core::{
config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable,
};
use abscissa_core::{config::Override, Clap, Command, Configurable, FrameworkError, Runnable};
use std::path::PathBuf;

/// {{title}} Configuration Filename
pub const CONFIG_FILE: &str = "{{name}}.toml";

/// {{title}} Subcommands
#[derive(Command, Debug, Options, Runnable)]
/// Subcommands need to be listed in an enum.
#[derive(Command, Debug, Clap, Runnable)]
pub enum {{command_type}} {
/// The `help` subcommand
#[options(help = "get usage information")]
Help(Help<Self>),

/// The `start` subcommand
#[options(help = "start the application")]
Start(StartCmd),
}

/// Entry point for the application. It needs to be a struct to allow using subcommands!
#[derive(Command, Debug, Clap)]
#[clap(author, about, version)]
pub struct EntryPoint {
#[clap(subcommand)]
cmd: {{command_type}},

/// Enable verbose logging
#[clap(short, long)]
pub verbose: bool,

/// The `version` subcommand
#[options(help = "display version information")]
Version(VersionCmd),
/// Use the specified config file
#[clap(short, long)]
pub config: Option<String>,
}

impl Runnable for EntryPoint {
fn run(&self) {
self.cmd.run()
}
}

/// This trait allows you to define how application configuration is loaded.
impl Configurable<{{~config_type~}}> for {{command_type}} {
impl Configurable<{{~config_type~}}> for EntryPoint {
/// Location of the configuration file
fn config_path(&self) -> Option<PathBuf> {
// Check if the config file exists, and if it does not, ignore it.
// If you'd like for a missing configuration file to be a hard error
// instead, always return `Some(CONFIG_FILE)` here.
let filename = PathBuf::from(CONFIG_FILE);
let filename = self
.config
.as_ref()
.map(PathBuf::from)
.unwrap_or_else(|| CONFIG_FILE.into());

if filename.exists() {
Some(filename)
Expand All @@ -64,9 +79,12 @@ impl Configurable<{{~config_type~}}> for {{command_type}} {
&self,
config: {{config_type}},
) -> Result<{{~config_type~}}, FrameworkError> {
match self {
match &self.cmd {
{{command_type}}::Start(cmd) => cmd.override_config(config),
_ => Ok(config),
//
// If you don't need special overrides for some
// subcommands, you can just use a catch all
// _ => Ok(config),
}
}
}
9 changes: 4 additions & 5 deletions cli/template/src/commands/start.rs.hbs
Expand Up @@ -5,19 +5,18 @@
use crate::prelude::*;

use crate::config::{{~config_type~}};
use abscissa_core::{config, Command, FrameworkError, Options, Runnable};
use abscissa_core::{config, Clap, Command, FrameworkError, Runnable};

/// `start` subcommand
///
/// The `Options` proc macro generates an option parser based on the struct
/// The `Clap` proc macro generates an option parser based on the struct
/// definition, and is defined in the `gumdrop` crate. See their documentation
/// for a more comprehensive example:
///
/// <https://docs.rs/gumdrop/>
#[derive(Command, Debug, Options)]
/// <https://docs.rs/clap/>
#[derive(Command, Debug, Clap)]
pub struct StartCmd {
/// To whom are we saying hello?
#[options(free)]
recipient: Vec<String>,
}

Expand Down
21 changes: 10 additions & 11 deletions cli/template/src/commands/subcommand.rs.hbs
@@ -1,26 +1,25 @@
//! `{{~name_lower~}}` subcommand

use abscissa_core::{Command, Options, Runnable};
use abscissa_core::{Command, Clap, Runnable};

/// `{{~name_lower~}}` subcommand
///
/// The `Options` proc macro generates an option parser based on the struct
/// definition, and is defined in the `gumdrop` crate. See their documentation
/// The `Clap` proc macro generates an option parser based on the struct
/// definition, and is defined in the `clap` crate. See their documentation
/// for a more comprehensive example:
///
/// <https://docs.rs/gumdrop/>
#[derive(Command, Debug, Options)]
/// <https://docs.rs/clap/>
#[derive(Command, Debug, Clap)]
pub struct {{name_capital}} {
// Example `--foobar` (with short `-f` argument)
// #[options(short = "f", help = "foobar path"]
// /// Option foobar. Doc comments are the help description
// #[clap(short)]
// foobar: Option<PathBuf>

// Example `--baz` argument with no short version
// #[options(no_short, help = "baz path")]
// /// Baz path
// #[clap(long)]
// baz: Option<PathBuf>

// "free" arguments don't have an associated flag
// #[options(free)]
// "free" arguments don't need a macro
// free_args: Vec<String>,
}

Expand Down
21 changes: 0 additions & 21 deletions cli/template/src/commands/version.rs.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion cli/template/tests/acceptance.rs.hbs
Expand Up @@ -86,6 +86,6 @@ fn start_with_config_and_args() {
#[test]
fn version_no_args() {
let mut runner = RUNNER.clone();
let mut cmd = runner.arg("version").capture_stdout().run();
let mut cmd = runner.arg("--version").capture_stdout().run();
cmd.stdout().expect_regex(r"\A\w+ [\d\.\-]+\z");
}
2 changes: 1 addition & 1 deletion cli/tests/app/exit_status.rs
Expand Up @@ -11,7 +11,7 @@ pub static RUNNER: Lazy<CmdRunner> = Lazy::new(|| CmdRunner::default());
#[test]
fn no_args() {
let mut runner = RUNNER.clone();
runner.capture_stdout().status().expect_success();
runner.capture_stdout().status().expect_code(1);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/generate_app.rs
Expand Up @@ -15,7 +15,7 @@ const APP_NAME: &str = "abscissa_gen_test_app";
const TEST_COMMANDS: &[&str] = &[
"fmt -- --check",
"test --release",
"run -- version",
"run -- --version",
"clippy",
];

Expand Down
8 changes: 0 additions & 8 deletions core/src/application.rs
Expand Up @@ -18,7 +18,6 @@ use crate::{
trace::{self, Tracing},
FrameworkError,
FrameworkErrorKind::*,
Version,
};
use std::{env, path::Path, process, vec};

Expand Down Expand Up @@ -142,13 +141,6 @@ pub trait Application: Default + Sized + 'static {
Self::Cmd::description()
}

/// Version of this application.
fn version(&self) -> Version {
Self::Cmd::version()
.parse::<Version>()
.unwrap_or_else(|e| fatal_error(self, &e))
}

/// Authors of this application.
fn authors(&self) -> Vec<String> {
Self::Cmd::authors().split(':').map(str::to_owned).collect()
Expand Down
3 changes: 0 additions & 3 deletions core/src/command.rs
Expand Up @@ -17,9 +17,6 @@ pub trait Command: Debug + Clap + Runnable {
/// Description of this program
fn description() -> &'static str;

/// Version of this program
fn version() -> &'static str;

/// Authors of this program
fn authors() -> &'static str;

Expand Down

0 comments on commit 7b2484e

Please sign in to comment.