class DTK::Client::OsUtil

Constants

DTK_FOLDER_DIR
DTK_IDENTITY_FILE

Public Class Methods

current_dir() click to toggle source
# File lib/client/util/os_util.rb, line 38
def self.current_dir
  Dir.getwd
end
delim() click to toggle source
# File lib/client/util/os_util.rb, line 66
def self.delim
  is_windows? ? '\\' : '/'
end
dtk_identity_file_location() click to toggle source
# File lib/client/util/os_util.rb, line 92
def self.dtk_identity_file_location
  path_to_identity_file = "#{dtk_local_folder}/#{DTK_IDENTITY_FILE}"
  return path_to_identity_file if File.exists?(path_to_identity_file)
  print_warning("TIP: You can save your identity file as '#{path_to_identity_file}' and it will be used as default identityfile.")
  nil
end
dtk_local_folder() click to toggle source
# File lib/client/util/os_util.rb, line 30
def self.dtk_local_folder
  "#{home_dir}/#{DTK_FOLDER_DIR}"
end
edit(file) click to toggle source
# File lib/client/util/os_util.rb, line 70
def self.edit(file)
  editor = ENV['EDITOR']
  if is_windows?
    raise Error, "Environment variable EDITOR needs to be set; exit dtk-shell, set variable and log back into dtk-shell." unless editor
  else
    editor = 'vim' unless editor
  end

  system("#{editor} #{file}")
end
gem_installed?(gem_name, gem_version = nil) click to toggle source
# File lib/client/util/os_util.rb, line 115
def self.gem_installed?(gem_name, gem_version = nil)
  gems_hash = gem_names_versions_hash
  if gem_version
    (gems_hash[gem_name]||[]).include?(gem_version)
  else
    gems_hash.keys.include?(gem_name)
  end
end
gem_list() click to toggle source
# File lib/client/util/os_util.rb, line 99
def self.gem_list
  `gem list`.split("\n")
end
gem_names_versions_hash() click to toggle source
# File lib/client/util/os_util.rb, line 103
def self.gem_names_versions_hash
  name_version_hash = {}
  gem_list.each do |gem|
    if gem_match = gem.match(/(^.*)\((.*)\)/)
      name = gem_match[1].strip
      versions = (gem_match[2]||'').split(',')
      name_version_hash.merge!(name => versions)
    end
  end
  name_version_hash
end
home_dir() click to toggle source
# File lib/client/util/os_util.rb, line 25
def self.home_dir
  is_windows? ? home_dir__windows : genv(:home)
end
parent_dir(path) click to toggle source
# File lib/client/util/os_util.rb, line 53
def self.parent_dir(path)
  parent_dir?(path) || raise(Error::Usage, "Cannot find parent directory of '#{path}'")
end
parent_dir?(path) click to toggle source

Returns parent directory; if at root returns nil

# File lib/client/util/os_util.rb, line 58
def self.parent_dir?(path)
  raise Error.new("Not implemented for windows") if is_windows?
  ret = File.expand_path('../', path)
  unless ret == path # meaning at root
    ret
  end
end
temp_dir() click to toggle source
# File lib/client/util/os_util.rb, line 34
def self.temp_dir
  is_windows? ? genv(:temp) : '/tmp'
end
user_input(message) click to toggle source
# File lib/client/util/os_util.rb, line 81
def self.user_input(message)
  trap("INT", "SIG_IGN")
  while line = Readline.readline("#{message}: ",true)
    unless line.chomp.empty?
      trap("INT", false)
      return line
    end
  end
end
which(cmd) click to toggle source
# File lib/client/util/os_util.rb, line 42
def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    }
  end
  nil
end

Private Class Methods

genv(name) click to toggle source
# File lib/client/util/os_util.rb, line 126
def self.genv(name)
  ENV[name.to_s.upcase].gsub(/\\/,'/')
end
home_dir__windows() click to toggle source
# File lib/client/util/os_util.rb, line 138
def self.home_dir__windows
  "#{genv(:homedrive)}#{genv(:homepath)}"
end
is_linux?() click to toggle source
# File lib/client/util/os_util.rb, line 142
def self.is_linux?
  RUBY_PLATFORM.downcase.include?('linux')
end
is_mac?() click to toggle source
# File lib/client/util/os_util.rb, line 130
def self.is_mac?
  RUBY_PLATFORM.downcase.include?('darwin')
end
is_windows?() click to toggle source
# File lib/client/util/os_util.rb, line 134
def self.is_windows?
  RUBY_PLATFORM =~ /mswin|mingw|cygwin/
end