summaryrefslogtreecommitdiff
path: root/cli/util/glob.rs
blob: 55c9a516ee3508e855749aba5a2955fca8484019 (plain)
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::path::Path;
use std::path::PathBuf;

use deno_core::anyhow::Context;
use deno_core::error::AnyError;

pub fn expand_globs(paths: Vec<PathBuf>) -> Result<Vec<PathBuf>, AnyError> {
  let mut new_paths = vec![];
  for path in paths {
    let path_str = path.to_string_lossy();
    if is_glob_pattern(&path_str) {
      let globbed_paths = glob(&path_str)?;

      for globbed_path_result in globbed_paths {
        new_paths.push(globbed_path_result?);
      }
    } else {
      new_paths.push(path);
    }
  }

  Ok(new_paths)
}

pub fn glob(pattern: &str) -> Result<glob::Paths, AnyError> {
  glob::glob_with(&escape_brackets(pattern), match_options())
    .with_context(|| format!("Failed to expand glob: \"{}\"", pattern))
}

pub struct GlobPattern(glob::Pattern);

impl GlobPattern {
  pub fn new_if_pattern(pattern: &str) -> Result<Option<Self>, AnyError> {
    if !is_glob_pattern(pattern) {
      return Ok(None);
    }
    Self::new(pattern).map(Some)
  }

  pub fn new(pattern: &str) -> Result<Self, AnyError> {
    let pattern = glob::Pattern::new(pattern)
      .with_context(|| format!("Failed to expand glob: \"{}\"", pattern))?;
    Ok(Self(pattern))
  }

  pub fn matches_path(&self, path: &Path) -> bool {
    self.0.matches_path(path)
  }
}

pub struct GlobSet(Vec<GlobPattern>);

impl GlobSet {
  pub fn new(matchers: Vec<GlobPattern>) -> Self {
    Self(matchers)
  }

  pub fn matches_path(&self, path: &Path) -> bool {
    for pattern in &self.0 {
      if pattern.matches_path(path) {
        return true;
      }
    }
    false
  }
}

pub fn is_glob_pattern(path: &str) -> bool {
  path.chars().any(|c| matches!(c, '*' | '?'))
}

fn escape_brackets(pattern: &str) -> String {
  // Escape brackets - we currently don't support them, because with introduction
  // of glob expansion paths like "pages/[id].ts" would suddenly start giving
  // wrong results. We might want to revisit that in the future.
  pattern.replace('[', "[[]").replace(']', "[]]")
}

fn match_options() -> glob::MatchOptions {
  // Matches what `deno_task_shell` does
  glob::MatchOptions {
    // false because it should work the same way on case insensitive file systems
    case_sensitive: false,
    // true because it copies what sh does
    require_literal_separator: true,
    // true because it copies with sh does—these files are considered "hidden"
    require_literal_leading_dot: true,
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  pub fn glob_set_matches_path() {
    let glob_set = GlobSet::new(vec![
      GlobPattern::new("foo/bar").unwrap(),
      GlobPattern::new("foo/baz").unwrap(),
    ]);

    assert!(glob_set.matches_path(Path::new("foo/bar")));
    assert!(glob_set.matches_path(Path::new("foo/baz")));
    assert!(!glob_set.matches_path(Path::new("foo/qux")));
  }
}