diff options
| author | Jack Nagel | 2014-05-17 17:12:40 -0500 | 
|---|---|---|
| committer | Jack Nagel | 2014-05-17 17:12:40 -0500 | 
| commit | fbf1c189a78370f30d8b6ebea1aa208a95312a25 (patch) | |
| tree | a39ebaba2a51c84d0af92842dcd89df1647eca2d /Library/Homebrew/cmd/unpack.rb | |
| parent | 69087812647a13564c704387e9c0b6dbf5e3614e (diff) | |
| download | brew-fbf1c189a78370f30d8b6ebea1aa208a95312a25.tar.bz2 | |
Move brew-unpack to core
Diffstat (limited to 'Library/Homebrew/cmd/unpack.rb')
| -rw-r--r-- | Library/Homebrew/cmd/unpack.rb | 74 | 
1 files changed, 74 insertions, 0 deletions
| diff --git a/Library/Homebrew/cmd/unpack.rb b/Library/Homebrew/cmd/unpack.rb new file mode 100644 index 000000000..ec9b0a126 --- /dev/null +++ b/Library/Homebrew/cmd/unpack.rb @@ -0,0 +1,74 @@ +require "stringio" +require "formula" + +module Homebrew +  extend self + +  module DATALoader +    # Original code from http://stackoverflow.com/a/2157556/371237 +    def self.load(path) +      data = StringIO.new +      path.open("r") do |f| +        begin +          line = f.gets +        end until line.nil? || /^__END__$/ === line +        data << line while line = f.gets +      end +      data.rewind +      data +    end +  end + +  module UnpackPatch +    def patch +      return unless ARGV.flag? "--patch" + +      begin +        old_verbose, $VERBOSE = $VERBOSE, nil +        Object.const_set "DATA", DATALoader.load(path) +      ensure +        $VERBOSE = old_verbose +      end + +      super +    end +  end + +  def unpack +    formulae = ARGV.formulae +    raise FormulaUnspecifiedError if formulae.empty? + +    if dir = ARGV.value("destdir") +      unpack_dir = Pathname.new(dir) +      unpack_dir.mkpath +    else +      unpack_dir = Pathname.pwd +    end + +    raise "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real? + +    formulae.each do |f| +      f.extend(UnpackPatch) +      stage_dir = unpack_dir.join("#{f.name}-#{f.version}") + +      if stage_dir.exist? +        raise "Destination #{stage_dir} already exists!" unless ARGV.force? +        rm_rf stage_dir +      end + +      oh1 "Unpacking #{f.name} to: #{stage_dir}" + +      ENV['VERBOSE'] = '1' # show messages about tar +      f.brew { cp_r getwd, stage_dir } +      ENV['VERBOSE'] = nil + +      if ARGV.flag? "--git" +        ohai "Setting up git repository" +        cd stage_dir +        system "git", "init", "-q" +        system "git", "add", "-A" +        system "git", "commit", "-q", "-m", "brew-unpack" +      end +    end +  end +end | 
