Skip to content

Commit

Permalink
Merge pull request #190 from dtolnay/version
Browse files Browse the repository at this point in the history
Make prettyplease version appear only if `--version --verbose` is used
  • Loading branch information
dtolnay authored Aug 5, 2023
2 parents edebe7b + 828ea84 commit 4f52594
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
14 changes: 6 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=build.rs");

let mut version = env!("CARGO_PKG_VERSION").to_owned();
if let Ok(prettyplease_version) = env::var("DEP_PRETTYPLEASE02_VERSION") {
// TODO: Make this appear only if `--version --verbose` is used.
version.push_str(" + prettyplease ");
version.push_str(&prettyplease_version);
}
let prettyplease_version = match env::var("DEP_PRETTYPLEASE02_VERSION") {
Ok(prettyplease_version) => format!(r#"Some("{}")"#, prettyplease_version.escape_debug()),
Err(_) => "None".to_owned(),
};

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let version_file = out_dir.join("version");
fs::write(version_file, version).unwrap();
let prettyplease_version_file = out_dir.join("prettyplease.version");
fs::write(prettyplease_version_file, prettyplease_version).unwrap();
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ mod fmt;
mod manifest;
mod opts;
mod unparse;
mod version;

use crate::cmd::Line;
use crate::config::Config;
use crate::error::Result;
use crate::opts::Coloring::*;
use crate::opts::{Coloring, Expand, Subcommand};
use crate::unparse::unparse_maximal;
use crate::version::Version;
use bat::{PagingMode, PrettyPrinter};
use clap::{CommandFactory, Parser, ValueEnum};
use clap::{Parser, ValueEnum};
use quote::quote;
use std::env;
use std::ffi::OsString;
Expand Down Expand Up @@ -64,8 +66,10 @@ fn cargo_expand() -> Result<i32> {
let Subcommand::Expand(args) = Subcommand::parse();

if args.version {
let mut stdout = io::stdout();
let _ = stdout.write_all(Subcommand::command().render_version().as_bytes());
let version = Version {
verbose: args.verbose,
};
let _ = writeln!(io::stdout(), "{}", version);
return Ok(0);
}

Expand Down
6 changes: 2 additions & 4 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use std::path::PathBuf;
use std::str::FromStr;
use syn_select::Selector;

const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version"));

#[derive(Parser)]
#[command(bin_name = "cargo", version = VERSION, author, disable_help_subcommand = true)]
#[command(bin_name = "cargo", version, author, disable_help_subcommand = true)]
pub enum Subcommand {
/// Show the result of macro expansion.
#[command(name = "expand", version = VERSION, author, disable_version_flag = true)]
#[command(name = "expand", version, author, disable_version_flag = true)]
Expand(Expand),
}

Expand Down
23 changes: 23 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fmt::{self, Display};

const CARGO_EXPAND_VERSION: &str = env!("CARGO_PKG_VERSION");
const PRETTYPLEASE_VERSION: Option<&str> =
include!(concat!(env!("OUT_DIR"), "/prettyplease.version"));

pub(crate) struct Version {
pub verbose: bool,
}

impl Display for Version {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("cargo-expand ")?;
formatter.write_str(CARGO_EXPAND_VERSION)?;
if self.verbose {
if let Some(prettyplease_version) = PRETTYPLEASE_VERSION {
formatter.write_str(" + prettyplease ")?;
formatter.write_str(prettyplease_version)?;
}
}
Ok(())
}
}

0 comments on commit 4f52594

Please sign in to comment.