Skip to content

Commit cd4fa41

Browse files
authored
Add option for setting arguments for tool compilation. (#2576)
* Add option for setting --no-sound-null-safety on tool compilation. * Switch to general compile args. * Added to README.md, changed name to compile_args from compile-args * Fix typo
1 parent d0a87c7 commit cd4fa41

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ dartdoc:
289289
command: ["bin/drill.dart"]
290290
setup_command: ["bin/setup.dart"]
291291
description: "Puts holes in things."
292+
compile_args: ["--no-sound-null-safety"]
292293
echo:
293294
macos: ['/bin/sh', '-c', 'echo']
294295
setup_macos: ['/bin/sh', '-c', 'setup.sh']
@@ -328,6 +329,9 @@ The `description` is just a short description of the tool for use as help text.
328329
Only tools which are configured in the `dartdoc_options.yaml` file are able to
329330
be invoked.
330331

332+
The `compile_args` tag is used to pass options to the dart compiler when the
333+
first run of the tool is being snapshotted.
334+
331335
To use the tools in comment documentation, use the `{@tool <name> [<options>
332336
...] [$INPUT]}` directive to invoke the tool:
333337

lib/src/dartdoc_options.dart

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const int _kIntVal = 0;
3939
const double _kDoubleVal = 0.0;
4040
const bool _kBoolVal = true;
4141

42+
const String _kCompileArgsTagName = 'compile_args';
43+
4244
int get _usageLineLength => stdout.hasTerminal ? stdout.terminalColumns : null;
4345

4446
typedef ConvertYamlToType<T> = T Function(YamlMap, String, ResourceProvider);
@@ -147,14 +149,22 @@ class ToolDefinition {
147149
List<String> command,
148150
List<String> setupCommand,
149151
String description,
150-
ResourceProvider resourceProvider) {
152+
ResourceProvider resourceProvider,
153+
{List<String> compileArgs}) {
151154
assert(command != null);
152155
assert(command.isNotEmpty);
153156
assert(description != null);
154157
if (isDartExecutable(command[0])) {
155158
return DartToolDefinition(
156-
command, setupCommand, description, resourceProvider);
159+
command, setupCommand, description, resourceProvider,
160+
compileArgs: compileArgs ?? const []);
157161
} else {
162+
if (compileArgs != null && compileArgs.isNotEmpty) {
163+
throw DartdocOptionError(
164+
'Compile arguments may only be specified for Dart tools, but '
165+
'$_kCompileArgsTagName of $compileArgs were specified for '
166+
'$command.');
167+
}
158168
return ToolDefinition(command, setupCommand, description);
159169
}
160170
}
@@ -274,6 +284,9 @@ class SnapshotCache {
274284
class DartToolDefinition extends ToolDefinition {
275285
final ResourceProvider _resourceProvider;
276286

287+
/// A list of arguments to add to the snapshot compilation arguments.
288+
final List<String> compileArgs;
289+
277290
/// Takes a list of args to modify, and returns the name of the executable
278291
/// to run. If no snapshot file existed, then create one and modify the args
279292
/// so that if they are executed with dart, will result in the snapshot being
@@ -295,7 +308,8 @@ class DartToolDefinition extends ToolDefinition {
295308
'--ignore-unrecognized-flags',
296309
'--verbosity=error',
297310
'--snapshot=${_resourceProvider.pathContext.absolute(snapshotFile.path)}',
298-
'--snapshot_kind=app-jit'
311+
'--snapshot_kind=app-jit',
312+
...compileArgs,
299313
]);
300314
} else {
301315
await snapshot.snapshotValid();
@@ -307,8 +321,10 @@ class DartToolDefinition extends ToolDefinition {
307321
}
308322

309323
DartToolDefinition(List<String> command, List<String> setupCommand,
310-
String description, this._resourceProvider)
311-
: super(command, setupCommand, description);
324+
String description, this._resourceProvider,
325+
{this.compileArgs = const []})
326+
: assert(compileArgs != null),
327+
super(command, setupCommand, description);
312328
}
313329

