module Buildr::Util
Public Instance Methods
# File lib/buildr/core/util.rb, line 21 def java_platform? !!(RUBY_PLATFORM =~ /java/) end
Just like File.expand_path, but for windows systems it capitalizes the drive name and ensures backslashes are used
# File lib/buildr/core/util.rb, line 61 def normalize_path(path, *dirs) path = File.expand_path(path, *dirs) if win_os? path.gsub!('/', '\').gsub!(/^[a-zA-Z]+:/) { |s| s.upcase } else path end end
Generally speaking, it's not a good idea to operate on dot files (files starting with dot). These are considered invisible files (.svn, .hg, .irbrc, etc). Dir.glob/FileList ignore them on purpose. There are few cases where we do have to work with them (filter, zip), a better solution is welcome, maybe being more explicit with include. For now, this will do.
# File lib/buildr/core/util.rb, line 111 def recursive_with_dot_files(*dirs) FileList[dirs.map { |dir| File.join(dir, '/**/{*,.*}') }].reject { |file| File.basename(file) =~ /^[.]{1,2}$/ } end
Return the path to the first argument, starting from the path provided by the second argument.
For example:
relative_path('foo/bar', 'foo') => 'bar' relative_path('foo/bar', 'baz') => '../foo/bar' relative_path('foo/bar') => 'foo/bar' relative_path('/foo/bar', 'baz') => '/foo/bar'
# File lib/buildr/core/util.rb, line 99 def relative_path(to, from = '.') to = Pathname.new(to).cleanpath return to.to_s if from.nil? to_path = Pathname.new(File.expand_path(to.to_s, "/")) from_path = Pathname.new(File.expand_path(from.to_s, "/")) to_path.relative_path_from(from_path).to_s end
Replace the file extension, e.g.,
replace_extension("foo.zip", "txt") => "foo.txt"
# File lib/buildr/core/util.rb, line 120 def replace_extension(filename, new_ext) ext = File.extname(filename) if filename =~ /\.$/ filename + new_ext elsif ext == "" filename + "." + new_ext else filename[0..-ext.length] + new_ext end end
Runs Ruby with these command line arguments. The last argument may be a hash, supporting the following keys:
:command -- Runs the specified script (e.g., :command=>'gem') :sudo -- Run as sudo on operating systems that require it. :verbose -- Override Rake's verbose flag.
# File lib/buildr/core/util.rb, line 43 def ruby(*args) options = Hash === args.last ? args.pop : {} cmd = [] ruby_bin = normalize_path(RbConfig::CONFIG['ruby_install_name'], RbConfig::CONFIG['bindir']) if options.delete(:sudo) && !(win_os? || Process.uid == File.stat(ruby_bin).uid) cmd << 'sudo' << '-u' << "##{File.stat(ruby_bin).uid}" end cmd << ruby_bin cmd << '-S' << options.delete(:command) if options[:command] cmd.concat args.flatten cmd.push options sh *cmd do |ok, status| ok or fail "Command ruby failed with status (#{status ? status.exitstatus : 'unknown'}): [#{cmd.join(" ")}]" end end
Return the timestamp of file, without having to create a file task
# File lib/buildr/core/util.rb, line 71 def timestamp(file) if File.exist?(file) File.mtime(file) else Rake::EARLY end end
# File lib/buildr/core/util.rb, line 79 def uuid return SecureRandom.uuid if SecureRandom.respond_to?(:uuid) ary = SecureRandom.random_bytes(16).unpack("NnnnnN") ary[2] = (ary[2] & 0x0fff) | 0x4000 ary[3] = (ary[3] & 0x3fff) | 0x8000 "%08x-%04x-%04x-%04x-%04x%08x" % ary end
In order to determine if we are running on a windows OS, prefer this function instead of using Gem.win_platform?.
Gem.win_platform? only checks these RUBY_PLATFORM global, that in some cases like when running on JRuby is not sufficient for our purpose:
For JRuby, the value for RUBY_PLATFORM will always be 'java' That's why this function checks on Config::CONFIG
# File lib/buildr/core/util.rb, line 34 def win_os? !!(RbConfig::CONFIG['host_os'] =~ /windows|cygwin|bccwin|cygwin|djgpp|mingw|mswin|mswin32|wince/i) end