-
Notifications
You must be signed in to change notification settings - Fork 204
/
options.dart
227 lines (198 loc) · 7.02 KB
/
options.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2016, 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:async';
import 'package:build/build.dart';
import 'package:build/experiments.dart';
import 'package:build_config/build_config.dart';
import 'package:build_resolvers/build_resolvers.dart';
import 'package:glob/glob.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import '../environment/build_environment.dart';
import '../package_graph/package_graph.dart';
import '../package_graph/target_graph.dart';
import 'exceptions.dart';
/// The default list of files visible for non-root packages.
///
/// This is also the default list of files for targets in non-root packages when
/// an explicit include is not provided.
const List<String> defaultNonRootVisibleAssets = [
'CHANGELOG*',
'lib/**',
'bin/**',
'LICENSE*',
'pubspec.yaml',
'README*',
];
/// The default list of files to include when an explicit include is not
/// provided.
///
/// This should be a superset of [defaultNonRootVisibleAssets].
const List<String> defaultRootPackageSources = [
'assets/**',
'benchmark/**',
'bin/**',
'CHANGELOG*',
'example/**',
'lib/**',
'test/**',
'integration_test/**',
'tool/**',
'web/**',
'node/**',
'LICENSE*',
'pubspec.yaml',
'pubspec.lock',
'README*',
r'$package$',
];
final _logger = Logger('BuildOptions');
class LogSubscription {
factory LogSubscription(BuildEnvironment environment,
{bool verbose = false, Level? logLevel}) {
// Set up logging
logLevel ??= verbose ? Level.ALL : Level.INFO;
// Severe logs can fail the build and should always be shown.
if (logLevel == Level.OFF) logLevel = Level.SEVERE;
Logger.root.level = logLevel;
var logListener = Logger.root.onRecord.listen(environment.onLog);
return LogSubscription._(logListener);
}
LogSubscription._(this.logListener);
final StreamSubscription<LogRecord> logListener;
}
/// Describes a set of files that should be built.
class BuildFilter {
/// The package name glob that files must live under in order to match.
final Glob _package;
/// A glob for files under [_package] that must match.
final Glob _path;
BuildFilter(this._package, this._path);
/// Builds a [BuildFilter] from a command line argument.
///
/// Both relative paths and package: uris are supported. Relative
/// paths are treated as relative to the [rootPackage].
///
/// Globs are supported in package names and paths.
factory BuildFilter.fromArg(String arg, String rootPackage) {
var uri = Uri.parse(arg);
if (uri.scheme == 'package') {
var package = uri.pathSegments.first;
var glob = Glob(p.url.joinAll([
'lib',
...uri.pathSegments.skip(1),
]));
return BuildFilter(Glob(package), glob);
} else if (uri.scheme.isEmpty) {
return BuildFilter(Glob(rootPackage), Glob(uri.path));
} else {
throw FormatException('Unsupported scheme ${uri.scheme}', uri);
}
}
/// Returns whether or not [id] mathes this filter.
bool matches(AssetId id) =>
_package.matches(id.package) && _path.matches(id.path);
@override
int get hashCode => Object.hash(
_package.context,
_package.pattern,
_package.recursive,
_path.context,
_path.pattern,
_path.recursive,
);
@override
bool operator ==(Object other) =>
other is BuildFilter &&
other._path.context == _path.context &&
other._path.pattern == _path.pattern &&
other._path.recursive == _path.recursive &&
other._package.context == _package.context &&
other._package.pattern == _package.pattern &&
other._package.recursive == _package.recursive;
}
/// Manages setting up consistent defaults for all options and build modes.
class BuildOptions {
final bool deleteFilesByDefault;
final bool enableLowResourcesMode;
final StreamSubscription logListener;
/// If present, the path to a directory to write performance logs to.
final String? logPerformanceDir;
final PackageGraph packageGraph;
final Resolvers resolvers;
final TargetGraph targetGraph;
final bool trackPerformance;
// Watch mode options.
Duration debounceDelay;
// For testing only, skips the build script updates check.
bool skipBuildScriptCheck;
BuildOptions._({
required this.debounceDelay,
required this.deleteFilesByDefault,
required this.enableLowResourcesMode,
required this.logListener,
required this.packageGraph,
required this.skipBuildScriptCheck,
required this.trackPerformance,
required this.targetGraph,
required this.logPerformanceDir,
required this.resolvers,
});
/// Creates a [BuildOptions] with sane defaults.
///
/// NOTE: If a custom [resolvers] instance is passed it must ensure that it
/// enables [enabledExperiments] on any analysis options it creates.
static Future<BuildOptions> create(
LogSubscription logSubscription, {
Duration debounceDelay = const Duration(milliseconds: 250),
bool deleteFilesByDefault = false,
bool enableLowResourcesMode = false,
required PackageGraph packageGraph,
Map<String, BuildConfig> overrideBuildConfig = const {},
bool skipBuildScriptCheck = false,
bool trackPerformance = false,
String? logPerformanceDir,
Resolvers? resolvers,
}) async {
TargetGraph targetGraph;
try {
targetGraph = await TargetGraph.forPackageGraph(packageGraph,
overrideBuildConfig: overrideBuildConfig,
defaultRootPackageSources: defaultRootPackageSources,
requiredSourcePaths: [r'lib/$lib$'],
requiredRootSourcePaths: [r'$package$', r'lib/$lib$']);
} on BuildConfigParseException catch (e, s) {
_logger.severe('''
Failed to parse `build.yaml` for ${e.packageName}.
If you believe you have gotten this message in error, especially if using a new
feature, you may need to run `dart run build_runner clean` and then rebuild.
''', e.exception, s);
throw const CannotBuildException();
}
/// Set up other defaults.
if (logPerformanceDir != null) {
// Requiring this to be under the root package allows us to use an
// `AssetWriter` to write logs.
if (!p.isWithin(p.current, logPerformanceDir)) {
_logger.severe('Performance logs may only be output under the root '
'package, but got `$logPerformanceDir` which is not.');
throw const CannotBuildException();
}
trackPerformance = true;
}
resolvers ??= AnalyzerResolvers.sharedInstance;
return BuildOptions._(
debounceDelay: debounceDelay,
deleteFilesByDefault: deleteFilesByDefault,
enableLowResourcesMode: enableLowResourcesMode,
logListener: logSubscription.logListener,
packageGraph: packageGraph,
skipBuildScriptCheck: skipBuildScriptCheck,
trackPerformance: trackPerformance,
targetGraph: targetGraph,
logPerformanceDir: logPerformanceDir,
resolvers: resolvers,
);
}
}