4 use super::verify_inside_clippy_dir;
6 const VSCODE_DIR: &str = ".vscode";
7 const TASK_SOURCE_FILE: &str = "util/etc/vscode-tasks.json";
8 const TASK_TARGET_FILE: &str = ".vscode/tasks.json";
10 pub fn install_tasks(force_override: bool) {
11 if !check_install_precondition(force_override) {
15 match fs::copy(TASK_SOURCE_FILE, TASK_TARGET_FILE) {
17 println!("info: the task file can be removed with `cargo dev remove vscode-tasks`");
18 println!("vscode tasks successfully installed");
20 Err(err) => eprintln!(
21 "error: unable to copy `{}` to `{}` ({})",
22 TASK_SOURCE_FILE, TASK_TARGET_FILE, err
27 fn check_install_precondition(force_override: bool) -> bool {
28 if !verify_inside_clippy_dir() {
32 let vs_dir_path = Path::new(VSCODE_DIR);
33 if vs_dir_path.exists() {
34 // verify the target will be valid
35 if !vs_dir_path.is_dir() {
36 eprintln!("error: the `.vscode` path exists but seems to be a file");
40 // make sure that we don't override any existing tasks by accident
41 let path = Path::new(TASK_TARGET_FILE);
44 return delete_vs_task_file(path);
48 "error: there is already a `task.json` file inside the `{}` directory",
51 println!("info: use the `--force-override` flag to override the existing `task.json` file");
55 match fs::create_dir(vs_dir_path) {
57 println!("info: created `{}` directory for clippy", VSCODE_DIR);
61 "error: the task target directory `{}` could not be created ({})",
71 pub fn remove_tasks() {
72 let path = Path::new(TASK_TARGET_FILE);
74 if delete_vs_task_file(path) {
75 try_delete_vs_directory_if_empty();
76 println!("vscode tasks successfully removed");
79 println!("no vscode tasks were found");
83 fn delete_vs_task_file(path: &Path) -> bool {
84 if let Err(err) = fs::remove_file(path) {
85 eprintln!("error: unable to delete the existing `tasks.json` file ({})", err);
92 /// This function will try to delete the `.vscode` directory if it's empty.
93 /// It may fail silently.
94 fn try_delete_vs_directory_if_empty() {
95 let path = Path::new(VSCODE_DIR);
96 if path.read_dir().map_or(false, |mut iter| iter.next().is_none()) {
97 // The directory is empty. We just try to delete it but allow a silence
98 // fail as an empty `.vscode` directory is still valid
99 let _silence_result = fs::remove_dir(path);
101 // The directory is not empty or could not be read. Either way don't take
102 // any further actions