Skip to content

Commit

Permalink
Merge pull request #112 from opscode-cookbooks/jdmundrawala/env-path-…
Browse files Browse the repository at this point in the history
…hack

Expand environment variables in path
  • Loading branch information
Adam Edwards committed Sep 22, 2014
2 parents 712f0de + 9b5c07b commit 873d4bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libraries/windows_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
# limitations under the License.

require 'uri'
require 'Win32API' if Chef::Platform.windows?
require 'chef/exceptions'

module Windows
module Helper

AUTO_RUN_KEY = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'.freeze unless defined?(AUTO_RUN_KEY)
ENV_KEY = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.freeze unless defined?(ENV_KEY)
ExpandEnvironmentStrings = Win32API.new('kernel32', 'ExpandEnvironmentStrings', ['P', 'P', 'L'], 'L') if Chef::Platform.windows?

# returns windows friendly version of the provided path,
# ensures backslashes are used everywhere
Expand Down Expand Up @@ -82,6 +85,17 @@ def cached_file(source, checksum=nil, windows_path=true)
end
end

# Expands the environment variables
def expand_env_vars(path)
# We pick 32k because that is the largest it could be:
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms724265%28v=vs.85%29.aspx
buf = 0.chr * 32 * 1024 # 32k
if ExpandEnvironmentStrings.call(path, buf, buf.length) == 0
raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings (received 0)"
end
buf.strip
end

end
end

Expand Down
16 changes: 16 additions & 0 deletions providers/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@
#
use_inline_resources if defined?(use_inline_resources)

include Windows::Helper

action :add do
env "path" do
action :modify
delim ::File::PATH_SEPARATOR
value new_resource.path
notifies :run, "ruby_block[fix ruby ENV['PATH']]", :immediately
end

# The windows Env provider does not correctly expand variables in
# the PATH environment variable. Ruby expects these to be expanded.
# This is a temporary fix for that.
#
# Follow at https://github.com/opscode/chef/pull/1876
#
ruby_block "fix ruby ENV['PATH']" do
block do
ENV['PATH'] = expand_env_vars(ENV['PATH'])
end
action :nothing
end
end

Expand Down

0 comments on commit 873d4bf

Please sign in to comment.