treefmt 0.5.0

one CLI to format the code tree
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! The main formatting engine logic is in this module.

use crate::{config, eval_cache::CacheManifest, formatter::FormatterName};
use crate::{expand_path, formatter::Formatter, get_meta, get_path_meta, FileMeta};
use anyhow::anyhow;
use ignore::WalkBuilder;
use log::{debug, error, info, warn};
use rayon::prelude::*;
use std::io::{self, Write};
use std::iter::Iterator;
use std::path::{Path, PathBuf};
use std::{collections::BTreeMap, time::Instant};

/// Controls how the information is displayed at the end of a run
pub enum DisplayType {
    /// Just display some numbers
    Summary,
    /// Display the list of files that were affected
    Long,
}

/// Run the treefmt
pub fn run_treefmt(
    tree_root: &Path,
    work_dir: &Path,
    cache_dir: &Path,
    treefmt_toml: &Path,
    paths: &[PathBuf],
    no_cache: bool,
    clear_cache: bool,
    fail_on_change: bool,
    allow_missing_formatter: bool,
    selected_formatters: &Option<Vec<String>>,
) -> anyhow::Result<()> {
    assert!(tree_root.is_absolute());
    assert!(work_dir.is_absolute());
    assert!(cache_dir.is_absolute());
    assert!(treefmt_toml.is_absolute());

    let start_time = Instant::now();
    let mut phase_time = Instant::now();
    let mut timed_debug = |description: &str| {
        let now = Instant::now();
        debug!(
            "{}: {:.2?} (Δ {:.2?})",
            description,
            start_time.elapsed(),
            now.saturating_duration_since(phase_time)
        );
        phase_time = now;
    };

    let mut traversed_files: usize = 0;
    let mut matched_files: usize = 0;

    // Make sure all the given paths are absolute. Ignore the ones that point outside of the project root.
    let paths = paths.iter().fold(vec![], |mut sum, path| {
        let abs_path = expand_path(path, work_dir);
        if abs_path.starts_with(&tree_root) {
            sum.push(abs_path);
        } else {
            warn!(
                "Ignoring path {}, it is not in the project root",
                path.display()
            );
        }
        sum
    });

    // Let's check that there is at least one path to format.
    if paths.is_empty() {
        warn!("Aborting, no paths to format");
        return Ok(());
    }

    // Load the treefmt.toml file
    let project_config = config::from_path(treefmt_toml)?;

    let global_excludes = project_config
        .global
        .map(|g| g.excludes)
        .unwrap_or_default();

    timed_debug("load config");

    // Load all the formatter instances from the config.
    let mut expected_count = 0;

    let formatters = project_config.formatter.into_iter().fold(
        BTreeMap::new(),
        |mut sum, (name, mut fmt_config)| {
            expected_count += 1;
            fmt_config.excludes.extend_from_slice(&global_excludes);
            match Formatter::from_config(tree_root, &name, &fmt_config) {
                Ok(fmt_matcher) => match selected_formatters {
                    Some(f) => {
                        if f.contains(&name) {
                            sum.insert(fmt_matcher.name.clone(), fmt_matcher);
                        }
                    }
                    None => {
                        sum.insert(fmt_matcher.name.clone(), fmt_matcher);
                    }
                },
                Err(err) => {
                    if allow_missing_formatter {
                        error!("Ignoring formatter #{} due to error: {}", name, err)
                    } else {
                        error!("Failed to load formatter #{} due to error: {}", name, err)
                    }
                }
            };
            sum
        },
    );

    timed_debug("load formatters");

    // Check the number of configured formatters matches the number of formatters loaded
    if !(allow_missing_formatter || formatters.len() == expected_count) {
        return Err(anyhow!("One or more formatters are missing"));
    }

    // Load the eval cache
    let mut cache = if no_cache || clear_cache {
        // Start with an empty cache
        CacheManifest::default()
    } else {
        CacheManifest::load(cache_dir, treefmt_toml)
    };
    timed_debug("load cache");

    if !no_cache {
        // Insert the new formatter configs
        cache.update_formatters(formatters.clone());
    }

    // Configure the tree walker
    let walker = {
        // For some reason the WalkBuilder must start with one path, but can add more paths later.
        // unwrap: we checked before that there is at least one path in the vector
        let mut builder = WalkBuilder::new(paths.first().unwrap());
        // Add the other paths
        for path in paths[1..].iter() {
            builder.add(path);
        }
        // TODO: builder has a lot of interesting options.
        // TODO: use build_parallel with a Visitor.
        //       See https://docs.rs/ignore/0.4.17/ignore/struct.WalkParallel.html#method.visit
        builder.build()
    };

    // Start a collection of formatter names to path to mtime
    let mut matches: BTreeMap<FormatterName, BTreeMap<PathBuf, FileMeta>> = BTreeMap::new();

    // Now traverse the filesystem and classify each file. We also want the file mtime to see if it changed
    // afterwards.
    for walk_entry in walker {
        match walk_entry {
            Ok(dir_entry) => {
                if let Some(file_type) = dir_entry.file_type() {
                    // Ignore folders and symlinks. We don't want to format files outside the
                    // directory, and if the symlink destination is in the repo, it'll be matched
                    // when iterating over it.
                    if !file_type.is_dir() && !file_type.is_symlink() {
                        // Keep track of how many files were traversed
                        traversed_files += 1;

                        let path = dir_entry.path().to_path_buf();
                        // FIXME: complain if multiple matchers match the same path.
                        for (_, fmt) in formatters.clone() {
                            if fmt.clone().is_match(&path) {
                                // Keep track of how many files were associated with a formatter
                                matched_files += 1;

                                // unwrap: since the file exists, we assume that the metadata is also available
                                let mtime = get_meta(&dir_entry.metadata().unwrap());

                                matches
                                    .entry(fmt.name)
                                    .or_insert_with(BTreeMap::new)
                                    .insert(path.clone(), mtime);
                            }
                        }
                    }
                } else {
                    warn!("Couldn't get file type for {:?}", dir_entry.path())
                }
            }
            Err(err) => {
                warn!("traversal error: {}", err);
            }
        }
    }
    timed_debug("tree walk");

    // Filter out all of the paths that were already in the cache
    let matches = if !no_cache {
        cache.filter_matches(matches)
    } else {
        matches
    };

    timed_debug("filter_matches");

    // Keep track of the paths that are actually going to be formatted
    let filtered_files: usize = matches.values().map(|x| x.len()).sum();

    // Now run all the formatters and collect the formatted paths.
    let new_matches: BTreeMap<FormatterName, BTreeMap<PathBuf, FileMeta>> = matches
        .par_iter()
        .map(|(formatter_name, path_mtime)| {
            let paths: Vec<PathBuf> = path_mtime.keys().cloned().collect();
            // unwrap: the key exists since matches was built from that previous collection
            let formatter = formatters.get(formatter_name).unwrap();

            // Don't run the formatter if there are no paths to format!
            if paths.is_empty() {
                Ok((formatter_name.clone(), path_mtime.clone()))
            } else {
                let start_time = Instant::now();

                // Run the formatter
                for path_chunks in paths.chunks(1024) {
                    formatter.clone().fmt(path_chunks)?;
                }

                // Get the new mtimes and compare them to the original ones
                let new_paths = paths
                    .clone()
                    .into_iter()
                    .fold(BTreeMap::new(), |mut sum, path| {
                        // unwrap: assume that the file still exists after formatting
                        let mtime = get_path_meta(&path).unwrap();
                        sum.insert(path, mtime);
                        sum
                    });

                info!(
                    "{}: {} files processed in {:.2?}",
                    formatter.name,
                    paths.len(),
                    start_time.elapsed()
                );

                // Return the new mtimes
                Ok((formatter_name.clone(), new_paths))
            }
        })
        .collect::<anyhow::Result<BTreeMap<FormatterName, BTreeMap<PathBuf, FileMeta>>>>()?;
    timed_debug("format");

    if !no_cache {
        // Record the new matches in the cache
        cache.add_results(new_matches.clone());
        // And write to disk
        cache.write(cache_dir, treefmt_toml);
        timed_debug("write cache");
    }

    // Diff the old matches with the new matches
    let changed_matches: BTreeMap<FormatterName, Vec<PathBuf>> =
        new_matches
            .into_iter()
            .fold(BTreeMap::new(), |mut sum, (name, new_paths)| {
                // unwrap: we know that the name exists
                let old_paths = matches.get(&name).unwrap().clone();
                let filtered = new_paths
                    .iter()
                    .filter_map(|(k, v)| {
                        // unwrap: we know that the key exists
                        if old_paths.get(k).unwrap() == v {
                            None
                        } else {
                            Some(k.clone())
                        }
                    })
                    .collect();

                sum.insert(name, filtered);
                sum
            });

    // Get how many files were reformatted.
    let reformatted_files: usize = changed_matches.values().map(|x| x.len()).sum();
    // TODO: this will be configurable by the user in the future.
    let mut display_type = DisplayType::Summary;
    let mut ret: anyhow::Result<()> = Ok(());

    // Fail if --fail-on-change was passed.
    if reformatted_files > 0 && fail_on_change {
        // Switch the display type to long
        display_type = DisplayType::Long;
        ret = Err(anyhow!("fail-on-change"))
    }

    match display_type {
        DisplayType::Summary => {
            print_summary(
                traversed_files,
                matched_files,
                filtered_files,
                reformatted_files,
                start_time,
            );
        }
        DisplayType::Long => {
            print_summary(
                traversed_files,
                matched_files,
                filtered_files,
                reformatted_files,
                start_time,
            );
            println!("\nformatted files:");
            for (name, paths) in changed_matches {
                if !paths.is_empty() {
                    println!("{}:", name);
                    for path in paths {
                        println!("- {}", path.display());
                    }
                }
            }
        }
    }

    ret
}

