Skip to content

Commit

Permalink
1、优化显示清理文件夹全路径
Browse files Browse the repository at this point in the history
2、优化处理 package/example 的情况
3、新增 gradle 清理
  • Loading branch information
BytesZero committed Dec 21, 2021
1 parent 2c23ef9 commit f81e607
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
7 changes: 5 additions & 2 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 13,9 @@ void main(List<String> arguments) {
'mode',
abbr: 'm',
defaultsTo: 'build',
allowed: ['build', 'pods', 'all'],
allowed: ['build', 'pods', 'gradle', 'all'],
help:
'🚀 Set the cleanup mode build is the build folder, pods is the ios/Pods folder, all folders',
'🚀 Set the cleanup mode build is the build folder, pods is the ios/Pods folder, gradle is the android/.gradle folder, all folders',
);

// 添加 version
Expand Down Expand Up @@ -63,6 63,9 @@ void main(List<String> arguments) {
case 'pods':
flutterCleanPods();
break;
case 'gradle':
flutterCleanGradle();
break;
default:
flutterCleanBuild();
}
Expand Down
83 changes: 73 additions & 10 deletions lib/cf_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 3,29 @@
import 'dart:io';

/// cli 版本号
const String kCliVersion = '1.1.0';
const String kCliVersion = '1.2.0';

// pubspec.yaml
const String kPubspecYaml = 'pubspec.yaml';
// example
const String kExampleDir = 'example';

// build
const String kBuildDir = 'build';

// ios/Pods
const String kIosPodsDir = 'ios/Pods';

// android/.gradle
const String kAndroidDir = 'android/.gradle';

/// 清理全部
Future<void> flutterCleanAll() async {
await forEachFlutterDir(
Directory.current.path,
clearBuild: true,
clearPods: true,
clearGradle: true,
);
print('🎉 All cleaned up');
}
Expand All @@ -31,9 46,22 @@ Future<void> flutterCleanPods() async {
print('🎉 All cleaned up');
}

/// 清理 gradle
Future<void> flutterCleanGradle() async {
await forEachFlutterDir(
Directory.current.path,
clearBuild: false,
clearPods: false,
clearGradle: true,
);
print('🎉 All cleaned up');
}

/// 遍历 Flutter dir
Future<void> forEachFlutterDir(String path,
{bool clearBuild = true, bool clearPods = false}) async {
{bool clearBuild = true,
bool clearPods = false,
bool clearGradle = false}) async {
// 不是文件夹退出
if (!await FileSystemEntity.isDirectory(path)) {
return;
Expand All @@ -48,19 76,44 @@ Future<void> forEachFlutterDir(String path,
var fileNameList =
fileList.map((e) => e.path.split(Platform.pathSeparator).last).toList();
// 检查 Flutter 目录
if (fileNameList.contains('pubspec.yaml')) {
if (fileNameList.contains(kPubspecYaml)) {
// 处理 package 有 example 的情况
if (fileNameList.contains(kExampleDir)) {
String exampleDirPath = '$path${Platform.pathSeparator}$kExampleDir';
if (Directory(exampleDirPath).existsSync()) {
await forEachFlutterDir(
exampleDirPath,
clearBuild: clearBuild,
clearPods: clearPods,
clearGradle: clearGradle,
);
return;
}
}
// 清理 build
if (clearBuild && fileNameList.contains('build')) {
if (clearBuild && fileNameList.contains(kBuildDir)) {
await runClean(path);
}
// 清理 Pods
if (clearPods && Directory('$path/ios/Pods').existsSync()) {
if (clearPods &&
Directory('$path${Platform.pathSeparator}$kIosPodsDir').existsSync()) {
await runPodsClean(path);
}

// 清理 gradle
if (clearGradle &&
Directory('$path${Platform.pathSeparator}$kAndroidDir').existsSync()) {
await runGradleClean(path);
}
} else {
// print('没有 Flutter 目录,继续遍历:$path');
// 没有 Flutter 目录,继续遍历:$path
for (var file in fileList) {
forEachFlutterDir(file.path);
forEachFlutterDir(
file.path,
clearBuild: clearBuild,
clearPods: clearPods,
clearGradle: clearGradle,
);
}
}
}
Expand All @@ -75,11 128,21 @@ Future<void> runClean(String dirPath) async {
await Future.delayed(const Duration(milliseconds: 50));
}

// 执行 Pods 清理
// 执行 ios/Pods 清理
Future<void> runPodsClean(String dirPath) async {
print('🧹 Cleaning up Pods:$dirPath');
print('🧹 Cleaning up Pods:$dirPath${Platform.pathSeparator}$kIosPodsDir');
var result =
await Process.run('rm', ['-rf', kIosPodsDir], workingDirectory: dirPath);
print(result.stdout);
// 延迟 50 毫秒
await Future.delayed(const Duration(milliseconds: 50));
}

// 执行 android/.gradle 清理
Future<void> runGradleClean(String dirPath) async {
print('🧹 Cleaning up gradle:$dirPath${Platform.pathSeparator}$kAndroidDir');
var result =
await Process.run('rm', ['-rf', 'ios/Pods'], workingDirectory: dirPath);
await Process.run('rm', ['-rf', kAndroidDir], workingDirectory: dirPath);
print(result.stdout);
// 延迟 50 毫秒
await Future.delayed(const Duration(milliseconds: 50));
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
name: cf_cli
description: Clean up the [build, ios/Pods] directory of Flutter projects in batches, optimize computer space
version: 1.1.0 #在 lib/cf.dart kCliVersion 设置版本
version: 1.2.0 #在 lib/cf.dart kCliVersion 设置版本
homepage: https://github.com/yy1300326388

environment:
Expand Down

0 comments on commit f81e607

Please sign in to comment.