Merge pull request #11537 from alebcay/short-brew-path
Utils: shortened brew paths
This commit is contained in:
commit
6a5891f1d8
@ -365,7 +365,7 @@ class Pathname
|
|||||||
target = Pathname.new(target) # allow pathnames or strings
|
target = Pathname.new(target) # allow pathnames or strings
|
||||||
join(target.basename).write <<~SH
|
join(target.basename).write <<~SH
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
exec "#{target}" "$@"
|
exec "#{Utils.shortened_brew_path(target)}" "$@"
|
||||||
SH
|
SH
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -381,7 +381,7 @@ class Pathname
|
|||||||
dirname.mkpath
|
dirname.mkpath
|
||||||
write <<~SH
|
write <<~SH
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#{env_export}exec "#{target}" #{args} "$@"
|
#{env_export}exec "#{Utils.shortened_brew_path(target)}" #{args} "$@"
|
||||||
SH
|
SH
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ class Pathname
|
|||||||
mkpath
|
mkpath
|
||||||
(self/script_name).write <<~EOS
|
(self/script_name).write <<~EOS
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
export JAVA_HOME="#{Language::Java.overridable_java_home_env(java_version)[:JAVA_HOME]}"
|
export JAVA_HOME="#{Language::Java.overridable_short_java_home_env(java_version)[:JAVA_HOME]}"
|
||||||
exec "${JAVA_HOME}/bin/java" #{java_opts} -jar "#{target_jar}" "$@"
|
exec "${JAVA_HOME}/bin/java" #{java_opts} -jar "#{target_jar}" "$@"
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|||||||
@ -36,13 +36,26 @@ module Language
|
|||||||
end
|
end
|
||||||
private_class_method :java_home_shell
|
private_class_method :java_home_shell
|
||||||
|
|
||||||
|
def self.short_java_home_shell(version = nil)
|
||||||
|
Utils.shortened_brew_path(java_home(version))
|
||||||
|
end
|
||||||
|
private_class_method :short_java_home_shell
|
||||||
|
|
||||||
def self.java_home_env(version = nil)
|
def self.java_home_env(version = nil)
|
||||||
{ JAVA_HOME: java_home_shell(version) }
|
{ JAVA_HOME: java_home_shell(version) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.short_java_home_env(version = nil)
|
||||||
|
{ JAVA_HOME: short_java_home_shell(version) }
|
||||||
|
end
|
||||||
|
|
||||||
def self.overridable_java_home_env(version = nil)
|
def self.overridable_java_home_env(version = nil)
|
||||||
{ JAVA_HOME: "${JAVA_HOME:-#{java_home_shell(version)}}" }
|
{ JAVA_HOME: "${JAVA_HOME:-#{java_home_shell(version)}}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.overridable_short_java_home_env(version = nil)
|
||||||
|
{ JAVA_HOME: "${JAVA_HOME:-#{short_java_home_shell(version)}}" }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,14 @@ describe Language::Java do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:expected_short_home) do
|
||||||
|
if OS.mac?
|
||||||
|
"$(brew --prefix)/opt/openjdk/libexec/openjdk.jdk/Contents/Home"
|
||||||
|
else
|
||||||
|
"$(brew --prefix)/opt/openjdk/libexec"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(Formula).to receive(:[]).and_return(f)
|
allow(Formula).to receive(:[]).and_return(f)
|
||||||
allow(f).to receive(:any_version_installed?).and_return(true)
|
allow(f).to receive(:any_version_installed?).and_return(true)
|
||||||
@ -49,6 +57,18 @@ describe Language::Java do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "::short_java_home_env" do
|
||||||
|
it "returns short java_home path if version specified" do
|
||||||
|
short_java_home_env = described_class.short_java_home_env("1.8+")
|
||||||
|
expect(short_java_home_env[:JAVA_HOME]).to eql(expected_short_home)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns short java_home path if version is not specified" do
|
||||||
|
short_java_home_env = described_class.short_java_home_env
|
||||||
|
expect(short_java_home_env[:JAVA_HOME]).to eql(expected_short_home)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "::overridable_java_home_env" do
|
describe "::overridable_java_home_env" do
|
||||||
it "returns java_home path if version specified" do
|
it "returns java_home path if version specified" do
|
||||||
overridable_java_home_env = described_class.overridable_java_home_env("1.8+")
|
overridable_java_home_env = described_class.overridable_java_home_env("1.8+")
|
||||||
@ -60,4 +80,16 @@ describe Language::Java do
|
|||||||
expect(overridable_java_home_env[:JAVA_HOME]).to eql("${JAVA_HOME:-#{expected_home}}")
|
expect(overridable_java_home_env[:JAVA_HOME]).to eql("${JAVA_HOME:-#{expected_home}}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "::overridable_short_java_home_env" do
|
||||||
|
it "returns short java_home path if version specified" do
|
||||||
|
overridable_short_java_home_env = described_class.overridable_short_java_home_env("1.8+")
|
||||||
|
expect(overridable_short_java_home_env[:JAVA_HOME]).to eql("${JAVA_HOME:-#{expected_short_home}}")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns short java_home path if version is not specified" do
|
||||||
|
overridable_short_java_home_env = described_class.overridable_short_java_home_env
|
||||||
|
expect(overridable_short_java_home_env[:JAVA_HOME]).to eql("${JAVA_HOME:-#{expected_short_home}}")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
33
Library/Homebrew/test/utils/path_spec.rb
Normal file
33
Library/Homebrew/test/utils/path_spec.rb
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# typed: false
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "utils/path"
|
||||||
|
|
||||||
|
describe Utils do
|
||||||
|
describe "::path_is_parent_of?" do
|
||||||
|
it "returns true when child path is a descendant of the parent" do
|
||||||
|
expect(described_class.path_is_parent_of?("/foo", "/foo/bar/baz")).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns false when child path is not a descendant of the parent" do
|
||||||
|
expect(described_class.path_is_parent_of?("/foo/bar/baz/qux", "/foo/bar")).to eq(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "::shortened_brew_path" do
|
||||||
|
it "returns shortened path when the path can be expressed with the output of a brew command" do
|
||||||
|
expect(described_class.shortened_brew_path("#{HOMEBREW_PREFIX}/foo/bar")).to eq("$(brew --prefix)/foo/bar")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns shortened path with $(brew --prefix <formula>) when path is in a linked keg", :integration_test do
|
||||||
|
install_test_formula "testball"
|
||||||
|
f = Formula["testball"]
|
||||||
|
|
||||||
|
expect(described_class.shortened_brew_path("#{f.opt_prefix}/main.c")).to eq("$(brew --prefix testball)/main.c")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the original path when the path cannot be shortened" do
|
||||||
|
expect(described_class.shortened_brew_path("/foo/bar/baz")).to eq("/foo/bar/baz")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -13,6 +13,7 @@ require "utils/git_repository"
|
|||||||
require "utils/github"
|
require "utils/github"
|
||||||
require "utils/inreplace"
|
require "utils/inreplace"
|
||||||
require "utils/link"
|
require "utils/link"
|
||||||
|
require "utils/path"
|
||||||
require "utils/popen"
|
require "utils/popen"
|
||||||
require "utils/repology"
|
require "utils/repology"
|
||||||
require "utils/svn"
|
require "utils/svn"
|
||||||
|
|||||||
51
Library/Homebrew/utils/path.rb
Normal file
51
Library/Homebrew/utils/path.rb
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# typed: strict
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Helper functions for working with paths
|
||||||
|
module Utils
|
||||||
|
extend T::Sig
|
||||||
|
|
||||||
|
# Checks if a a child path is a descendant of a given parent path
|
||||||
|
sig { params(parent_path: T.any(String, Pathname), child_path: T.any(String, Pathname)).returns(T::Boolean) }
|
||||||
|
def self.path_is_parent_of?(parent_path, child_path)
|
||||||
|
parent_component_array = Pathname(parent_path).each_filename.to_a
|
||||||
|
child_component_array = Pathname(child_path).each_filename.to_a
|
||||||
|
|
||||||
|
child_component_array.first(parent_component_array.length) == parent_component_array
|
||||||
|
end
|
||||||
|
|
||||||
|
# Gets a condensed short brew path for a given path, or the original path if it cannot be condensed
|
||||||
|
sig { params(long_path: T.any(String, Pathname)).returns(String) }
|
||||||
|
def self.shortened_brew_path(long_path)
|
||||||
|
short_path = long_path.to_s
|
||||||
|
long_path = Pathname(long_path)
|
||||||
|
|
||||||
|
if long_path.exist?
|
||||||
|
begin
|
||||||
|
k = Keg.for(long_path)
|
||||||
|
opt_record = k.opt_record
|
||||||
|
formula_name = k.to_formula.name
|
||||||
|
rescue FormulaUnavailableError, NotAKegError
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
short_path = short_path.sub(/\A#{Regexp.escape(opt_record.to_s)}/, "$(brew --prefix #{formula_name})")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
try_paths = {
|
||||||
|
HOMEBREW_CACHE => "--cache",
|
||||||
|
HOMEBREW_CELLAR => "--cellar",
|
||||||
|
HOMEBREW_REPOSITORY => "--repository",
|
||||||
|
HOMEBREW_PREFIX => "--prefix",
|
||||||
|
}
|
||||||
|
|
||||||
|
try_paths.each do |try_path, flag|
|
||||||
|
if path_is_parent_of?(try_path, long_path)
|
||||||
|
short_path = short_path.sub(/\A#{Regexp.escape(try_path.to_s)}/, "$(brew #{flag})")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
short_path
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user