Skip to content

Commit f30365f

Browse files
committed
Simplify and fix clean command.
1 parent ca4c5eb commit f30365f

File tree

2 files changed

+9
-65
lines changed

2 files changed

+9
-65
lines changed

build_runner/bin/src/commands/clean.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import 'dart:async';
66

77
import 'package:args/args.dart';
88
import 'package:args/command_runner.dart';
9-
import 'package:build_runner/src/build_script_generate/build_script_generate.dart';
109
import 'package:build_runner/src/entrypoint/base_command.dart' show lineLength;
11-
import 'package:build_runner/src/entrypoint/clean.dart' show cleanFor;
12-
import 'package:build_runner_core/build_runner_core.dart';
10+
import 'package:build_runner/src/entrypoint/clean.dart' show clean;
1311
import 'package:logging/logging.dart';
1412

1513
class CleanCommand extends Command<int> {
@@ -22,12 +20,11 @@ class CleanCommand extends Command<int> {
2220

2321
@override
2422
String get description =>
25-
'Cleans up output from previous builds. Does not clean up --output '
26-
'directories.';
23+
'Deletes the build cache. The next build will be a full build.';
2724

2825
@override
2926
Future<int> run() async {
30-
await cleanFor(assetGraphPathFor(scriptLocation));
27+
clean();
3128
return 0;
3229
}
3330
}

build_runner/lib/src/entrypoint/clean.dart

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'dart:io';
88
import 'package:args/args.dart';
99
import 'package:args/command_runner.dart';
1010
import 'package:build_runner_core/build_runner_core.dart';
11-
// ignore: implementation_imports
12-
import 'package:build_runner_core/src/asset_graph/graph.dart';
1311

1412
import 'base_command.dart';
1513

@@ -22,70 +20,19 @@ class CleanCommand extends Command<int> {
2220

2321
@override
2422
String get description =>
25-
'Cleans up output from previous builds. Does not clean up --output '
26-
'directories.';
23+
'Deletes the build cache. The next build will be a full build.';
2724

2825
@override
2926
Future<int> run() async {
30-
await cleanFor(assetGraphPath);
27+
clean();
3128
return 0;
3229
}
3330
}
3431

35-
Future<void> cleanFor(String assetGraphPath) async {
36-
buildLog.warning(
37-
'Deleting cache and generated source files.\n'
38-
'This shouldn\'t be necessary for most applications, unless you have '
39-
'made intentional edits to generated files (i.e. for testing). '
40-
'Consider filing a bug at '
41-
'https://github.com/dart-lang/build/issues/new if you are using this '
42-
'to work around an apparent (and reproducible) bug.',
43-
);
44-
45-
var assetGraphFile = File(assetGraphPath);
46-
if (!assetGraphFile.existsSync()) {
47-
buildLog.warning(
48-
'No asset graph found. '
49-
'Skipping cleanup of generated files in source directories.',
50-
);
51-
return;
52-
}
53-
AssetGraph assetGraph;
54-
try {
55-
assetGraph = AssetGraph.deserialize(await assetGraphFile.readAsBytes());
56-
} catch (_) {
57-
buildLog.warning(
58-
'Failed to deserialize AssetGraph. '
59-
'Skipping cleanup of generated files in source directories.',
60-
);
61-
return;
62-
}
63-
var packageGraph = await PackageGraph.forThisPackage();
64-
await _cleanUpSourceOutputs(assetGraph, packageGraph);
65-
await _cleanUpGeneratedDirectory();
66-
}
67-
68-
Future<void> _cleanUpSourceOutputs(
69-
AssetGraph assetGraph,
70-
PackageGraph packageGraph,
71-
) async {
72-
var writer = ReaderWriter(packageGraph);
73-
for (var id in assetGraph.outputs) {
74-
if (id.package != packageGraph.root.name) continue;
75-
var node = assetGraph.get(id)!;
76-
if (node.wasOutput) {
77-
// Note that this does a file.exists check in the root package and
78-
// only tries to delete the file if it exists. This way we only
79-
// actually delete to_source outputs, without reading in the build
80-
// actions.
81-
await writer.delete(id);
82-
}
83-
}
84-
}
85-
86-
Future<void> _cleanUpGeneratedDirectory() async {
32+
void clean() {
33+
buildLog.doing('Deleting the build cache.');
8734
var generatedDir = Directory(cacheDir);
88-
if (await generatedDir.exists()) {
89-
await generatedDir.delete(recursive: true);
35+
if (generatedDir.existsSync()) {
36+
generatedDir.deleteSync(recursive: true);
9037
}
9138
}

0 commit comments

Comments
 (0)