1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const _escapeRegExp = require("lodash.escaperegexp");
|
4 | const _flatten = require("lodash.flatten");
|
5 | const _uniq = require("lodash.uniq");
|
6 | const gem_version_1 = require("./gem-version");
|
7 | const OPS = {
|
8 | '=': (v, r) => v.compare(r) === 0,
|
9 | '!=': (v, r) => v.compare(r) !== 0,
|
10 | '>': (v, r) => v.compare(r) > 0,
|
11 | '<': (v, r) => v.compare(r) < 0,
|
12 | '>=': (v, r) => v.compare(r) >= 0,
|
13 | '<=': (v, r) => v.compare(r) <= 0,
|
14 | '~>': (v, r) => v.compare(r) >= 0 && v.release().compare(r.bump()) < 0,
|
15 | };
|
16 | const quoted = Object.keys(OPS)
|
17 | .map((k) => _escapeRegExp(k))
|
18 | .join('|');
|
19 | const PATTERN_RAW = `\\s*(${quoted})?\\s*(${gem_version_1.GemVersion.VERSION_PATTERN})\\s*`;
|
20 | // --
|
21 | // A regular expression that matches a requirement
|
22 | const PATTERN = new RegExp(`^${PATTERN_RAW}$`);
|
23 | // --
|
24 | // The default requirement matches any version
|
25 | const DefaultRequirement = ['>=', new gem_version_1.GemVersion('0')];
|
26 | class GemRequirement {
|
27 | // --
|
28 | // Constructs a requirement from +requirements+. Requirements can be
|
29 | // Strings, GemVersions, or Arrays of those. +nil+ and duplicate
|
30 | // requirements are ignored. An empty set of +requirements+ is the
|
31 | // same as <tt>">= 0"</tt>.
|
32 | constructor(...requirements) {
|
33 | const flat = _uniq(_flatten(requirements)).filter(Boolean);
|
34 | if (flat.length === 0) {
|
35 | this.requirements = [DefaultRequirement];
|
36 | }
|
37 | else {
|
38 | this.requirements = flat.map((req) => GemRequirement.parse(req));
|
39 | }
|
40 | }
|
41 | // --
|
42 | // Factory method to create a GemRequirement object. Input may be
|
43 | // a Version, a String, or nil. Intended to simplify client code.
|
44 | //
|
45 | // If the input is "weird", the default version requirement is
|
46 | // returned.
|
47 | static create(input) {
|
48 | if (input instanceof GemRequirement) {
|
49 | return input;
|
50 | }
|
51 | return new GemRequirement(input);
|
52 | }
|
53 | // --
|
54 | // A default "version requirement" can surely _only_ be '>= 0'.
|
55 | static default() {
|
56 | return new GemRequirement('>= 0');
|
57 | }
|
58 | // --
|
59 | // Parse +obj+, returning an <tt>[op, version]</tt> pair. +obj+ can
|
60 | // be a String or a GemVersion.
|
61 | //
|
62 | // If +obj+ is a String, it can be either a full requirement
|
63 | // specification, like <tt>">= 1.2"</tt>, or a simple version number,
|
64 | // like <tt>"1.2"</tt>.
|
65 | //
|
66 | // parse("> 1.0") // => [">", GemVersion.new("1.0")]
|
67 | // parse("1.0") // => ["=", GemVersion.new("1.0")]
|
68 | // parse(GemVersion.new("1.0")) # => ["=, GemVersion.new("1.0")]
|
69 | static parse(obj) {
|
70 | if (obj instanceof gem_version_1.GemVersion) {
|
71 | return ['=', obj];
|
72 | }
|
73 | const match = String(obj).match(PATTERN);
|
74 | if (!match) {
|
75 | throw new Error(`Illformed requirement [${obj}]`);
|
76 | }
|
77 | if (match[1] == '>=' && match[2] == '0') {
|
78 | return DefaultRequirement;
|
79 | }
|
80 | else {
|
81 | return [match[1] || '=', new gem_version_1.GemVersion(match[2])];
|
82 | }
|
83 | }
|
84 | // --
|
85 | // Concatenates the +new+ requirements onto this requirement.
|
86 | // def concat new
|
87 | // new = new.flatten
|
88 | // new.compact!
|
89 | // new.uniq!
|
90 | // new = new.map { |r| self.class.parse r }
|
91 | // @requirements.concat new
|
92 | // end
|
93 | // --
|
94 | // Formats this requirement for use in a GemRequestSet::Lockfile.
|
95 | // def for_lockfile # :nodoc:
|
96 | // return if [DefaultRequirement] == @requirements
|
97 | // list = requirements.sort_by { |_, version|
|
98 | // version
|
99 | // }.map { |op, version|
|
100 | // "${op} ${version}"
|
101 | // }.uniq
|
102 | // " (${list.join ', '})"
|
103 | // end
|
104 | // --
|
105 | // true if this gem has no requirements.
|
106 | // def none?
|
107 | // if @requirements.size == 1
|
108 | // @requirements[0] == DefaultRequirement
|
109 | // else
|
110 | // false
|
111 | // end
|
112 | // end
|
113 | // --
|
114 | // true if the requirement is for only an exact version
|
115 | // def exact?
|
116 | // return false unless @requirements.size == 1
|
117 | // @requirements[0][0] == "="
|
118 | // end
|
119 | asList() {
|
120 | return this.requirements.map(([op, version]) => `${op} ${version}`).sort();
|
121 | }
|
122 | // def hash # :nodoc:
|
123 | // requirements.sort.hash
|
124 | // end
|
125 | // def marshal_dump # :nodoc:
|
126 | // fix_syck_default_key_in_requirements
|
127 | // [@requirements]
|
128 | // end
|
129 | // def marshal_load array # :nodoc:
|
130 | // @requirements = array[0]
|
131 | // fix_syck_default_key_in_requirements
|
132 | // end
|
133 | // def yaml_initialize(tag, vals) # :nodoc:
|
134 | // vals.each do |ivar, val|
|
135 | // instance_variable_set "@${ivar}", val
|
136 | // end
|
137 | // Gem.load_yaml
|
138 | // fix_syck_default_key_in_requirements
|
139 | // end
|
140 | // def init_with coder # :nodoc:
|
141 | // yaml_initialize coder.tag, coder.map
|
142 | // end
|
143 | // def to_yaml_properties # :nodoc:
|
144 | // ["@requirements"]
|
145 | // end
|
146 | // def encode_with coder # :nodoc:
|
147 | // coder.add 'requirements', @requirements
|
148 | // end
|
149 | // --
|
150 | // A requirement is a prerelease if any of the versions inside of it
|
151 | // are prereleases
|
152 | isPrerelease() {
|
153 | return this.requirements.some((r) => r[1].isPrerelease());
|
154 | }
|
155 | // def pretty_print q # :nodoc:
|
156 | // q.group 1, 'GemRequirement.new(', ')' do
|
157 | // q.pp as_list
|
158 | // end
|
159 | // end
|
160 | // --
|
161 | // True if +version+ satisfies this Requirement.
|
162 | // def satisfied_by? version
|
163 | // raise ArgumentError, "Need a GemVersion: ${version.inspect}" unless
|
164 | // GemVersion === version
|
165 | // // #28965: syck has a bug with unquoted '=' YAML.loading as YAML::DefaultKey
|
166 | // requirements.all? { |op, rv| (OPS[op] || OPS["="]).call version, rv }
|
167 | // end
|
168 | satisfiedBy(version) {
|
169 | if (!(version instanceof gem_version_1.GemVersion)) {
|
170 | throw new Error(`Need a GemVersion: ${version}`);
|
171 | }
|
172 | return this.requirements.every(([op, rv]) => {
|
173 | const fn = OPS[op] || OPS['='];
|
174 | return fn(version, rv);
|
175 | });
|
176 | }
|
177 | // alias :=== :satisfied_by?
|
178 | // alias :=~ :satisfied_by?
|
179 | // --
|
180 | // True if the requirement will not always match the latest version.
|
181 | // def specific?
|
182 | // return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly
|
183 | // not %w[> >=].include? @requirements.first.first # grab the operator
|
184 | // end
|
185 | toString() {
|
186 | return this.asList().join(', ');
|
187 | }
|
188 | }
|
189 | exports.GemRequirement = GemRequirement;
|
190 | //# sourceMappingURL=gem-requirement.js.map |
\ | No newline at end of file |