|
| 1 | +// Copyright (c) 2020, 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:io'; |
| 6 | + |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | +import 'package:pubspec_parse/pubspec_parse.dart'; |
| 9 | +import 'package:yaml/yaml.dart'; |
| 10 | +import 'package:yaml_edit/yaml_edit.dart'; |
| 11 | + |
| 12 | +import '../../mono_repo.dart'; |
| 13 | +import '../root_config.dart'; |
| 14 | +import 'mono_repo_command.dart'; |
| 15 | + |
| 16 | +class AddOverridesCommand extends MonoRepoCommand { |
| 17 | + @override |
| 18 | + String get name => 'add-overrides'; |
| 19 | + |
| 20 | + @override |
| 21 | + String get description => |
| 22 | + 'Generates the CI configuration for child packages.'; |
| 23 | + |
| 24 | + @override |
| 25 | + void run() => addOverrides(rootConfig()); |
| 26 | +} |
| 27 | + |
| 28 | +void addOverrides(RootConfig config) { |
| 29 | + final dependencyOverrides = config.monoConfig.dependencyOverrides; |
| 30 | + if (dependencyOverrides.isEmpty) { |
| 31 | + throw UserException('No dependency_overrides defined!'); |
| 32 | + } |
| 33 | + |
| 34 | + final updateQueue = <String, String>{}; |
| 35 | + |
| 36 | + for (var package in config) { |
| 37 | + print(package.relativePath); |
| 38 | + final pubspecPath = p.join(package.relativePath, 'pubspec.yaml'); |
| 39 | + final lockPath = p.join(package.relativePath, 'pubspec.lock'); |
| 40 | + final lockFile = File(lockPath); |
| 41 | + |
| 42 | + final lockYaml = |
| 43 | + loadYaml(lockFile.readAsStringSync(), sourceUrl: Uri.parse(lockPath)) |
| 44 | + as YamlMap; |
| 45 | + final packages = lockYaml['packages'] as YamlMap; |
| 46 | + |
| 47 | + final toOverride = packages.keys |
| 48 | + .cast<String>() |
| 49 | + .where((element) => dependencyOverrides.keys.contains(element)) |
| 50 | + .toSet(); |
| 51 | + |
| 52 | + if (toOverride.isEmpty) { |
| 53 | + print('Nothing to update in "$pubspecPath".'); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + final pubspecFile = File(pubspecPath); |
| 58 | + final pubspecYaml = YamlEditor(pubspecFile.readAsStringSync()); |
| 59 | + |
| 60 | + final pubspecOverrides = pubspecYaml |
| 61 | + .parseAt(['dependency_overrides'], orElse: () => wrapAsYamlNode(null)); |
| 62 | + |
| 63 | + if (pubspecOverrides is YamlScalar && pubspecOverrides.value == null) { |
| 64 | + // no overrides! |
| 65 | + pubspecYaml.update(['dependency_overrides'], {}); |
| 66 | + } else if (pubspecOverrides is! YamlMap) { |
| 67 | + throw UserException( |
| 68 | + '"${pubspecFile.path}" has a `dependency_overrides` value, but it is ' |
| 69 | + 'not a Map. Not sure what to do!', |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // We're ready to start adding dependency overrides! |
| 74 | + for (var newOverride in toOverride) { |
| 75 | + final newDep = dependencyOverrides[newOverride]!; |
| 76 | + |
| 77 | + Map<String, dynamic> newValue; |
| 78 | + |
| 79 | + if (newDep is PathDependency) { |
| 80 | + final path = p.isAbsolute(newDep.path) |
| 81 | + ? newDep.path |
| 82 | + : p.relative(p.absolute(newDep.path), from: package.relativePath); |
| 83 | + newValue = {'path': path}; |
| 84 | + } else if (newDep is GitDependency) { |
| 85 | + newValue = { |
| 86 | + 'git': <String, String>{ |
| 87 | + 'url': newDep.url.toString(), |
| 88 | + if (newDep.path != null) 'path': newDep.path!, |
| 89 | + if (newDep.ref != null) 'ref': newDep.ref!, |
| 90 | + } |
| 91 | + }; |
| 92 | + } else { |
| 93 | + // TODO: support "normal" dependencies |
| 94 | + throw UserException('Not sure how to write `$newDep`.'); |
| 95 | + } |
| 96 | + |
| 97 | + // TODO: if there is already a value at the target and it doesn't equal |
| 98 | + // the value we're adding – throw! |
| 99 | + |
| 100 | + pubspecYaml.update( |
| 101 | + ['dependency_overrides', newOverride], |
| 102 | + newValue, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + updateQueue[pubspecPath] = pubspecYaml.toString(); |
| 107 | + } |
| 108 | + |
| 109 | + assert(updateQueue.isNotEmpty); |
| 110 | + for (var entry in updateQueue.entries) { |
| 111 | + File(entry.key).writeAsStringSync(entry.value); |
| 112 | + } |
| 113 | +} |
0 commit comments