From 9853a22025d1093e4bbd3d398568cd75ed29d149 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 17 Oct 2023 14:42:38 -0500 Subject: [PATCH] Add `ruff version` with long version display --- Cargo.lock | 146 ++++++++++++++++++++++++ crates/ruff_cli/Cargo.toml | 5 + crates/ruff_cli/build.rs | 3 + crates/ruff_cli/src/args.rs | 5 + crates/ruff_cli/src/commands/mod.rs | 1 + crates/ruff_cli/src/commands/version.rs | 82 +++++++++++++ crates/ruff_cli/src/lib.rs | 10 ++ 7 files changed, 252 insertions(+) create mode 100644 crates/ruff_cli/build.rs create mode 100644 crates/ruff_cli/src/commands/version.rs diff --git a/Cargo.lock b/Cargo.lock index 00d62d7baee88..c18b171b1a9e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -252,6 +252,7 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ + "jobserver", "libc", ] @@ -483,6 +484,32 @@ dependencies = [ "web-sys", ] +[[package]] +name = "const_fn" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" + +[[package]] +name = "const_format" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -894,6 +921,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "git2" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd" +dependencies = [ + "bitflags 2.4.0", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glob" version = "0.3.1" @@ -1143,6 +1183,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_debug" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06d198e9919d9822d5f7083ba8530e04de87841eaf21ead9af8f2304efd57c89" + [[package]] name = "itertools" version = "0.10.5" @@ -1167,6 +1213,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "jobserver" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.64" @@ -1291,6 +1346,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "libgit2-sys" +version = "0.16.1+1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2a2bb3680b094add03bb3732ec520ece34da31a8cd2d633d1389d0f0fb60d0c" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libmimalloc-sys" version = "0.1.35" @@ -1301,6 +1368,18 @@ dependencies = [ "libc", ] +[[package]] +name = "libz-sys" +version = "1.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -1472,6 +1551,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "number_prefix" version = "0.4.0" @@ -1688,6 +1776,12 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + [[package]] name = "pmutil" version = "0.5.3" @@ -2095,6 +2189,7 @@ dependencies = [ "rustc-hash", "serde", "serde_json", + "shadow-rs", "shellexpand", "similar", "strum", @@ -2755,6 +2850,19 @@ dependencies = [ "syn 2.0.38", ] +[[package]] +name = "shadow-rs" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9198caff1c94f1a5df6664bddbc379896b51b98a55b0b3fedcb23078fe00c77" +dependencies = [ + "const_format", + "git2", + "is_debug", + "time", + "tzdb", +] + [[package]] name = "sharded-slab" version = "0.1.4" @@ -3024,8 +3132,12 @@ version = "0.3.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" dependencies = [ + "itoa", + "libc", + "num_threads", "serde", "time-core", + "time-macros", ] [[package]] @@ -3034,6 +3146,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -3182,6 +3303,25 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" +[[package]] +name = "tz-rs" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33851b15c848fad2cf4b105c6bb66eb9512b6f6c44a4b13f57c53c73c707e2b4" +dependencies = [ + "const_fn", +] + +[[package]] +name = "tzdb" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec758958f2fb5069cd7fae385be95cc8eceb8cdfd270c7d14de6034f0108d99e" +dependencies = [ + "iana-time-zone", + "tz-rs", +] + [[package]] name = "unic-char-property" version = "0.9.0" @@ -3349,6 +3489,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/crates/ruff_cli/Cargo.toml b/crates/ruff_cli/Cargo.toml index d8f2b7d4d292b..a562818aabbec 100644 --- a/crates/ruff_cli/Cargo.toml +++ b/crates/ruff_cli/Cargo.toml @@ -10,6 +10,7 @@ documentation = { workspace = true } repository = { workspace = true } license = { workspace = true } readme = "../../README.md" +build = "build.rs" [[bin]] name = "ruff" @@ -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" } diff --git a/crates/ruff_cli/build.rs b/crates/ruff_cli/build.rs new file mode 100644 index 0000000000000..4a0dfc4591dbf --- /dev/null +++ b/crates/ruff_cli/build.rs @@ -0,0 +1,3 @@ +fn main() -> shadow_rs::SdResult<()> { + shadow_rs::new() +} diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 9732919997f14..f73abd333f9eb 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -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))] diff --git a/crates/ruff_cli/src/commands/mod.rs b/crates/ruff_cli/src/commands/mod.rs index 794a58788b352..554a7a454add2 100644 --- a/crates/ruff_cli/src/commands/mod.rs +++ b/crates/ruff_cli/src/commands/mod.rs @@ -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; diff --git a/crates/ruff_cli/src/commands/version.rs b/crates/ruff_cli/src/commands/version.rs new file mode 100644 index 0000000000000..5bed0d0fe3f0e --- /dev/null +++ b/crates/ruff_cli/src/commands/version.rs @@ -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(()) +} diff --git a/crates/ruff_cli/src/lib.rs b/crates/ruff_cli/src/lib.rs index 993f0f8771bf7..ea83853b35537 100644 --- a/crates/ruff_cli/src/lib.rs +++ b/crates/ruff_cli/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::print_stdout)] + use std::fs::File; use std::io::{self, stdout, BufWriter, Write}; use std::path::{Path, PathBuf}; @@ -28,6 +30,10 @@ mod printer; pub mod resolve; mod stdin; +use shadow_rs::shadow; + +shadow!(build); + #[derive(Copy, Clone)] pub enum ExitStatus { /// Linting was successful and there were no linting errors. @@ -134,6 +140,10 @@ pub fn run( set_up_logging(&log_level)?; match command { + Command::Version { output_format } => { + commands::version::version(output_format)?; + Ok(ExitStatus::Success) + } Command::Rule { rule, all, format } => { if all { commands::rule::rules(format)?;