Skip to content

Commit b65796d

Browse files
committed
Add ruff version with long version display
1 parent 88c0106 commit b65796d

7 files changed

Lines changed: 251 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_cli/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ documentation = { workspace = true }
1010
repository = { workspace = true }
1111
license = { workspace = true }
1212
readme = "../../README.md"
13+
build = "build.rs"
1314

1415
[[bin]]
1516
name = "ruff"
@@ -61,6 +62,10 @@ thiserror = { workspace = true }
6162
tracing = { workspace = true, features = ["log"] }
6263
walkdir = { version = "2.3.2" }
6364
wild = { version = "2" }
65+
shadow-rs = { version = "0.24.1" }
66+
67+
[build-dependencies]
68+
shadow-rs = { version = "0.24.1" }
6469

6570
[dev-dependencies]
6671
assert_cmd = { version = "2.0.8" }

crates/ruff_cli/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() -> shadow_rs::SdResult<()> {
2+
shadow_rs::new()
3+
}

crates/ruff_cli/src/args.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ pub struct Args {
3535
pub enum Command {
3636
/// Run Ruff on the given files or directories (default).
3737
Check(CheckCommand),
38+
/// Display full Ruff version information
39+
Version {
40+
#[arg(long, value_enum, default_value = "text")]
41+
output_format: HelpFormat,
42+
},
3843
/// Explain a rule (or all rules).
3944
#[clap(alias = "--explain")]
4045
#[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))]

crates/ruff_cli/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pub(crate) mod linter;
99
pub(crate) mod rule;
1010
pub(crate) mod show_files;
1111
pub(crate) mod show_settings;
12+
pub(crate) mod version;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use chrono;
2+
use std::fmt::{Display, Formatter};
3+
use std::io::{self, BufWriter, Write};
4+
5+
use anyhow::Result;
6+
use serde::Serialize;
7+
8+
use crate::args::HelpFormat;
9+
use crate::build;
10+
11+
#[derive(Serialize)]
12+
struct VersionMetadata {
13+
ruff_version: &'static str,
14+
rust_version: &'static str,
15+
build_time: &'static str,
16+
cargo_version: &'static str,
17+
build_os: &'static str,
18+
commit: &'static str,
19+
commit_time: &'static str,
20+
target: &'static str,
21+
dirty: bool,
22+
release: bool,
23+
}
24+
25+
impl<'a> VersionMetadata {
26+
const fn from_build() -> Self {
27+
Self {
28+
ruff_version: build::PKG_VERSION,
29+
rust_version: build::RUST_VERSION,
30+
build_time: build::BUILD_TIME_3339,
31+
cargo_version: build::CARGO_VERSION,
32+
build_os: build::BUILD_OS,
33+
target: build::BUILD_TARGET,
34+
release: !build::TAG.is_empty(),
35+
commit: build::SHORT_COMMIT,
36+
commit_time: build::COMMIT_DATE_3339,
37+
dirty: !build::GIT_CLEAN,
38+
}
39+
}
40+
}
41+
42+
impl Display for VersionMetadata {
43+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
44+
write!(f, "ruff {}", self.ruff_version)
45+
}
46+
}
47+
48+
/// Display version information
49+
pub(crate) fn version(output_format: HelpFormat) -> Result<()> {
50+
let mut stdout = BufWriter::new(io::stdout().lock());
51+
let metadata = VersionMetadata::from_build();
52+
53+
let build_date = chrono::DateTime::parse_from_rfc3339(metadata.build_time)?;
54+
55+
match output_format {
56+
HelpFormat::Text => {
57+
let status = if metadata.dirty {
58+
"-dirty"
59+
} else {
60+
if !metadata.release {
61+
"-dev"
62+
} else {
63+
""
64+
}
65+
};
66+
67+
writeln!(
68+
stdout,
69+
"ruff {}{} ({} {})",
70+
&metadata.ruff_version,
71+
&status,
72+
&metadata.commit,
73+
build_date.format("%Y-%m-%d")
74+
)?;
75+
writeln!(stdout, "{}", &metadata.rust_version)?;
76+
writeln!(stdout, "{}", &metadata.cargo_version)?;
77+
}
78+
HelpFormat::Json => {
79+
serde_json::to_writer_pretty(stdout, &metadata)?;
80+
}
81+
};
82+
Ok(())
83+
}

crates/ruff_cli/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ mod printer;
2828
pub mod resolve;
2929
mod stdin;
3030

31+
use shadow_rs::shadow;
32+
33+
shadow!(build);
34+
3135
#[derive(Copy, Clone)]
3236
pub enum ExitStatus {
3337
/// Linting was successful and there were no linting errors.
@@ -134,6 +138,10 @@ pub fn run(
134138
set_up_logging(&log_level)?;
135139

136140
match command {
141+
Command::Version { output_format } => {
142+
commands::version::version(output_format)?;
143+
Ok(ExitStatus::Success)
144+
}
137145
Command::Rule { rule, all, format } => {
138146
if all {
139147
commands::rule::rules(format)?;

0 commit comments

Comments
 (0)