From 783b0fc48aac2d4205749f2cd963ea3b268c9911 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Fri, 7 May 2021 17:44:25 +0800 Subject: [PATCH] update grind tool --- pubspec.lock | 4 +-- pubspec.yaml | 6 ++-- tool/_build.dart | 28 +++++++++++++++ tool/_project_manage.dart | 26 ++++++++++++++ tool/grind.dart | 73 +++++++++++---------------------------- tool/version_tool.dart | 59 +++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 57 deletions(-) create mode 100644 tool/_build.dart create mode 100644 tool/_project_manage.dart create mode 100644 tool/version_tool.dart diff --git a/pubspec.lock b/pubspec.lock index c874fc0e..006f4b97 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -838,7 +838,7 @@ packages: source: hosted version: "5.0.0" pub_semver: - dependency: transitive + dependency: "direct dev" description: name: pub_semver url: "https://pub.flutter-io.cn" @@ -1174,7 +1174,7 @@ packages: source: hosted version: "5.1.0" yaml: - dependency: transitive + dependency: "direct dev" description: name: yaml url: "https://pub.flutter-io.cn" diff --git a/pubspec.yaml b/pubspec.yaml index 27c0c8bf..1b1742fd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,10 +2,10 @@ name: aku_community description: A new Flutter project. publish_to: "none" -version: 1.1.21-dev+27 +version: 1.2.0-dev+37 environment: - sdk: '>=2.12.0 <3.0.0' + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: @@ -77,6 +77,8 @@ dev_dependencies: flutter_native_splash: ^1.1.8+4 json_serializable: ^4.1.1 build_runner: ^2.0.2 + yaml: ^3.1.0 + pub_semver: ^2.0.0 flutter: uses-material-design: true diff --git a/tool/_build.dart b/tool/_build.dart new file mode 100644 index 00000000..067b6168 --- /dev/null +++ b/tool/_build.dart @@ -0,0 +1,28 @@ +part of './grind.dart'; + +@Task('打包Android项目') +buildApk() async { + await runAsync( + 'flutter', + arguments: [ + 'build', + 'apk', + '--target-platform=android-arm64', + '--dart-define', + 'BUILD_TYPE=PRODUCT', + ], + ); +} + +@Task('打包iOS项目') +buildIos() async { + await runAsync( + 'flutter', + arguments: [ + 'build', + 'ios', + '--dart-define', + 'BUILD_TYPE=PRODUCT', + ], + ); +} diff --git a/tool/_project_manage.dart b/tool/_project_manage.dart new file mode 100644 index 00000000..5ec4f1c6 --- /dev/null +++ b/tool/_project_manage.dart @@ -0,0 +1,26 @@ +part of 'grind.dart'; + +@Task('import 排序') +void sort() { + Pub.run('import_sorter:main'); +} + +@Task('格式化dart代码') +void format() { + DartFmt.format(libDir); +} + +@Task('自动提交修改') +@Depends(sort, format) +void git() { + log(' commit to git'); + run( + 'git', + arguments: [ + 'commit', + '-a', + '-m', + '[auto task] sort & format', + ], + ); +} diff --git a/tool/grind.dart b/tool/grind.dart index e2261420..416dc5fa 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -1,61 +1,28 @@ +import 'dart:io'; + import 'package:grinder/grinder.dart'; +import 'package:path/path.dart'; +import 'package:pub_semver/pub_semver.dart'; +import 'package:yaml/yaml.dart'; -main(args) => grind(args); +import 'version_tool.dart'; -@Task() -test() => new TestRunner().testAsync(); +part '_build.dart'; +part '_project_manage.dart'; -@Task() -buildApk() async { - await runAsync( - 'flutter', - arguments: [ - 'build', - 'apk', - '--target-platform=android-arm64', - '--dart-define', - 'BUILD_TYPE=PRODUCT', - ], - ); -} - -@Task('build ios') -buildIos() async { - await runAsync( - 'flutter', - arguments: [ - 'build', - 'ios', - '--dart-define', - 'BUILD_TYPE=PRODUCT', - ], - ); -} - -@Task() -clean() => defaultClean(); +main(args) => grind(args); -@Task() -void sort() { - Pub.run('import_sorter:main'); -} +@Task('add version number') +void addVersion() async { + String projectPath = Directory('.').absolute.path; + String yamlPath = join(projectPath, 'pubspec.yaml'); + String yamlContent = await File(yamlPath).readAsString(); + dynamic content = loadYaml(yamlContent); + String version = content['version']; + //rename version -@Task() -void format() { - DartFmt.format(libDir); -} + Version resultVersion = VersionTool.fromText(version).nextMinorTag('dev'); -@Task('auto sort and format code') -@Depends(sort, format) -void git() { - log(' commit to git'); - run( - 'git', - arguments: [ - 'commit', - '-a', - '-m', - '[auto task] sort & format', - ], - ); + String result = yamlContent.replaceFirst(version, resultVersion.toString()); + await File(yamlPath).writeAsString(result); } diff --git a/tool/version_tool.dart b/tool/version_tool.dart new file mode 100644 index 00000000..0a3cd994 --- /dev/null +++ b/tool/version_tool.dart @@ -0,0 +1,59 @@ +import 'package:pub_semver/pub_semver.dart'; + +enum VersionNumber { + MAJOR, + MINOR, + PATCH, +} + +class VersionTool { + Version version; + VersionTool(this.version); + VersionTool.fromText(String text) : version = Version.parse(text); + + Version get nextMajor => _addBuildNumber(VersionNumber.MAJOR); + Version get nextMinor => _addBuildNumber(VersionNumber.MINOR); + Version get nextPatch => _addBuildNumber(VersionNumber.PATCH); + + Version nextMajorTag(String tag) => _addBuildNumber( + VersionNumber.MAJOR, + tag: tag, + ); + Version nextMinorTag(String tag) => _addBuildNumber( + VersionNumber.MINOR, + tag: tag, + ); + Version nextPatchTag(String tag) => _addBuildNumber( + VersionNumber.PATCH, + tag: tag, + ); + + Version _addBuildNumber(VersionNumber type, {String? tag}) { + switch (type) { + case VersionNumber.MAJOR: + return Version( + version.major, + version.minor, + version.patch + 1, + pre: tag, + build: '${(version.build.first as int) + 1}', + ); + case VersionNumber.MINOR: + return Version( + version.major, + version.minor + 1, + 0, + pre: tag, + build: '${(version.build.first as int) + 1}', + ); + case VersionNumber.PATCH: + return Version( + version.major + 1, + 0, + 0, + pre: tag, + build: '${(version.build.first as int) + 1}', + ); + } + } +}