Skip to content

Commit

Permalink
Add cargo dev setup toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Apr 16, 2024
1 parent f5e2501 commit ff9014c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 15 deletions.
55 changes: 40 additions & 15 deletions clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ fn main() {
}
},
Some(("setup", sub_command)) => match sub_command.subcommand() {
Some(("git-hook", matches)) => {
if matches.get_flag("remove") {
setup::git_hook::remove_hook();
} else {
setup::git_hook::install_hook(matches.get_flag("force-override"));
}
},
Some(("intellij", matches)) => {
if matches.get_flag("remove") {
setup::intellij::remove_rustc_src();
Expand All @@ -57,12 +64,12 @@ fn main() {
);
}
},
Some(("git-hook", matches)) => {
if matches.get_flag("remove") {
setup::git_hook::remove_hook();
} else {
setup::git_hook::install_hook(matches.get_flag("force-override"));
}
Some(("toolchain", matches)) => {
setup::toolchain::create(
matches.get_flag("force"),
matches.get_flag("release"),
&matches.get_one::<String>("name").unwrap(),
);
},
Some(("vscode-tasks", matches)) => {
if matches.get_flag("remove") {
Expand Down Expand Up @@ -210,6 +217,19 @@ fn get_clap_config() -> ArgMatches {
.about("Support for setting up your personal development environment")
.arg_required_else_help(true)
.subcommands([
Command::new("git-hook")
.about("Add a pre-commit git hook that formats your code to make it look pretty")
.args([
Arg::new("remove")
.long("remove")
.action(ArgAction::SetTrue)
.help("Remove the pre-commit hook added with 'cargo dev setup git-hook'"),
Arg::new("force-override")
.long("force-override")
.short('f')
.action(ArgAction::SetTrue)
.help("Forces the override of an existing git pre-commit hook"),
]),
Command::new("intellij")
.about("Alter dependencies so Intellij Rust can find rustc internals")
.args([
Expand All @@ -225,18 +245,23 @@ fn get_clap_config() -> ArgMatches {
.conflicts_with("remove")
.required(true),
]),
Command::new("git-hook")
.about("Add a pre-commit git hook that formats your code to make it look pretty")
Command::new("toolchain")
.about("Install a rustup toolchain pointing to the local clippy build")
.args([
Arg::new("remove")
.long("remove")
.action(ArgAction::SetTrue)
.help("Remove the pre-commit hook added with 'cargo dev setup git-hook'"),
Arg::new("force-override")
.long("force-override")
Arg::new("force")
.long("force")
.short('f')
.action(ArgAction::SetTrue)
.help("Forces the override of an existing git pre-commit hook"),
.help("Override an existing toolchain"),
Arg::new("release")
.long("release")
.short('r')
.action(ArgAction::SetTrue)
.help("Point to --release clippy binaries"),
Arg::new("name")
.long("name")
.default_value("clippy")
.help("The name of the created toolchain"),
]),
Command::new("vscode-tasks")
.about("Add several tasks to vscode for formatting, validation and testing")
Expand Down
1 change: 1 addition & 0 deletions clippy_dev/src/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod git_hook;
pub mod intellij;
pub mod toolchain;
pub mod vscode;

use std::path::Path;
Expand Down
75 changes: 75 additions & 0 deletions clippy_dev/src/setup/toolchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::env::consts::EXE_SUFFIX;
use std::env::current_dir;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

use super::verify_inside_clippy_dir;

pub fn create(force: bool, release: bool, name: &str) {
if !verify_inside_clippy_dir() {
return;
}

let rustup_home = std::env::var("RUSTUP_HOME").unwrap();
let toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap();

let src = PathBuf::from_iter([&rustup_home, "toolchains", &toolchain]);
let dest = PathBuf::from_iter([&rustup_home, "toolchains", name]);

if dest.exists() {
if force {
fs::remove_dir_all(&dest).unwrap();
} else {
println!("{} already exists, pass `--force` to override it", dest.display());
return;
}
}

for entry in WalkDir::new(&src) {
let entry = entry.unwrap();
let relative = entry.path().strip_prefix(&src).unwrap();

if relative.starts_with("bin")
&& matches!(
relative.file_stem().and_then(OsStr::to_str),
Some("cargo-clippy" | "clippy-driver")
)
{
continue;
}

let target = dest.join(relative);
if entry.file_type().is_dir() {
fs::create_dir(&target).unwrap();
} else {
fs::hard_link(entry.path(), target).unwrap();
}
}

symlink_bin("cargo-clippy", &dest, release);
symlink_bin("clippy-driver", &dest, release);

println!("Created toolchain {name}, use it in other projects with e.g. `cargo +{name} clippy`");
println!("Note: This will need to be re-run whenever the Clippy `rust-toolchain` changes")
}

fn symlink_bin(bin: &str, dest: &Path, release: bool) {
#[cfg(windows)]
use std::os::windows::fs::symlink_file as symlink;

#[cfg(not(windows))]
use std::os::unix::fs::symlink;

let profile = if release { "release" } else { "debug" };
let file_name = format!("{bin}{EXE_SUFFIX}");

let mut src = current_dir().unwrap();
src.extend(["target", profile, &file_name]);

let mut dest = dest.to_path_buf();
dest.extend(["bin", &file_name]);

symlink(src, dest).unwrap();
}

0 comments on commit ff9014c

Please sign in to comment.