Skip to content

Commit ed21045

Browse files
committed
wip - add-overrides command
1 parent 8ee1a0b commit ed21045

File tree

10 files changed

+138
-5
lines changed

10 files changed

+138
-5
lines changed

.github/workflows/dart.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created with package:mono_repo v4.0.0
1+
# Created with package:mono_repo v4.1.0-dev
22
name: Dart CI
33
on:
44
push:

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created with package:mono_repo v4.0.0
1+
# Created with package:mono_repo v4.1.0-dev
22
language: dart
33

44
jobs:

mono_repo.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ github:
1919
"${CHAT_WEBHOOK_URL}"
2020
env:
2121
CHAT_WEBHOOK_URL: ${{ secrets.CHAT_WEBHOOK_URL }}
22+
23+
dependency_overrides:
24+
shelf_static:
25+
git: https://github.com/dart-lang/shelf_static/

mono_repo/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.0-dev
2+
3+
- WIP on `add-overrides`
4+
15
## 4.0.0
26

37
- Use `FLUTTER_HOME`, if it exists, when using the `pub` command.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

mono_repo/lib/src/mono_config.dart

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'package:json_annotation/json_annotation.dart';
66
import 'package:path/path.dart' as p;
7+
// ignore: implementation_imports
8+
import 'package:pubspec_parse/src/dependency.dart';
79

810
import 'github_config.dart';
911
import 'yaml.dart';
@@ -16,6 +18,7 @@ const _monoConfigFileName = 'mono_repo.yaml';
1618
const _reservedTravisKeys = {'cache', 'jobs', 'language'};
1719

1820
const _allowedMonoConfigKeys = {
21+
'dependency_overrides',
1922
'github',
2023
'merge_stages',
2124
'pretty_ansi',
@@ -41,6 +44,7 @@ class MonoConfig {
4144
final String? selfValidateStage;
4245
final Map<String, dynamic> travis;
4346
final GitHubConfig github;
47+
final Map<String, Dependency> dependencyOverrides;
4448

4549
MonoConfig._({
4650
required Set<CI>? ci,
@@ -52,6 +56,7 @@ class MonoConfig {
5256
required this.selfValidateStage,
5357
required this.travis,
5458
required this.github,
59+
required this.dependencyOverrides,
5560
}) : ci = ci ?? const {CI.travis};
5661

5762
factory MonoConfig({
@@ -62,6 +67,7 @@ class MonoConfig {
6267
required String? selfValidateStage,
6368
required Map travis,
6469
required Map github,
70+
required Map dependencyOverrides,
6571
}) {
6672
final overlappingKeys =
6773
travis.keys.where(_reservedTravisKeys.contains).toList();
@@ -87,6 +93,7 @@ class MonoConfig {
8793
selfValidateStage: selfValidateStage,
8894
travis: travis.map((k, v) => MapEntry(k as String, v))..remove('stages'),
8995
github: GitHubConfig.fromJson(github),
96+
dependencyOverrides: parseDeps(dependencyOverrides),
9097
);
9198
}
9299

@@ -184,6 +191,7 @@ class MonoConfig {
184191
selfValidateStage: _selfValidateFromValue(selfValidate),
185192
travis: travis,
186193
github: github,
194+
dependencyOverrides: json['dependency_overrides'] as Map? ?? {},
187195
);
188196
} else {
189197
throw CheckedFromJsonException(
@@ -208,6 +216,7 @@ class MonoConfig {
208216
selfValidateStage: null,
209217
travis: {},
210218
github: {},
219+
dependencyOverrides: {},
211220
);
212221
}
213222

mono_repo/lib/src/runner.dart

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:async';
77
import 'package:args/args.dart';
88
import 'package:args/command_runner.dart';
99

10+
import 'commands/add_overrides.dart';
1011
import 'commands/check.dart';
1112
import 'commands/generate.dart';
1213
import 'commands/mono_repo_command.dart';
@@ -22,6 +23,7 @@ final commands = List<Command<void>>.unmodifiable(
2223
PresubmitCommand(),
2324
PubCommand(),
2425
TravisCommand(),
26+
AddOverridesCommand(),
2527
],
2628
);
2729

mono_repo/lib/src/version.dart

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mono_repo/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: mono_repo
22
description: >-
33
CLI tools to make it easier to manage a single source repository containing
44
multiple Dart packages.
5-
version: 4.0.0
5+
version: 4.1.0-dev
66
repository: https://github.com/google/mono_repo.dart
77

88
environment:
@@ -20,6 +20,7 @@ dependencies:
2020
pub_semver: ^2.0.0
2121
pubspec_parse: ^1.0.0
2222
yaml: ^3.0.0
23+
yaml_edit: ^2.0.0
2324

2425
dev_dependencies:
2526
build_runner: ^2.0.1

tool/ci.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Created with package:mono_repo v4.0.0
2+
# Created with package:mono_repo v4.1.0-dev
33

44
# Support built in commands on windows out of the box.
55
# When it is a flutter repo (check the pubspec.yaml for "sdk: flutter")

0 commit comments

Comments
 (0)