Module: ImageOptim::Runner::GlobHelpers

Defined in:
lib/image_optim/runner/glob_helpers.rb

Overview

Helper methods for glob

Constant Summary collapse

BRACE_REGEXP =

Match inner curly braces in glob Negative lookbehind is not used as is not supported by ruby before 1.9

/
  \A
  (
    (?:.*[^\\]|)  # anything ending not with slash or nothing
    (?:\\\\)*     # any number of self escaped slashes
  )
  \{              # open brace
  (
    (?:|.*?[^\\]) # nothing or non greedy anything ending not with slash
    (?:\\\\)*     # any number of self escaped slashes
  )
  \}              # close brace
  (
    .*            # what is left
  )
  \z
/x.freeze

Class Method Summary collapse

Class Method Details

.expand_braces(original_glob) ⇒ Object

Expand curly braces in glob as fnmatch in ruby before 2.0 doesn't support them


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/image_optim/runner/glob_helpers.rb', line 28

def expand_braces(original_glob)
  expanded = []
  unexpanded = [original_glob]
  while (glob = unexpanded.shift)
    if (m = BRACE_REGEXP.match(glob))
      m[2].split(',', -1).each do |variant|
        unexpanded << "#{m[1]}#{variant}#{m[3]}"
      end
    else
      expanded << glob
    end
  end
  expanded.uniq
end