diff options
| author | Charlie Sharpsteen | 2011-09-12 23:28:36 -0700 |
|---|---|---|
| committer | Charlie Sharpsteen | 2011-09-12 23:32:01 -0700 |
| commit | 5790935b1267bbd8dac3fd2564b3dd9c46b5dae9 (patch) | |
| tree | 0bd231a1c49ab604137b2156bb67e866856413af /Library | |
| parent | b0f55665666642f64034c2451e7c5f0d362539a3 (diff) | |
| download | brew-5790935b1267bbd8dac3fd2564b3dd9c46b5dae9.tar.bz2 | |
brew-unpack: Apply patches and use formula options
`brew-unpack` now runs `Formula.brew` and copies the stage directory to the
unpack location. This provides two improvements:
- Patches may be applied. The `patch` metho of the Formula class is
overloaded such that patches are only applied if the `--patch` option is
passed to `brew unpack`.
- Formula can respond better to flags passed by `ARGV`.
Some trickery is pulled to ensure `DATA` is correctly set for each unpacked
formula.
Diffstat (limited to 'Library')
| -rwxr-xr-x | Library/Contributions/examples/brew-unpack.rb | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/Library/Contributions/examples/brew-unpack.rb b/Library/Contributions/examples/brew-unpack.rb index ab3438d1a..d0d505b0a 100755 --- a/Library/Contributions/examples/brew-unpack.rb +++ b/Library/Contributions/examples/brew-unpack.rb @@ -1,14 +1,56 @@ require 'formula' +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 + if ARGV.include? '--patch' + # Yes Ruby, we are about to redefine a constant. Just breathe. + orig_v = $VERBOSE; $VERBOSE = nil + Formula.const_set 'DATA', ScriptDataReader.load(path) + $VERBOSE = orig_v + + do_patch + end + end +end + module Homebrew extend self def unpack unpack_usage = <<-EOS -Usage: brew unpack [--destdir=path/to/extract/in] <formulae ...> +Usage: brew unpack [--patch] [--destdir=path/to/extract/in] <formulae ...> Unpack formulae source code for inspection. Formulae archives will be extracted to subfolders inside the current working -directory or a directory specified by `--destdir`. +directory or a directory specified by `--destdir`. If the `--patch` option is +supplied, patches will also be downloaded and applied. EOS if ARGV.empty? @@ -16,7 +58,7 @@ directory or a directory specified by `--destdir`. exit 0 end - formulae = ARGV.named + formulae = ARGV.formulae raise FormulaUnspecifiedError if formulae.empty? unpack_dir = ARGV.options_only.select {|o| o.start_with? "--destdir="} @@ -26,24 +68,18 @@ directory or a directory specified by `--destdir`. unpack_dir = Pathname.new(unpack_dir.first.split('=')[1]).realpath unpack_dir.mkpath unless unpack_dir.exist? end + raise "Cannot write to #{unpack_dir}" unless unpack_dir.writable? formulae.each do |f| - f = Formula.factory(f) + # Create a nice name for the stage folder. + stage_dir = unpack_dir + [f.name, f.version].join('-') + raise "Destination #{stage_dir} allready exists!" if stage_dir.exist? - unless f.downloader.kind_of? CurlDownloadStrategy - stage_dir = unpack_dir + [f.name, f.version].join('-') - stage_dir.mkpath unless stage_dir.exist? - else - stage_dir = unpack_dir + f.brew do + oh1 "Unpacking #{f.name} to: #{stage_dir}" + cp_r Dir.getwd, stage_dir end - - # Can't use a do block here without DownloadStrategy do blocks throwing: - # warning: conflicting chdir during another chdir block - Dir.chdir stage_dir - f.downloader.fetch - f.downloader.stage - Dir.chdir unpack_dir end end end |
