Preserve alias when installing formulae

Part of #567
This commit is contained in:
Alyssa Ross 2016-09-03 21:10:44 +01:00
parent 9fd97dd3cf
commit 0ef3e1e0dc
10 changed files with 58 additions and 7 deletions

View File

@ -58,6 +58,11 @@ class Formula
# e.g. `this-formula` # e.g. `this-formula`
attr_reader :name attr_reader :name
# The name specified when installing this {Formula}.
# Could be the name of the {Formula}, or an alias.
# e.g. `another-name-for-this-formula`
attr_reader :install_name
# The fully-qualified name of this {Formula}. # The fully-qualified name of this {Formula}.
# For core formula it's the same as {#name}. # For core formula it's the same as {#name}.
# e.g. `homebrew/tap-name/this-formula` # e.g. `homebrew/tap-name/this-formula`
@ -143,9 +148,10 @@ class Formula
attr_accessor :build attr_accessor :build
# @private # @private
def initialize(name, path, spec) def initialize(name, path, spec, install_name: name)
@name = name @name = name
@path = path @path = path
@install_name = install_name
@revision = self.class.revision || 0 @revision = self.class.revision || 0
@version_scheme = self.class.version_scheme || 0 @version_scheme = self.class.version_scheme || 0
@ -502,6 +508,21 @@ class Formula
installed_prefixes.map { |dir| Keg.new(dir) } installed_prefixes.map { |dir| Keg.new(dir) }
end end
# Formula reference to use to get an identical installation of the formula.
#
# Usually, the formula's path is a good canonical identifier for the formula.
# Just using whatever was passed to `brew install` isn't a good idea, because
# if it was e.g. a URL, it could end up being downloaded twice.
#
# However, if the formula was installed with an alias (i.e. `install_name` is
# different from `name`), that should be used instead so that information is
# preserved. Aliases are looked up in repositories that have already been
# tapped, so we don't have to worry about doing extra HTTP requests, or other
# expensive operations, when looking them up again.
def install_ref
install_name == name ? path : install_name
end
# The directory where the formula's binaries should be installed. # The directory where the formula's binaries should be installed.
# This is symlinked into `HOMEBREW_PREFIX` after installation or with # This is symlinked into `HOMEBREW_PREFIX` after installation or with
# `brew link` for formulae that are not keg-only. # `brew link` for formulae that are not keg-only.

View File

@ -589,7 +589,7 @@ class FormulaInstaller
-I #{HOMEBREW_LOAD_PATH} -I #{HOMEBREW_LOAD_PATH}
-- --
#{HOMEBREW_LIBRARY_PATH}/build.rb #{HOMEBREW_LIBRARY_PATH}/build.rb
#{formula.path} #{formula.install_ref}
].concat(build_argv) ].concat(build_argv)
Sandbox.print_sandbox_message if Sandbox.formula?(formula) Sandbox.print_sandbox_message if Sandbox.formula?(formula)

View File

@ -68,15 +68,18 @@ class Formulary
attr_reader :name attr_reader :name
# The formula's ruby file's path or filename # The formula's ruby file's path or filename
attr_reader :path attr_reader :path
# The name used to install the formula
attr_reader :install_name
def initialize(name, path) def initialize(name, path)
@name = name @name = name
@path = path.resolved_path @path = path.resolved_path
@install_name = name
end end
# Gets the formula instance. # Gets the formula instance.
def get_formula(spec) def get_formula(spec)
klass.new(name, path, spec) klass.new(name, path, spec, install_name: install_name)
end end
def klass def klass
@ -118,6 +121,7 @@ class Formulary
path = alias_path.resolved_path path = alias_path.resolved_path
name = path.basename(".rb").to_s name = path.basename(".rb").to_s
super name, path super name, path
@install_name = alias_path.basename.to_s
end end
end end

View File

