2011-05-18 13:21:12 -07:00
|
|
|
require 'formula'
|
|
|
|
|
2011-09-12 23:28:36 -07:00
|
|
|
require 'stringio'
|
|
|
|
module ScriptDataReader
|
|
|
|
# This module contains a method for extracting the contents of DATA from a
|
|
|
|
# Ruby file other than the script containing the currently executing
|
|
|
|
# function. Many thanks to Glenn Jackman's Stackoverflow answer which
|
|
|
|
# provided this code:
|
|
|
|
#
|
|
|
|
# http://stackoverflow.com/questions/2156629/can-i-access-the-data-from-a-required-script-in-ruby/2157556#2157556
|
|
|
|
def self.load(filename)
|
|
|
|
data = StringIO.new
|
|
|
|
File.open(filename) do |f|
|
|
|
|
begin
|
|
|
|
line = f.gets
|
|
|
|
end until line.nil? or line.match(/^__END__$/)
|
|
|
|
while line = f.gets
|
|
|
|
data << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
data.rewind
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Need to tweak the Formula class slightly so that patching is option and `DATA`
|
|
|
|
# patches work correctly.
|
|
|
|
class Formula
|
|
|
|
# Create a reference to the original Formula.patch method and then override
|
|
|
|
# so that paching only happens if the user asks.
|
|
|
|
alias do_patch patch
|
|
|
|
def patch
|
2012-09-11 20:55:16 -04:00
|
|
|
if ARGV.flag? '--patch'
|
2013-08-14 20:11:11 -05:00
|
|
|
begin
|
|
|
|
old_verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
Formula.const_set 'DATA', ScriptDataReader.load(path)
|
|
|
|
ensure
|
|
|
|
$VERBOSE = old_verbose
|
|
|
|
end
|
2011-09-12 23:28:36 -07:00
|
|
|
|
|
|
|
do_patch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-18 13:21:12 -07:00
|
|
|
module Homebrew extend self
|
2013-08-14 22:13:26 -05:00
|
|
|
def unpack_usage; <<-EOS.undent
|
|
|
|
Usage: brew unpack [-pg] [--destdir=path/to/extract/in] <formulae ...>
|
2011-05-18 13:21:12 -07:00
|
|
|
|
2013-08-14 22:13:26 -05:00
|
|
|
Unpack formulae source code for inspection.
|
2011-05-18 13:21:12 -07:00
|
|
|
|
2013-08-14 22:13:26 -05:00
|
|
|
Formulae archives will be extracted to subfolders inside the current working
|
|
|
|
directory or a directory specified by `--destdir`. If the `-p` option is
|
|
|
|
supplied, patches will also be downloaded and applied. If the `-g` option is
|
|
|
|
specified a git repository is created and all files added so that you can diff
|
|
|
|
changes.
|
2011-05-18 13:21:12 -07:00
|
|
|
EOS
|
2013-08-14 22:13:26 -05:00
|
|
|
end
|
2011-05-18 13:21:12 -07:00
|
|
|
|
2013-08-14 22:13:26 -05:00
|
|
|
def unpack
|
2012-09-11 20:55:16 -04:00
|
|
|
abort unpack_usage if ARGV.empty?
|
2011-05-18 13:21:12 -07:00
|
|
|
|
2011-09-12 23:28:36 -07:00
|
|
|
formulae = ARGV.formulae
|
2011-05-18 13:21:12 -07:00
|
|
|
raise FormulaUnspecifiedError if formulae.empty?
|
|
|
|
|
2013-08-14 20:11:40 -05:00
|
|
|
if (dir = ARGV.value('destdir')).nil?
|
|
|
|
unpack_dir = Pathname.pwd
|
2011-05-18 13:21:12 -07:00
|
|
|
else
|
2013-08-14 22:13:55 -05:00
|
|
|
unpack_dir = Pathname.new(dir)
|
2011-05-18 13:21:12 -07:00
|
|
|
unpack_dir.mkpath unless unpack_dir.exist?
|
|
|
|
end
|
2011-09-12 23:28:36 -07:00
|
|
|
|
2012-07-28 11:36:08 -07:00
|
|
|
raise "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real?
|
2011-05-18 13:21:12 -07:00
|
|
|
|
|
|
|
formulae.each do |f|
|
2011-09-12 23:28:36 -07:00
|
|
|
# Create a nice name for the stage folder.
|
|
|
|
stage_dir = unpack_dir + [f.name, f.version].join('-')
|
2011-05-18 13:21:12 -07:00
|
|
|
|
2012-09-11 20:55:16 -04:00
|
|
|
if stage_dir.exist?
|
2013-05-22 09:38:59 -05:00
|
|
|
raise "Destination #{stage_dir} already exists!" unless ARGV.force?
|
2012-09-11 20:55:16 -04:00
|
|
|
rm_rf stage_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
oh1 "Unpacking #{f.name} to: #{stage_dir}"
|
|
|
|
ENV['VERBOSE'] = '1' # show messages about tar
|
2011-09-12 23:28:36 -07:00
|
|
|
f.brew do
|
2013-08-14 22:14:34 -05:00
|
|
|
entries = Dir['*']
|
2013-10-26 21:48:14 -07:00
|
|
|
cd entries.first if entries.length == 1 && File.directory?(entries.first)
|
2012-10-16 00:53:15 -05:00
|
|
|
cp_r getwd, stage_dir
|
2012-09-11 20:55:16 -04:00
|
|
|
end
|
|
|
|
ENV['VERBOSE'] = nil
|
|
|
|
|
|
|
|
if ARGV.switch? 'g'
|
|
|
|
ohai "Setting up git repository"
|
|
|
|
cd stage_dir
|
|
|
|
system "git init -q"
|
|
|
|
system "git add -A"
|
|
|
|
system 'git commit -qm"Vanilla"'
|
2011-05-18 13:21:12 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Here is the actual code that gets run when `brew` loads this external
|
|
|
|
# command.
|
|
|
|
Homebrew.unpack
|