clean.dart 2.57 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
import 'package:args/args.dart';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import 'package:args/command_runner.dart';
import 'package:file/file.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({
23
    required this.checkouts,
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
  })  : 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
Alex's avatar
Alex committed
52
  Future<void> run() {
53 54
    final ArgResults argumentResults = argResults!;
    final File stateFile = checkouts.fileSystem.file(argumentResults[kStateOption]);
55
    if (!stateFile.existsSync()) {
Alex's avatar
Alex committed
56
      throw ConductorException('No persistent state file found at ${stateFile.path}!');
57 58
    }

59
    if (!(argumentResults[kYesFlag] as bool)) {
60 61 62 63 64 65 66 67 68 69 70 71
      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.');
      }
    }
    stdio.printStatus('Deleting persistent state file ${stateFile.path}...');
Alex's avatar
Alex committed
72

73
    final CleanContext cleanContext = CleanContext(
Alex's avatar
Alex committed
74 75
      stateFile: stateFile,
    );
76
    return cleanContext.run();
Alex's avatar
Alex committed
77 78 79 80 81 82
  }
}

/// Context for cleaning up persistent state file.
///
/// This is a frontend-agnostic implementation.
83 84
class CleanContext {
  CleanContext({
Alex's avatar
Alex committed
85 86 87 88 89 90 91
    required this.stateFile,
  });

  final File stateFile;

  Future<void> run() {
    return stateFile.delete();
92 93
  }
}