add version tool

add version to 1.6.0
hmxc
张萌 3 years ago
parent 5b8c9ae418
commit cac11a76a3

@ -829,7 +829,7 @@ packages:
source: hosted source: hosted
version: "5.0.0" version: "5.0.0"
pub_semver: pub_semver:
dependency: transitive dependency: "direct dev"
description: description:
name: pub_semver name: pub_semver
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"

@ -3,7 +3,7 @@ description: A new Flutter application.
publish_to: "none" publish_to: "none"
version: 1.5.1+11 version: 1.6.0-dev+12
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
@ -86,6 +86,7 @@ dev_dependencies:
json_serializable: ^4.1.1 json_serializable: ^4.1.1
build_runner: ^2.0.3 build_runner: ^2.0.3
yaml: ^3.1.0 yaml: ^3.1.0
pub_semver: ^2.0.0
flutter: flutter:
uses-material-design: true uses-material-design: true

@ -0,0 +1,41 @@
part of 'grind.dart';
@Task('add minor 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
Version resultVersion = VersionTool.fromText(version).nextMinorTag('dev');
String result = yamlContent.replaceFirst(version, resultVersion.toString());
await File(yamlPath).writeAsString(result);
}
@Task('add path version number')
void addVersionPatch() 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
Version resultVersion = VersionTool.fromText(version).nextPatchTag('dev');
String result = yamlContent.replaceFirst(version, resultVersion.toString());
await File(yamlPath).writeAsString(result);
}
@Task()
Future<String> getVersion() 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'];
return version;
}

@ -2,10 +2,13 @@ import 'dart:io';
import 'package:common_utils/common_utils.dart'; import 'package:common_utils/common_utils.dart';
import 'package:grinder/grinder.dart'; import 'package:grinder/grinder.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'config.dart'; import 'config.dart';
import 'version_tool.dart';
part '_add_version.dart';
main(args) => grind(args); main(args) => grind(args);
@ -88,13 +91,3 @@ void genClean() async {
await Pub.runAsync('build_runner', await Pub.runAsync('build_runner',
arguments: ['build', '--delete-conflicting-outputs']); arguments: ['build', '--delete-conflicting-outputs']);
} }
@Task()
Future<String> getVersion() 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'];
return version;
}

@ -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}',
);
}
}
}
Loading…
Cancel
Save