Class: PathExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/path_expander.rb

Overview

PathExpander helps pre-process command-line arguments expanding directories into their constituent files. It further helps by providing additional mechanisms to make specifying subsets easier with path subtraction and allowing for command-line arguments to be saved in a file.

NOTE: this is NOT an options processor. It is a path processor (basically everything else besides options). It does provide a mechanism for pre-filtering cmdline options, but not with the intent of actually processing them in PathExpander. Use OptionParser to deal with options either before or after passing ARGV through PathExpander.

Constant Summary collapse

VERSION =

:nodoc:

"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, glob) ⇒ PathExpander

Create a new path expander that operates on args and expands via glob as necessary.



32
33
34
35
# File 'lib/path_expander.rb', line 32

def initialize args, glob
  self.args = args
  self.glob = glob
end

Instance Attribute Details

#argsObject

The args array to process.



21
22
23
# File 'lib/path_expander.rb', line 21

def args
  @args
end

#globObject

The glob used to expand dirs to files.



26
27
28
# File 'lib/path_expander.rb', line 26

def glob
  @glob
end

Instance Method Details

#expand_dirs_to_files(*dirs) ⇒ Object

Takes an array of paths and returns an array of paths where all directories are expanded to all files found via the glob provided to PathExpander.



42
43
44
45
46
47
48
49
50
# File 'lib/path_expander.rb', line 42

def expand_dirs_to_files *dirs
  dirs.flatten.map { |p|
    if File.directory? p then
      Dir[File.join(p, glob)]
    else
      p
    end
  }.flatten
end

#filter_files(files, ignore) ⇒ Object

A file filter mechanism similar to, but not as extensive as, .gitignore files:

+ If a pattern does not contain a slash, it is treated as a shell glob. + If a pattern ends in a slash, it matches on directories (and contents). + Otherwise, it matches on relative paths.

File.fnmatch is used throughout, so glob patterns work for all 3 types.

Takes a list of files and either an io or path of ignore data and returns a list of files left after filtering.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/path_expander.rb', line 143

def filter_files files, ignore
  ignore_paths = if ignore.respond_to? :read then
                   ignore.read
                 elsif File.exists? ignore then
                   File.read ignore
                 end

  if ignore_paths then
    nonglobs, globs = ignore_paths.split("\n").partition { |p| p.include? "/" }
    dirs, ifiles    = nonglobs.partition { |p| p.end_with? "/" }
    dirs            = dirs.map { |s| s.chomp "/" }

    only_paths = File::FNM_PATHNAME
    files = files.reject { |f|
      dirs.any?     { |i| File.fnmatch?(i, File.dirname(f), only_paths) } ||
        globs.any?  { |i| File.fnmatch?(i, f) } ||
        ifiles.any? { |i| File.fnmatch?(i, f, only_paths) }
    }
  end

  files
end

#processObject

Top-level method processes args. It replaces args’ contents with a new array of flags to process and returns a list of files to process. Eg

PathExpander.new(ARGV).process.each do |f|
  puts "./#{f}"
end


122
123
124
125
126
127
128
# File 'lib/path_expander.rb', line 122

def process
  files, flags = process_args

  args.replace process_flags flags

  files.uniq
end

#process_argsObject

Enumerate over args passed to PathExpander and return a list of files and flags to process. Arguments are processed as:

-file_path

Subtract path from file to be processed.

-dir_path

Expand and subtract paths from files to be processed.

-not_a_path

Add to flags to be processed.

dir_path

Expand and add to files to be processed.

file_path

Add to files to be processed.

See expand_dirs_to_files for details on how expansion occurs.

Subtraction happens last, regardless of argument ordering.



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
# File 'lib/path_expander.rb', line 75

def process_args
  pos_files = []
  neg_files = []
  flags     = []

  args.each do |arg|
    case arg
    when /^@(.*)/ then # push back on, so they can have dirs/-/@ as well
      args.concat process_file $1
    when /^-(.*)/ then
      if File.exist? $1 then
        neg_files += expand_dirs_to_files($1)
      else
        flags << arg
      end
    else
      if File.exist? arg then
        pos_files += expand_dirs_to_files(arg)
      else
        flags << arg
      end
    end
  end

  [pos_files - neg_files, flags]
end

#process_file(path) ⇒ Object

Process a file into more arguments. Override this to add additional capabilities.



56
57
58
# File 'lib/path_expander.rb', line 56

def process_file path
  File.readlines(path).map(&:chomp)
end

#process_flags(flags) ⇒ Object

Process over flags and treat any special ones here. Returns an array of the flags you haven’t processed.

This version does nothing. Subclass and override for customization.



109
110
111
# File 'lib/path_expander.rb', line 109

def process_flags flags
  flags
end