-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakefile.rs
More file actions
170 lines (153 loc) · 4.07 KB
/
makefile.rs
File metadata and controls
170 lines (153 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::domain::{Stack, Task, Tasks};
use big_s::S;
use regex::Regex;
use std::fmt::Display;
use std::fs;
use std::io::ErrorKind;
use std::process::Command;
use std::sync::LazyLock;
struct MakefileStack {
tasks: Tasks,
}
impl Display for MakefileStack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Makefile")
}
}
impl Stack for MakefileStack {
fn setup(&self) -> Option<Command> {
None
}
fn install(&self) -> Option<Command> {
None
}
fn tasks(&self) -> &Tasks {
&self.tasks
}
}
pub(crate) fn scan() -> Option<Box<dyn Stack>> {
let text = match fs::read_to_string("Makefile") {
Ok(text) => text,
Err(e) if e.kind() == ErrorKind::NotFound => return None,
Err(e) => {
println!("Warning: Cannot read file \"Makefile\": {e}");
return None;
}
};
Some(Box::new(MakefileStack {
tasks: parse_text(&text),
}))
}
/// provides the tasks in the given Makefile content
fn parse_text(text: &str) -> Tasks {
let mut result = Tasks::new();
for line in text.lines() {
if let Some(task) = parse_line(line) {
result.push(task);
}
}
result
}
/// provides a task for the Makefile target defined on the given line, if one exists
fn parse_line(line: &str) -> Option<Task> {
let capture = RE.captures(line)?;
let name = capture.get(1).unwrap().as_str();
let desc = match capture.get(4) {
Some(desc) => desc.as_str().to_string(),
None => String::new(),
};
Some(Task {
name: name.into(),
cmd: S("make"),
argv: vec![S("--no-print-directory"), name.into()],
desc,
})
}
static RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([[[:alnum:]]-]+):([^#]*)?(#[[:blank:]]*(.*))?").unwrap());
#[cfg(test)]
mod tests {
mod parse_line {
use crate::domain::Task;
use big_s::S;
#[test]
fn no_task() {
let give = "\techo hello";
let want = None;
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name() {
let give = "cuke:";
let want = Some(Task {
name: S("cuke"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("cuke")],
desc: S(""),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name_and_deps() {
let give = "cuke: build, lint";
let want = Some(Task {
name: S("cuke"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("cuke")],
desc: S(""),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name_and_desc() {
let give = "cuke: # run cucumber";
let want = Some(Task {
name: S("cuke"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("cuke")],
desc: S("run cucumber"),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name_and_deps_and_desc() {
let give = "cuke: build, lint # run cucumber";
let want = Some(Task {
name: S("cuke"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("cuke")],
desc: S("run cucumber"),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name_with_dash() {
let give = "cuke-this: # run only the tagged Cucumber scenario";
let want = Some(Task {
name: S("cuke-this"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("cuke-this")],
desc: S("run only the tagged Cucumber scenario"),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
#[test]
fn name_with_number() {
let give = "task-1: # task 1";
let want = Some(Task {
name: S("task-1"),
cmd: S("make"),
argv: vec![S("--no-print-directory"), S("task-1")],
desc: S("task 1"),
});
let have = super::super::parse_line(give);
pretty::assert_eq!(have, want);
}
}
}