-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathexecutable.dart
187 lines (158 loc) · 5.79 KB
/
executable.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
import 'dart:async';
import 'dart:io';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:args/command_runner.dart';
import 'package:dart_dev/dart_dev.dart';
import 'package:dart_dev/src/dart_dev_tool.dart';
import 'package:dart_dev/src/utils/format_tool_builder.dart';
import 'package:dart_dev/src/utils/parse_flag_from_args.dart';
import 'package:io/ansi.dart';
import 'package:io/io.dart' show ExitCode;
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import '../utils.dart';
import 'dart_dev_runner.dart';
import 'tools/over_react_format_tool.dart';
import 'utils/assert_dir_is_dart_package.dart';
import 'utils/dart_tool_cache.dart';
import 'utils/ensure_process_exit.dart';
import 'utils/logging.dart';
typedef _ConfigGetter = Map<String, DevTool> Function();
final _runScriptPath = p.join(cacheDirPath, 'run.dart');
final _runScript = File(_runScriptPath);
const _configPath = 'tool/dart_dev/config.dart';
const _oldDevDartPath = 'tool/dev.dart';
final _relativeDevDartPath = p.relative(
p.absolute(_configPath),
from: p.absolute(p.dirname(_runScriptPath)),
);
Future<void> run(List<String> args) async {
attachLoggerToStdio(args);
final configExists = File(_configPath).existsSync();
final oldDevDartExists = File(_oldDevDartPath).existsSync();
if (!configExists) {
log.fine('No custom `tool/dart_dev/config.dart` file found; '
'using default config.');
}
if (oldDevDartExists) {
log.warning(yellow.wrap(
'dart_dev v3 now expects configuration to be at `$_configPath`,\n'
'but `$_oldDevDartPath` still exists. View the guide to see how to upgrade:\n'
'https://github.com/Workiva/dart_dev/blob/master/doc/v3-upgrade-guide.md'));
}
if (args.contains('hackFastFormat') && !oldDevDartExists) {
await handleFastFormat(args);
return;
}
generateRunScript();
final process = await Process.start(
Platform.executable, [_runScriptPath, ...args],
mode: ProcessStartMode.inheritStdio);
ensureProcessExit(process);
exitCode = await process.exitCode;
}
Future<void> handleFastFormat(List<String> args) async {
assertDirIsDartPackage();
DevTool formatTool;
final configFile = File(_configPath);
if (configFile.existsSync()) {
final toolBuilder = FormatToolBuilder();
parseString(content: configFile.readAsStringSync())
.unit
.accept(toolBuilder);
formatTool = toolBuilder
.formatDevTool; // could be null if no custom `format` entry found
if (formatTool == null && toolBuilder.failedToDetectAKnownFormatter) {
exitCode = ExitCode.config.code;
log.severe('Failed to reconstruct the format tool\'s configuration.\n\n'
'This is likely because dart_dev expects either the FormatTool class or the\n'
'OverReactFormatTool class.');
return;
}
}
formatTool ??= chooseDefaultFormatTool();
try {
exitCode = await DartDevRunner({'hackFastFormat': formatTool}).run(args);
} catch (error, stack) {
log.severe('Uncaught Exception:', error, stack);
if (!parseFlagFromArgs(args, 'verbose', abbr: 'v')) {
// Always print the stack trace for an uncaught exception.
stderr.writeln(stack);
}
exitCode = ExitCode.unavailable.code;
}
}
void generateRunScript() {
if (shouldWriteRunScript) {
logTimedSync(log, 'Generating run script', () {
createCacheDir();
_runScript.writeAsStringSync(buildDartDevRunScriptContents());
}, level: Level.INFO);
}
}
bool get shouldWriteRunScript =>
!_runScript.existsSync() ||
_runScript.readAsStringSync() != buildDartDevRunScriptContents();
String buildDartDevRunScriptContents() {
final hasCustomToolDevDart = File(_configPath).existsSync();
return '''
import 'dart:io';
import 'package:dart_dev/src/core_config.dart';
import 'package:dart_dev/src/executable.dart' as executable;
${hasCustomToolDevDart ? "import '$_relativeDevDartPath' as custom_dev;" : ""}
void main(List<String> args) async {
await executable.runWithConfig(args,
() => ${hasCustomToolDevDart ? 'custom_dev.config' : 'coreConfig'});
}
''';
}
Future<void> runWithConfig(
List<String> args, _ConfigGetter configGetter) async {
attachLoggerToStdio(args);
try {
assertDirIsDartPackage();
} on DirectoryIsNotPubPackage catch (error) {
log.severe(error);
return ExitCode.usage.code;
}
Map<String, DevTool> config;
try {
config = configGetter();
} catch (error) {
stderr
..writeln(
'Invalid "tool/dart_dev/config.dart" in ${p.absolute(p.current)}')
..writeln()
..writeln('It should provide a `Map<String, DevTool> config;` getter,'
' but it either does not exist or threw unexpectedly:')
..writeln(' $error')
..writeln()
..writeln('For more info: http://github.com/Workiva/dart_dev#TODO');
return ExitCode.config.code;
}
try {
exitCode = await DartDevRunner(config).run(args);
} on UsageException catch (error) {
stderr.writeln(error);
exitCode = ExitCode.usage.code;
} catch (error, stack) {
log.severe('Uncaught Exception:', error, stack);
if (!parseFlagFromArgs(args, 'verbose', abbr: 'v')) {
// Always print the stack trace for an uncaught exception.
stderr.writeln(stack);
}
exitCode = ExitCode.unavailable.code;
}
}
/// Returns [OverReactFormatTool] if `over_react_format` is a direct dependency,
/// and the default [FormatTool] otherwise.
DevTool chooseDefaultFormatTool({String path}) {
final pubspec = cachedPubspec(path: path);
const orf = 'over_react_format';
final hasOverReactFormat = pubspec.dependencies.containsKey(orf) ||
pubspec.devDependencies.containsKey(orf) ||
pubspec.dependencyOverrides.containsKey(orf);
return hasOverReactFormat
? OverReactFormatTool()
: (FormatTool()..formatter = Formatter.dartStyle);
}