Skip to content

Commit 8b3b1ef

Browse files
[tool] Add a flag to skip cleanup (flutter#4357)
It can be useful in debugging snippet setup to look at the extraction output, but the tool cleans that up automatically. Running the extraction manually is complicated due to the on-the-fly pubspec modifications, so this adds a `--no-cleanup` flag that can be used to skip the deletion of the extraction output, and instead log its location to the terminal.
1 parent 41d5ca9 commit 8b3b1ef

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

script/tool/lib/src/update_excerpts_command.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io' as io;
66

77
import 'package:file/file.dart';
88
import 'package:git/git.dart';
9+
import 'package:meta/meta.dart';
910
import 'package:platform/platform.dart';
1011
import 'package:yaml/yaml.dart';
1112
import 'package:yaml_edit/yaml_edit.dart';
@@ -30,18 +31,24 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
3031
gitDir: gitDir,
3132
) {
3233
argParser.addFlag(_failOnChangeFlag, hide: true);
34+
argParser.addFlag(_noCleanupFlag,
35+
help: 'Skips the step of cleaning up the excerpt extraction output. '
36+
'This can be useful when debugging extraction or checking paths to '
37+
'reference in snippets.');
3338
}
3439

3540
static const String _failOnChangeFlag = 'fail-on-change';
41+
static const String _noCleanupFlag = 'no-cleanup';
3642

3743
static const String _buildRunnerConfigName = 'excerpt';
3844
// The name of the build_runner configuration file that will be in an example
3945
// directory if the package is set up to use `code-excerpt`.
4046
static const String _buildRunnerConfigFile =
4147
'build.$_buildRunnerConfigName.yaml';
4248

43-
// The relative directory path to put the extracted excerpt yaml files.
44-
static const String _excerptOutputDir = 'excerpts';
49+
/// The relative directory path to put the extracted excerpt yaml files.
50+
@visibleForTesting
51+
static const String excerptOutputDir = 'excerpts';
4552

4653
// The filename to store the pre-modification copy of the pubspec.
4754
static const String _originalPubspecFilename =
@@ -97,9 +104,16 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
97104
// Clean up the pubspec changes and extracted excerpts directory.
98105
_undoPubspecChanges(example);
99106
final Directory excerptDirectory =
100-
example.directory.childDirectory(_excerptOutputDir);
107+
example.directory.childDirectory(excerptOutputDir);
101108
if (excerptDirectory.existsSync()) {
102-
excerptDirectory.deleteSync(recursive: true);
109+
if (getBoolArg(_noCleanupFlag)) {
110+
final String relativeDir =
111+
getRelativePosixPath(excerptDirectory, from: package.directory);
112+
print(
113+
'\n\nSKIPPING CLEANUP: Extraction output is in $relativeDir/');
114+
} else {
115+
excerptDirectory.deleteSync(recursive: true);
116+
}
103117
}
104118
}
105119
}
@@ -134,7 +148,7 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
134148
'--config',
135149
_buildRunnerConfigName,
136150
'--output',
137-
_excerptOutputDir,
151+
excerptOutputDir,
138152
'--delete-conflicting-outputs',
139153
],
140154
workingDir: example.directory);

script/tool/test/update_excerpts_command_test.dart

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void main() {
5858
'--config',
5959
'excerpt',
6060
'--output',
61-
'excerpts',
61+
UpdateExcerptsCommand.excerptOutputDir,
6262
'--delete-conflicting-outputs',
6363
],
6464
example.path),
@@ -85,7 +85,7 @@ void main() {
8585
'--config',
8686
'excerpt',
8787
'--output',
88-
'excerpts',
88+
UpdateExcerptsCommand.excerptOutputDir,
8989
'--delete-conflicting-outputs',
9090
],
9191
example.path),
@@ -129,7 +129,7 @@ void main() {
129129
'--config',
130130
'excerpt',
131131
'--output',
132-
'excerpts',
132+
UpdateExcerptsCommand.excerptOutputDir,
133133
'--delete-conflicting-outputs',
134134
],
135135
example.path),
@@ -174,7 +174,7 @@ void main() {
174174
'--config',
175175
'excerpt',
176176
'--output',
177-
'excerpts',
177+
UpdateExcerptsCommand.excerptOutputDir,
178178
'--delete-conflicting-outputs',
179179
],
180180
example.path),
@@ -416,4 +416,52 @@ void main() {
416416
contains('Unable to determine local file state'),
417417
]));
418418
});
419+
420+
test('cleans up excerpt output by default', () async {
421+
final RepositoryPackage package = createFakePackage(
422+
'a_package', packagesDir,
423+
extraFiles: <String>[kReadmeExcerptConfigPath]);
424+
// Simulate the creation of the output directory.
425+
final Directory excerptOutputDir = package
426+
.getExamples()
427+
.first
428+
.directory
429+
.childDirectory(UpdateExcerptsCommand.excerptOutputDir);
430+
excerptOutputDir.createSync(recursive: true);
431+
432+
const String changedFilePath = 'packages/a_plugin/linux/CMakeLists.txt';
433+
processRunner.mockProcessesForExecutable['git'] = <FakeProcessInfo>[
434+
FakeProcessInfo(MockProcess(stdout: changedFilePath)),
435+
];
436+
437+
await runCapturingPrint(runner, <String>['update-excerpts']);
438+
439+
expect(excerptOutputDir.existsSync(), false);
440+
});
441+
442+
test('cleans up excerpt output by default', () async {
443+
final RepositoryPackage package = createFakePackage(
444+
'a_package', packagesDir,
445+
extraFiles: <String>[kReadmeExcerptConfigPath]);
446+
// Simulate the creation of the output directory.
447+
const String outputDirName = UpdateExcerptsCommand.excerptOutputDir;
448+
final Directory excerptOutputDir =
449+
package.getExamples().first.directory.childDirectory(outputDirName);
450+
excerptOutputDir.createSync(recursive: true);
451+
452+
const String changedFilePath = 'packages/a_plugin/linux/CMakeLists.txt';
453+
processRunner.mockProcessesForExecutable['git'] = <FakeProcessInfo>[
454+
FakeProcessInfo(MockProcess(stdout: changedFilePath)),
455+
];
456+
457+
final List<String> output = await runCapturingPrint(
458+
runner, <String>['update-excerpts', '--no-cleanup']);
459+
460+
expect(
461+
output,
462+
containsAllInOrder(<Matcher>[
463+
contains('Extraction output is in example/$outputDirName/'),
464+
]));
465+
expect(excerptOutputDir.existsSync(), true);
466+
});
419467
}

0 commit comments

Comments
 (0)