Module: Buildr::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/buildr/core/util.rb

Defined Under Namespace

Modules: Gems

Instance Method Summary collapse

Instance Method Details

#java_platform?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/buildr/core/util.rb', line 32

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



72
73
74
75
76
77
78
79
# File 'lib/buildr/core/util.rb', line 72

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

#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.



114
115
116
# File 'lib/buildr/core/util.rb', line 114

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'


102
103
104
105
106
107
108
# File 'lib/buildr/core/util.rb', line 102

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_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"


123
124
125
126
127
128
129
130
131
132
# File 'lib/buildr/core/util.rb', line 123

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.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/buildr/core/util.rb', line 54

def ruby(*args)
  options = Hash === args.last ? args.pop : {}
  cmd = []
  ruby_bin = normalize_path(Config::CONFIG['ruby_install_name'], Config::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

#timestamp(file) ⇒ Object

Return the timestamp of file, without having to create a file task



82
83
84
85
86
87
88
# File 'lib/buildr/core/util.rb', line 82

def timestamp(file)
  if File.exist?(file)
    File.mtime(file)
  else
    Rake::EARLY
  end
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 succifient for our purpose:

For JRuby, the value for RUBY_PLATFORM will always be ‘java’ That’s why this function checks on Config::CONFIG

Returns:

  • (Boolean)


45
46
47
# File 'lib/buildr/core/util.rb', line 45

def win_os?
  Config::CONFIG['host_os'] =~ /windows|cygwin|bccwin|cygwin|djgpp|mingw|mswin|wince/i
end