Module: Buildr::Util
Overview
:nodoc:
Instance Method Summary collapse
- #java_platform? ⇒ Boolean
-
#normalize_path(path, *dirs) ⇒ Object
Just like File.expand_path, but for windows systems it capitalizes the drive name and ensures backslashes are used.
-
#recursive_with_dot_files(*dirs) ⇒ Object
Generally speaking, it’s not a good idea to operate on dot files (files starting with dot).
-
#relative_path(to, from = '.') ⇒ Object
Return the path to the first argument, starting from the path provided by the second argument.
-
#replace_extension(filename, new_ext) ⇒ Object
:call-seq: replace_extension(filename) => filename_with_updated_extension.
-
#ruby(*args) ⇒ Object
Runs Ruby with these command line arguments.
-
#timestamp(file) ⇒ Object
Return the timestamp of file, without having to create a file task.
- #uuid ⇒ Object
-
#win_os? ⇒ Boolean
In order to determine if we are running on a windows OS, prefer this function instead of using Gem.win_platform?.
Instance Method Details
#java_platform? ⇒ Boolean
21 22 23 |
# File 'lib/buildr/core/util.rb', line 21 def java_platform? !!(RUBY_PLATFORM =~ /java/) end |
#normalize_path(path, *dirs) ⇒ Object
Just like File.expand_path, but for windows systems it capitalizes the drive name and ensures backslashes are used
61 62 63 64 65 66 67 68 |
# File 'lib/buildr/core/util.rb', line 61 def normalize_path(path, *dirs) path = File.(path, *dirs) if win_os? path.gsub!('/', '\\').gsub!(/^[a-zA-Z]+:/) { |s| s.upcase } else path end end |
#recursive_with_dot_files(*dirs) ⇒ Object
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.
111 112 113 |
# 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 |
#relative_path(to, from = '.') ⇒ Object
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'
99 100 101 102 103 104 105 |
# 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.(to.to_s, "/")) from_path = Pathname.new(File.(from.to_s, "/")) to_path.relative_path_from(from_path).to_s end |
#replace_extension(filename, new_ext) ⇒ Object
:call-seq:
replace_extension(filename) => filename_with_updated_extension
Replace the file extension, e.g.,
replace_extension("foo.zip", "txt") => "foo.txt"
120 121 122 123 124 125 126 127 128 129 |
# 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 |
#ruby(*args) ⇒ Object
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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/buildr/core/util.rb', line 43 def ruby(*args) = Hash === args.last ? args.pop : {} cmd = [] ruby_bin = normalize_path(RbConfig::CONFIG['ruby_install_name'], RbConfig::CONFIG['bindir']) if .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' << .delete(:command) if [:command] cmd.concat args.flatten cmd.push sh *cmd do |ok, status| ok or fail "Command ruby failed with status (#{status ? status.exitstatus : 'unknown'}): [#{cmd.join(" ")}]" end end |
#timestamp(file) ⇒ Object
Return the timestamp of file, without having to create a file task
71 72 73 74 75 76 77 |
# File 'lib/buildr/core/util.rb', line 71 def (file) if File.exist?(file) File.mtime(file) else Rake::EARLY end end |
#uuid ⇒ Object
79 80 81 82 83 84 85 |
# 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 |
#win_os? ⇒ Boolean
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
34 35 36 |
# 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 |