-
Notifications
You must be signed in to change notification settings - Fork 203
/
options.dart
153 lines (133 loc) · 4.69 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
// 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_config/build_config.dart';
import 'package:build_resolvers/build_resolvers.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.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 to include when an explicit include is not
/// provided.
const List<String> defaultRootPackageWhitelist = [
'benchmark/**',
'bin/**',
'example/**',
'lib/**',
'test/**',
'tool/**',
'web/**',
'pubspec.yaml',
'pubspec.lock',
];
final _logger = Logger('BuildOptions');
class LogSubscription {
factory LogSubscription(BuildEnvironment environment,
{bool verbose, Level logLevel}) {
// Set up logging
verbose ??= false;
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._(verbose, logListener);
}
LogSubscription._(this.verbose, this.logListener);
final bool verbose;
final StreamSubscription<LogRecord> logListener;
}
/// Manages setting up consistent defaults for all options and build modes.
class BuildOptions {
// Build mode options.
final StreamSubscription logListener;
final PackageGraph packageGraph;
final bool deleteFilesByDefault;
final bool enableLowResourcesMode;
final bool trackPerformance;
final bool verbose;
final List<String> buildDirs;
final TargetGraph targetGraph;
final Resolvers resolvers;
/// If present, the path to a directory to write performance logs to.
final String logPerformanceDir;
// 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.verbose,
@required this.buildDirs,
@required this.targetGraph,
@required this.logPerformanceDir,
@required this.resolvers,
});
static Future<BuildOptions> create(
LogSubscription logSubscription, {
Duration debounceDelay,
bool deleteFilesByDefault,
bool enableLowResourcesMode,
@required PackageGraph packageGraph,
Map<String, BuildConfig> overrideBuildConfig,
bool skipBuildScriptCheck,
bool trackPerformance,
List<String> buildDirs,
String logPerformanceDir,
Resolvers resolvers,
}) async {
TargetGraph targetGraph;
try {
targetGraph = await TargetGraph.forPackageGraph(packageGraph,
overrideBuildConfig: overrideBuildConfig,
defaultRootPackageWhitelist: defaultRootPackageWhitelist);
} on BuildConfigParseException catch (e, s) {
_logger.severe(
'Failed to parse `build.yaml` for ${e.packageName}.', e.exception, s);
throw CannotBuildException();
}
/// Set up other defaults.
debounceDelay ??= const Duration(milliseconds: 250);
deleteFilesByDefault ??= false;
skipBuildScriptCheck ??= false;
enableLowResourcesMode ??= false;
buildDirs ??= [];
trackPerformance ??= false;
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 CannotBuildException();
}
trackPerformance = true;
}
resolvers ??= AnalyzerResolvers();
return BuildOptions._(
debounceDelay: debounceDelay,
deleteFilesByDefault: deleteFilesByDefault,
enableLowResourcesMode: enableLowResourcesMode,
logListener: logSubscription.logListener,
packageGraph: packageGraph,
skipBuildScriptCheck: skipBuildScriptCheck,
trackPerformance: trackPerformance,
verbose: logSubscription.verbose,
buildDirs: buildDirs,
targetGraph: targetGraph,
logPerformanceDir: logPerformanceDir,
resolvers: resolvers,
);
}
}