migrate_update_locks.dart 3.9 KB
Newer Older
Gary Qian's avatar
Gary Qian committed
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/terminal.dart';
import '../project.dart';
import 'migrate_utils.dart';

/// Checks if the project uses pubspec dependency locking and prompts if
/// the pub upgrade should be run.
Future<void> updatePubspecDependencies(
    FlutterProject flutterProject,
    MigrateUtils migrateUtils,
    Logger logger,
    Terminal terminal
) async {
  final File pubspecFile = flutterProject.directory.childFile('pubspec.yaml');
  if (!pubspecFile.existsSync()) {
    return;
  }
  if (!pubspecFile.readAsStringSync().contains('# THIS LINE IS AUTOGENERATED')) {
    return;
  }
  logger.printStatus('\nDart dependency locking detected in pubspec.yaml.');
  terminal.usesTerminalUi = true;
  String selection = 'y';
  selection = await terminal.promptForCharInput(
    <String>['y', 'n'],
    logger: logger,
    prompt: 'Do you want the tool to run `flutter pub upgrade --major-versions`? (y)es, (n)o',
    defaultChoiceIndex: 1,
  );
  if (selection == 'y') {
    // Runs `flutter pub upgrade --major-versions`
    await migrateUtils.flutterPubUpgrade(flutterProject.directory.path);
  }
}

/// Checks if gradle dependency locking is used and prompts the developer to
/// remove and back up the gradle dependency lockfile.
Future<void> updateGradleDependencyLocking(
    FlutterProject flutterProject,
    MigrateUtils migrateUtils,
    Logger logger,
    Terminal terminal,
    bool verbose,
    FileSystem fileSystem
) async {
  final Directory androidDir = flutterProject.directory.childDirectory('android');
  if (!androidDir.existsSync()) {
    return;
  }
  final List<FileSystemEntity> androidFiles = androidDir.listSync();
  final List<File> lockfiles = <File>[];
  final List<String> backedUpFilePaths = <String>[];
  for (final FileSystemEntity entity in androidFiles) {
    if (entity is! File) {
      continue;
    }
    final File file = entity.absolute;
    // Don't re-handle backed up lockfiles.
    if (file.path.contains('_backup_')) {
      continue;
    }
    try {
      // lockfiles generated by gradle start with this prefix.
      if (file.readAsStringSync().startsWith(
            '# This is a Gradle generated file for dependency locking.\n# '
            'Manual edits can break the build and are not advised.\n# This '
            'file is expected to be part of source control.')) {
        lockfiles.add(file);
      }
    } on FileSystemException {
      if (verbose) {
        logger.printStatus('Unable to check ${file.path}');
      }
    }
  }
  if (lockfiles.isNotEmpty) {
    logger.printStatus('\nGradle dependency locking detected.');
    logger.printStatus('Flutter can backup the lockfiles and regenerate updated '
                       'lockfiles.');
    terminal.usesTerminalUi = true;
    String selection = 'y';
    selection = await terminal.promptForCharInput(
      <String>['y', 'n'],
      logger: logger,
      prompt: 'Do you want the tool to update locked dependencies? (y)es, (n)o',
      defaultChoiceIndex: 1,
    );
    if (selection == 'y') {
      for (final File file in lockfiles) {
        int counter = 0;
        while (true) {
          final String newPath = '${file.absolute.path}_backup_$counter';
          if (!fileSystem.file(newPath).existsSync()) {
            file.renameSync(newPath);
            backedUpFilePaths.add(newPath);
            break;
          } else {
            counter++;
          }
        }
      }
      // Runs `./gradlew tasks`in the project's android directory.
      await migrateUtils.gradlewTasks(flutterProject.directory.childDirectory('android').path);
      logger.printStatus('Old lockfiles renamed to:');
      for (final String path in backedUpFilePaths) {
        logger.printStatus(path, color: TerminalColor.grey, indent: 2);
      }
    }
  }
}