Module: DTK::Client::OsUtil

Extended by:
Auxiliary
Defined in:
lib/util/os_util.rb

Constant Summary collapse

DTK_IDENTITY_FILE =
'dtk.pem'

Class Method Summary collapse

Methods included from Auxiliary

cap_form, snake_form

Class Method Details

.actual_length(string_with_escapes) ⇒ Object



369
370
371
# 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_locationObject



182
183
184
# File 'lib/util/os_util.rb', line 182

def assembly_module_base_location()
  clone_base_path(:assembly_module)
end

.backups_locationObject



186
187
188
189
190
# 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_screenObject



59
60
61
62
# File 'lib/util/os_util.rb', line 59

def clear_screen
  command = is_windows? ? "cls" : "clear"
  system(command)
end

.clone_base_path(module_type) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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

.colorize(message, color) ⇒ Object

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



247
248
249
250
# 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_locationObject



170
171
172
# File 'lib/util/os_util.rb', line 170

def component_clone_location()
  clone_base_path(:component_module)
end

.current_dirObject



98
99
100
101
# File 'lib/util/os_util.rb', line 98

def current_dir
  current_dir = Dir.getwd()
  current_dir.gsub(home_dir, '~')
end

.dev_reload_shellObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# 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_folderObject



86
87
88
# 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_dirObject



64
65
66
# File 'lib/util/os_util.rb', line 64

def dtk_home_dir
  return "#{home_dir}"
end

.dtk_identity_file_locationObject

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



212
213
214
215
216
217
# 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_folderObject



90
91
92
# 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_folderObject

for Windows app folder is already under OS username



78
79
80
81
82
83
84
# 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) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object



103
104
105
# File 'lib/util/os_util.rb', line 103

def genv(name)
  return ENV[name.to_s.upcase].gsub(/\\/,'/')
end

.get_dtk_class(command_name) ⇒ Object

This will return class object from DTK::Client namespace



69
70
71
72
73
74
75
# 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_locationObject



55
56
57
# File 'lib/util/os_util.rb', line 55

def get_log_location
  return "#{dtk_local_folder}"
end

.get_temp_locationObject



44
45
46
# File 'lib/util/os_util.rb', line 44

def get_temp_location
  is_windows? ? genv(:temp) : '/tmp'
end

.home_dirObject



94
95
96
# File 'lib/util/os_util.rb', line 94

def home_dir
  return (is_windows? ? "#{genv(:homedrive)}#{genv(:homepath)}" : "#{genv(:home)}")
end

.is_linux?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/util/os_util.rb', line 40

def is_linux?
  RUBY_PLATFORM.downcase.include?('linux')
end

.is_mac?Boolean

Returns:

  • (Boolean)


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

def is_mac?
  RUBY_PLATFORM.downcase.include?('darwin')
end

.is_windows?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/util/os_util.rb', line 36

def is_windows?
  RUBY_PLATFORM =~ /mswin|mingw|cygwin/
end

.local_component_module_listObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# 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 = {}) ⇒ Object



118
119
120
121
122
# 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 = {}) ⇒ Object

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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 = {}) ⇒ Object



146
147
148
149
150
151
# 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) ⇒ Object



48
49
50
51
52
53
# 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

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



258
259
260
# File 'lib/util/os_util.rb', line 258

def print(message, color = :white)
  puts colorize(message, color)
end


266
267
268
# File 'lib/util/os_util.rb', line 266

def print_deprecate_message(message)
  print_warning(message)
end


262
263
264
# File 'lib/util/os_util.rb', line 262

def print_warning(message)
  print(message, :yellow)
end

.put_warning(prefix, text, color) ⇒ Object



338
339
340
341
342
343
344
345
346
# 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) ⇒ Object



373
374
375
# File 'lib/util/os_util.rb', line 373

def remove_html_tags(string_with_tags)
  string_with_tags.gsub(/<\/?[^>]+>/, '')
end

.service_clone_locationObject



174
175
176
# File 'lib/util/os_util.rb', line 174

def service_clone_location()
  clone_base_path(:service_module)
end

.suspend_outputObject

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

Example suspend_output do

# some calls

end



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# 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_locationObject



166
167
168
# File 'lib/util/os_util.rb', line 166

def temp_git_remote_location()
  File::join(dtk_local_folder, 'temp_remote_location')
end

.test_clone_locationObject



178
179
180
# File 'lib/util/os_util.rb', line 178

def test_clone_location()
  clone_base_path(:test_module)
end

.which(cmd) ⇒ Object



298
299
300
301
302
303
304
305
306
307
# 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) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# 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