Move brew-unpack to core

This commit is contained in:
Jack Nagel 2014-05-17 17:12:40 -05:00
parent 6908781264
commit fbf1c189a7
5 changed files with 110 additions and 100 deletions

View File

@ -370,6 +370,17 @@ _brew_uninstall ()
__brew_complete_installed
}
_brew_unpack ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
--*)
__brewcomp "--git --patch --destdir="
return
;;
esac
__brew_complete_formulae
}
_brew_update ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
@ -520,6 +531,7 @@ _brew ()
switch) _brew_switch ;;
tap) _brew_complete_tap ;;
uninstall|remove|rm) _brew_uninstall ;;
unpack) _brew_unpack ;;
unpin) __brew_complete_formulae ;;
untap) __brew_complete_tapped ;;
update) _brew_update ;;

View File

@ -1,100 +0,0 @@
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
module UnpackPatch
def patch
return unless ARGV.flag? "--patch"
begin
old_verbose = $VERBOSE
$VERBOSE = nil
Object.const_set "DATA", ScriptDataReader.load(path)
ensure
$VERBOSE = old_verbose
end
super
end
end
module Homebrew extend self
def unpack_usage; <<-EOS.undent
Usage: brew unpack [-pg] [--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`. 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.
EOS
end
def unpack
abort unpack_usage if ARGV.empty?
formulae = ARGV.formulae
raise FormulaUnspecifiedError if formulae.empty?
if (dir = ARGV.value('destdir')).nil?
unpack_dir = Pathname.pwd
else
unpack_dir = Pathname.new(dir)
unpack_dir.mkpath unless unpack_dir.exist?
end
raise "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real?
formulae.each do |f|
f.extend(UnpackPatch)
# Create a nice name for the stage folder.
stage_dir = unpack_dir + [f.name, f.version].join('-')
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.switch? 'g'
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
# Here is the actual code that gets run when `brew` loads this external
# command.
Homebrew.unpack

View File

@ -352,6 +352,18 @@ Note that these flags should only appear after a command.
* `unlinkapps [--local]`:
Removes links created by `brew linkapps`.
* `unpack [--git|--patch] [--destdir=<path>]` <formulae>:
Unpack the source files for <formulae> into subdirectories of the current
working directory. If `--destdir=<path>` is given, the subdirectories will
be created in the directory named by `<path>` instead.
If `--patch` is passed, patches for <formulae> will be applied to the
unpacked source.
If `--git` is passed, a Git repository will be initalized in the unpacked
source. This is useful for creating patches for the software.
* `unpin` <formulae>:
Unpin <formulae>, allowing them to be upgraded by `brew upgrade`. See also
`pin`.

View File

@ -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

View File

@ -369,6 +369,18 @@ Remove symlinks for \fIformula\fR from the Homebrew prefix\. This can be useful
Removes links created by \fBbrew linkapps\fR\.
.
.TP
\fBunpack [\-\-git|\-\-patch] [\-\-destdir=<path>]\fR \fIformulae\fR:
.
.IP
Unpack the source files for \fIformulae\fR into subdirectories of the current working directory\. If \fB\-\-destdir=<path>\fR is given, the subdirectories will be created in the directory named by \fB<path>\fR instead\.
.
.IP
If \fB\-\-patch\fR is passed, patches for \fIformulae\fR will be applied to the unpacked source\.
.
.IP
If \fB\-\-git\fR is passed, a Git repository will be initalized in the unpacked source\. This is useful for creating patches for the software\.
.
.TP
\fBunpin\fR \fIformulae\fR
Unpin \fIformulae\fR, allowing them to be upgraded by \fBbrew upgrade\fR\. See also \fBpin\fR\.
.