clean.dart 3.84 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7
import '../base/file_system.dart';
8
import '../base/logger.dart';
9
import '../build_info.dart';
10
import '../globals.dart' as globals;
11
import '../ios/xcodeproj.dart';
12
import '../project.dart';
13 14 15
import '../runner/flutter_command.dart';

class CleanCommand extends FlutterCommand {
16 17 18
  CleanCommand({
    bool verbose = false,
  }) : _verbose = verbose {
19 20 21
    requiresPubspecYaml();
  }

22 23
  final bool _verbose;

24 25 26 27
  @override
  final String name = 'clean';

  @override
28
  final String description = 'Delete the build/ and .dart_tool/ directories.';
29

30 31 32
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};

33
  @override
34
  Future<FlutterCommandResult> runCommand() async {
35 36 37
    // Clean Xcode to remove intermediate DerivedData artifacts.
    // Do this before removing ephemeral directory, which would delete the xcworkspace.
    final FlutterProject flutterProject = FlutterProject.current();
38
    if (globals.xcode.isInstalledAndMeetsVersionCheck) {
39 40 41 42
      await _cleanXcode(flutterProject.ios);
      await _cleanXcode(flutterProject.macos);
    }

43
    final Directory buildDir = globals.fs.directory(getBuildDirectory());
44
    deleteFile(buildDir);
45

46
    deleteFile(flutterProject.dartTool);
47

48
    deleteFile(flutterProject.android.ephemeralDirectory);
49

50 51 52 53
    deleteFile(flutterProject.ios.ephemeralDirectory);
    deleteFile(flutterProject.ios.generatedXcodePropertiesFile);
    deleteFile(flutterProject.ios.generatedEnvironmentVariableExportScript);
    deleteFile(flutterProject.ios.compiledDartFramework);
54

55 56 57
    deleteFile(flutterProject.linux.ephemeralDirectory);
    deleteFile(flutterProject.macos.ephemeralDirectory);
    deleteFile(flutterProject.windows.ephemeralDirectory);
58 59
    deleteFile(flutterProject.flutterPluginsDependenciesFile);
    deleteFile(flutterProject.flutterPluginsFile);
60

61 62 63
    return const FlutterCommandResult(ExitStatus.success);
  }

64 65 66 67
  Future<void> _cleanXcode(XcodeBasedProject xcodeProject) async {
    if (!xcodeProject.existsSync()) {
      return;
    }
68 69 70
    final Status xcodeStatus = globals.logger.startProgress(
      'Cleaning Xcode workspace...',
    );
71 72
    try {
      final Directory xcodeWorkspace = xcodeProject.xcodeWorkspace;
73
      final XcodeProjectInfo projectInfo = await globals.xcodeProjectInterpreter.getInfo(xcodeWorkspace.parent.path);
74
      for (final String scheme in projectInfo.schemes) {
75
        await globals.xcodeProjectInterpreter.cleanWorkspace(xcodeWorkspace.path, scheme, verbose: _verbose);
76
      }
77
    } on Exception catch (error) {
78
      globals.printTrace('Could not clean Xcode workspace: $error');
79 80 81 82 83 84 85
    } finally {
      xcodeStatus?.stop();
    }
  }

  @visibleForTesting
  void deleteFile(FileSystemEntity file) {
86 87 88 89 90 91
    // This will throw a FileSystemException if the directory is missing permissions.
    try {
      if (!file.existsSync()) {
        return;
      }
    } on FileSystemException catch (err) {
92
      globals.printError('Cannot clean ${file.path}.\n$err');
93 94
      return;
    }
95 96 97
    final Status deletionStatus = globals.logger.startProgress(
      'Deleting ${file.basename}...',
    );
98 99 100 101
    try {
      file.deleteSync(recursive: true);
    } on FileSystemException catch (error) {
      final String path = file.path;
102 103
      if (globals.platform.isWindows) {
        globals.printError(
104
          'Failed to remove $path. '
105 106 107
            'A program may still be using a file in the directory or the directory itself. '
            'To find and stop such a program, see: '
            'https://superuser.com/questions/1333118/cant-delete-empty-folder-because-it-is-used');
108
      } else {
109
        globals.printError('Failed to remove $path: $error');
110
      }
111 112
    } finally {
      deletionStatus.stop();
113
    }
114
  }
115
}