Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Dec 29, 2024
1 parent dda470d commit 622997d
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 112 deletions.
6 changes: 3 additions & 3 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
edition = "2021"
max_width = 120
fn_call_width = 120
# edition = "2021"
# max_width = 120
# fn_call_width = 120
27 changes: 21 additions & 6 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,17 @@ pub fn history_file_path() -> Option<(PathBuf, String)> {
Ok(_) => {
// When testing
let cwd = env::current_dir().unwrap();
Some((cwd.join(PathBuf::from("test_data/history")), HISTORY_FILE_NAME.to_string()))
}
_ => {
home_dir().map(|home_dir| (home_dir.join(PathBuf::from(".config/fzf-make")), HISTORY_FILE_NAME.to_string()))
Some((
cwd.join(PathBuf::from("test_data/history")),
HISTORY_FILE_NAME.to_string(),
))
}
_ => home_dir().map(|home_dir| {
(
home_dir.join(PathBuf::from(".config/fzf-make")),
HISTORY_FILE_NAME.to_string(),
)
}),
}
}

Expand All @@ -155,7 +161,11 @@ pub fn create_or_update_history_file(
fs::create_dir_all(history_directory_path.clone())?;
}
let mut history_file = File::create(history_directory_path.join(history_file_name))?;
history_file.write_all(toml::to_string(&Histories::from(new_history)).unwrap().as_bytes())?;
history_file.write_all(
toml::to_string(&Histories::from(new_history))
.unwrap()
.as_bytes(),
)?;
history_file.flush()?;

Ok(())
Expand Down Expand Up @@ -262,7 +272,12 @@ args = "app1 build"

