Skip to content

Commit f0fa363

Browse files
xFrednetflip1995
authored andcommitted
Added cargo dev remove intellij
1 parent 8fdf289 commit f0fa363

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

clippy_dev/src/main.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ fn main() {
4545
("git-hook", Some(matches)) => setup::git_hook::install_hook(matches.is_present("force-override")),
4646
_ => {},
4747
},
48-
("remove", Some(sub_command)) => {
49-
if let ("git-hook", Some(_)) = sub_command.subcommand() {
50-
setup::git_hook::remove_hook();
51-
}
48+
("remove", Some(sub_command)) => match sub_command.subcommand() {
49+
("git-hook", Some(_)) => setup::git_hook::remove_hook(),
50+
("intellij", Some(_)) => setup::intellij::remove_rustc_src(),
51+
_ => {},
5252
},
5353
("serve", Some(matches)) => {
5454
let port = matches.value_of("port").unwrap().parse().unwrap();
@@ -186,7 +186,11 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
186186
SubCommand::with_name("remove")
187187
.about("Support for undoing changes done by the setup command")
188188
.setting(AppSettings::ArgRequiredElseHelp)
189-
.subcommand(SubCommand::with_name("git-hook").about("Remove any existing pre-commit git hook")),
189+
.subcommand(SubCommand::with_name("git-hook").about("Remove any existing pre-commit git hook"))
190+
.subcommand(
191+
SubCommand::with_name("intellij")
192+
.about("Removes rustc source paths added via `cargo dev setup intellij`"),
193+
),
190194
)
191195
.subcommand(
192196
SubCommand::with_name("serve")

clippy_dev/src/setup/intellij.rs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::path::{Path, PathBuf};
88
// This allows rust analyzer to analyze rustc internals and show proper information inside clippy
99
// code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
1010

11+
const RUSTC_PATH_SECTION: &str = "[target.'cfg(NOT_A_PLATFORM)'.dependencies]";
12+
const DEPENDENCIES_SECTION: &str = "[dependencies]";
13+
1114
const CLIPPY_PROJECTS: &[ClippyProjectInfo] = &[
1215
ClippyProjectInfo::new("root", "Cargo.toml", "src/driver.rs"),
1316
ClippyProjectInfo::new("clippy_lints", "clippy_lints/Cargo.toml", "clippy_lints/src/lib.rs"),
@@ -43,6 +46,8 @@ pub fn setup_rustc_src(rustc_path: &str) {
4346
return;
4447
}
4548
}
49+
50+
println!("info: the source paths can be removed again with `cargo dev remove intellij`");
4651
}
4752

4853
fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
@@ -51,26 +56,26 @@ fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
5156
if path.is_relative() {
5257
match path.canonicalize() {
5358
Ok(absolute_path) => {
54-
println!("note: the rustc path was resolved to: `{}`", absolute_path.display());
59+
println!("info: the rustc path was resolved to: `{}`", absolute_path.display());
5560
path = absolute_path;
5661
},
5762
Err(err) => {
58-
println!("error: unable to get the absolute path of rustc ({})", err);
63+
eprintln!("error: unable to get the absolute path of rustc ({})", err);
5964
return Err(());
6065
},
6166
};
6267
}
6368

6469
let path = path.join("compiler");
65-
println!("note: looking for compiler sources at: {}", path.display());
70+
println!("info: looking for compiler sources at: {}", path.display());
6671

6772
if !path.exists() {
68-
println!("error: the given path does not exist");
73+
eprintln!("error: the given path does not exist");
6974
return Err(());
7075
}
7176

7277
if !path.is_dir() {
73-
println!("error: the given path is a file and not a directory");
78+
eprintln!("error: the given path is a file and not a directory");
7479
return Err(());
7580
}
7681

@@ -82,7 +87,7 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
8287
let lib_content = read_project_file(project.lib_rs_file, "lib.rs", project.name)?;
8388

8489
if inject_deps_into_manifest(rustc_source_dir, project.cargo_file, &cargo_content, &lib_content).is_err() {
85-
println!(
90+
eprintln!(
8691
"error: unable to inject dependencies into {} with the Cargo file {}",
8792
project.name, project.cargo_file
8893
);
@@ -98,7 +103,7 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
98103
fn read_project_file(file_path: &str, file_name: &str, project: &str) -> Result<String, ()> {
99104
let path = Path::new(file_path);
100105
if !path.exists() {
101-
println!(
106+
eprintln!(
102107
"error: unable to find the `{}` file for the project {}",
103108
file_name, project
104109
);
@@ -123,19 +128,19 @@ fn inject_deps_into_manifest(
123128
cargo_toml: &str,
124129
lib_rs: &str,
125130
) -> std::io::Result<()> {
126-
// do not inject deps if we have aleady done so
127-
if cargo_toml.contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]") {
131+
// do not inject deps if we have already done so
132+
if cargo_toml.contains(RUSTC_PATH_SECTION) {
128133
eprintln!(
129-
"warn: dependencies are already setup inside {}, skipping file.",
134+
"warn: dependencies are already setup inside {}, skipping file",
130135
manifest_path
131136
);
132137
return Ok(());
133138
}
134139

135140
let extern_crates = lib_rs
136141
.lines()
137-
// get the deps
138-
.filter(|line| line.starts_with("extern crate"))
142+
// only take dependencies starting with `rustc_`
143+
.filter(|line| line.starts_with("extern crate rustc_"))
139144
// we have something like "extern crate foo;", we only care about the "foo"
140145
// ↓ ↓
141146
// extern crate rustc_middle;
@@ -168,7 +173,56 @@ fn inject_deps_into_manifest(
168173
let mut file = File::create(manifest_path)?;
169174
file.write_all(new_manifest.as_bytes())?;
170175

171-
println!("note: successfully setup dependencies inside {}", manifest_path);
176+
println!("info: successfully setup dependencies inside {}", manifest_path);
172177

173178
Ok(())
174179
}
180+
181+
pub fn remove_rustc_src() {
182+
for project in CLIPPY_PROJECTS {
183+
// We don't care about the result here as we want to go through all
184+
// dependencies either way. Any info and error message will be issued by
185+
// the removal code itself.
186+
let _ = remove_rustc_src_from_project(project);
187+
}
188+
}
189+
190+
fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> Result<(), ()> {
191+
let mut cargo_content = read_project_file(project.cargo_file, "Cargo.toml", project.name)?;
192+
let section_start = if let Some(section_start) = cargo_content.find(RUSTC_PATH_SECTION) {
193+
section_start
194+
} else {
195+
println!(
196+
"info: dependencies could not be found in `{}` for {}, skipping file",
197+
project.cargo_file, project.name
198+
);
199+
return Ok(());
200+
};
201+
202+
let end_point = if let Some(end_point) = cargo_content.find(DEPENDENCIES_SECTION) {
203+
end_point
204+
} else {
205+
eprintln!(
206+
"error: the end of the rustc dependencies section could not be found in `{}`",
207+
project.cargo_file
208+
);
209+
return Err(());
210+
};
211+
212+
cargo_content.replace_range(section_start..end_point, "");
213+
214+
match File::create(project.cargo_file) {
215+
Ok(mut file) => {
216+
file.write_all(cargo_content.as_bytes()).unwrap();
217+
println!("info: successfully removed dependencies inside {}", project.cargo_file);
218+
Ok(())
219+
},
220+
Err(err) => {
221+
eprintln!(
222+
"error: unable to open file `{}` to remove rustc dependencies for {} ({})",
223+
project.cargo_file, project.name, err
224+
);
225+
Err(())
226+
},
227+
}
228+
}

0 commit comments

Comments
 (0)