Module: RubyGit::FileHelpers
- Defined in:
- lib/ruby_git/file_helpers.rb
Overview
A namespace for several file utility methods that I wish were part of FileUtils.
Class Method Summary collapse
-
.which(cmd, paths: ENV['PATH'].split(File::PATH_SEPARATOR), exts: (ENV['PATHEXT']&.split(';') || [''])) ⇒ Pathname?
Cross platform way to find an executable file within a list of paths.
Class Method Details
.which(cmd, paths: ENV['PATH'].split(File::PATH_SEPARATOR), exts: (ENV['PATHEXT']&.split(';') || [''])) ⇒ Pathname?
Cross platform way to find an executable file within a list of paths
Works for both Linux/Unix and Windows.
exts
is for Windows. Other platforms should accept the default.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby_git/file_helpers.rb', line 28 def self.which( cmd, paths: ENV['PATH'].split(File::PATH_SEPARATOR), exts: (ENV['PATHEXT']&.split(';') || ['']) ) raise 'PATH is not set' unless ENV.keys.include?('PATH') paths .product(exts) .map { |path, ext| Pathname.new(File.join(path, "#{cmd}#{ext}")) } .reject { |path| path.directory? || !path.executable? } .find { |exe_path| !exe_path.directory? && exe_path.executable? } end |