Platform-independent path normalization in guards, etc

What’s a good pattern for platform-independent path normalization in
guards, execute commands, etc.

Are there libs for this by chance? I’m ignorant as I’m not a
cross-platform ruby developer and also trying to do it a “Chef way” vs.
a pure-ruby way if possible.

For example:

if platform?(‘windows’)
diffcmd = 'fc’
catcmd = 'type’
else
diffcmd = 'diff’
catcmd = 'cat’
end

execute “Really, something that HAS TO be an execute” do
not_if “#{diffcmd} some-file other-file”

This breaks on Windows due to ‘/’

command "#{catcmd} #{Chef::Config[:file_cache_path]}/noodle >>
/chef/humor-me"
end

Thanks for ideas.

On Thursday, May 28, 2015 at 4:58 PM, Jeff Blaine wrote:

What’s a good pattern for platform-independent path normalization in
guards, execute commands, etc.

Are there libs for this by chance? I’m ignorant as I’m not a
cross-platform ruby developer and also trying to do it a “Chef way” vs.
a pure-ruby way if possible.

For example:

if platform?(‘windows’)
diffcmd = 'fc’
catcmd = 'type’
else
diffcmd = 'diff’
catcmd = 'cat’
end

execute “Really, something that HAS TO be an execute” do
not_if “#{diffcmd} some-file other-file”

This breaks on Windows due to ‘/’

command "#{catcmd} #{Chef::Config[:file_cache_path]}/noodle >>
/chef/humor-me"
end

Thanks for ideas.
In Chef we have a PathHelper class for this, for example: https://github.com/chef/chef/blob/master/chef-config/lib/chef-config/path_helper.rb#L113

Note that even though it got moved to the chef-config gem, you can (and probably should) access it as Chef::Util::PathHelper https://github.com/chef/chef/blob/master/lib/chef/util/path_helper.rb

HTH,


Daniel DeLeo