Class: VagrantPlugins::GlobalStatus::GlobalEnvironment
- Inherits:
-
Object
- Object
- VagrantPlugins::GlobalStatus::GlobalEnvironment
- Defined in:
- lib/vagrant-global-status/global_environment.rb
Constant Summary collapse
- RUBY_VERSION_MANAGERS =
[ /\/\.rbenv\//, /\/\.rvm\// ]
- ORIGINAL_ENV =
ENV.to_hash
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, data) ⇒ GlobalEnvironment
constructor
A new instance of GlobalEnvironment.
- #plugin_sources? ⇒ Boolean
- #remove_from_path(regex) ⇒ Object
- #run_vagrant_status ⇒ Object
-
#status(all = false) ⇒ Object
REFACTOR: Extract a machine class.
- #status_line(status, length) ⇒ Object
- #vagrant_status ⇒ Object
- #with_clean_ruby_env(regex) ⇒ Object
- #with_clean_vagrant_env ⇒ Object
- #with_original_env ⇒ Object
Constructor Details
#initialize(path, data) ⇒ GlobalEnvironment
Returns a new instance of GlobalEnvironment.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/vagrant-global-status/global_environment.rb', line 8 def initialize(path, data) @path = Pathname(path) @machine_names = Array(data['machines']).map{|machine| machine['name']} @created_at = {} data['machines'].each do |machine| ctime = machine['created_at'] if ctime =~ /[\d]+/ ctime = Time.at(ctime.to_i).to_s end @created_at.store(machine['name'], ctime) end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/vagrant-global-status/global_environment.rb', line 4 def path @path end |
Instance Method Details
#plugin_sources? ⇒ Boolean
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vagrant-global-status/global_environment.rb', line 120 def plugin_sources? return @plugin_sources if defined?(@plugin_sources) gemfile_dir = @path gemfile = gemfile_dir.join('Gemfile') # If the parent is the same dir, we reached the root dir while !gemfile.file? && gemfile_dir.parent != gemfile_dir gemfile_dir = gemfile_dir.parent gemfile = gemfile_dir.join('Gemfile') end @plugin_sources = gemfile.file? ? gemfile.read =~ /gem\s+['"]vagrant['"]/ : false end |
#remove_from_path(regex) ⇒ Object
101 102 103 104 105 |
# File 'lib/vagrant-global-status/global_environment.rb', line 101 def remove_from_path(regex) ENV['PATH'].split(':'). reject{ |p| p =~ regex }. join(':') end |
#run_vagrant_status ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/vagrant-global-status/global_environment.rb', line 107 def run_vagrant_status if File.exists?(@path) cmd = "cd #{@path} && " if plugin_sources? cmd << "bundle exec " end cmd << "vagrant status" `#{cmd}` else `echo ""` end end |
#status(all = false) ⇒ Object
REFACTOR: Extract a machine class
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vagrant-global-status/global_environment.rb', line 22 def status(all = false) return " Not found!" unless File.exists?(@path) matches = vagrant_status.scan(/(\w[\w-]+)\s+(\w[\w\s]+)\s+\((\w+)\)/) matches.map do |vm, status, provider| if all || (@machine_names.include?(vm) and status == "running") provider = "(#{provider})" " #{vm.ljust(12)} #{status_line(status, 12)} #{provider.ljust(14)} #{@created_at[vm]}" end end.compact.join("\n") end |
#status_line(status, length) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vagrant-global-status/global_environment.rb', line 34 def status_line(status, length) case status when "running" then color = 32 # green when "not running", "poweroff" then color = 33 # yellow when "not created" then color = 34 # blue else return status.ljust(length) end sprintf("\e[%dm%s\e[m", color, status.ljust(length)) end |
#vagrant_status ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/vagrant-global-status/global_environment.rb', line 48 def vagrant_status return @vagrant_status if @vagrant_status # Plugins development if defined?(::Bundler) if plugin_sources? Bundler.with_clean_env do @vagrant_status = run_vagrant_status end else Bundler.with_clean_env do with_clean_ruby_env(RUBY_VERSION_MANAGERS) do @vagrant_status = run_vagrant_status end end end # Vagrant installer else if plugin_sources? with_clean_vagrant_env do @vagrant_status = run_vagrant_status end else @vagrant_status = run_vagrant_status end end end |
#with_clean_ruby_env(regex) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/vagrant-global-status/global_environment.rb', line 90 def with_clean_ruby_env(regex) with_original_env do Array(regex).each do |reg| ENV['PATH'] = remove_from_path(reg) end ENV.delete('GEM_HOME') ENV.delete('GEM_PATH') yield end end |
#with_clean_vagrant_env ⇒ Object
84 85 86 87 88 |
# File 'lib/vagrant-global-status/global_environment.rb', line 84 def with_clean_vagrant_env with_clean_ruby_env(/\/vagrant\//) do yield end end |
#with_original_env ⇒ Object
78 79 80 81 82 |
# File 'lib/vagrant-global-status/global_environment.rb', line 78 def with_original_env yield ensure ENV.replace(ORIGINAL_ENV) end |