module DTK::Client::OsUtil

Constants

DTK_IDENTITY_FILE

Public Class Methods

actual_length( string_with_escapes ) click to toggle source
# File lib/util/os_util.rb, line 369
def actual_length( string_with_escapes )
  string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length
end
assembly_module_base_location() click to toggle source
# File lib/util/os_util.rb, line 182
def assembly_module_base_location()
  clone_base_path(:assembly_module)
end
backups_location() click to toggle source
# File lib/util/os_util.rb, line 186
def backups_location()
  path = Config[:backups_location]
  final_path = path && path.start_with?('/') ? path : "#{dtk_local_folder}#{path}"
  final_path.gsub(/\/$/,'')
end
clear_screen() click to toggle source
# File lib/util/os_util.rb, line 59
def clear_screen
  command = is_windows? ? "cls" : "clear"
  system(command)
end
colorize(message, color) click to toggle source

Public method will convert given string, to string with colorize output

message - String to be colorized color - Symbol describing color to be used

Returns String with colorize output

# File lib/util/os_util.rb, line 247
def colorize(message, color)
  # at the moment we do not support colors in windows
  ((is_windows? || message.nil?) ? message : message.colorize(color))
end
component_clone_location() click to toggle source
# File lib/util/os_util.rb, line 170
def component_clone_location()
  clone_base_path(:component_module)
end
current_dir() click to toggle source
# File lib/util/os_util.rb, line 98
def current_dir
  current_dir = Dir.getwd()
  current_dir.gsub(home_dir, '~')
end
dev_reload_shell() click to toggle source
# File lib/util/os_util.rb, line 309
def dev_reload_shell()
  suspend_output do
    load File.expand_path('../../lib/util/os_util.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/util/ssh_util.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/util/remote_dependency_util.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/help_monkey_patch.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/domain/context_entity.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/domain/active_context.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/domain/context_params.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/domain/override_tasks.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/context.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/domain/git_adapter.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/command_helpers/git_repo.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/command_helpers/service_importer.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/view_processor/table_print.rb', File.dirname(__FILE__))
    load File.expand_path('../../lib/shell/interactive_wizard.rb', File.dirname(__FILE__))
    paths = []
    paths << File.expand_path('../../lib/commands/thor/*.rb', File.dirname(__FILE__))
    paths << File.expand_path('../../lib/commands/common/thor/*.rb', File.dirname(__FILE__))

    paths.each do |path|
      Dir[path].each do |thor_class_file|
        load thor_class_file
      end
    end
  end
end
dtk_app_folder() click to toggle source
# File lib/util/os_util.rb, line 86
def dtk_app_folder
  return (is_windows? ? "#{genv(:homedrive)}#{genv(:homepath)}/dtk/" : "#{/etc/}dtk/")
end
dtk_home_dir() click to toggle source
# File lib/util/os_util.rb, line 64
def dtk_home_dir
  return "#{home_dir}"
end
dtk_identity_file_location() click to toggle source

Checks to find dtk.pem in configuration node, if not found displays tip message

# File lib/util/os_util.rb, line 212
def 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("TIP: You can save your identity file as '#{path_to_identity_file}' and it will be used as default identityfile.", :yellow)
  nil
end
dtk_local_folder() click to toggle source
# File lib/util/os_util.rb, line 90
def dtk_local_folder
  return (is_windows? ? "#{genv(:homedrive)}#{genv(:homepath)}/dtk/" : "#{home_dir}/dtk/")
end
dtk_user_app_folder() click to toggle source

for Windows app folder is already under OS username

# File lib/util/os_util.rb, line 78
def dtk_user_app_folder
  if is_windows?
    dtk_app_folder()
  else
    "#{dtk_app_folder}#{::DTK::Common::Aux.running_process_user()}/"
  end
end
edit(file) click to toggle source
# File lib/util/os_util.rb, line 107
def edit(file)
  editor = ENV['EDITOR']
  if is_windows?
    raise Client::DtkError, "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
genv(name) click to toggle source
# File lib/util/os_util.rb, line 103
def genv(name)
  return ENV[name.to_s.upcase].gsub(/\\/,'/')