fn print_summary(
    traversed_files: usize,
    matched_files: usize,
    filtered_files: usize,
    reformatted_files: usize,
    start_time: std::time::Instant,
) {
    println!(
        r#"
traversed {} files
matched {} files to formatters
left with {} files after cache
of whom {} files were re-formatted
all of this in {:.0?}
        "#,
        traversed_files,
        matched_files,
        filtered_files,
        reformatted_files,
        start_time.elapsed()
    );
}

/// Run the treefmt in a stdin buffer, and print it out back to stdout
pub fn run_treefmt_stdin(
    tree_root: &Path,
    work_dir: &Path,
    cache_dir: &Path,
    treefmt_toml: &Path,
    path: &Path,
    selected_formatters: &Option<Vec<String>>,
) -> anyhow::Result<()> {
    assert!(tree_root.is_absolute());
    assert!(work_dir.is_absolute());
    assert!(cache_dir.is_absolute());
    assert!(treefmt_toml.is_absolute());
    assert!(path.is_absolute());

    // Make sure all the given paths are absolute. Ignore the ones that point outside of the project root.
    if !path.starts_with(&tree_root) {
        return Err(anyhow!(
            "Ignoring path {}, it is not in the project root",
            path.display()
        ));
    };

    // Load the treefmt.toml file
    let project_config = config::from_path(treefmt_toml)?;

    let global_excludes = project_config
        .global
        .map(|g| g.excludes)
        .unwrap_or_default();

    // Load all the formatter instances from the config. Ignore the ones that failed.
    let formatters = project_config.formatter.into_iter().fold(
        BTreeMap::new(),
        |mut sum, (name, mut fmt_config)| {
            fmt_config.excludes.extend_from_slice(&global_excludes);
            match Formatter::from_config(tree_root, &name, &fmt_config) {
                Ok(fmt_matcher) => match selected_formatters {
                    Some(f) => {
                        if f.contains(&name) {
                            sum.insert(fmt_matcher.name.clone(), fmt_matcher);
                        }
                    }
                    None => {
                        sum.insert(fmt_matcher.name.clone(), fmt_matcher);
                    }
                },
                Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
            };
            sum
        },
    );

    // Collect all formatters that match the path
    let formatters: Vec<&Formatter> = formatters.values().filter(|f| f.is_match(&path)).collect();

    if formatters.is_empty() {
        warn!("no formatter found for path {:?}", path);
        // Just copy stdin to stdout
        io::copy(&mut io::stdin().lock(), &mut io::stdout().lock())?;
        return Ok(()); // Nothing more to do here
    } else if formatters.len() > 1 {
        warn!("multiple formatters matched the path. picking the first one");
    } else {
        info!("running {}", formatters.first().unwrap().name)
    }

    // Construct a "unique" filename. We want the code formatter to recognise the file type so it has the same extension.
    // And it has to live in the project's folder.
    //
    // NOTE: in some conditions like SIGINT and panic, the tmpfile won't be cleaned.
    let mut tmpfile = tempfile::Builder::new()
        .prefix("_tmp")
        .suffix(&path.file_name().unwrap())
        .tempfile_in(path.parent().unwrap())?;

    // Wrap this in a closure to control the error flow.
    let mut run = || -> anyhow::Result<()> {
        // Copy stdin to the file
        io::copy(&mut io::stdin().lock(), &mut tmpfile)?;

        // Make sure the file content is written to disk.
        tmpfile.flush()?;

        // Now that the file has been written, invoke the formatter.
        formatters
            .first()
            .unwrap()
            .fmt(&[tmpfile.path().to_path_buf()])?;

        // Seek back to start
        let mut tmpfile = tmpfile.reopen()?;

        // Copy the file to stdout
        io::copy(&mut tmpfile, &mut io::stdout().lock())?;

        Ok(())
    };

    let ret = run();

    // Free the temporary file explicitly here
    tmpfile.close()?;

    ret
}