Skip to content

Commit

Permalink
refactor(makefile): refactor Makefile struct to Runner enum to be…
Browse files Browse the repository at this point in the history
… able to add other command runner like pnpm, yarn, etc. #315 (#321)

* define Runner trait

* wip: Implement Makefile runner

* rename: model::Makefile -> model::Make

* add: implement Selector for Make

* fix: use Selector trait from caller side

* rename: makefile -> runners

* fix: append_history

* fix: narrow_down_targets

* fix: app.rs

* fix: history x UI周辺を追従

* add: command::Command

* fix: comment

* wip: trait object

* wip: enum

* Command

* fix: comment

* delete: pnpm

* comment out history related test code

* treat with temporal dead code

* delete dead code

* fix: how to print command to run

* chore: refactor
  • Loading branch information
kyu08 authored Nov 11, 2024
1 parent a323fa8 commit 2ea6fc8
Show file tree
Hide file tree
Showing 14 changed files with 889 additions and 500 deletions.
1 change: 1 addition & 0 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn parse_history(content: String) -> Result<Vec<(PathBuf, Vec<String>)>> {
Ok(result)
}

#[allow(dead_code)] // TODO(#321): remove
pub fn store_history(
history_directory_path: PathBuf,
history_file_name: String,
Expand Down
33 changes: 33 additions & 0 deletions src/model/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{fmt, path::PathBuf};

use super::runner_type;

#[derive(PartialEq, Debug, Clone)]
pub struct Command {
pub runner_type: runner_type::RunnerType,
pub name: String,
pub file_name: PathBuf,
pub line_number: u32,
}

impl Command {
pub fn new(
runner_type: runner_type::RunnerType,
command_name: String,
file_name: PathBuf,
line_number: u32,
) -> Self {
Self {
runner_type,
name: command_name,
file_name,
line_number,
}
}
}

impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}) {}", self.runner_type, self.name)
}
}
249 changes: 131 additions & 118 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,54 @@ impl Histories {
}
}

pub fn get_history(&self, path: &PathBuf) -> Option<Vec<String>> {
self.histories
.iter()
.find(|h| h.path == *path)
.map(|h| h.executed_targets.clone())
}

pub fn append(&self, path: &PathBuf, executed_target: &str) -> Option<Self> {
let mut new_histories = self.histories.clone();
// TODO(#321): Make this fn returns Vec<runner::Command>
// pub fn get_histories(&self, paths: Vec<PathBuf>) -> Vec<String> {
// let mut histories: Vec<String> = Vec::new();
//
// for path in paths {
// let executed_targets = self
// .histories
// .iter()
// .find(|h| h.path == path)
// .map(|h| h.executed_targets.clone())
// .unwrap_or(Vec::new());
// histories = [histories, executed_targets].concat();
// }
//
// histories
// }

new_histories
.iter()
.position(|h| h.path == *path)
.map(|index| {
let new_history = new_histories[index].append(executed_target.to_string());
new_histories[index] = new_history;

Self {
histories: new_histories,
}
})
}
// pub fn append(&self, path: &PathBuf, executed_target: &str) -> Option<Self> {
// let mut new_histories = self.histories.clone();
//
// new_histories
// .iter()
// .position(|h| h.path == *path)
// .map(|index| {
// let new_history = new_histories[index].append(executed_target.to_string());
// new_histories[index] = new_history;
//
// Self {
// histories: new_histories,
// }
// })
// }

pub fn to_tuple(&self) -> Vec<(PathBuf, Vec<String>)> {
let mut result = Vec::new();
// pub fn to_tuple(&self) -> Vec<(PathBuf, Vec<String>)> {
// let mut result = Vec::new();
//
// for history in &self.histories {
// result.push((history.path.clone(), history.executed_targets.clone()));
// }
// result
// }

for history in &self.histories {
result.push((history.path.clone(), history.executed_targets.clone()));
}
result
}

pub fn get_latest_target(&self, path: &PathBuf) -> Option<&String> {
self.histories
.iter()
.find(|h| h.path == *path)
.map(|h| h.executed_targets.first())?
}
// pub fn get_latest_target(&self, path: &PathBuf) -> Option<&String> {
// self.histories
// .iter()
// .find(|h| h.path == *path)
// .map(|h| h.executed_targets.first())?
// }

fn default(path: PathBuf) -> Self {
let histories = vec![History::default(path)];
Expand All @@ -75,6 +85,7 @@ impl Histories {
}
}

