Refactor
This commit is contained in:
parent
af9b82f1d8
commit
d4ed6ba465
|
@ -1,18 +1,42 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
Future<void> main(List<String> args) async {
|
Future<void> main(List<String> args) async {
|
||||||
final res =
|
await assertNoStagedGit();
|
||||||
await Process.run('git', ['diff-index', '--cached', '--quiet', 'HEAD']);
|
|
||||||
if (res.exitCode != 0) {
|
|
||||||
print('You have staged files, commit or unstage them.');
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.isEmpty || !{'patch', 'minor', 'major'}.contains(args[0])) {
|
if (args.isEmpty || !{'patch', 'minor', 'major'}.contains(args[0])) {
|
||||||
print('Unknown version bump type');
|
print('Unknown version bump type');
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final version = await bumpedVersion(args[0]);
|
||||||
|
|
||||||
|
await updatePubspec(version);
|
||||||
|
|
||||||
|
await updateChangelog(version);
|
||||||
|
|
||||||
|
await runGitCommands(version);
|
||||||
|
|
||||||
|
print(
|
||||||
|
'Before pushing review the changes with `git show`. Once reviewed and pushed, push the new tag with `git push --tags`');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> assertNoStagedGit() async {
|
||||||
|
final res =
|
||||||
|
await Process.run('git', ['diff-index', '--cached', '--quiet', 'HEAD']);
|
||||||
|
|
||||||
|
if (res.exitCode != 0) {
|
||||||
|
print('You have staged files, commit or unstage them.');
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Version {
|
||||||
|
int major, minor, patch, code;
|
||||||
|
String toString() => '$major.$minor.$patch+$code';
|
||||||
|
String toStringNoCode() => '$major.$minor.$patch';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Version> bumpedVersion(String versionBumpType) async {
|
||||||
final pubspecFile = File('pubspec.yaml');
|
final pubspecFile = File('pubspec.yaml');
|
||||||
final pubspecContents = await pubspecFile.readAsString();
|
final pubspecContents = await pubspecFile.readAsString();
|
||||||
|
|
||||||
|
@ -24,7 +48,7 @@ Future<void> main(List<String> args) async {
|
||||||
var patch = int.parse(versionMatch.group(3));
|
var patch = int.parse(versionMatch.group(3));
|
||||||
var code = int.parse(versionMatch.group(4));
|
var code = int.parse(versionMatch.group(4));
|
||||||
|
|
||||||
switch (args[0]) {
|
switch (versionBumpType) {
|
||||||
case 'patch':
|
case 'patch':
|
||||||
patch++;
|
patch++;
|
||||||
break;
|
break;
|
||||||
|
@ -40,50 +64,67 @@ Future<void> main(List<String> args) async {
|
||||||
}
|
}
|
||||||
code++;
|
code++;
|
||||||
|
|
||||||
final newVersionName = '$major.$minor.$patch';
|
return Version()
|
||||||
final newVersionString = '$newVersionName+$code';
|
..major = major
|
||||||
|
..minor = minor
|
||||||
|
..patch = patch
|
||||||
|
..code = code;
|
||||||
|
}
|
||||||
|
|
||||||
confirm('Version looks good? $newVersionString');
|
Future<void> updatePubspec(Version version) async {
|
||||||
final updatedPubspec = pubspecContents.replaceFirst(
|
final pubspecFile = File('pubspec.yaml');
|
||||||
versionMatch.group(0), 'version: $newVersionString');
|
final pubspecContents = await pubspecFile.readAsString();
|
||||||
|
|
||||||
|
confirm('Version looks good? $version');
|
||||||
|
|
||||||
|
final updatedPubspec =
|
||||||
|
pubspecContents.replaceFirst(RegExp('version: .+'), 'version: $version');
|
||||||
await pubspecFile.writeAsString(updatedPubspec);
|
await pubspecFile.writeAsString(updatedPubspec);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateChangelog(Version version) async {
|
||||||
final changelogFile = File('CHANGELOG.md');
|
final changelogFile = File('CHANGELOG.md');
|
||||||
final changelogContents = await changelogFile.readAsString();
|
final changelogContents = await changelogFile.readAsString();
|
||||||
|
|
||||||
var thisChangelog =
|
var currentChangelog =
|
||||||
RegExp(r'^## Unreleased$.+?^##[^#]', multiLine: true, dotAll: true)
|
RegExp(r'^## Unreleased$.+?^##[^#]', multiLine: true, dotAll: true)
|
||||||
.stringMatch(changelogContents);
|
.stringMatch(changelogContents);
|
||||||
thisChangelog = thisChangelog.substring(0, thisChangelog.length - 4);
|
currentChangelog = currentChangelog.substring(0, currentChangelog.length - 4);
|
||||||
|
|
||||||
final date = DateTime.now();
|
final date = DateTime.now();
|
||||||
final dateString =
|
final dateString =
|
||||||
'${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
'${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||||
thisChangelog = thisChangelog.replaceFirst(
|
|
||||||
'Unreleased', 'v$newVersionName - $dateString');
|
currentChangelog = currentChangelog.replaceFirst(
|
||||||
confirm('Changelog looks good?\n$thisChangelog\n');
|
'Unreleased', 'v${version.toStringNoCode()} - $dateString');
|
||||||
|
|
||||||
|
confirm('Changelog looks good?\n$currentChangelog\n');
|
||||||
|
|
||||||
await changelogFile.writeAsString(changelogContents.replaceFirst(
|
await changelogFile.writeAsString(changelogContents.replaceFirst(
|
||||||
'Unreleased', 'v$newVersionName - $dateString'));
|
'Unreleased', 'v${version.toStringNoCode()} - $dateString'));
|
||||||
|
|
||||||
await File('fastlane/metadata/android/en-US/changelogs/$code.txt')
|
await File('fastlane/metadata/android/en-US/changelogs/${version.code}.txt')
|
||||||
.writeAsString(thisChangelog.split('\n').skip(2).join('\n'));
|
.writeAsString(currentChangelog.split('\n').skip(2).join('\n'));
|
||||||
|
}
|
||||||
stdout.write('Running git tag... ');
|
|
||||||
await Process.run('git', ['tag', 'v$newVersionName']);
|
|
||||||
print('done');
|
|
||||||
|
|
||||||
|
Future<void> runGitCommands(Version version) async {
|
||||||
stdout.write('Running git add... ');
|
stdout.write('Running git add... ');
|
||||||
await Process.run('git', [
|
await Process.run('git', [
|
||||||
'add',
|
'add',
|
||||||
'CHANGELOG.md',
|
'CHANGELOG.md',
|
||||||
'pubspec.yaml',
|
'pubspec.yaml',
|
||||||
'fastlane/metadata/android/en-US/changelogs/$code.txt'
|
'fastlane/metadata/android/en-US/changelogs/${version.code}.txt'
|
||||||
]);
|
]);
|
||||||
print('done');
|
print('done');
|
||||||
|
|
||||||
print(
|
stdout.write('Running git commit... ');
|
||||||
'Review your staged files, commit them with a message of "Release v$newVersionName" push it and finish off with `git push --tags`');
|
await Process.run(
|
||||||
|
'git', ['commit', '-m', 'Release v${version.toStringNoCode()}']);
|
||||||
|
print('done');
|
||||||
|
|
||||||
|
stdout.write('Running git tag... ');
|
||||||
|
await Process.run('git', ['tag', 'v${version.toStringNoCode()}']);
|
||||||
|
print('done');
|
||||||
}
|
}
|
||||||
|
|
||||||
void confirm(String message) {
|
void confirm(String message) {
|
||||||
|
|
Loading…
Reference in New Issue