for case in cases {
match case.expect {
Ok(v) => assert_eq!(v, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,),
Ok(v) => assert_eq!(
v,
parse_history(case.content).unwrap(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
Err(e) => assert_eq!(
e.to_string(),
parse_history(case.content).unwrap_err().to_string(),
Expand Down
29 changes: 24 additions & 5 deletions src/file/toml_old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ impl Histories {
for h in self.history.clone() {
let mut commands: Vec<fzf_make_toml::HistoryCommand> = vec![];
for c in h.executed_targets {
commands.push(fzf_make_toml::HistoryCommand::new(model::runner_type::RunnerType::Make, c));
commands.push(fzf_make_toml::HistoryCommand::new(
model::runner_type::RunnerType::Make,
c,
));
}
// NOTE: In old format, the path includes the file name but new format does not.
let mut makefile_path = PathBuf::from(h.path);
Expand Down Expand Up @@ -66,14 +69,25 @@ mod test {
after: fzf_make_toml::Histories::new(vec![fzf_make_toml::History::new(
PathBuf::from("path"),
vec![
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command1".to_string()),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command2".to_string()),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"command1".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"command2".to_string(),
),
],
)]),
}];

for case in cases {
assert_eq!(case.after, case.before.into_histories(), "\nFailed: 🚨{:?}🚨\n", case.title,)
assert_eq!(
case.after,
case.before.into_histories(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
}
}

Expand Down Expand Up @@ -131,7 +145,12 @@ executed-targets = ["run", "echo1"]

for case in cases {
match case.expect {
Ok(e) => assert_eq!(e, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,),
Ok(e) => assert_eq!(
e,
parse_history(case.content).unwrap(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
Err(err) => assert_eq!(
err.to_string(),
parse_history(case.content).unwrap_err().to_string(),
Expand Down
7 changes: 6 additions & 1 deletion src/model/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ pub struct Command {
}

impl Command {
pub fn new(runner_type: runner_type::RunnerType, args: String, file_path: PathBuf, line_number: u32) -> Self {
pub fn new(
runner_type: runner_type::RunnerType,
args: String,
file_path: PathBuf,
line_number: u32,
) -> Self {
Self {
runner_type,
args,
Expand Down
15 changes: 12 additions & 3 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ impl Histories {

// Update the whole histories.
let mut new_histories = self.histories.clone();
match new_histories.iter().position(|h| h.path == new_history.path) {
match new_histories
.iter()
.position(|h| h.path == new_history.path)
{
Some(index) => {
new_histories[index] = new_history;
}
Expand Down Expand Up @@ -196,7 +199,8 @@ mod test {
for case in cases {
assert_eq!(
case.after,
case.before.append(path_to_append.clone(), case.command_to_append),
case.before
.append(path_to_append.clone(), case.command_to_append),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
Expand Down Expand Up @@ -417,7 +421,12 @@ mod test {
];

for case in cases {
assert_eq!(case.after, case.before.append(case.command_to_append), "\nFailed: 🚨{:?}🚨\n", case.title,)
assert_eq!(
case.after,
case.before.append(case.command_to_append),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
}
}
}
7 changes: 5 additions & 2 deletions src/model/js_package_manager/js_package_manager_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ impl JsPackageManager {
if let Some(object) = v.as_object() {
for (k, v) in object {
let args = k.to_string();
let line_number = files.line_index(file, k.start() as u32).number().to_usize() as u32;
let line_number =
files.line_index(file, k.start() as u32).number().to_usize() as u32;
if let Some(v) = v.as_string() {
result.push((args, v.to_string(), line_number));
}
Expand All @@ -96,7 +97,9 @@ impl JsPackageManager {

pub fn get_js_package_manager_runner(current_dir: PathBuf) -> Option<JsPackageManager> {
let entries = fs::read_dir(current_dir.clone()).unwrap();
let file_names = entries.map(|e| e.unwrap().file_name().into_string().unwrap()).collect();
let file_names = entries
.map(|e| e.unwrap().file_name().into_string().unwrap())
.collect();

JsPackageManager::new(current_dir, file_names)
}
Expand Down
68 changes: 50 additions & 18 deletions src/model/js_package_manager/pnpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,30 @@ impl Pnpm {
}

pub fn new(current_dir: PathBuf, cwd_file_names: Vec<String>) -> Option<Pnpm> {
let package_json_exist = Iterator::find(&mut cwd_file_names.iter(), |&f| f == js::METADATA_FILE_NAME);
let lockfile_exist_in_current_dir = Iterator::find(&mut cwd_file_names.iter(), |&f| f == PNPM_LOCKFILE_NAME);
let package_json_exist =
Iterator::find(&mut cwd_file_names.iter(), |&f| f == js::METADATA_FILE_NAME);
let lockfile_exist_in_current_dir =
Iterator::find(&mut cwd_file_names.iter(), |&f| f == PNPM_LOCKFILE_NAME);
let lockfile_exist_in_ancestors =
file_util::find_file_in_ancestors(current_dir.clone(), vec![PNPM_LOCKFILE_NAME]);

match (package_json_exist, lockfile_exist_in_current_dir, lockfile_exist_in_ancestors) {
match (
package_json_exist,
lockfile_exist_in_current_dir,
lockfile_exist_in_ancestors,
) {
(None, _, _) => None,
(Some(_), Some(_), _) => Pnpm::collect_workspace_scripts(current_dir.clone()).map(|commands| Pnpm {
path: current_dir,
commands,
}),
(Some(_), None, Some(_)) => {
Self::collect_scripts_in_package_json(current_dir.clone()).map(|commands| Pnpm {
(Some(_), Some(_), _) => {
Pnpm::collect_workspace_scripts(current_dir.clone()).map(|commands| Pnpm {
path: current_dir,
commands,
})
}
(Some(_), None, Some(_)) => Self::collect_scripts_in_package_json(current_dir.clone())
.map(|commands| Pnpm {
path: current_dir,
commands,
}),
// Not a workspace children && not a workspace root
// In this case, package manager can not be determined.
(Some(_), None, None) => None,
Expand Down Expand Up @@ -96,7 +103,9 @@ impl Pnpm {
continue;
}
result.push(command::Command::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Pnpm),
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Pnpm,
),
// pnpm executes workspace script following format: `pnpm --filter {package_name} {script_name}`
// e.g. `pnpm --filter app4 build`
format!("--filter {} {}", name.clone(), key.as_str()),
Expand Down Expand Up @@ -127,7 +136,9 @@ impl Pnpm {
.filter(|(_, value, _)| !Self::use_filtering(value.to_string()))
.map(|(key, _value, line_number)| {
command::Command::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Pnpm),
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Pnpm,
),
key.to_string(),
current_dir.clone().join(js::METADATA_FILE_NAME),
*line_number,
Expand All @@ -153,7 +164,10 @@ impl Pnpm {

let output = String::from_utf8(output.stdout)?;
// split by newline to remove unnecessary lines.
let lines = output.split("\n").filter(|l| !l.is_empty()).collect::<Vec<&str>>();
let lines = output
.split("\n")
.filter(|l| !l.is_empty())
.collect::<Vec<&str>>();

Ok(lines
.iter()
Expand Down Expand Up @@ -193,16 +207,34 @@ mod test {
assert_eq!(true, Pnpm::use_filtering("pnpm -F app1".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm -F \"app1\"".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm --filter app2".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm -r --filter app3".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm -C packages/app3".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm --dir packages/app3".to_string()));
assert_eq!(
true,
Pnpm::use_filtering("pnpm -r --filter app3".to_string())
);
assert_eq!(
true,
Pnpm::use_filtering("pnpm -C packages/app3".to_string())
);
assert_eq!(
true,
Pnpm::use_filtering("pnpm --dir packages/app3".to_string())
);
assert_eq!(true, Pnpm::use_filtering("pnpm -F".to_string()));
assert_eq!(true, Pnpm::use_filtering("pnpm --filter".to_string()));
assert_eq!(false, Pnpm::use_filtering("pnpm -C packages/app1 run test".to_string()));
assert_eq!(false, Pnpm::use_filtering("pnpm --filter app1 run test".to_string()));
assert_eq!(
false,
Pnpm::use_filtering("pnpm -C packages/app1 run test".to_string())
);
assert_eq!(
false,
Pnpm::use_filtering("pnpm --filter app1 run test".to_string())
);
assert_eq!(false, Pnpm::use_filtering("yarn run".to_string()));
assert_eq!(false, Pnpm::use_filtering("pnpm run".to_string()));
assert_eq!(false, Pnpm::use_filtering("pnpm -r hoge".to_string()));
assert_eq!(false, Pnpm::use_filtering("yarn -r --filter app3".to_string()));
assert_eq!(
false,
Pnpm::use_filtering("yarn -r --filter app3".to_string())
);
}
}
12 changes: 9 additions & 3 deletions src/model/js_package_manager/yarn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ impl Yarn {
if let Ok(workspace_package_json_paths) = package_json_in_workspace {
for path in workspace_package_json_paths {
if let Ok(c) = path_to_content::path_to_content(&path) {
if let Some((name, parsing_result)) = js::JsPackageManager::parse_package_json(&c) {
if let Some((name, parsing_result)) =
js::JsPackageManager::parse_package_json(&c)
{
for (key, _, line_number) in parsing_result {
result.push(command::Command::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Yarn,
),
// yarn executes workspace script following format: `yarn workspace {package_name} {script_name}`
// e.g. `yarn workspace app4 build`
format!("workspace {} {}", name.clone(), key.as_str()),
Expand Down Expand Up @@ -164,7 +168,9 @@ impl Yarn {
.iter()
.map(|(key, _value, line_number)| {
command::Command::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Yarn,
),
key.to_string(),
current_dir.clone().join(js::METADATA_FILE_NAME),
*line_number,
Expand Down
22 changes: 14 additions & 8 deletions src/model/just/just_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ impl Just {
}

fn get_command(&self, command: command::Command) -> Option<command::Command> {
self.to_commands().iter().find(|c| **c == command).map(|_| command)
self.to_commands()
.iter()
.find(|c| **c == command)
.map(|_| command)
}

fn find_justfile(current_dir: PathBuf) -> Option<PathBuf> {
Expand Down Expand Up @@ -105,12 +108,14 @@ impl Just {
'recipe: for recipes_and_its_siblings in tree.root_node().named_children(&mut tree.walk()) {
if recipes_and_its_siblings.kind() == "recipe" {
let mut should_skip = false;
recipes_and_its_siblings.children(&mut tree.walk()).for_each(|attr| {
let attr_name = &source_code[attr.byte_range()];
if attr_name.contains("private") {
should_skip = true;
}
});
recipes_and_its_siblings
.children(&mut tree.walk())
.for_each(|attr| {
let attr_name = &source_code[attr.byte_range()];
if attr_name.contains("private") {
should_skip = true;
}
});
if should_skip {
continue;
}
Expand Down Expand Up @@ -278,7 +283,8 @@ clippy:
];

for case in cases {
let commands = Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string());
let commands =
Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string());
assert_eq!(commands, case.expected, "{}", case.name);
}
}
Expand Down
Loading

0 comments on commit 622997d

Please sign in to comment.