end
get_dtk_class(command_name) click to toggle source

This will return class object from DTK::Client namespace

# File lib/util/os_util.rb, line 69
def get_dtk_class(command_name)
  begin
    Object.const_get('DTK').const_get('Client').const_get(cap_form(command_name))
  rescue Exception => e
    return nil
  end
end
get_log_location() click to toggle source
# File lib/util/os_util.rb, line 55
def get_log_location
  return "#{dtk_local_folder}"
end
get_temp_location() click to toggle source
# File lib/util/os_util.rb, line 44
def get_temp_location
  is_windows? ? genv(:temp) : '/tmp'
end
home_dir() click to toggle source
# File lib/util/os_util.rb, line 94
def home_dir
  return (is_windows? ? "#{genv(:homedrive)}#{genv(:homepath)}" : "#{genv(:home)}")
end
is_linux?() click to toggle source
# File lib/util/os_util.rb, line 40
def is_linux?
  RUBY_PLATFORM.downcase.include?('linux')
end
is_mac?() click to toggle source
# File lib/util/os_util.rb, line 32
def is_mac?
  RUBY_PLATFORM.downcase.include?('darwin')
end
is_windows?() click to toggle source
# File lib/util/os_util.rb, line 36
def is_windows?
  RUBY_PLATFORM =~ /mswin|mingw|cygwin/
end
local_component_module_list() click to toggle source
# File lib/util/os_util.rb, line 224
def local_component_module_list()
  component_module_dir = component_clone_location()

  directories = Dir.entries(component_module_dir).map do |entry|
    next if (entry =='.' || entry == '..' || entry.index('.') == 0 || !File.directory?(entry))

    Dir.entries("#{component_module_dir}/#{entry}").map do |m_entry|
      next unless File.directory? File.join(component_module_dir,entry,m_entry)
      next if (m_entry =='.' || m_entry == '..' || m_entry.index('.') == 0)

      ModuleUtil.join_name(m_entry, entry)
    end
  end

  directories.flatten.select { |d| !d.nil? }
end
module_clone_location(module_type) click to toggle source
# File lib/util/os_util.rb, line 153
def module_clone_location(module_type)
  case module_type.to_s
    when "component_module"
      return component_clone_location
    when "service_module"
      return service_clone_location
    when "test_module"
      test_clone_location
    else
      raise Client::DtkError, "Unexpected module_type (#{module_type}) when determining module location"
    end
end
module_location(module_type,module_name,version=nil,opts={}) click to toggle source
# File lib/util/os_util.rb, line 118
def module_location(module_type,module_name,version=nil,opts={})
  # compact used because module_name can be nil
  location = module_location_parts(module_type,module_name,version,opts).compact.join('/')
  location
end
module_location_parts(module_type,module_name,version=nil,opts={}) click to toggle source

if module location is /a/b/d/mod it returns ['/a/b/d','mod']

