From 8297810e3c4d5a15cb9b176f5a02a6023a6db7be Mon Sep 17 00:00:00 2001 From: Xu Cheng <xucheng@me.com> Date: Fri, 8 May 2015 13:48:36 +0800 Subject: [PATCH] add Formulary::path method This is a little code refactoring splited from Homebrew/homebrew#36753 The idea is to eliminate `Formula#path` outside of `formulary.rb`. And I indent to deprecate `Formula#path` method when I reimplement symlink free tap function. Closes Homebrew/homebrew#39313. --- Library/Homebrew/cmd/create.rb | 6 +++--- Library/Homebrew/cmd/edit.rb | 10 ++++------ Library/Homebrew/cmd/log.rb | 9 +++------ Library/Homebrew/cmd/readall.rb | 2 +- Library/Homebrew/formulary.rb | 6 +++++- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Library/Homebrew/cmd/create.rb b/Library/Homebrew/cmd/create.rb index b9bc9cd136..11c51d5983 100644 --- a/Library/Homebrew/cmd/create.rb +++ b/Library/Homebrew/cmd/create.rb @@ -40,7 +40,7 @@ module Homebrew stem = Pathname.new(url).stem print "Formula name [#{stem}]: " fc.name = __gets || stem - fc.path = Formula.path(fc.name) + fc.path = Formulary.path(fc.name) end # Don't allow blacklisted formula, or names that shadow aliases, @@ -84,9 +84,9 @@ class FormulaCreator @name ||= $1 /(.*?)[-_.]?#{path.version}/.match path.basename @name ||= $1 - @path = Formula.path @name unless @name.nil? + @path = Formulary.path @name unless @name.nil? else - @path = Formula.path name + @path = Formulary.path name end if @version @version = Version.new(@version) diff --git a/Library/Homebrew/cmd/edit.rb b/Library/Homebrew/cmd/edit.rb index c4f137ea49..534fcba330 100644 --- a/Library/Homebrew/cmd/edit.rb +++ b/Library/Homebrew/cmd/edit.rb @@ -26,13 +26,11 @@ module Homebrew else # Don't use ARGV.formulae as that will throw if the file doesn't parse paths = ARGV.named.map do |name| - name = Formulary.canonical_name(name) - Formula.path(name) - end - unless ARGV.force? - paths.each do |path| - raise FormulaUnavailableError, path.basename('.rb').to_s unless path.file? + path = Formulary.path(name) + unless path.file? || ARGV.force? + raise FormulaUnavailableError, name end + path end exec_editor(*paths) end diff --git a/Library/Homebrew/cmd/log.rb b/Library/Homebrew/cmd/log.rb index c45f4735c1..6ffeab1e44 100644 --- a/Library/Homebrew/cmd/log.rb +++ b/Library/Homebrew/cmd/log.rb @@ -1,15 +1,12 @@ +require "formula" + module Homebrew def log if ARGV.named.empty? cd HOMEBREW_REPOSITORY exec "git", "log", *ARGV.options_only else - begin - path = ARGV.formulae.first.path - rescue FormulaUnavailableError - # Maybe the formula was deleted - path = Formula.path(ARGV.named.first) - end + path = Formulary.path(ARGV.named.first) cd path.dirname # supports taps exec "git", "log", *ARGV.options_only + ["--", path] end diff --git a/Library/Homebrew/cmd/readall.rb b/Library/Homebrew/cmd/readall.rb index 513a9b7c21..ce5be80a8b 100644 --- a/Library/Homebrew/cmd/readall.rb +++ b/Library/Homebrew/cmd/readall.rb @@ -46,7 +46,7 @@ module Homebrew begin Formulary.factory(n) rescue Exception => e - onoe "problem in #{Formula.path(n)}" + onoe "problem in #{Formulary.path(n)}" puts e Homebrew.failed = true end diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 3815105c27..24c2788a7b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -182,7 +182,7 @@ class Formulary class NullLoader < FormulaLoader def initialize(name) - @name = name + super name, Formula.path(name) end def get_formula(spec) @@ -204,6 +204,10 @@ class Formulary loader_for(ref).name end + def self.path(ref) + loader_for(ref).path + end + def self.loader_for(ref) case ref when %r[(https?|ftp)://] -- GitLab