314330
/// A configuration class that can interpret [ToolDefinition]s from a YAML map.
@@ -335,6 +351,7 @@ class ToolConfiguration {
335351
for (var entry in yamlMap.entries) {
336352
var name = entry.key.toString();
337353
var toolMap = entry.value;
354+
List<String> compileArgs;
338355
String description;
339356
List<String> command;
340357
List<String> setupCommand;
@@ -376,6 +393,27 @@ class ToolConfiguration {
376393

377394
command = findCommand();
378395
setupCommand = findCommand('setup_');
396+
397+
List<String> findArgs() {
398+
List<String> args;
399+
if (toolMap.containsKey(_kCompileArgsTagName)) {
400+
var compileArgs = toolMap[_kCompileArgsTagName];
401+
if (compileArgs is String) {
402+
args = [toolMap[_kCompileArgsTagName].toString()];
403+
} else if (compileArgs is YamlList) {
404+
args =
405+
compileArgs.map<String>((node) => node.toString()).toList();
406+
} else {
407+
throw DartdocOptionError(
408+
'Tool compile arguments must be a list of strings. The tool '
409+
'$name has a $_kCompileArgsTagName entry that is a '
410+
'${compileArgs.runtimeType}');
411+
}
412+
}
413+
return args;
414+
}
415+
416+
compileArgs = findArgs();
379417
} else {
380418
throw DartdocOptionError(
381419
'Tools must be defined as a map of tool names to definitions. Tool '
@@ -421,7 +459,8 @@ class ToolConfiguration {
421459
setupCommand;
422460
}
423461
newToolDefinitions[name] = ToolDefinition.fromCommand(
424-
[executable] + command, setupCommand, description, resourceProvider);
462+
[executable] + command, setupCommand, description, resourceProvider,
463+
compileArgs: compileArgs ?? const []);
425464
}
426465
return ToolConfiguration._(newToolDefinitions, resourceProvider);
427466
}

lib/src/model/documentation_comment.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,14 @@ mixin DocumentationComment
191191
/// ```yaml
192192
/// dartdoc:
193193
/// tools:
194-
/// # Prefixes the given input with "## "
195-
/// # Path is relative to project root.
196-
/// prefix: "bin/prefix.dart"
197-
/// # Prints the date
198-
/// date: "/bin/date"
194+
/// prefix:
195+
/// # Path is relative to project root.
196+
/// command: ["bin/prefix.dart"]
197+
/// description: "Prefixes the given input with '##'."
198+
/// compile_args: ["--no-sound-null-safety"]
199+
/// date:
200+
/// command: ["/bin/date"]
201+
/// description: "Prints the date"
199202
/// ```
200203
///
201204
/// In code:

test/tool_runner_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void main() {
5959
drill:
6060
command: ["bin/drill.dart"]
6161
description: "Puts holes in things."
62+
compile_args: ["--no-sound-null-safety"]
6263
snapshot_drill:
6364
command: ["${snapshotFile.replaceAll(r'\', r'\\')}"]
6465
description: "Puts holes in things, but faster."
@@ -104,6 +105,10 @@ echo:
104105
});
105106
// This test must come first, to verify that the first run creates
106107
// a snapshot.
108+
test('Tool definition includes compile arguments.', () async {
109+
DartToolDefinition definition = runner.toolConfiguration.tools['drill'];
110+
expect(definition.compileArgs, equals(['--no-sound-null-safety']));
111+
});
107112
test('can invoke a Dart tool, and second run is a snapshot.', () async {
108113
var result = await runner.run(
109114
['drill', r'--file=$INPUT'],
@@ -114,6 +119,8 @@ echo:
114119
expect(result, contains('--file=<INPUT_FILE>'));
115120
expect(result, contains('## `TEST INPUT`'));
116121
expect(result, contains('Script location is in dartdoc tree.'));
122+
// Compile args shouldn't be in the args passed to the tool.
123+
expect(result, isNot(contains('--no-sound-null-safety')));
117124
expect(setupFile.existsSync(), isFalse);
118125
result = await runner.run(
119126
['drill', r'--file=$INPUT'],

0 commit comments

Comments
 (0)