Skip to content

Commit 7fcff7e

Browse files
jcollins-gcommit-bot@chromium.org
authored andcommitted
Allow passing the sdk root for analysis to trial_migration.
Also redirects usage warnings to stderr, adds --help, and verifies that the sdk you pass in actually supports NNBD. Change-Id: Ia758f69648f88108b3c0df9de46ab246d3db0c9e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127202 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Mike Fairhurst <mfairhurst@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Janice Collins <jcollins@google.com>
1 parent 929877c commit 7fcff7e

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

pkg/nnbd_migration/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ environment:
55
dependencies:
66
_fe_analyzer_shared: 1.0.0
77
analyzer: ^0.37.0
8+
dev_dependencies:
9+
args: ^1.5.2
10+
path: ^1.6.2

pkg/nnbd_migration/tool/trial_migration.dart

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,51 @@
88
// result of migration, as well as categories (and counts) of exceptions that
99
// occurred.
1010

11+
import 'dart:convert';
1112
import 'dart:io';
1213

13-
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
14+
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
1415
import 'package:analyzer/dart/ast/ast.dart';
1516
import 'package:analyzer/src/generated/source.dart';
1617
import 'package:analyzer_plugin/protocol/protocol_common.dart';
18+
import 'package:args/args.dart';
1719
import 'package:nnbd_migration/nnbd_migration.dart';
20+
import 'package:path/path.dart' as path;
1821

1922
main(List<String> args) async {
20-
if (args.length > 1) {
23+
ArgParser argParser = ArgParser();
24+
ArgResults parsedArgs;
25+
26+
argParser.addFlag('help', abbr: 'h', help: 'Display options');
27+
28+
argParser.addOption('sdk',
29+
abbr: 's',
30+
defaultsTo: path.dirname(path.dirname(Platform.resolvedExecutable)),
31+
help: 'Select the root of the SDK to analyze against for this run '
32+
'(compiled with --nnbd). For example: ../../xcodebuild/DebugX64NNBD/dart-sdk');
33+
34+
try {
35+
parsedArgs = argParser.parse(args);
36+
} on ArgParserException {
37+
stderr.writeln(argParser.usage);
38+
exit(1);
39+
}
40+
if (parsedArgs['help'] as bool) {
41+
print(argParser.usage);
42+
exit(0);
43+
}
44+
45+
if (parsedArgs.rest.length > 1) {
2146
throw 'invalid args. Specify *one* argument to get exceptions of interest.';
2247
}
2348

49+
String sdkPath = path.canonicalize(parsedArgs['sdk'] as String);
50+
2451
warnOnNoAssertions();
25-
String categoryOfInterest = args.isEmpty ? null : args.single;
52+
warnOnNoSdkNnbd(sdkPath);
53+
54+
String categoryOfInterest =
55+
parsedArgs.rest.isEmpty ? null : parsedArgs.rest.single;
2656
var rootUri = Platform.script.resolve('../../..');
2757
var listener = _Listener(categoryOfInterest);
2858
for (var testPath in [
@@ -43,8 +73,8 @@ main(List<String> args) async {
4373
]) {
4474
print('Migrating $testPath');
4575
var testUri = rootUri.resolve(testPath);
46-
var contextCollection =
47-
AnalysisContextCollection(includedPaths: [testUri.toFilePath()]);
76+
var contextCollection = AnalysisContextCollectionImpl(
77+
includedPaths: [testUri.toFilePath()], sdkPath: sdkPath);
4878
var context = contextCollection.contexts.single;
4979
var files = context.contextRoot
5080
.analyzedFiles()
@@ -85,18 +115,35 @@ main(List<String> args) async {
85115
}
86116
}
87117

118+
void printWarning(String warn) {
119+
stderr.writeln('''
120+
!!!
121+
!!! Warning! $warn
122+
!!!
123+
''');
124+
}
125+
88126
void warnOnNoAssertions() {
89127
try {
90128
assert(false);
91129
} catch (e) {
92130
return;
93131
}
94132

95-
print('''
96-
!!!
97-
!!! Warning! You didn't --enable-asserts!
98-
!!!
99-
''');
133+
printWarning("You didn't --enable-asserts!");
134+
}
135+
136+
void warnOnNoSdkNnbd(String sdkPath) {
137+
// TODO(jcollins-g): contact eng-prod for a more foolproof detection method
138+
String libraries = path.join(sdkPath, 'lib', 'libraries.json');
139+
try {
140+
var decodedJson = JsonDecoder().convert(File(libraries).readAsStringSync());
141+
if ((decodedJson['comment:1'] as String).contains('sdk_nnbd')) return;
142+
} on Exception {
143+
printWarning('Unable to determine whether this SDK supports NNBD');
144+
return;
145+
}
146+
printWarning('SDK at $sdkPath not compiled with --nnbd, use --sdk option');
100147
}
101148

102149
class _Listener implements NullabilityMigrationListener {

0 commit comments

Comments
 (0)