Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1134)

Side by Side Diff: lib/src/pubspec.dart

Issue 2165423002: Add support for Flutter SDK constraints. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Code review changes Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/lock_file.dart ('k') | lib/src/solver/backtracking_solver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'package:path/path.dart' as path; 5 import 'package:path/path.dart' as path;
6 import 'package:pub_semver/pub_semver.dart'; 6 import 'package:pub_semver/pub_semver.dart';
7 import 'package:source_span/source_span.dart'; 7 import 'package:source_span/source_span.dart';
8 import 'package:yaml/yaml.dart'; 8 import 'package:yaml/yaml.dart';
9 9
10 import 'barback/transformer_config.dart'; 10 import 'barback/transformer_config.dart';
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 if (map == null) return false; 215 if (map == null) return false;
216 216
217 if (map is! Map) { 217 if (map is! Map) {
218 _error('"$field" field must be a map.', fields.nodes[field].span); 218 _error('"$field" field must be a map.', fields.nodes[field].span);
219 } 219 }
220 220
221 return map.containsKey(package); 221 return map.containsKey(package);
222 }); 222 });
223 } 223 }
224 224
225 /// The environment-related metadata. 225 /// The constraint on the Dart SDK, or [VersionConstraint.any] if none is
226 PubspecEnvironment get environment { 226 /// specified.
227 if (_environment != null) return _environment; 227 VersionConstraint get dartSdkConstraint {
228 _parseEnvironment();
229 return _dartSdkConstraint;
230 }
231 VersionConstraint _dartSdkConstraint;
232
233 /// The constraint on the Flutter SDK, or `null` if none is specified.
234 VersionConstraint get flutterSdkConstraint {
235 _parseEnvironment();
236 return _flutterSdkConstraint;
237 }
238 VersionConstraint _flutterSdkConstraint;
239
240 /// Parses the "environment" field and sets [_dartSdkConstraint] and
241 /// [_flutterSdkConstraint] accordingly.
242 void _parseEnvironment() {
243 if (_dartSdkConstraint != null) return;
228 244
229 var yaml = fields['environment']; 245 var yaml = fields['environment'];
230 if (yaml == null) { 246 if (yaml == null) {
231 _environment = new PubspecEnvironment(VersionConstraint.any); 247 _dartSdkConstraint = VersionConstraint.any;
232 return _environment; 248 return;
233 } 249 }
234 250
235 if (yaml is! Map) { 251 if (yaml is! Map) {
236 _error('"environment" field must be a map.', 252 _error('"environment" field must be a map.',
237 fields.nodes['environment'].span); 253 fields.nodes['environment'].span);
238 } 254 }
239 255
240 _environment = new PubspecEnvironment( 256 _dartSdkConstraint = _parseVersionConstraint(yaml.nodes['sdk']);
241 _parseVersionConstraint(yaml.nodes['sdk'])); 257 _flutterSdkConstraint = yaml.containsKey('flutter')
242 return _environment; 258 ? _parseVersionConstraint(yaml.nodes['flutter'])
259 : null;
243 } 260 }
244 PubspecEnvironment _environment;
245 261
246 /// The URL of the server that the package should default to being published 262 /// The URL of the server that the package should default to being published
247 /// to, "none" if the package should not be published, or `null` if it should 263 /// to, "none" if the package should not be published, or `null` if it should
248 /// be published to the default server. 264 /// be published to the default server.
249 /// 265 ///
250 /// If this does return a URL string, it will be a valid parseable URL. 266 /// If this does return a URL string, it will be a valid parseable URL.
251 String get publishTo { 267 String get publishTo {
252 if (_parsedPublishTo) return _publishTo; 268 if (_parsedPublishTo) return _publishTo;
253 269
254 var publishTo = fields['publish_to']; 270 var publishTo = fields['publish_to'];
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 pubspecPath); 366 pubspecPath);
351 } 367 }
352 368
353 return new Pubspec.parse(readTextFile(pubspecPath), sources, 369 return new Pubspec.parse(readTextFile(pubspecPath), sources,
354 expectedName: expectedName, location: pubspecUri); 370 expectedName: expectedName, location: pubspecUri);
355 } 371 }
356 372
357 Pubspec(this._name, {Version version, Iterable<PackageDep> dependencies, 373 Pubspec(this._name, {Version version, Iterable<PackageDep> dependencies,
358 Iterable<PackageDep> devDependencies, 374 Iterable<PackageDep> devDependencies,
359 Iterable<PackageDep> dependencyOverrides, 375 Iterable<PackageDep> dependencyOverrides,
360 VersionConstraint sdkConstraint, 376 VersionConstraint dartSdkConstraint,
377 VersionConstraint flutterSdkConstraint,
361 Iterable<Iterable<TransformerConfig>> transformers, 378 Iterable<Iterable<TransformerConfig>> transformers,
362 Map fields, SourceRegistry sources}) 379 Map fields, SourceRegistry sources})
363 : _version = version, 380 : _version = version,
364 _dependencies = dependencies == null ? null : dependencies.toList(), 381 _dependencies = dependencies == null ? null : dependencies.toList(),
365 _devDependencies = devDependencies == null ? null : 382 _devDependencies = devDependencies == null ? null :
366 devDependencies.toList(), 383 devDependencies.toList(),
367 _dependencyOverrides = dependencyOverrides == null ? null : 384 _dependencyOverrides = dependencyOverrides == null ? null :
368 dependencyOverrides.toList(), 385 dependencyOverrides.toList(),
369 _environment = new PubspecEnvironment(sdkConstraint), 386 _dartSdkConstraint = dartSdkConstraint ?? VersionConstraint.any,
387 _flutterSdkConstraint = flutterSdkConstraint,
370 _transformers = transformers == null ? [] : 388 _transformers = transformers == null ? [] :
371 transformers.map((phase) => phase.toSet()).toList(), 389 transformers.map((phase) => phase.toSet()).toList(),
372 fields = fields == null ? new YamlMap() : new YamlMap.wrap(fields), 390 fields = fields == null ? new YamlMap() : new YamlMap.wrap(fields),
373 _sources = sources; 391 _sources = sources;
374 392
375 Pubspec.empty() 393 Pubspec.empty()
376 : _sources = null, 394 : _sources = null,
377 _name = null, 395 _name = null,
378 _version = Version.none, 396 _version = Version.none,
379 _dependencies = <PackageDep>[], 397 _dependencies = <PackageDep>[],
380 _devDependencies = <PackageDep>[], 398 _devDependencies = <PackageDep>[],
381 _environment = new PubspecEnvironment(), 399 _dartSdkConstraint = VersionConstraint.any,
400 _flutterSdkConstraint = null,
382 _transformers = <Set<TransformerConfig>>[], 401 _transformers = <Set<TransformerConfig>>[],
383 fields = new YamlMap(); 402 fields = new YamlMap();
384 403
385 /// Returns a Pubspec object for an already-parsed map representing its 404 /// Returns a Pubspec object for an already-parsed map representing its
386 /// contents. 405 /// contents.
387 /// 406 ///
388 /// If [expectedName] is passed and the pubspec doesn't have a matching name 407 /// If [expectedName] is passed and the pubspec doesn't have a matching name
389 /// field, this will throw a [PubspecError]. 408 /// field, this will throw a [PubspecError].
390 /// 409 ///
391 /// [location] is the location from which this pubspec was loaded. 410 /// [location] is the location from which this pubspec was loaded.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 } on PubspecException catch (e) { 450 } on PubspecException catch (e) {
432 errors.add(e); 451 errors.add(e);
433 } 452 }
434 } 453 }
435 454
436 _getError(() => this.name); 455 _getError(() => this.name);
437 _getError(() => this.version); 456 _getError(() => this.version);
438 _getError(() => this.dependencies); 457 _getError(() => this.dependencies);
439 _getError(() => this.devDependencies); 458 _getError(() => this.devDependencies);
440 _getError(() => this.transformers); 459 _getError(() => this.transformers);
441 _getError(() => this.environment);
442 _getError(() => this.publishTo); 460 _getError(() => this.publishTo);
461 _getError(() => this._parseEnvironment());
443 return errors; 462 return errors;
444 } 463 }
445 464
446 /// Parses the dependency field named [field], and returns the corresponding 465 /// Parses the dependency field named [field], and returns the corresponding
447 /// list of dependencies. 466 /// list of dependencies.
448 List<PackageDep> _parseDependencies(String field) { 467 List<PackageDep> _parseDependencies(String field) {
449 var dependencies = <PackageDep>[]; 468 var dependencies = <PackageDep>[];
450 469
451 var yaml = fields[field]; 470 var yaml = fields[field];
452 // Allow an empty dependencies key. 471 // Allow an empty dependencies key.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 }); 541 });
523 542
524 dependencies.add(ref.withConstraint(versionConstraint)); 543 dependencies.add(ref.withConstraint(versionConstraint));
525 }); 544 });
526 545
527 return dependencies; 546 return dependencies;
528 } 547 }
529 548
530 /// Parses [node] to a [VersionConstraint]. 549 /// Parses [node] to a [VersionConstraint].
531 VersionConstraint _parseVersionConstraint(YamlNode node) { 550 VersionConstraint _parseVersionConstraint(YamlNode node) {
532 if (node.value == null) return VersionConstraint.any; 551 if (node?.value == null) return VersionConstraint.any;
533 if (node.value is! String) { 552 if (node.value is! String) {
534 _error('A version constraint must be a string.', node.span); 553 _error('A version constraint must be a string.', node.span);
535 } 554 }
536 555
537 return _wrapFormatException('version constraint', node.span, 556 return _wrapFormatException('version constraint', node.span,
538 () => new VersionConstraint.parse(node.value)); 557 () => new VersionConstraint.parse(node.value));
539 } 558 }
540 559
541 /// Makes sure the same package doesn't appear as both a regular and dev 560 /// Makes sure the same package doesn't appear as both a regular and dev
542 /// dependency. 561 /// dependency.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 _error('Invalid $description: ${e.message}', e.span); 601 _error('Invalid $description: ${e.message}', e.span);
583 } 602 }
584 } 603 }
585 604
586 /// Throws a [PubspecException] with the given message. 605 /// Throws a [PubspecException] with the given message.
587 void _error(String message, SourceSpan span) { 606 void _error(String message, SourceSpan span) {
588 throw new PubspecException(message, span); 607 throw new PubspecException(message, span);
589 } 608 }
590 } 609 }
591 610
592 /// The environment-related metadata in the pubspec.
593 ///
594 /// Corresponds to the data under the "environment:" key in the pubspec.
595 class PubspecEnvironment {
596 /// The version constraint specifying which SDK versions this package works
597 /// with.
598 final VersionConstraint sdkVersion;
599
600 PubspecEnvironment([VersionConstraint sdk])
601 : sdkVersion = sdk != null ? sdk : VersionConstraint.any;
602 }
603
604 /// An exception thrown when parsing a pubspec. 611 /// An exception thrown when parsing a pubspec.
605 /// 612 ///
606 /// These exceptions are often thrown lazily while accessing pubspec properties. 613 /// These exceptions are often thrown lazily while accessing pubspec properties.
607 class PubspecException extends SourceSpanFormatException 614 class PubspecException extends SourceSpanFormatException
608 implements ApplicationException { 615 implements ApplicationException {
609 PubspecException(String message, SourceSpan span) 616 PubspecException(String message, SourceSpan span)
610 : super(message, span); 617 : super(message, span);
611 } 618 }
612 619
613 /// Returns whether [uri] is a file URI. 620 /// Returns whether [uri] is a file URI.
614 /// 621 ///
615 /// This is slightly more complicated than just checking if the scheme is 622 /// This is slightly more complicated than just checking if the scheme is
616 /// 'file', since relative URIs also refer to the filesystem on the VM. 623 /// 'file', since relative URIs also refer to the filesystem on the VM.
617 bool _isFileUri(Uri uri) => uri.scheme == 'file' || uri.scheme == ''; 624 bool _isFileUri(Uri uri) => uri.scheme == 'file' || uri.scheme == '';
OLDNEW
« no previous file with comments | « lib/src/lock_file.dart ('k') | lib/src/solver/backtracking_solver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698