-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtool.rs
314 lines (282 loc) · 9.5 KB
/
tool.rs
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use super::manifest::create_prjfmt_manifest;
use crate::formatters::check::check_prjfmt;
use crate::formatters::manifest::{read_prjfmt_manifest, RootManifest};
use crate::{emoji, CLOG};
use anyhow::{anyhow, Error, Result};
use filetime::FileTime;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::fs::{metadata, read_to_string};
use std::iter::{IntoIterator, Iterator};
use std::path::PathBuf;
use xshell::cmd;
/// Make sure that formatter binary exists. This also for other formatter
pub fn check_bin(command: &str) -> Result<()> {
let cmd_bin = command.split_ascii_whitespace().next().unwrap_or("");
if let Ok(str) = cmd!("which {cmd_bin}").read() {
CLOG.info(&format!("Found {} at {}", cmd_bin, str));
return Ok(());
}
anyhow::bail!(
"Failed to locate formatter named {}. \
Please make sure it is available in your PATH",
command
)
}
/// Run the prjfmt
pub fn run_prjfmt(cwd: PathBuf, cache_dir: PathBuf) -> anyhow::Result<()> {
let prjfmt_toml = cwd.as_path().join("prjfmt.toml");
// Once the prjfmt found the $XDG_CACHE_DIR/prjfmt/eval-cache/ folder,
// it will try to scan the manifest and passed it into check_prjfmt function
let manifest: RootManifest = read_prjfmt_manifest(&prjfmt_toml, &cache_dir)?;
let old_ctx = create_command_context(&prjfmt_toml)?;
let ctxs = check_prjfmt(&prjfmt_toml, &old_ctx, &manifest)?;
let context = if manifest.manifest.is_empty() && ctxs.is_empty() {
&old_ctx
} else {
&ctxs
};
if !prjfmt_toml.as_path().exists() {
return Err(anyhow!(
"{}prjfmt.toml not found, please run --init command",
emoji::ERROR
));
}
for c in context {
check_bin(&c.command)?;
}
println!("===========================");
for c in context {
if !c.metadata.is_empty() {
println!("Command: {}", c.command);
println!("Files:");
for m in &c.metadata {
let path = &m.path;
println!(" - {}", path.display());
}
println!("===========================");
}
}
// TODO: report errors (both Err(_), and Ok(bad status))
let _outputs: Vec<xshell::Result<std::process::Output>> = context
.par_iter()
.flat_map(|c| {
c.metadata.par_iter().cloned().map(move |m| {
let arg = &c.args;
let cmd_arg = &c.command;
let path = &m.path;
cmd!("{cmd_arg} {arg...} {path}").output()
})
})
.collect();
let new_ctx: Vec<CmdContext> = old_ctx
.iter()
.flat_map(|octx| {
ctxs.iter().clone().map(move |c| {
if c.command == octx.command {
CmdContext {
command: c.command.clone(),
args: c.args.clone(),
metadata: octx.metadata.union(&c.metadata).cloned().collect(),
}
} else {
octx.clone()
}
})
})
.collect();
if manifest.manifest.is_empty() || ctxs.is_empty() {
create_prjfmt_manifest(prjfmt_toml, cache_dir, old_ctx)?;
} else {
println!("Format successful");
println!("capturing formatted file's state...");
create_prjfmt_manifest(prjfmt_toml, cache_dir, new_ctx)?;
}
Ok(())
}
/// Convert glob pattern into list of pathBuf
pub fn glob_to_path(
cwd: &PathBuf,
extensions: &FileExtensions,
includes: &Option<Vec<String>>,
excludes: &Option<Vec<String>>,
) -> anyhow::Result<Vec<PathBuf>> {
use ignore::{overrides::OverrideBuilder, WalkBuilder};
let mut overrides_builder = OverrideBuilder::new(cwd);
if let Some(includes) = includes {
for include in includes {
// Remove trailing `/` as we add one explicitly in the override
let include = include.trim_end_matches('/');
for extension in extensions.into_iter() {
overrides_builder.add(&format!("{}/**/{}", include, extension))?;
}
}
} else {
for extension in extensions.into_iter() {
overrides_builder.add(&extension)?;
}
}
if let Some(excludes) = excludes {
for exclude in excludes {
overrides_builder.add(&format!("!{}", exclude))?;
}
}
let overrides = overrides_builder.build()?;
Ok(WalkBuilder::new(cwd)
.overrides(overrides)
.build()
.filter_map(|e| {
e.ok().and_then(|e| {
match e.file_type() {
// Skip directory entries
Some(t) if t.is_dir() => None,
_ => Some(e.into_path()),
}
})
})
.collect())
}
/// Convert each PathBuf into FileMeta
/// FileMeta consist of file's path and its modification times
pub fn path_to_filemeta(paths: Vec<PathBuf>) -> Result<BTreeSet<FileMeta>> {
let mut filemeta = BTreeSet::new();
for p in paths {
let metadata = metadata(&p)?;
let mtime = FileTime::from_last_modification_time(&metadata).unix_seconds();
if !filemeta.insert(FileMeta {
mtimes: mtime,
path: p.clone(),
}) {
CLOG.warn(&format!("Duplicated file detected:"));
CLOG.warn(&format!(" - {:?} ", p.display()));
CLOG.warn(&format!(
"Maybe you want to format one file with different formatter?"
));
// return Err(anyhow!("prjfmt failed to run."));
}
}
Ok(filemeta)
}
/// Creating command configuration based on prjfmt.toml
pub fn create_command_context(prjfmt_toml: &PathBuf) -> Result<Vec<CmdContext>> {
let open_prjfmt = match read_to_string(prjfmt_toml.as_path()) {
Ok(file) => file,
Err(err) => {
return Err(anyhow!(
"cannot open {} due to {}.",
prjfmt_toml.display(),
err
))
}
};
let cwd = match prjfmt_toml.parent() {
Some(path) => path,
None => {
return Err(anyhow!(
"{}prjfmt.toml not found, please run --init command",
emoji::ERROR
))
}
};
let toml_content: Root = toml::from_str(&open_prjfmt)?;
let cmd_context: Vec<CmdContext> = toml_content
.formatters
.values()
.map(|config| {
let list_files = glob_to_path(
&cwd.to_path_buf(),
&config.files,
&config.includes,
&config.excludes,
)?;
Ok(CmdContext {
command: config.command.clone().unwrap_or_default(),
args: config.args.clone().unwrap_or_default(),
metadata: path_to_filemeta(list_files)?,
})
})
.collect::<Result<Vec<CmdContext>, Error>>()?;
Ok(cmd_context)
}
/// prjfmt.toml structure
#[derive(Debug, Deserialize)]
pub struct Root {
/// Map of formatters into the config
pub formatters: BTreeMap<String, FmtConfig>,
}
/// Config for each formatters
#[derive(Debug, Deserialize)]
pub struct FmtConfig {
/// File extensions that want to be formatted
pub files: FileExtensions,
/// File or Folder that is included to be formatted
pub includes: Option<Vec<String>>,
/// File or Folder that is excluded to be formatted
pub excludes: Option<Vec<String>>,
/// Command formatter to run
pub command: Option<String>,
/// Argument for formatter
pub args: Option<Vec<String>>,
}
/// File extensions can be single string (e.g. "*.hs") or
/// list of string (e.g. [ "*.hs", "*.rs" ])
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum FileExtensions {
/// Single file type
SingleFile(String),
/// List of file type
MultipleFile(Vec<String>),
}
impl<'a> IntoIterator for &'a FileExtensions {
type Item = &'a String;
type IntoIter = either::Either<std::iter::Once<&'a String>, std::slice::Iter<'a, String>>;
fn into_iter(self) -> Self::IntoIter {
match self {
FileExtensions::SingleFile(glob) => either::Either::Left(std::iter::once(glob)),
FileExtensions::MultipleFile(globs) => either::Either::Right(globs.iter()),
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
/// Each context of the formatter config
pub struct CmdContext {
/// formatter command to run
pub command: String,
/// formatter arguments or flags
pub args: Vec<String>,
/// formatter target path
pub metadata: BTreeSet<FileMeta>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
/// File metadata created after the first prjfmt run
pub struct FileMeta {
/// Last modification time listed in the file's metadata
pub mtimes: i64,
/// Path to the formatted file
pub path: PathBuf,
}
impl Ord for FileMeta {
fn cmp(&self, other: &Self) -> Ordering {
if self.eq(other) {
return Ordering::Equal;
}
if self.mtimes.eq(&other.mtimes) {
return self.path.cmp(&other.path);
}
self.mtimes.cmp(&other.mtimes)
}
}
impl PartialOrd for FileMeta {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for FileMeta {
fn eq(&self, other: &Self) -> bool {
self.mtimes == other.mtimes && self.path == other.path
}
}
impl Eq for FileMeta {}