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
|
# File 'lib/envbash/read.rb', line 12
def EnvBash.read(envbash, bash: 'bash', env: ENV, missing_ok: false, fixups: FIXUPS)
begin
File.open(envbash).close
rescue Errno::ENOENT
return if missing_ok
raise
end
inline = " set -a\n source \#{envbash.shellescape} >/dev/null\n \#{Gem.ruby.shellescape} -e 'p ENV'\n EOT\n\n # Process.spawn treats env as overriding ENV, and anything that should be\n # omitted needs to have a nil value. If env == ENV then this is a noop.\n env = Hash[ENV.keys.map {|k| [k, nil]}].merge(env)\n\n # run the inline script with bash -c, capturing stdout. if there is any\n # error output from env.bash, it will pass through to stderr.\n # exit status is ignored.\n output, _ = Open3.capture2(env, 'bash', '-c', inline, :in=>\"/dev/null\")\n\n # the only stdout from the inline script should be\n # `p ENV` so there should be no syntax errors eval'ing this. however there\n # will be no output to eval if the sourced env.bash exited early, and that\n # indicates script failure.\n raise ScriptExitedEarly if output.empty?\n\n # the eval'd output should return a hash.\n nenv = eval(output)\n\n # there are a few environment variables that vary between this process and\n # running the inline script with bash -c, but are certainly not part of the\n # intentional settings in env.bash.\n for f in fixups\n if env[f] # not .include? because env might have nil values\n nenv[f] = env[f]\n else\n nenv.delete(f)\n end\n end\n\n nenv\nend\n"
|