-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Added cargo dev setup vscode-tasks
for simplicity
#7409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,23 @@ | ||
pub mod git_hook; | ||
pub mod intellij; | ||
pub mod vscode; | ||
|
||
use std::path::Path; | ||
|
||
const CLIPPY_DEV_DIR: &str = "clippy_dev"; | ||
|
||
/// This function verifies that the tool is being executed in the clippy directory. | ||
/// This is useful to ensure that setups only modify Clippys resources. The verification | ||
/// is done by checking that `clippy_dev` is a sub directory of the current directory. | ||
/// | ||
/// It will print an error message and return `false` if the directory could not be | ||
/// verified. | ||
fn verify_inside_clippy_dir() -> bool { | ||
let path = Path::new(CLIPPY_DEV_DIR); | ||
if path.exists() && path.is_dir() { | ||
true | ||
} else { | ||
eprintln!("error: unable to verify that the working directory is clippys directory"); | ||
false | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
use super::verify_inside_clippy_dir; | ||
|
||
const VSCODE_DIR: &str = ".vscode"; | ||
const TASK_SOURCE_FILE: &str = "util/etc/vscode-tasks.json"; | ||
const TASK_TARGET_FILE: &str = ".vscode/tasks.json"; | ||
|
||
pub fn install_tasks(force_override: bool) { | ||
if !check_install_precondition(force_override) { | ||
return; | ||
} | ||
|
||
match fs::copy(TASK_SOURCE_FILE, TASK_TARGET_FILE) { | ||
Ok(_) => { | ||
println!("info: the task file can be removed with `cargo dev remove vscode-tasks`"); | ||
println!("vscode tasks successfully installed"); | ||
}, | ||
Err(err) => eprintln!( | ||
"error: unable to copy `{}` to `{}` ({})", | ||
TASK_SOURCE_FILE, TASK_TARGET_FILE, err | ||
), | ||
} | ||
} | ||
|
||
fn check_install_precondition(force_override: bool) -> bool { | ||
if !verify_inside_clippy_dir() { | ||
return false; | ||
} | ||
|
||
let vs_dir_path = Path::new(VSCODE_DIR); | ||
if vs_dir_path.exists() { | ||
// verify the target will be valid | ||
if !vs_dir_path.is_dir() { | ||
eprintln!("error: the `.vscode` path exists but seems to be a file"); | ||
return false; | ||
} | ||
|
||
// make sure that we don't override any existing tasks by accident | ||
let path = Path::new(TASK_TARGET_FILE); | ||
if path.exists() { | ||
if force_override { | ||
return delete_vs_task_file(path); | ||
} | ||
|
||
eprintln!( | ||
"error: there is already a `task.json` file inside the `{}` directory", | ||
VSCODE_DIR | ||
); | ||
println!("info: use the `--force-override` flag to override the existing `task.json` file"); | ||
return false; | ||
} | ||
} else { | ||
match fs::create_dir(vs_dir_path) { | ||
Ok(_) => { | ||
println!("info: created `{}` directory for clippy", VSCODE_DIR); | ||
}, | ||
Err(err) => { | ||
eprintln!( | ||
"error: the task target directory `{}` could not be created ({})", | ||
VSCODE_DIR, err | ||
); | ||
}, | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
pub fn remove_tasks() { | ||
let path = Path::new(TASK_TARGET_FILE); | ||
if path.exists() { | ||
if delete_vs_task_file(path) { | ||
try_delete_vs_directory_if_empty(); | ||
println!("vscode tasks successfully removed"); | ||
} | ||
} else { | ||
println!("no vscode tasks were found"); | ||
} | ||
} | ||
|
||
fn delete_vs_task_file(path: &Path) -> bool { | ||
if let Err(err) = fs::remove_file(path) { | ||
eprintln!("error: unable to delete the existing `tasks.json` file ({})", err); | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
/// This function will try to delete the `.vscode` directory if it's empty. | ||
/// It may fail silently. | ||
fn try_delete_vs_directory_if_empty() { | ||
let path = Path::new(VSCODE_DIR); | ||
if path.read_dir().map_or(false, |mut iter| iter.next().is_none()) { | ||
// The directory is empty. We just try to delete it but allow a silence | ||
// fail as an empty `.vscode` directory is still valid | ||
let _silence_result = fs::remove_dir(path); | ||
} else { | ||
// The directory is not empty or could not be read. Either way don't take | ||
// any further actions | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "cargo check", | ||
"type": "shell", | ||
"command": "cargo check", | ||
"problemMatcher": [], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true, | ||
}, | ||
}, | ||
{ | ||
"label": "cargo fmt", | ||
"type": "shell", | ||
"command": "cargo dev fmt", | ||
"problemMatcher": [], | ||
"group": "none", | ||
}, | ||
{ | ||
"label": "cargo uitest", | ||
"type": "shell", | ||
"command": "cargo uitest", | ||
"options": { | ||
"env": { | ||
"RUST_BACKTRACE": "1", | ||
// This task will usually execute all UI tests inside `tests/ui` you can | ||
// optionally uncomment the line below and only run a specific test. | ||
// | ||
// See: https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md#testing | ||
// | ||
// "TESTNAME": "<TODO>", | ||
}, | ||
}, | ||
"problemMatcher": [], | ||
"group": { | ||
"kind": "test", | ||
"isDefault": true, | ||
} | ||
}, | ||
{ | ||
"label": "cargo test", | ||
"type": "shell", | ||
"command": "cargo test", | ||
"problemMatcher": [], | ||
"group": "test", | ||
}, | ||
{ | ||
"label": "cargo dev bless", | ||
"type": "shell", | ||
"command": "cargo dev bless", | ||
"problemMatcher": [], | ||
"group": "none", | ||
}, | ||
], | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.