clean.dart 3.92 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
    deleteFile(flutterProject.ios.ephemeralDirectory);
    deleteFile(flutterProject.ios.generatedXcodePropertiesFile);
    deleteFile(flutterProject.ios.generatedEnvironmentVariableExportScript);
53
    deleteFile(flutterProject.ios.deprecatedCompiledDartFramework);
54
    deleteFile(flutterProject.ios.deprecatedProjectFlutterFramework);
55

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

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

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

  @visibleForTesting
  void deleteFile(FileSystemEntity file) {
87 88 89 90 91 92
    // This will throw a FileSystemException if the directory is missing permissions.
    try {
      if (!file.existsSync()) {
        return;
      }
    } on FileSystemException catch (err) {
93
      globals.printError('Cannot clean ${file.path}.\n$err');
94 95
      return;
    }
96 97 98
    final Status deletionStatus = globals.logger.startProgress(
      'Deleting ${file.basename}...',
    );
99 100 101 102
    try {
      file.deleteSync(recursive: true);
    } on FileSystemException catch (error) {
      final String path = file.path;
103 104
      if (globals.platform.isWindows) {
        globals.printError(
105
          'Failed to remove $path. '
106 107 108
            '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');
109
      } else {
110
        globals.printError('Failed to remove $path: $error');
111
      }
112 113
    } finally {
      deletionStatus.stop();
114
    }
115
  }
116
}