@ -27,6 +27,7 @@ class Tab < OpenStruct
"time" => Time.now.to_i, "time" => Time.now.to_i,
"source_modified_time" => formula.source_modified_time.to_i, "source_modified_time" => formula.source_modified_time.to_i,
"HEAD" => HOMEBREW_REPOSITORY.git_head, "HEAD" => HOMEBREW_REPOSITORY.git_head,
"install_name" => formula.install_name,
"compiler" => compiler, "compiler" => compiler,
"stdlib" => stdlib, "stdlib" => stdlib,
"source" => { "source" => {
@ -161,6 +162,7 @@ class Tab < OpenStruct
"time" => nil, "time" => nil,
"source_modified_time" => 0, "source_modified_time" => 0,
"HEAD" => nil, "HEAD" => nil,
"install_name" => nil,
"stdlib" => nil, "stdlib" => nil,
"compiler" => DevelopmentTools.default_compiler, "compiler" => DevelopmentTools.default_compiler,
"source" => { "source" => {
@ -292,6 +294,7 @@ class Tab < OpenStruct
"time" => time, "time" => time,
"source_modified_time" => source_modified_time.to_i, "source_modified_time" => source_modified_time.to_i,
"HEAD" => self.HEAD, "HEAD" => self.HEAD,
"install_name" => install_name,
"stdlib" => (stdlib.to_s if stdlib), "stdlib" => (stdlib.to_s if stdlib),
"compiler" => (compiler.to_s if compiler), "compiler" => (compiler.to_s if compiler),
"source" => source "source" => source

View File

@ -11,6 +11,7 @@
"poured_from_bottle": true, "poured_from_bottle": true,
"time": 1403827774, "time": 1403827774,
"HEAD": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", "HEAD": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"install_name": "test-formula",
"stdlib": "libcxx", "stdlib": "libcxx",
"compiler": "clang", "compiler": "clang",
"source": { "source": {

View File

@ -8,13 +8,30 @@ class FormulaTests < Homebrew::TestCase
name = "formula_name" name = "formula_name"
path = Formulary.core_path(name) path = Formulary.core_path(name)
spec = :stable spec = :stable
install_name = "formula_alias"
f = klass.new(name, path, spec) f = klass.new(name, path, spec, install_name: install_name)
assert_equal name, f.name assert_equal name, f.name
assert_equal path, f.path assert_equal path, f.path
assert_equal install_name, f.install_name
assert_raises(ArgumentError) { klass.new } assert_raises(ArgumentError) { klass.new }
end end
def test_install_ref_with_alias
name = "formula_name"
path = Formulary.core_path(name)
spec = :stable
install_name = "formula_alias"
f = Testball.new(name, path, spec, install_name: install_name)
assert_equal f.install_name, f.install_ref
end
def test_install_ref_with_non_alias
f = Testball.new
assert_equal f.path, f.install_ref
end
def test_prefix def test_prefix
f = Testball.new f = Testball.new
assert_equal HOMEBREW_CELLAR/f.name/"0.1", f.prefix assert_equal HOMEBREW_CELLAR/f.name/"0.1", f.prefix

View File

@ -86,7 +86,9 @@ class FormularyFactoryTest < Homebrew::TestCase
alias_dir = CoreTap.instance.alias_dir alias_dir = CoreTap.instance.alias_dir
alias_dir.mkpath alias_dir.mkpath
FileUtils.ln_s @path, alias_dir/"foo" FileUtils.ln_s @path, alias_dir/"foo"
assert_kind_of Formula, Formulary.factory("foo") result = Formulary.factory("foo")
assert_kind_of Formula, result
assert_equal "foo", result.install_name
ensure ensure
alias_dir.rmtree alias_dir.rmtree
end end

View File

@ -43,6 +43,7 @@ class TabTests < Homebrew::TestCase
assert_nil tab.stable_version assert_nil tab.stable_version
assert_nil tab.devel_version assert_nil tab.devel_version
assert_nil tab.head_version assert_nil tab.head_version
assert_nil tab.install_name
assert_equal DevelopmentTools.default_compiler, tab.cxxstdlib.compiler assert_equal DevelopmentTools.default_compiler, tab.cxxstdlib.compiler
assert_nil tab.cxxstdlib.type assert_nil tab.cxxstdlib.type
end end
@ -116,6 +117,7 @@ class TabTests < Homebrew::TestCase
assert_equal "2.14", tab.stable_version.to_s assert_equal "2.14", tab.stable_version.to_s
assert_equal "2.15", tab.devel_version.to_s assert_equal "2.15", tab.devel_version.to_s
assert_equal "HEAD-0000000", tab.head_version.to_s assert_equal "HEAD-0000000", tab.head_version.to_s
assert_equal "test-formula", tab.install_name
end end
def test_to_json def test_to_json
@ -133,6 +135,7 @@ class TabTests < Homebrew::TestCase
assert_equal @tab.stable_version, tab.stable_version assert_equal @tab.stable_version, tab.stable_version
assert_equal @tab.devel_version, tab.devel_version assert_equal @tab.devel_version, tab.devel_version
assert_equal @tab.head_version, tab.head_version assert_equal @tab.head_version, tab.head_version
assert_equal @tab.install_name, tab.install_name
end end
def test_remap_deprecated_options def test_remap_deprecated_options

View File

@ -1,5 +1,5 @@
class Testball < Formula class Testball < Formula
def initialize(name = "testball", path = Pathname.new(__FILE__).expand_path, spec = :stable) def initialize(name = "testball", path = Pathname.new(__FILE__).expand_path, spec = :stable, install_name: name)
self.class.instance_eval do self.class.instance_eval do
stable.url "file://#{File.expand_path("..", __FILE__)}/tarballs/testball-0.1.tbz" stable.url "file://#{File.expand_path("..", __FILE__)}/tarballs/testball-0.1.tbz"
stable.sha256 TESTBALL_SHA256 stable.sha256 TESTBALL_SHA256

View File

@ -1,5 +1,5 @@
class TestballBottle < Formula class TestballBottle < Formula
def initialize(name = "testball_bottle", path = Pathname.new(__FILE__).expand_path, spec = :stable) def initialize(name = "testball_bottle", path = Pathname.new(__FILE__).expand_path, spec = :stable, install_name: name)
self.class.instance_eval do self.class.instance_eval do
stable.url "file://#{File.expand_path("..", __FILE__)}/tarballs/testball-0.1.tbz" stable.url "file://#{File.expand_path("..", __FILE__)}/tarballs/testball-0.1.tbz"
stable.sha256 TESTBALL_SHA256 stable.sha256 TESTBALL_SHA256