88// result of migration, as well as categories (and counts) of exceptions that
99// occurred.
1010
11+ import 'dart:convert' ;
1112import 'dart:io' ;
1213
13- import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
14+ import 'package:analyzer/src/ dart/analysis/analysis_context_collection.dart' ;
1415import 'package:analyzer/dart/ast/ast.dart' ;
1516import 'package:analyzer/src/generated/source.dart' ;
1617import 'package:analyzer_plugin/protocol/protocol_common.dart' ;
18+ import 'package:args/args.dart' ;
1719import 'package:nnbd_migration/nnbd_migration.dart' ;
20+ import 'package:path/path.dart' as path;
1821
1922main (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+
88126void 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
102149class _Listener implements NullabilityMigrationListener {
0 commit comments