forked from google/mono_repo.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_config.dart
182 lines (148 loc) · 5.2 KB
/
root_config.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:yaml/yaml.dart';
import 'commands/github/generate.dart';
import 'commands/github/github_yaml.dart';
import 'mono_config.dart';
import 'package_config.dart';
import 'user_exception.dart';
import 'yaml.dart';
const _legacyPkgConfigFileName = '.mono_repo.yml';
const _pubspecFileName = 'pubspec.yaml';
PackageConfig? _packageConfigFromDir(
String rootDirectory,
String pkgRelativePath,
) {
final legacyConfigPath =
p.join(rootDirectory, pkgRelativePath, _legacyPkgConfigFileName);
if (FileSystemEntity.isFileSync(legacyConfigPath)) {
throw UserException(
'Found legacy package configuration file '
'("$_legacyPkgConfigFileName") in `$pkgRelativePath`.',
details: 'Rename to "$monoPkgFileName".',
);
}
final pkgConfigRelativePath = p.join(pkgRelativePath, monoPkgFileName);
final pkgConfigYaml = yamlMapOrNull(rootDirectory, pkgConfigRelativePath);
if (pkgConfigYaml == null) {
return null;
}
final pubspecFile =
File(p.join(rootDirectory, pkgRelativePath, _pubspecFileName));
if (!pubspecFile.existsSync()) {
throw UserException(
'A `$monoPkgFileName` file was found, but missing'
' an expected `$_pubspecFileName` in `$pkgRelativePath`.',
);
}
final pubspec = Pubspec.parse(
pubspecFile.readAsStringSync(),
sourceUrl: Uri.parse(pubspecFile.path),
);
return PackageConfig.parse(pkgRelativePath, pubspec, pkgConfigYaml);
}
class RootConfig extends ListBase<PackageConfig> {
final String rootDirectory;
final MonoConfig monoConfig;
final List<PackageConfig> _configs;
final Map<String, String>? existingActionVersions;
factory RootConfig({String? rootDirectory, bool recursive = true}) {
rootDirectory ??= p.current;
final configs = <PackageConfig>[];
void visitDirectory(Directory directory) {
final dirs = directory.listSync().whereType<Directory>().toList()
..sort((a, b) => a.path.compareTo(b.path));
for (var subdir in dirs) {
final relativeSubDirPath = p.relative(subdir.path, from: rootDirectory);
final pkgConfig =
_packageConfigFromDir(rootDirectory!, relativeSubDirPath);
if (pkgConfig != null) {
configs.add(pkgConfig);
}
if (recursive) {
visitDirectory(subdir);
}
}
}
visitDirectory(Directory(rootDirectory));
if (configs.isEmpty) {
throw UserException(
'No packages found.',
details: 'Each target package directory must contain '
'a `$monoPkgFileName` file.',
);
}
// If a dependabot configuration file exists, assume the action versions in
// the generated workflow file are maintained by dependabot; parse and use
// those versions.
Map<String, String>? existingActionVersions;
final hasDependabot = dependabotFileNames
.map((name) => File(p.join(rootDirectory!, name)))
.any((file) => file.existsSync());
final githubWorkflowFile =
File(p.join(rootDirectory, defaultGitHubWorkflowFilePath));
if (hasDependabot && githubWorkflowFile.existsSync()) {
existingActionVersions = parseActionVersions(
githubWorkflowFile.readAsStringSync(),
);
}
return RootConfig._(
rootDirectory,
MonoConfig.fromRepo(rootDirectory: rootDirectory),
configs,
existingActionVersions,
);
}
RootConfig._(
this.rootDirectory,
this.monoConfig,
this._configs,
this.existingActionVersions,
);
@override
int get length => _configs.length;
@override
set length(int newLength) =>
throw UnsupportedError('This List is read-only.');
@override
PackageConfig operator [](int index) => _configs[index];
@override
void operator []=(int index, PackageConfig pkg) =>
throw UnsupportedError('This List is read-only.');
/// Parse any github action versions from a workflow file.
///
/// This returns a map of <action name> to <action version>.
static Map<String, String> parseActionVersions(String yamlText) {
// "dart-lang/setup-dart@6a218f2413a3e78e9087f638a238f6b40893203d"
final usageRegex = RegExp(r'([\w\.-]+)\/([\w\.-]+)@([\w\.]+)');
final yaml = loadYaml(yamlText);
final result = <String, String>{};
void collect(dynamic yaml) {
if (yaml is List) {
for (var item in yaml) {
collect(item);
}
} else if (yaml is Map) {
const usesKey = 'uses';
if (yaml.containsKey(usesKey)) {
// dart-lang/setup-dart@6a218f2413a3e78e9087f638a238f6b40893203d
final usage = yaml[usesKey] as String;
final match = usageRegex.firstMatch(usage);
if (match != null) {
result['${match.group(1)}/${match.group(2)}'] = match.group(3)!;
}
}
for (var item in yaml.entries) {
collect(item.value);
}
}
}
collect(yaml);
return result;
}
}