@@ -6,6 +6,7 @@ import 'dart:async';
66import 'dart:convert' show jsonDecode;
77import 'dart:io' hide File;
88
9+ import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
910import 'package:analyzer/dart/analysis/results.dart' ;
1011import 'package:analyzer/diagnostic/diagnostic.dart' ;
1112import 'package:analyzer/error/error.dart' ;
@@ -155,8 +156,8 @@ class DependencyChecker {
155156 _logger.stderr ('Visit https://dart.dev/tools/pub/cmd/pub-outdated for '
156157 'more information.' );
157158 _logger.stderr ('' );
158- _logger.stderr ('Force migration with '
159- '--${CommandLineOptions .skipPubOutdatedFlag } (not recommended)' );
159+ _logger.stderr ('You can force migration with '
160+ " '--${CommandLineOptions .skipPubOutdatedFlag }' (not recommended)." );
160161 return false ;
161162 }
162163 return true ;
@@ -235,6 +236,8 @@ class MigrationCli {
235236
236237 _FixCodeProcessor _fixCodeProcessor;
237238
239+ AnalysisContextCollection _contextCollection;
240+
238241 MigrationCli (
239242 {@required this .binaryName,
240243 @visibleForTesting this .loggerFactory = _defaultLoggerFactory,
@@ -316,6 +319,12 @@ class MigrationCli {
316319 argResults[CommandLineOptions .skipPubOutdatedFlag] as bool ,
317320 summary: argResults[CommandLineOptions .summaryOption] as String ,
318321 webPreview: webPreview);
322+
323+ _contextCollection = AnalysisContextCollectionImpl (
324+ includedPaths: [options.directory],
325+ resourceProvider: resourceProvider,
326+ sdkPath: pathContext.normalize (options.sdkPath));
327+
319328 if (isVerbose) {
320329 logger = loggerFactory (true );
321330 }
@@ -352,16 +361,19 @@ class MigrationCli {
352361 logger.stdout ('Migrating ${options .directory }' );
353362 logger.stdout ('' );
354363
364+ if (hasMultipleAnalysisContext) {
365+ logger.stdout (
366+ 'Note: more than one project found; migrating the top-level project.' );
367+ logger.stdout ('' );
368+ }
369+
370+ DriverBasedAnalysisContext context = analysisContext;
371+
355372 List <String > previewUrls;
356373 NonNullableFix nonNullableFix;
374+
357375 await _withProgress (
358376 '${ansi .emphasized ('Generating migration suggestions' )}' , () async {
359- var contextCollection = AnalysisContextCollectionImpl (
360- includedPaths: [options.directory],
361- resourceProvider: resourceProvider,
362- sdkPath: options.sdkPath);
363- DriverBasedAnalysisContext context =
364- contextCollection.contexts.single as DriverBasedAnalysisContext ;
365377 _fixCodeProcessor = _FixCodeProcessor (context, this );
366378 _dartFixListener =
367379 DartFixListener (DriverProviderImpl (resourceProvider, context));
@@ -376,6 +388,8 @@ class MigrationCli {
376388 _fixCodeProcessor.nonNullableFixTask = nonNullableFix;
377389
378390 try {
391+ // TODO(devoncarew): The progress written by the fix processor conflicts
392+ // with the progress written by the ansi logger above.
379393 await _fixCodeProcessor.runFirstPhase ();
380394 _fixCodeProcessor._progressBar.clear ();
381395 _checkForErrors ();
@@ -437,6 +451,23 @@ Use this interactive web view to review, improve, or apply the results.
437451 exitCode = 0 ;
438452 }
439453
454+ @visibleForTesting
455+ DriverBasedAnalysisContext get analysisContext {
456+ // Handle the case of more than one analysis context being found (typically,
457+ // the current directory and one or more sub-directories).
458+ if (hasMultipleAnalysisContext) {
459+ return _contextCollection.contextFor (options.directory)
460+ as DriverBasedAnalysisContext ;
461+ } else {
462+ return _contextCollection.contexts.single as DriverBasedAnalysisContext ;
463+ }
464+ }
465+
466+ @visibleForTesting
467+ bool get hasMultipleAnalysisContext {
468+ return _contextCollection.contexts.length > 1 ;
469+ }
470+
440471 /// Perform the indicated source edits to the given source, returning the
441472 /// resulting transformed text.
442473 String _applyEdits (SourceFileEdit sourceFileEdit, String source) {
0 commit comments