// TODO(#321): should return Result not Option(returns when it fails to get the home dir)
pub fn history_file_path() -> Option<(PathBuf, String)> {
const HISTORY_FILE_NAME: &str = "history.toml";

Expand All @@ -98,8 +109,8 @@ pub fn history_file_path() -> Option<(PathBuf, String)> {

#[derive(Clone, PartialEq, Debug)]
struct History {
path: PathBuf,
executed_targets: Vec<String>,
path: PathBuf, // TODO: rename to working_directory
executed_targets: Vec<String>, // TODO: make this to Vec<runner::Command>
}

impl History {
Expand All @@ -117,6 +128,8 @@ impl History {
}
}

// TODO(#321): remove
#[allow(dead_code)]
fn append(&self, executed_target: String) -> Self {
let mut executed_targets = self.executed_targets.clone();
executed_targets.retain(|t| *t != executed_target);
Expand Down Expand Up @@ -226,87 +239,87 @@ mod test {
}
}

#[test]
fn histories_append_test() {
struct Case {
title: &'static str,
path: PathBuf,
appending_target: &'static str,
histories: Histories,
expect: Option<Histories>,
}
let cases = vec![
Case {
title: "Success",
path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
appending_target: "history1",
histories: Histories {
histories: vec![
History {
path: PathBuf::from("/Users/user/code/rustc".to_string()),
executed_targets: vec!["history0".to_string(), "history1".to_string()],
},
History {
path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
executed_targets: vec![
"history0".to_string(),
"history1".to_string(),
"history2".to_string(),
],
},
],
},
expect: Some(Histories {
histories: vec![
History {
path: PathBuf::from("/Users/user/code/rustc".to_string()),
executed_targets: vec!["history0".to_string(), "history1".to_string()],
},
History {
path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
executed_targets: vec![
"history1".to_string(),
"history0".to_string(),
"history2".to_string(),
],
},
],
}),
},
Case {
title: "Returns None when path is not found",
path: PathBuf::from("/Users/user/code/non-existent-dir".to_string()),
appending_target: "history1",
histories: Histories {
histories: vec![
History {
path: PathBuf::from("/Users/user/code/rustc".to_string()),
executed_targets: vec!["history0".to_string(), "history1".to_string()],
},
History {
path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
executed_targets: vec![
"history0".to_string(),
"history1".to_string(),
"history2".to_string(),
],
},
],
},
expect: None,
},
];

for case in cases {
assert_eq!(
case.expect,
case.histories.append(&case.path, case.appending_target),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
}
}

// TODO(#321): comment in this test
// #[test]
// fn histories_append_test() {
// struct Case {
// title: &'static str,
// path: PathBuf,
// appending_target: &'static str,
// histories: Histories,
// expect: Option<Histories>,
// }
// let cases = vec![
// Case {
// title: "Success",
// path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
// appending_target: "history1",
// histories: Histories {
// histories: vec![
// History {
// path: PathBuf::from("/Users/user/code/rustc".to_string()),
// executed_targets: vec!["history0".to_string(), "history1".to_string()],
// },
// History {
// path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
// executed_targets: vec![
// "history0".to_string(),
// "history1".to_string(),
// "history2".to_string(),
// ],
// },
// ],
// },
// expect: Some(Histories {
// histories: vec![
// History {
// path: PathBuf::from("/Users/user/code/rustc".to_string()),
// executed_targets: vec!["history0".to_string(), "history1".to_string()],
// },
// History {
// path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
// executed_targets: vec![
// "history1".to_string(),
// "history0".to_string(),
// "history2".to_string(),
// ],
// },
// ],
// }),
// },
// Case {
// title: "Returns None when path is not found",
// path: PathBuf::from("/Users/user/code/non-existent-dir".to_string()),
// appending_target: "history1",
// histories: Histories {
// histories: vec![
// History {
// path: PathBuf::from("/Users/user/code/rustc".to_string()),
// executed_targets: vec!["history0".to_string(), "history1".to_string()],
// },
// History {
// path: PathBuf::from("/Users/user/code/fzf-make".to_string()),
// executed_targets: vec![
// "history0".to_string(),
// "history1".to_string(),
// "history2".to_string(),
// ],
// },
// ],
// },
// expect: None,
// },
// ];
//
// for case in cases {
// assert_eq!(
// case.expect,
// case.histories.append(&case.path, case.appending_target),
// "\nFailed: 🚨{:?}🚨\n",
// case.title,
// )
// }
// }
#[test]
fn history_append_test() {
struct Case {
Expand Down
Loading

0 comments on commit 2ea6fc8

Please sign in to comment.