Module: VisualStudio::Environment

Defined in:
lib/visual_studio/environment.rb

Constant Summary collapse

SEARCH_DIRECTORIES =
['PATH', 'INCLUDE', 'LIB', 'LIBPATH']

Class Method Summary collapse

Class Method Details

.merge(base, overlay) ⇒ Object



5
6
7
8
9
10
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
# File 'lib/visual_studio/environment.rb', line 5

def self.merge(base, overlay)
  # We merge case insensitively, because Windows. We treat |base| case as canonical
  # unless the variable is only named by |overlay|.
  cased = (overlay.keys + base.keys).uniq.map{|key| [key.upcase, key]}.to_h

  # TODO(mtwilliams): Verify match-up between VCInstallDir and VisualStudioVersion?
  # TODO(mtwilliams): Rederive environment variables based on VCInstallDir and/or WindowsSdkDir.
  env = canonicalize(base).merge(canonicalize(overlay)) do |variable, base, overlay|
    if SEARCH_DIRECTORIES.include? variable.upcase
      # TODO(mtwilliams): Detect new Visual Studio or Windows SDK related
      # paths and replace the old ones based on that.
      base    = base.split(';')
      overlay = overlay.split(';')

      should_include_cwd = base.include?('.') || overlay.include?('.')

      # HACK(mtwilliams): We're using File.expand_path here to "normalize"
      # paths to prevent duplicates, but this could very likely have
      # disastrous effects.
      base = base.reject{|p| p=='.'}.map{|p| File.expand_path(p)}
      overlay = overlay.reject{|p| p=='.'}.map{|p| File.expand_path(p)}

      path = base | overlay
      path = ['.'] + path if should_include_cwd

      path.join(';')
    else
      # Right-hand side takes precedence.
      overlay
    end
  end

  env.map { |canonical, value|
    [cased[canonical], value]
  }.to_h
end