Class: VagrantPlugins::EnvBash::Action::LoadEnvBash

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-envbash/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ LoadEnvBash

Returns a new instance of LoadEnvBash.



5
6
7
8
9
# File 'lib/vagrant-envbash/action.rb', line 5

def initialize(app, env)
  # This method is defined to avoid a stack trace if it doesn't exist.
  # We have no real need to save @app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
76
77
# File 'lib/vagrant-envbash/action.rb', line 11

def call(env)
  # Note that this loads early enough that there's no env[:ui] yet for
  # outputting informational messages. Use the "vagrant env" command
  # instead to investigate.

  # The passed `env` is a wrapper around the env we want.
  e = env[:env]

  # This plugin will can be called multiple times, especially when
  # operating on ids, for example "vagrant status 0fb925d". Don't try to
  # load env.bash until we have a root path, and don't load twice.
  return unless e.root_path
  begin
    return if e.envbash_ran
  rescue
    class << e
      attr_accessor :envbash_ran, :envbash_file, :envbash_loaded,
                    :envbash_before, :envbash_after
    end

    # Avoid running twice.
    e.envbash_ran = true

    # We haven't loaded yet.
    e.envbash_file = nil
    e.envbash_loaded = false

    # Save the original ENV for comparison in "vagrant env"
    e.envbash_before = ENV.to_h
  end

  # Try to find env.bash, since it will be adjacent to Vagrantfile.
  keep_vagrant_envbash_file = !! ENV['VAGRANT_ENVBASH_FILE']
  if ! keep_vagrant_envbash_file
    ENV['VAGRANT_ENVBASH_FILE'] = (e.root_path + 'env.bash').to_s
  end
  e.envbash_file = ENV['VAGRANT_ENVBASH_FILE']

  # Load env.bash. This runs bash inside %x because Ruby uses /bin/sh
  # for backticks.
  new_env = eval %x{bash -c '
    if [[ -s $VAGRANT_ENVBASH_FILE ]]; then
      source "$VAGRANT_ENVBASH_FILE"
    fi
    ruby -e "p ENV"
  '}

  # Ignore modification to SHLVL which is just a shell artifact.
  if e.envbash_before['SHLVL']
    new_env['SHLVL'] = e.envbash_before['SHLVL']
  else
    new_env.delete('SHLVL')
  end

  # Replace the entire ENV (rather than update) so that env.bash can
  # both set and unset vars.
  ENV.replace(new_env)

  # We are loaded!
  e.envbash_loaded = true

  # Remove VAGRANT_ENVBASH_FILE from ENV if we set it.
  ENV.delete('VAGRANT_ENVBASH_FILE') unless keep_vagrant_envbash_file

  # Save the new ENV for comparison in "vagrant env"
  e.envbash_after = ENV.to_h
end