Skip to content

Commit

Permalink
Add ruff version with long version display
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 18, 2023
1 parent 88c0106 commit 9853a22
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 0 deletions.
146 changes: 146 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/ruff_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ documentation = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
readme = "../../README.md"
build = "build.rs"

[[bin]]
name = "ruff"
Expand Down Expand Up @@ -61,6 +62,10 @@ thiserror = { workspace = true }
tracing = { workspace = true, features = ["log"] }
walkdir = { version = "2.3.2" }
wild = { version = "2" }
shadow-rs = { version = "0.24.1" }

[build-dependencies]
shadow-rs = { version = "0.24.1" }

[dev-dependencies]
assert_cmd = { version = "2.0.8" }
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::new()
}
5 changes: 5 additions & 0 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub struct Args {
pub enum Command {
/// Run Ruff on the given files or directories (default).
Check(CheckCommand),
/// Display full Ruff version information
Version {
#[arg(long, value_enum, default_value = "text")]
output_format: HelpFormat,
},
/// Explain a rule (or all rules).
#[clap(alias = "--explain")]
#[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))]
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pub(crate) mod linter;
pub(crate) mod rule;
pub(crate) mod show_files;
pub(crate) mod show_settings;
pub(crate) mod version;
82 changes: 82 additions & 0 deletions crates/ruff_cli/src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::fmt::{Display, Formatter};
use std::io::{self, BufWriter, Write};

use anyhow::Result;
use serde::Serialize;

use crate::args::HelpFormat;
use crate::build;

#[derive(Serialize)]
struct VersionMetadata {
ruff_version: &'static str,
rust_version: &'static str,
build_time: &'static str,
cargo_version: &'static str,
build_os: &'static str,
commit: &'static str,
commit_time: &'static str,
target: &'static str,
dirty: bool,
release: bool,
}

impl VersionMetadata {
const fn from_build() -> Self {
Self {
ruff_version: build::PKG_VERSION,
rust_version: build::RUST_VERSION,
build_time: build::BUILD_TIME_3339,
cargo_version: build::CARGO_VERSION,
build_os: build::BUILD_OS,
target: build::BUILD_TARGET,
release: !build::TAG.is_empty(),
commit: build::SHORT_COMMIT,
commit_time: build::COMMIT_DATE_3339,
dirty: !build::GIT_CLEAN,
}
}
}

impl Display for VersionMetadata {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "ruff {}", self.ruff_version)
}
}

/// Display version information
pub(crate) fn version(output_format: HelpFormat) -> Result<()> {
let mut stdout = BufWriter::new(io::stdout().lock());
let metadata = VersionMetadata::from_build();

let build_date = chrono::DateTime::parse_from_rfc3339(metadata.build_time)?;

match output_format {
HelpFormat::Text => {
let status = if metadata.dirty {
"-dirty"
} else {
if metadata.release {
""
} else {
"-dev"
}
};

writeln!(
stdout,
"ruff {}{} ({} {})",
&metadata.ruff_version,
&status,
&metadata.commit,
build_date.format("%Y-%m-%d")
)?;
writeln!(stdout, "{}", &metadata.rust_version)?;
writeln!(stdout, "{}", &metadata.cargo_version)?;
}
HelpFormat::Json => {
serde_json::to_writer_pretty(stdout, &metadata)?;
}
};
Ok(())
}
Loading

0 comments on commit 9853a22

Please sign in to comment.