# File lib/util/os_util.rb, line 125
def module_location_parts(module_type,module_name,version=nil,opts={})
  base_path = clone_base_path(opts[:assembly_module] ? :assembly_module : module_type)
  if assembly_module = opts[:assembly_module]
    assembly_name = opts[:assembly_module][:assembly_name]
    base_all_types = "#{base_path}/#{assembly_name}"
    if module_type == :all
      [base_all_types,nil]
    else
      type = clone_base_path(module_type).split('/').last
      ["#{base_all_types}/#{type}", module_name]
    end
  else
    # we detect if we are using full name
    if (module_name.match(/(.*)#{ModuleUtil::NAMESPACE_SEPERATOR}(.*)/))
      [base_path, "#{$1}", "#{$2}#{version && "-#{version}"}"]
    else
      [base_path, "#{module_name}#{version && "-#{version}"}"]
    end
  end
end
module_version_locations(module_type, module_name, version = nil, opts={}) click to toggle source
# File lib/util/os_util.rb, line 146
def module_version_locations(module_type, module_name, version = nil, opts={})
  base_path, namespace, name = module_location_parts(module_type, module_name, version, opts) #.first
  namespace_path = "#{base_path}/#{namespace}"
  module_versions = Dir.entries(namespace_path).select{|a| a.match(/^#{name}-\d{1,2}.\d{1,2}.\d{1,2}$/)}
  module_versions.map{ |version| "#{namespace_path}/#{version}" }
end
pop_readline_history(number_of_last_commands) click to toggle source
# File lib/util/os_util.rb, line 48
def pop_readline_history(number_of_last_commands)
  number_of_last_commands.downto(1) do
    Readline::HISTORY.pop
  end
  nil
end
print(message, color = :white) click to toggle source

Public method will print to STDOUT with given color

message - String to be colorize and printed color - Symbol describing the color to be used on STDOUT

Void

print_deprecate_message(message) click to toggle source
print_warning(message) click to toggle source
put_warning(prefix, text, color) click to toggle source
# File lib/util/os_util.rb, line 338
def put_warning(prefix, text, color)
  width = HighLine::SystemExtensions.terminal_size[0] - (prefix.length + 1)
  text_split = wrap(text, width)
  Kernel.print colorize(prefix, color), " "
  text_split.lines.each_with_index do |line, index|
    line = " "*(prefix.length + 1) + line unless index == 0
    puts line
  end
end
remove_html_tags(string_with_tags) click to toggle source
# File lib/util/os_util.rb, line 373
def remove_html_tags(string_with_tags)
  string_with_tags.gsub(/<\/?[^>]+>/, '')
end
service_clone_location() click to toggle source
# File lib/util/os_util.rb, line 174
def service_clone_location()
  clone_base_path(:service_module)
end
suspend_output() { || ... } click to toggle source

Public block, method will suspend STDOUT, STDERR in body of it

Example suspend_output do

# some calls

end

# File lib/util/os_util.rb, line 276
def suspend_output
  if is_windows?
    retval = yield
  else
    orig_stderr = $stderr.clone
    orig_stdout = $stdout.clone
    begin
      $stderr.reopen File.new('/dev/null', 'w')
      $stdout.reopen File.new('/dev/null', 'w')
      retval = yield
    rescue Exception => e
      $stdout.reopen orig_stdout
      $stderr.reopen orig_stderr
      raise e
    ensure
      $stdout.reopen orig_stdout
      $stderr.reopen orig_stderr
    end
  end
  retval
end
temp_git_remote_location() click to toggle source
# File lib/util/os_util.rb, line 166
def temp_git_remote_location()
  File::join(dtk_local_folder, 'temp_remote_location')
end
test_clone_location() click to toggle source
# File lib/util/os_util.rb, line 178
def test_clone_location()
  clone_base_path(:test_module)
end
which(cmd) click to toggle source
# File lib/util/os_util.rb, line 298
def 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
  return nil
end
wrap(text, wrap_at) click to toggle source
# File lib/util/os_util.rb, line 348
def wrap(text, wrap_at)
  wrapped = [ ]
  text.each_line do |line|
  # take into account color escape sequences when wrapping
  wrap_at = wrap_at + (line.length - actual_length(line))
  while line =~ /([^\n]{#{wrap_at + 1},})/
    search  = $1.dup
    replace = $1.dup
    if index = replace.rindex(" ", wrap_at)
      replace[index, 1] = "\n"
      replace.sub!(/\n[ \t]+/, "\n")
      line.sub!(search, replace)
    else
      line[$~.begin(1) + wrap_at, 0] = "\n"
    end
  end
  wrapped << line
  end
  return wrapped.join
end

Private Class Methods

clone_base_path(module_type) click to toggle source
# File lib/util/os_util.rb, line 192
def clone_base_path(module_type)

  path =
    case module_type.to_sym
      when :service_module then Config[:service_location]
      when :component_module then Config[:module_location]
      when :test_module then Config[:test_module_location]
      when :assembly_module then Config[:assembly_module_base_location]
      else raise Client::DtkError, "Unexpected module_type (#{module_type}) when determining base path"
    end


  final_path = path && path.start_with?('/') ? path : "#{dtk_local_folder}#{path}"
  # remove last slash if set in configuration by mistake
  final_path.gsub(/\/$/,'')
end
seperator() click to toggle source
# File lib/util/os_util.rb, line 379
def seperator
  return (is_windows? ? "\\" : "/")
end