Make PATH enumerable.
This commit is contained in:
parent
e70f2ec332
commit
22f624b373
@ -1,4 +1,9 @@
|
|||||||
class PATH
|
class PATH
|
||||||
|
include Enumerable
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegator :@paths, :each
|
||||||
|
|
||||||
def initialize(*paths)
|
def initialize(*paths)
|
||||||
@paths = parse(*paths)
|
@paths = parse(*paths)
|
||||||
end
|
end
|
||||||
@ -13,6 +18,11 @@ class PATH
|
|||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def insert(index, *paths)
|
||||||
|
@paths = parse(*@paths.insert(index, *paths))
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def to_ary
|
def to_ary
|
||||||
@paths
|
@paths
|
||||||
end
|
end
|
||||||
|
|||||||
@ -23,7 +23,7 @@ module Homebrew
|
|||||||
ENV.setup_build_environment
|
ENV.setup_build_environment
|
||||||
if superenv?
|
if superenv?
|
||||||
# superenv stopped adding brew's bin but generally users will want it
|
# superenv stopped adding brew's bin but generally users will want it
|
||||||
ENV["PATH"] = PATH.new(PATH.new(ENV["PATH"]).to_a.insert(1, HOMEBREW_PREFIX/"bin"))
|
ENV["PATH"] = PATH.new(ENV["PATH"]).insert(1, HOMEBREW_PREFIX/"bin")
|
||||||
end
|
end
|
||||||
ENV["PS1"] = 'brew \[\033[1;32m\]\w\[\033[0m\]$ '
|
ENV["PS1"] = 'brew \[\033[1;32m\]\w\[\033[0m\]$ '
|
||||||
ENV["VERBOSE"] = "1"
|
ENV["VERBOSE"] = "1"
|
||||||
|
|||||||
@ -100,8 +100,7 @@ module Homebrew
|
|||||||
|
|
||||||
# See https://github.com/Homebrew/legacy-homebrew/pull/9986
|
# See https://github.com/Homebrew/legacy-homebrew/pull/9986
|
||||||
def check_path_for_trailing_slashes
|
def check_path_for_trailing_slashes
|
||||||
all_paths = PATH.new(ENV["PATH"]).to_a
|
bad_paths = PATH.new(ENV["PATH"]).select { |p| p[-1..-1] == "/" }
|
||||||
bad_paths = all_paths.select { |p| p[-1..-1] == "/" }
|
|
||||||
return if bad_paths.empty?
|
return if bad_paths.empty?
|
||||||
|
|
||||||
inject_file_list bad_paths, <<-EOS.undent
|
inject_file_list bad_paths, <<-EOS.undent
|
||||||
|
|||||||
@ -56,7 +56,7 @@ HOMEBREW_PULL_OR_COMMIT_URL_REGEX = %r[https://github\.com/([\w-]+)/([\w-]+)?/(?
|
|||||||
require "compat" unless ARGV.include?("--no-compat") || ENV["HOMEBREW_NO_COMPAT"]
|
require "compat" unless ARGV.include?("--no-compat") || ENV["HOMEBREW_NO_COMPAT"]
|
||||||
|
|
||||||
ENV["HOMEBREW_PATH"] ||= ENV["PATH"]
|
ENV["HOMEBREW_PATH"] ||= ENV["PATH"]
|
||||||
ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).to_a.map do |p|
|
ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).map do |p|
|
||||||
begin
|
begin
|
||||||
Pathname.new(p).expand_path
|
Pathname.new(p).expand_path
|
||||||
rescue
|
rescue
|
||||||
|
|||||||
@ -96,7 +96,7 @@ class Requirement
|
|||||||
# PATH.
|
# PATH.
|
||||||
parent = satisfied_result_parent
|
parent = satisfied_result_parent
|
||||||
return unless parent
|
return unless parent
|
||||||
return if PATH.new(ENV["PATH"]).to_a.include?(parent.to_s)
|
return if PATH.new(ENV["PATH"]).include?(parent.to_s)
|
||||||
ENV.append_path("PATH", parent)
|
ENV.append_path("PATH", parent)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -51,6 +51,41 @@ describe PATH do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#insert" do
|
||||||
|
it "inserts a path at a given index" do
|
||||||
|
expect(described_class.new("/path1").insert(0, "/path2").to_str).to eq("/path2:/path1")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can insert multiple paths" do
|
||||||
|
expect(described_class.new("/path1").insert(0, "/path2", "/path3")).to eq("/path2:/path3:/path1")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#include?" do
|
||||||
|
it "returns true if a path is included" do
|
||||||
|
path = described_class.new("/path1", "/path2")
|
||||||
|
expect(path).to include("/path1")
|
||||||
|
expect(path).to include("/path2")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns false if a path is not included" do
|
||||||
|
expect(described_class.new("/path1")).not_to include("/path2")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns false if the given string contains a separator" do
|
||||||
|
expect(described_class.new("/path1", "/path2")).not_to include("/path1:")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#each" do
|
||||||
|
it "loops through each path" do
|
||||||
|
enum = described_class.new("/path1", "/path2").each
|
||||||
|
|
||||||
|
expect(enum.next).to eq("/path1")
|
||||||
|
expect(enum.next).to eq("/path2")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "#validate" do
|
describe "#validate" do
|
||||||
it "returns a new PATH without non-existent paths" do
|
it "returns a new PATH without non-existent paths" do
|
||||||
allow(File).to receive(:directory?).with("/path1").and_return(true)
|
allow(File).to receive(:directory?).with("/path1").and_return(true)
|
||||||
|
|||||||
@ -293,7 +293,7 @@ def quiet_system(cmd, *args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def which(cmd, path = ENV["PATH"])
|
def which(cmd, path = ENV["PATH"])
|
||||||
PATH.new(path).to_a.each do |p|
|
PATH.new(path).each do |p|
|
||||||
begin
|
begin
|
||||||
pcmd = File.expand_path(cmd, p)
|
pcmd = File.expand_path(cmd, p)
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
@ -307,7 +307,7 @@ def which(cmd, path = ENV["PATH"])
|
|||||||
end
|
end
|
||||||
|
|
||||||
def which_all(cmd, path = ENV["PATH"])
|
def which_all(cmd, path = ENV["PATH"])
|
||||||
PATH.new(path).to_a.map do |p|
|
PATH.new(path).map do |p|
|
||||||
begin
|
begin
|
||||||
pcmd = File.expand_path(cmd, p)
|
pcmd = File.expand_path(cmd, p)
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
@ -416,7 +416,7 @@ def nostdout
|
|||||||
end
|
end
|
||||||
|
|
||||||
def paths(env_path = ENV["PATH"])
|
def paths(env_path = ENV["PATH"])
|
||||||
@paths ||= PATH.new(env_path).to_a.collect do |p|
|
@paths ||= PATH.new(env_path).collect do |p|
|
||||||
begin
|
begin
|
||||||
File.expand_path(p).chomp("/")
|
File.expand_path(p).chomp("/")
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user