|
| 1 | +// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:pub_semver/pub_semver.dart'; |
| 9 | +import 'package:yaml/yaml.dart'; |
| 10 | + |
| 11 | +import 'util.dart'; |
| 12 | + |
| 13 | +class PackageException implements Exception { |
| 14 | + final List<PackageExceptionDetails> details; |
| 15 | + |
| 16 | + PackageException._(this.details); |
| 17 | +} |
| 18 | + |
| 19 | +class PackageExceptionDetails { |
| 20 | + final String error; |
| 21 | + final String description; |
| 22 | + |
| 23 | + const PackageExceptionDetails._(this.error, {this.description}); |
| 24 | + |
| 25 | + static const noPubspecLock = const PackageExceptionDetails._( |
| 26 | + '`pubspec.lock` does not exist.', |
| 27 | + description: |
| 28 | + 'Run `webdev` in a Dart package directory. Run `pub get` first.'); |
| 29 | + |
| 30 | + static const noBuildRunnerDep = const PackageExceptionDetails._( |
| 31 | + 'A dependency on `build_runner` was not found.', |
| 32 | + description: |
| 33 | + 'You must have a dependency on `build_runner` in `pubspec.yaml`. ' |
| 34 | + 'It can be in either `dependencies` or `dev_dependencies`.'); |
| 35 | + |
| 36 | + @override |
| 37 | + String toString() => [error, description].join('\n'); |
| 38 | +} |
| 39 | + |
| 40 | +Future checkPubspecLock() async { |
| 41 | + var file = new File('pubspec.lock'); |
| 42 | + if (!file.existsSync()) { |
| 43 | + throw new PackageException._([PackageExceptionDetails.noPubspecLock]); |
| 44 | + } |
| 45 | + |
| 46 | + var pubspecLock = loadYaml(await file.readAsString()) as YamlMap; |
| 47 | + |
| 48 | + var packages = pubspecLock['packages'] as YamlMap; |
| 49 | + |
| 50 | + var issues = <PackageExceptionDetails>[]; |
| 51 | + |
| 52 | + var buildRunner = packages['build_runner'] as YamlMap; |
| 53 | + if (buildRunner == null) { |
| 54 | + issues.add(PackageExceptionDetails.noBuildRunnerDep); |
| 55 | + } else { |
| 56 | + var dependency = buildRunner['dependency'] as String; |
| 57 | + if (!dependency.startsWith('direct ')) { |
| 58 | + issues.add(PackageExceptionDetails.noBuildRunnerDep); |
| 59 | + } |
| 60 | + |
| 61 | + var version = buildRunner['version'] as String; |
| 62 | + if (version == null) { |
| 63 | + // TODO: warning? |
| 64 | + } else { |
| 65 | + var buildRunnerVersion = new Version.parse(version); |
| 66 | + |
| 67 | + if (!supportedBuildRunnerVersionRange.allows(buildRunnerVersion)) { |
| 68 | + var error = 'The `build_runner` version – $buildRunnerVersion – is not ' |
| 69 | + 'within the supported range – $supportedBuildRunnerVersionRange.'; |
| 70 | + issues.add(new PackageExceptionDetails._(error)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + var source = buildRunner['source'] as String; |
| 75 | + if (source == 'hosted') { |
| 76 | + //var description = buildRunner['description'] as YamlMap; |
| 77 | + // TODO: check for `{url: https://pub.dartlang.org, name: build_runner}` |
| 78 | + // If not, print a warning |
| 79 | + } else { |
| 80 | + // TODO: print a warning that we're assuming hosted |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // TODO: validate build_web_compilers |
| 85 | + //var buldWebCompilers = packages['build_web_compilers']; |
| 86 | + |
| 87 | + if (issues.isNotEmpty) { |
| 88 | + throw new PackageException._(issues); |
| 89 | + } |
| 90 | +} |
0 commit comments