2015-03-13 20:59:17 -04:00
|
|
|
require "fileutils"
|
|
|
|
require "tmpdir"
|
2012-03-04 16:48:00 -08:00
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# Homebrew extends Ruby's `FileUtils` to make our code more readable.
|
|
|
|
# @see http://ruby-doc.org/stdlib-1.8.7/libdoc/fileutils/rdoc/FileUtils.html Ruby's FileUtils API
|
2014-06-09 21:36:36 -05:00
|
|
|
module FileUtils
|
2012-03-04 16:48:00 -08:00
|
|
|
# Create a temporary directory then yield. When the block returns,
|
|
|
|
# recursively delete the temporary directory.
|
2015-08-03 13:09:07 +01:00
|
|
|
def mktemp(prefix = name)
|
2015-03-13 20:59:17 -04:00
|
|
|
prev = pwd
|
|
|
|
tmp = Dir.mktmpdir(prefix, HOMEBREW_TEMP)
|
2015-03-10 23:26:22 -04:00
|
|
|
|
|
|
|
begin
|
2015-03-13 20:59:17 -04:00
|
|
|
cd(tmp)
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
cd(prev)
|
|
|
|
end
|
2015-03-10 23:26:22 -04:00
|
|
|
ensure
|
2015-04-28 16:32:11 +08:00
|
|
|
ignore_interrupts { rm_rf(tmp) }
|
2015-03-10 23:26:22 -04:00
|
|
|
end
|
2012-03-04 16:48:00 -08:00
|
|
|
end
|
2014-06-09 21:36:36 -05:00
|
|
|
module_function :mktemp
|
2012-03-04 16:48:00 -08:00
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# @private
|
2014-06-09 21:36:36 -05:00
|
|
|
alias_method :old_mkdir, :mkdir
|
2015-08-29 10:56:24 +01:00
|
|
|
|
|
|
|
# A version of mkdir that also changes to that folder in a block.
|
2015-08-03 13:09:07 +01:00
|
|
|
def mkdir(name, &_block)
|
2014-06-09 21:36:36 -05:00
|
|
|
old_mkdir(name)
|
2012-03-04 16:48:00 -08:00
|
|
|
if block_given?
|
|
|
|
chdir name do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-09 21:36:36 -05:00
|
|
|
module_function :mkdir
|
2012-03-04 16:48:00 -08:00
|
|
|
|
2012-09-27 09:52:06 -05:00
|
|
|
# The #copy_metadata method in all current versions of Ruby has a
|
|
|
|
# bad bug which causes copying symlinks across filesystems to fail;
|
|
|
|
# see #14710.
|
|
|
|
# This was resolved in Ruby HEAD after the release of 1.9.3p194, but
|
2013-10-06 14:06:07 -07:00
|
|
|
# never backported into the 1.9.3 branch. Fixed in 2.0.0.
|
2012-09-27 09:52:06 -05:00
|
|
|
# The monkey-patched method here is copied directly from upstream fix.
|
2013-10-06 14:06:07 -07:00
|
|
|
if RUBY_VERSION < "2.0.0"
|
2015-08-29 10:56:24 +01:00
|
|
|
# @private
|
2012-09-27 09:52:06 -05:00
|
|
|
class Entry_
|
2013-03-28 17:37:29 -05:00
|
|
|
alias_method :old_copy_metadata, :copy_metadata
|
2012-09-27 09:52:06 -05:00
|
|
|
def copy_metadata(path)
|
2015-08-03 13:09:07 +01:00
|
|
|
st = lstat
|
|
|
|
unless st.symlink?
|
2012-09-27 09:52:06 -05:00
|
|
|
File.utime st.atime, st.mtime, path
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
if st.symlink?
|
|
|
|
begin
|
|
|
|
File.lchown st.uid, st.gid, path
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
File.chown st.uid, st.gid, path
|
|
|
|
end
|
|
|
|
rescue Errno::EPERM
|
|
|
|
# clear setuid/setgid
|
|
|
|
if st.symlink?
|
|
|
|
begin
|
|
|
|
File.lchmod st.mode & 01777, path
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
File.chmod st.mode & 01777, path
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if st.symlink?
|
|
|
|
begin
|
|
|
|
File.lchmod st.mode, path
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
File.chmod st.mode, path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# Run `scons` using a Homebrew-installed version rather than whatever is in the `PATH`.
|
2015-08-03 13:09:07 +01:00
|
|
|
def scons(*args)
|
2014-03-14 16:41:57 -05:00
|
|
|
system Formulary.factory("scons").opt_bin/"scons", *args
|
2014-02-28 07:20:52 -08:00
|
|
|
end
|
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# Run the `rake` from the `ruby` Homebrew is using rather than whatever is in the `PATH`.
|
2015-08-03 13:09:07 +01:00
|
|
|
def rake(*args)
|
|
|
|
system RUBY_BIN/"rake", *args
|
2013-02-02 11:24:55 -06:00
|
|
|
end
|
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
if method_defined?(:ruby)
|
|
|
|
# @private
|
|
|
|
alias_method :old_ruby, :ruby
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the `ruby` Homebrew is using rather than whatever is in the `PATH`.
|
2015-08-03 13:09:07 +01:00
|
|
|
def ruby(*args)
|
2013-03-10 17:02:06 +00:00
|
|
|
system RUBY_PATH, *args
|
2013-02-02 11:24:55 -06:00
|
|
|
end
|
2014-02-27 21:47:38 -06:00
|
|
|
|
2015-08-29 10:56:24 +01:00
|
|
|
# Run `xcodebuild` without Homebrew's compiler environment variables set.
|
2015-08-03 13:09:07 +01:00
|
|
|
def xcodebuild(*args)
|
2014-02-27 21:47:38 -06:00
|
|
|
removed = ENV.remove_cc_etc
|
|
|
|
system "xcodebuild", *args
|
|
|
|
ensure
|
|
|
|
ENV.update(removed)
|
|
|
|
end
|
2012-03-04 16:48:00 -08:00
|
|
|
end
|