Class: Flintlock::Util

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

Class Method Summary collapse

Class Method Details

.depends_on(what) ⇒ Object

Raises:



53
54
55
# File 'lib/flintlock/util.rb', line 53

def self.depends_on(what)
  raise DependencyError.new(what) if which(what).nil?
end

.detect_runtime(script, default = "/bin/sh") ⇒ Object



42
43
44
45
46
47
# File 'lib/flintlock/util.rb', line 42

def self.detect_runtime(script, default="/bin/sh")
  raw = File.open(script, &:readline)[/^\s*#!\s*(.+)/, 1] || default
  raw.split
rescue EOFError
  [default]
end

.empty_directory?(directory) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/flintlock/util.rb', line 7

def self.empty_directory?(directory)
  Dir[File.join(directory, '*')].empty?
end

.full_extname(filename) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flintlock/util.rb', line 11

def self.full_extname(filename)
  data = []
  current_filename = filename.dup
  while true
    ext = File.extname(current_filename)
    break if ext.empty? || ext =~ /^\.\d+$/
    current_filename = current_filename.gsub(/#{ext}$/, '')
    data << ext
  end
  data.reverse.join
end

.get_uri_scheme(uri) ⇒ Object



31
32
33
34
# File 'lib/flintlock/util.rb', line 31

def self.get_uri_scheme(uri)
  scheme = URI.parse(uri).scheme
  return scheme.nil? ? nil : scheme.split('+', 0)[0] 
end

.load_logger(debug = false) ⇒ Object



36
37
38
39
40
# File 'lib/flintlock/util.rb', line 36

def self.load_logger(debug = false)
  log = Logger.new(STDOUT)
  log.silence! if ! debug
  log
end

.mime_type(filename) ⇒ Object



57
58
59
60
61
# File 'lib/flintlock/util.rb', line 57

def self.mime_type(filename)
  depends_on 'file'
  stdout, stderr, status = Runner.new.run(['file', '--mime-type', filename], :capture => true) 
  stdout.split(':')[1].strip
end

.path_split(path) ⇒ Object



49
50
51
# File 'lib/flintlock/util.rb', line 49

def self.path_split(path)
  path.split(File::SEPARATOR).select { |x| ! x.empty? }
end

.which(command) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/flintlock/util.rb', line 23

def self.which(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exe = File.join(path, command)
    return exe if File.executable?(exe)
  end
  nil
end