clean.dart 2.2 KB
Newer Older
1 2 3 4
// 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.

5 6
// @dart = 2.8

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
import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';

import './globals.dart';
import './repository.dart';
import './state.dart';
import './stdio.dart';

const String kYesFlag = 'yes';
const String kStateOption = 'state-file';

/// Command to clean up persistent state file.
///
/// If the release was not completed, this command will abort the release.
class CleanCommand extends Command<void> {
  CleanCommand({
    @required this.checkouts,
  })  : platform = checkouts.platform,
        fileSystem = checkouts.fileSystem,
        stdio = checkouts.stdio {
    final String defaultPath = defaultStateFilePath(platform);
    argParser.addFlag(
      kYesFlag,
      help: 'Override confirmation checks.',
    );
    argParser.addOption(
      kStateOption,
      defaultsTo: defaultPath,
      help: 'Path to persistent state file. Defaults to $defaultPath',
    );
  }

  final Checkouts checkouts;
  final FileSystem fileSystem;
  final Platform platform;
  final Stdio stdio;

  @override
  String get name => 'clean';

  @override
  String get description => 'Cleanup persistent state file. '
      'This will abort a work in progress release.';

  @override
  void run() {
    final File stateFile = checkouts.fileSystem.file(argResults[kStateOption]);
    if (!stateFile.existsSync()) {
      throw ConductorException(
          'No persistent state file found at ${stateFile.path}!');
    }

    if (!(argResults[kYesFlag] as bool)) {
      stdio.printStatus(
        'Are you sure you want to clean up the persistent state file at\n'
        '${stateFile.path} (y/n)?',
      );
      final String response = stdio.readLineSync();

      // Only proceed if the first character of stdin is 'y' or 'Y'
      if (response.isEmpty || response[0].toLowerCase() != 'y') {
        stdio.printStatus('Aborting clean operation.');
        return;
      }
    }
    stdio.printStatus('Deleting persistent state file ${stateFile.path}...');
    stateFile.deleteSync();
  }
}