Module: Hoodie::FileHelper

Extended by:
FileHelper
Included in:
FileHelper
Defined in:
lib/hoodie/utils/file_helper.rb

Overview

Class methods that are added when you include Hoodie

Constant Summary collapse

Win32Exts =
%w{.exe .com .bat .cmd}

Instance Method Summary collapse

Instance Method Details

#all_files_under(path, &ignore) ⇒ Array<Pathname>

Get a recusive list of files inside a path.

Parameters:

  • path (String)

    some path string or Pathname

  • ignore (Block)

    a proc/block that returns true if a given path should be ignored, if a path is ignored, nothing below it will be searched either.

Returns:

  • (Array<Pathname>)

    array of Pathnames for each file (no directories)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hoodie/utils/file_helper.rb', line 166

def all_files_under(path, &ignore)
  path = Pathname(path)

  if path.directory?
    path.children.flat_map do |child|
      all_files_under(child, &ignore)
    end.compact
  elsif path.file?
    if block_given? && ignore.call(path)
      []
    else
      [path]
    end
  else
    []
  end
end

#command_in_path?(command) ⇒ Boolean

Checks in PATH returns true if the command is found.

Parameters:

  • command (String)

    The name of the command to look for.

Returns:

  • (Boolean)

    True if the command is found in the path.



35
36
37
38
39
40
# File 'lib/hoodie/utils/file_helper.rb', line 35

def command_in_path?(command)
  found = ENV['PATH'].split(File::PATH_SEPARATOR).map do |p|
    File.exist?(File.join(p, command))
  end
  found.include?(true)
end

#normalize_path(path) ⇒ String

Normalize a path to not include a leading slash

Parameters:

Returns:



221
222
223
# File 'lib/hoodie/utils/file_helper.rb', line 221

def normalize_path(path)
  path.sub(%r{^/}, '').tr('', '')
end

#path_match(matcher, path) ⇒ Boolean

Takes an object, which can be a literal string or a string containing glob expressions, or a regexp, or a proc, or anything else that responds to #match or #call, and returns whether or not the given path matches that matcher.

Parameters:

  • matcher (String, #match, #call)

    a matcher String, RegExp, Proc, etc.

  • path (String)

    a path as a string

Returns:

  • (Boolean)

    whether the path matches the matcher



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/hoodie/utils/file_helper.rb', line 198

def path_match(matcher, path)
  case
  when matcher.is_a?(String)
    if matcher.include? '*'
      File.fnmatch(matcher, path)
    else
      path == matcher
    end
  when matcher.respond_to?(:match)
    !matcher.match(path).nil?
  when matcher.respond_to?(:call)
    matcher.call(path)
  else
    File.fnmatch(matcher.to_s, path)
  end
end

#whereis(prog, path = ENV['PATH']) ⇒ String, ...

In block form, yields each program within path. In non-block form, returns an array of each program within path. Returns nil if not found found. On the Shit for Windows platform, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name.

Examples:

whereis('ruby')
  # => [
      [0] "/opt/chefdk/embedded/bin/ruby",
      [1] "/usr/bin/ruby",
      [2] "/Users/sharding/.rvm/rubies/ruby-2.2.0/bin/ruby",
      [3] "/usr/bin/ruby"
  ]

Parameters:

  • cmd (String)

    The name of the command to find.

  • path (String) (defaults to: ENV['PATH'])

    The path to search for the command.

Returns:



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
# File 'lib/hoodie/utils/file_helper.rb', line 116

def whereis(prog, path=ENV['PATH'])
  dirs = []
  path.split(File::PATH_SEPARATOR).each do |dir|
    if File::ALT_SEPARATOR
      if prog.include?('.')
        f = File.join(dir,prog)
        if File.executable?(f) && !File.directory?(f)
          if block_given?
            yield f.gsub(/\//,'\\')
          else
            dirs << f.gsub(/\//,'\\')
          end
        end
      else
        Win32Exts.find_all do |ext|
          f = File.join(dir,prog+ext)
          if File.executable?(f) && !File.directory?(f)
              if block_given?
                yield f.gsub(/\//,'\\')
              else
                dirs << f.gsub(/\//,'\\')
              end
            end
          end
        end
      else
        f = File.join(dir,prog)
        if File.executable?(f) && !File.directory?(f)
          if block_given?
            yield f
          else
            dirs << f
          end
        end
      end
    end
  dirs.empty? ? nil : dirs
end

#which(prog, path = ENV['PATH']) ⇒ String, NilClass

Looks for the first occurrence of program within path. On the pure crap OS, also known as Windows, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name.

Parameters:

  • cmd (String)

    The name of the command to find.

  • path (String) (defaults to: ENV['PATH'])

    The path to search for the command.

Returns:



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
# File 'lib/hoodie/utils/file_helper.rb', line 62

def which(prog, path = ENV['PATH'])
  path.split(File::PATH_SEPARATOR).each do |dir|
    if File::ALT_SEPARATOR
      ext = Win32Exts.find do |ext|
        if prog.include?('.')
          f = File.join(dir, prog)
        else
          f = File.join(dir, prog+ext)
        end
        File.executable?(f) && !File.directory?(f)
      end
      if ext
        if prog.include?('.')
          f = File.join(dir, prog).gsub(/\//,'\\')
        else
          f = File.join(dir, prog + ext).gsub(/\//,'\\')
        end
        return f
      end
    else
      f = File.join(dir, prog)
      if File.executable?(f) && !File.directory?(f)
        return File::join(dir, prog)
      end
    end
  end

  nil
end