system_wrapper.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'rbconfig'
  2. class SystemWrapper
  3. # static method for use in defaults
  4. def self.windows?
  5. return ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw/) ? true : false) if defined?(RbConfig)
  6. return ((Config::CONFIG['host_os'] =~ /mswin|mingw/) ? true : false)
  7. end
  8. # class method so as to be mockable for tests
  9. def windows?
  10. return SystemWrapper.windows?
  11. end
  12. def module_eval(string)
  13. return Object.module_eval("\"" + string + "\"")
  14. end
  15. def eval(string)
  16. return eval(string)
  17. end
  18. def search_paths
  19. return ENV['PATH'].split(File::PATH_SEPARATOR)
  20. end
  21. def cmdline_args
  22. return ARGV
  23. end
  24. def env_set(name, value)
  25. ENV[name] = value
  26. end
  27. def env_get(name)
  28. return ENV[name]
  29. end
  30. def time_now
  31. return Time.now.asctime
  32. end
  33. def shell_backticks(command)
  34. return {
  35. :output => `#{command}`.freeze,
  36. :exit_code => ($?.exitstatus).freeze
  37. }
  38. end
  39. def shell_system(command)
  40. system( command )
  41. return {
  42. :output => ''.freeze,
  43. :exit_code => ($?.exitstatus).freeze
  44. }
  45. end
  46. def add_load_path(path)
  47. $LOAD_PATH.unshift(path)
  48. end
  49. def require_file(path)
  50. require(path)
  51. end
  52. def ruby_success
  53. return ($!.nil? || $!.is_a?(SystemExit) && $!.success?)
  54. end
  55. def constants_include?(item)
  56. # forcing to strings provides consistency across Ruby versions
  57. return Object.constants.map{|constant| constant.to_s}.include?(item.to_s)
  58. end
  59. end