Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a19d69d

Browse files
authored
Sync and Release automation (rust-lang#13694)
Based on rust-lang/rust-clippy#13693 Adds 2 subcommands to `cargo dev`: - `cargo dev sync update_nightly`: Which updates the nightly versions in `rust-toolchain` and `clippy_utils/README.md` - `cargo dev release bump_version`: Bumps the version in all relevant `Cargo.toml` files Those are pulled out of rust-lang/rust-clippy#12759, which I'll rebase on this. Next step is to update the documentation, which I'll partly pull out of rust-lang/rust-clippy#12762 r? @blyxyas (as you reviewed the first PR in the chain and were assigned to the second one) cc rust-lang/rust-clippy#13556 changelog: none
2 parents cfd17d4 + 93d5ccd commit a19d69d

File tree

16 files changed

+263
-154
lines changed

16 files changed

+263
-154
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[package]
22
name = "clippy"
3+
# begin autogenerated version
34
version = "0.1.84"
5+
# end autogenerated version
46
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
57
repository = "https://github.com/rust-lang/rust-clippy"
68
readme = "README.md"

clippy_config/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[package]
22
name = "clippy_config"
3+
# begin autogenerated version
34
version = "0.1.84"
5+
# end autogenerated version
46
edition = "2021"
57
publish = false
68

clippy_dev/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
aho-corasick = "1.0"
9+
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
910
clap = { version = "4.4", features = ["derive"] }
1011
indoc = "1.0"
1112
itertools = "0.12"

clippy_dev/src/dogfood.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{clippy_project_root, exit_if_err};
1+
use crate::utils::{clippy_project_root, exit_if_err};
22
use std::process::Command;
33

44
/// # Panics

clippy_dev/src/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clippy_project_root;
1+
use crate::utils::clippy_project_root;
22
use itertools::Itertools;
33
use rustc_lexer::{TokenKind, tokenize};
44
use shell_escape::escape;

clippy_dev/src/lib.rs

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,13 @@
1414
extern crate rustc_driver;
1515
extern crate rustc_lexer;
1616

17-
use std::io;
18-
use std::path::PathBuf;
19-
use std::process::{self, ExitStatus};
20-
2117
pub mod dogfood;
2218
pub mod fmt;
2319
pub mod lint;
2420
pub mod new_lint;
21+
pub mod release;
2522
pub mod serve;
2623
pub mod setup;
24+
pub mod sync;
2725
pub mod update_lints;
28-
29-
#[cfg(not(windows))]
30-
static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
31-
#[cfg(windows)]
32-
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
33-
34-
/// Returns the path to the `cargo-clippy` binary
35-
///
36-
/// # Panics
37-
///
38-
/// Panics if the path of current executable could not be retrieved.
39-
#[must_use]
40-
pub fn cargo_clippy_path() -> PathBuf {
41-
let mut path = std::env::current_exe().expect("failed to get current executable name");
42-
path.set_file_name(CARGO_CLIPPY_EXE);
43-
path
44-
}
45-
46-
/// Returns the path to the Clippy project directory
47-
///
48-
/// # Panics
49-
///
50-
/// Panics if the current directory could not be retrieved, there was an error reading any of the
51-
/// Cargo.toml files or ancestor directory is the clippy root directory
52-
#[must_use]
53-
pub fn clippy_project_root() -> PathBuf {
54-
let current_dir = std::env::current_dir().unwrap();
55-
for path in current_dir.ancestors() {
56-
let result = std::fs::read_to_string(path.join("Cargo.toml"));
57-
if let Err(err) = &result {
58-
if err.kind() == io::ErrorKind::NotFound {
59-
continue;
60-
}
61-
}
62-
63-
let content = result.unwrap();
64-
if content.contains("[package]\nname = \"clippy\"") {
65-
return path.to_path_buf();
66-
}
67-
}
68-
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
69-
}
70-
71-
/// # Panics
72-
/// Panics if given command result was failed.
73-
pub fn exit_if_err(status: io::Result<ExitStatus>) {
74-
match status.expect("failed to run command").code() {
75-
Some(0) => {},
76-
Some(n) => process::exit(n),
77-
None => {
78-
eprintln!("Killed by signal");
79-
process::exit(1);
80-
},
81-
}
82-
}
26+
pub mod utils;

clippy_dev/src/lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{cargo_clippy_path, exit_if_err};
1+
use crate::utils::{cargo_clippy_path, exit_if_err};
22
use std::process::{self, Command};
33
use std::{env, fs};
44

clippy_dev/src/main.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![warn(rust_2018_idioms, unused_lifetimes)]
44

55
use clap::{Args, Parser, Subcommand};
6-
use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, update_lints};
6+
use clippy_dev::{dogfood, fmt, lint, new_lint, release, serve, setup, sync, update_lints, utils};
77
use std::convert::Infallible;
88

