Additional fixups for extract command
- Rework command line options - Make specifying a version optional - Remove stdout option and require a tap to be specified - Do not allow user to extract into homebrew/core - Rework new class name generation to use existing Formulary tools
This commit is contained in:
parent
bd2ac70c0f
commit
1003aa72c9
@ -1,14 +1,15 @@
|
|||||||
#: * `extract` [`--force`] [`--stdout`] <formula> <version> [<tap>]:
|
#: * `extract` [`--force`] <formula> `--tap=`<tap> [`--version=`<version>]:
|
||||||
#: Looks through repository history to find the <version> of <formula> and
|
#: Looks through repository history to find the <version> of <formula> and
|
||||||
#: creates a copy in <tap>/Formula/<formula>@<version>.rb. If the tap is
|
#: creates a copy in <tap>/Formula/<formula>@<version>.rb. If the tap is
|
||||||
#: not installed yet, attempts to install/clone the tap before continuing.
|
#: not installed yet, attempts to install/clone the tap before continuing.
|
||||||
|
#: A tap must be passed through `--tap` in order for `extract` to work.
|
||||||
#:
|
#:
|
||||||
#: If `--force` is passed, the file at the destination will be overwritten
|
#: If `--force` is passed, the file at the destination will be overwritten
|
||||||
#: if it already exists. Otherwise, existing files will be preserved.
|
#: if it already exists. Otherwise, existing files will be preserved.
|
||||||
#:
|
#:
|
||||||
#: If `--stdout` is passed, the file will be written to stdout on the
|
#: If an argument is passed through `--version`, <version> of <formula>
|
||||||
#: terminal instead of written to a file. A <tap> cannot be passed when
|
#: will be extracted and placed in the destination tap. Otherwise, the most
|
||||||
#: using `--stdout`.
|
#: recent version that can be found will be used.
|
||||||
|
|
||||||
require "utils/git"
|
require "utils/git"
|
||||||
require "formula_versions"
|
require "formula_versions"
|
||||||
@ -20,51 +21,65 @@ module Homebrew
|
|||||||
|
|
||||||
def extract
|
def extract
|
||||||
Homebrew::CLI::Parser.parse do
|
Homebrew::CLI::Parser.parse do
|
||||||
switch "--stdout", description: "Output to stdout on terminal instead of file"
|
flag "--tap="
|
||||||
switch :debug
|
flag "--version="
|
||||||
switch :force
|
switch :debug
|
||||||
|
switch :force
|
||||||
end
|
end
|
||||||
|
|
||||||
odie "Cannot use a tap and --stdout at the same time!" if ARGV.named.length == 3 && args.stdout?
|
# If no formula args are given, ask specifically for a formula to be specified
|
||||||
raise UsageError unless (ARGV.named.length == 3 && !args.stdout?) || (ARGV.named.length == 2 && args.stdout?)
|
raise FormulaUnspecifiedError if ARGV.named.empty?
|
||||||
|
|
||||||
|
# If some other number of args are given, provide generic usage information
|
||||||
|
raise UsageError if ARGV.named.length != 1
|
||||||
|
|
||||||
|
odie "The tap to which the formula is extracted must be specified!" if args.tap.nil?
|
||||||
|
|
||||||
formula = Formulary.factory(ARGV.named.first)
|
formula = Formulary.factory(ARGV.named.first)
|
||||||
version = ARGV.named[1]
|
if args.version.nil?
|
||||||
destination_tap = Tap.fetch(ARGV.named[2]) unless args.stdout?
|
version = formula.version
|
||||||
destination_tap.install unless destination_tap.installed? || args.stdout?
|
else
|
||||||
|
version = args.version
|
||||||
|
end
|
||||||
|
destination_tap = Tap.fetch(args.tap)
|
||||||
|
destination_tap.install unless destination_tap.installed?
|
||||||
|
|
||||||
unless args.stdout?
|
odie "Cannot extract formula to homebrew/core!" if destination_tap.name == "homebrew/core"
|
||||||
path = Pathname.new("#{destination_tap.path}/Formula/#{formula}@#{version}.rb")
|
|
||||||
if path.exist?
|
path = Pathname.new("#{destination_tap.path}/Formula/#{formula}@#{version}.rb")
|
||||||
unless ARGV.force?
|
if path.exist?
|
||||||
odie <<~EOS
|
unless ARGV.force?
|
||||||
Destination formula already exists: #{path}
|
odie <<~EOS
|
||||||
To overwrite it and continue anyways, run `brew extract #{formula} #{version} #{destination_tap.name} --force`.
|
Destination formula already exists: #{path}
|
||||||
EOS
|
To overwrite it and continue anyways, run:
|
||||||
end
|
`brew extract #{formula} --version=#{version} --tap=#{destination_tap.name} --force`
|
||||||
ohai "Overwriting existing formula at #{path}" if ARGV.debug?
|
EOS
|
||||||
path.delete
|
|
||||||
end
|
end
|
||||||
|
ohai "Overwriting existing formula at #{path}" if ARGV.debug?
|
||||||
|
path.delete
|
||||||
end
|
end
|
||||||
|
|
||||||
rev = "HEAD"
|
if args.version.nil?
|
||||||
version_resolver = FormulaVersions.new(formula)
|
rev = Git.last_revision_commit_of_file(formula.path.parent.parent, formula.path)
|
||||||
until version_resolver.formula_at_revision(rev) { |f| version_matches?(f, version, rev) || rev.empty? } do
|
odie "Could not find #{formula} #{version}!" if rev.empty?
|
||||||
rev = Git.last_revision_commit_of_file(formula.path.parent.parent, formula.path, before_commit: "#{rev}~1")
|
version_resolver = FormulaVersions.new(formula)
|
||||||
|
else
|
||||||
|
rev = "HEAD"
|
||||||
|
version_resolver = FormulaVersions.new(formula)
|
||||||
|
until version_resolver.formula_at_revision(rev) { |f| version_matches?(f, version, rev) || rev.empty? } do
|
||||||
|
rev = Git.last_revision_commit_of_file(formula.path.parent.parent, formula.path, before_commit: "#{rev}~1")
|
||||||
|
end
|
||||||
|
odie "Could not find #{formula} #{version}!" if rev.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
odie "Could not find #{formula} #{version}!" if rev.empty?
|
|
||||||
|
|
||||||
result = version_resolver.file_contents_at_revision(rev)
|
result = version_resolver.file_contents_at_revision(rev)
|
||||||
|
|
||||||
# The class name has to be renamed to match the new filename, e.g. Foo version 1.2.3 becomes FooAT123 and resides in Foo@1.2.3.rb.
|
# The class name has to be renamed to match the new filename, e.g. Foo version 1.2.3 becomes FooAT123 and resides in Foo@1.2.3.rb.
|
||||||
result.gsub!("class #{formula.name.capitalize} < Formula", "class #{formula.name.capitalize}AT#{version.gsub(/[^0-9a-z ]/i, "")} < Formula")
|
name = formula.name.capitalize
|
||||||
if args.stdout?
|
versioned_name = Formulary.class_s("#{name}@#{version}")
|
||||||
puts result if args.stdout?
|
result.gsub!("class #{name} < Formula", "class #{versioned_name} < Formula")
|
||||||
else
|
ohai "Writing formula for #{formula} from #{rev} to #{path}"
|
||||||
ohai "Writing formula for #{formula} from #{rev} to #{path}"
|
path.write result
|
||||||
path.write result
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @private
|
# @private
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user