Module: Gem::Micro::Utils

Extended by:
Utils
Included in:
Gem::Micro, BareSpecification, Config, Installer, Source, Unpacker, Utils
Defined in:
lib/microgem/utils.rb

Instance Method Summary collapse

Instance Method Details

#configObject

Returns the Config.



10
11
12
# File 'lib/microgem/utils.rb', line 10

def config
  Config
end

#ensure_dir(dir) ⇒ Object

Creates a directory if it does not yet exist and it.



24
25
26
27
28
29
30
# File 'lib/microgem/utils.rb', line 24

def ensure_dir(dir)
  unless File.exist?(dir)
    log(:debug, "Creating directory `#{dir}'")
    FileUtils.mkdir_p(dir)
  end
  dir
end

#log(level, message) ⇒ Object

Prints a message to stdout with the specified log level.

TODO: Add proper Logger.



17
18
19
20
21
# File 'lib/microgem/utils.rb', line 17

def log(level, message)
  unless level == :debug && config.log_level != :debug
    puts "[#{level}] #{message}"
  end
end

#replace(old_path, new_path) ⇒ Object

Removes new_path, if it exists, before moving old_path to to.



33
34
35
36
37
# File 'lib/microgem/utils.rb', line 33

def replace(old_path, new_path)
  log(:debug, "Moving `#{old_path}' to `#{new_path}'")
  FileUtils.rm_rf(new_path) if File.exist?(new_path)
  FileUtils.mv(old_path, new_path)
end

#tmpdirObject

Returns a temporary directory and ensures it exists.

File.exist?('/path/to/tmp/microgem') # => false
tmpdir # => '/path/to/tmp/microgem'
File.exist?('/path/to/tmp/microgem') # => true


44
45
46
47
48
# File 'lib/microgem/utils.rb', line 44

def tmpdir
  tmpdir = File.join(Dir.tmpdir, 'microgem')
  ensure_dir(tmpdir)
  tmpdir
end