99
fn main() {
@@ -23,9 +23,9 @@ fn main() {
2323
if print_only {
2424
update_lints::print_lints();
2525
} else if check {
26-
update_lints::update(update_lints::UpdateMode::Check);
26+
update_lints::update(utils::UpdateMode::Check);
2727
} else {
28-
update_lints::update(update_lints::UpdateMode::Change);
28+
update_lints::update(utils::UpdateMode::Change);
2929
}
3030
},
3131
DevCommand::NewLint {
@@ -35,7 +35,7 @@ fn main() {
3535
r#type,
3636
msrv,
3737
} => match new_lint::create(&pass, &name, &category, r#type.as_deref(), msrv) {
38-
Ok(()) => update_lints::update(update_lints::UpdateMode::Change),
38+
Ok(()) => update_lints::update(utils::UpdateMode::Change),
3939
Err(e) => eprintln!("Unable to create lint: {e}"),
4040
},
4141
DevCommand::Setup(SetupCommand { subcommand }) => match subcommand {
@@ -75,6 +75,12 @@ fn main() {
7575
uplift,
7676
} => update_lints::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
7777
DevCommand::Deprecate { name, reason } => update_lints::deprecate(&name, &reason),
78+
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
79+
SyncSubcommand::UpdateNightly => sync::update_nightly(),
80+
},
81+
DevCommand::Release(ReleaseCommand { subcommand }) => match subcommand {
82+
ReleaseSubcommand::BumpVersion => release::bump_version(),
83+
},
7884
}
7985
}
8086

@@ -225,6 +231,10 @@ enum DevCommand {
225231
/// The reason for deprecation
226232
reason: String,
227233
},
234+
/// Sync between the rust repo and the Clippy repo
235+
Sync(SyncCommand),
236+
/// Manage Clippy releases
237+
Release(ReleaseCommand),
228238
}
229239

230240
#[derive(Args)]
@@ -291,3 +301,29 @@ enum RemoveSubcommand {
291301
/// Remove the tasks added with 'cargo dev setup vscode-tasks'
292302
VscodeTasks,
293303
}
304+
305+
#[derive(Args)]
306+
struct SyncCommand {
307+
#[command(subcommand)]
308+
subcommand: SyncSubcommand,
309+
}
310+
311+
#[derive(Subcommand)]
312+
enum SyncSubcommand {
313+
#[command(name = "update_nightly")]
314+
/// Update nightly version in rust-toolchain and `clippy_utils`
315+
UpdateNightly,
316+
}
317+
318+
#[derive(Args)]
319+
struct ReleaseCommand {
320+
#[command(subcommand)]
321+
subcommand: ReleaseSubcommand,
322+
}
323+
324+
#[derive(Subcommand)]
325+
enum ReleaseSubcommand {
326+
#[command(name = "bump_version")]
327+
/// Bump the version in the Cargo.toml files
328+
BumpVersion,
329+
}

clippy_dev/src/new_lint.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clippy_project_root;
1+
use crate::utils::{clippy_project_root, clippy_version};
22
use indoc::{formatdoc, writedoc};
33
use std::fmt;
44
use std::fmt::Write as _;
@@ -186,23 +186,8 @@ fn to_camel_case(name: &str) -> String {
186186
}
187187

188188
pub(crate) fn get_stabilization_version() -> String {
189-
fn parse_manifest(contents: &str) -> Option<String> {
190-
let version = contents
191-
.lines()
192-
.filter_map(|l| l.split_once('='))
193-
.find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
194-
let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
195-
return None;
196-
};
197-
let (minor, patch) = version.split_once('.')?;
198-
Some(format!(
199-
"{}.{}.0",
200-
minor.parse::<u32>().ok()?,
201-
patch.parse::<u32>().ok()?
202-
))
203-
}
204-
let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
205-
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
189+
let (minor, patch) = clippy_version();
190+
format!("{minor}.{patch}.0")
206191
}
207192

208193
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {

clippy_dev/src/release.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::fmt::Write;
2+
use std::path::Path;
3+
4+
use crate::utils::{UpdateMode, clippy_version, replace_region_in_file};
5+
6+
const CARGO_TOML_FILES: [&str; 4] = [
7+
"clippy_config/Cargo.toml",
8+
"clippy_lints/Cargo.toml",
9+
"clippy_utils/Cargo.toml",
10+
"Cargo.toml",
11+
];
12+
13+
pub fn bump_version() {
14+
let (minor, mut patch) = clippy_version();
15+
patch += 1;
16+
for file in &CARGO_TOML_FILES {
17+
replace_region_in_file(
18+
UpdateMode::Change,
19+
Path::new(file),
20+
"# begin autogenerated version\n",
21+
"# end autogenerated version",
22+
|res| {
23+
writeln!(res, "version = \"0.{minor}.{patch}\"").unwrap();
24+
},
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)