97 lines
2.3 KiB
Ruby
Raw Normal View History

2020-10-19 22:10:21 +02:00
# typed: true
# frozen_string_literal: true
2020-08-14 02:06:33 +02:00
# Represention of a `*PATH` environment variable.
#
# @api private
2017-04-27 08:48:29 +02:00
class PATH
2020-10-19 22:10:21 +02:00
extend T::Sig
2017-04-28 12:39:00 +02:00
include Enumerable
extend Forwardable
def_delegator :@paths, :each
2020-10-19 22:10:21 +02:00
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
# rubocop:disable Style/MutableConstant
Element = T.type_alias { T.nilable(T.any(Pathname, String, PATH)) }
private_constant :Element
Elements = T.type_alias { T.any(Element, T::Array[Element]) }
private_constant :Elements
# rubocop:enable Style/MutableConstant
sig { params(paths: Elements).void }
2017-04-27 08:48:29 +02:00
def initialize(*paths)
2020-10-19 22:10:21 +02:00
@paths = parse(paths)
2017-04-27 08:48:29 +02:00
end
2020-10-19 22:10:21 +02:00
sig { params(paths: Elements).returns(T.self_type) }
2017-04-27 08:48:29 +02:00
def prepend(*paths)
2020-10-19 22:10:21 +02:00
@paths = parse(paths + @paths)
2017-04-27 08:48:29 +02:00
self
end
2020-10-19 22:10:21 +02:00
sig { params(paths: Elements).returns(T.self_type) }
2017-04-27 08:48:29 +02:00
def append(*paths)
2020-10-19 22:10:21 +02:00
@paths = parse(@paths + paths)
2017-04-27 08:48:29 +02:00
self
end
2020-10-19 22:10:21 +02:00
sig { params(index: Integer, paths: Elements).returns(T.self_type) }
2017-04-28 12:39:00 +02:00
def insert(index, *paths)
2020-10-19 22:10:21 +02:00
@paths = parse(@paths.insert(index, *paths))
2017-04-28 12:39:00 +02:00
self
end
2020-10-19 22:10:21 +02:00
sig { params(block: T.proc.params(arg0: String).returns(T::Boolean)).returns(T.self_type) }
def select(&block)
self.class.new(@paths.select(&block))
end
2020-10-19 22:10:21 +02:00
sig { params(block: T.proc.params(arg0: String).returns(T::Boolean)).returns(T.self_type) }
def reject(&block)
self.class.new(@paths.reject(&block))
end
2020-10-19 22:10:21 +02:00
sig { returns(T::Array[String]) }
2017-04-27 08:48:29 +02:00
def to_ary
2018-04-14 06:46:01 +02:00
@paths.dup.to_ary
2017-04-27 08:48:29 +02:00
end
alias to_a to_ary
2020-10-19 22:10:21 +02:00
sig { returns(String) }
2017-04-27 08:48:29 +02:00
def to_str
@paths.join(File::PATH_SEPARATOR)
end
alias to_s to_str
2020-10-19 22:10:21 +02:00
sig { params(other: T.untyped).returns(T::Boolean) }
2017-04-28 11:12:02 +02:00
def ==(other)
2020-11-13 17:21:51 +01:00
(other.respond_to?(:to_ary) && to_ary == other.to_ary) ||
(other.respond_to?(:to_str) && to_str == other.to_str) ||
2020-09-01 14:05:52 +01:00
false
2017-04-27 08:48:29 +02:00
end
2020-10-19 22:10:21 +02:00
sig { returns(T::Boolean) }
2017-04-27 08:48:29 +02:00
def empty?
@paths.empty?
end
2020-10-19 22:10:21 +02:00
sig { returns(T.nilable(T.self_type)) }
def existing
existing_path = select(&File.method(:directory?))
# return nil instead of empty PATH, to unset environment variables
existing_path unless existing_path.empty?
2017-04-27 08:48:29 +02:00
end
private
2020-10-19 22:10:21 +02:00
sig { params(paths: T::Array[Elements]).returns(T::Array[String]) }
def parse(paths)
2017-04-28 20:46:52 +02:00
paths.flatten
.compact
2020-10-19 22:10:21 +02:00
.flat_map { |p| Pathname(p).to_path.split(File::PATH_SEPARATOR) }
2017-04-28 20:46:52 +02:00
.uniq
2017-04-27 08:48:29 +02:00
end
end