lemmur-app-android/scripts/release.dart

137 lines
3.9 KiB
Dart
Raw Normal View History

// ignore_for_file: avoid_print
/// Used to create a new lemmur release. Bumps semver, build number, updates changelog, fastlane, pubspec, and finishes by adding a git tag
2021-02-04 21:34:40 +01:00
import 'dart:io';
import 'common.dart';
2021-02-04 21:34:40 +01:00
Future<void> main(List<String> args) async {
2021-02-05 00:04:14 +01:00
await assertNoStagedGit();
if (args.isEmpty || !{'patch', 'minor', 'major'}.contains(args[0])) {
2021-03-01 09:55:30 +01:00
printError('Unknown version bump type');
2021-02-05 00:04:14 +01:00
}
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 {
2021-02-04 22:07:41 +01:00
final res =
await Process.run('git', ['diff-index', '--cached', '--quiet', 'HEAD']);
2021-02-05 00:04:14 +01:00
2021-02-04 22:07:41 +01:00
if (res.exitCode != 0) {
2021-03-01 09:55:30 +01:00
printError('You have staged files, commit or unstage them.');
2021-02-04 22:07:41 +01:00
}
2021-02-05 00:04:14 +01:00
}
2021-02-04 22:07:41 +01:00
2021-02-05 00:04:14 +01:00
class Version {
2021-04-09 11:41:11 +02:00
final int major, minor, patch, code;
Version(this.major, this.minor, this.patch, this.code);
2021-02-05 00:04:14 +01:00
String toString() => '$major.$minor.$patch+$code';
String toStringNoCode() => '$major.$minor.$patch';
}
2021-02-04 21:34:40 +01:00
2021-02-05 00:04:14 +01:00
Future<Version> bumpedVersion(String versionBumpType) async {
2021-02-04 21:34:40 +01:00
final pubspecFile = File('pubspec.yaml');
final pubspecContents = await pubspecFile.readAsString();
final versionMatch = RegExp(r'version: (\d+)\.(\d+)\.(\d+)\+(\d+)')
.firstMatch(pubspecContents);
2021-04-09 11:41:11 +02:00
if (versionMatch == null) {
printError('Failed to find version in pubspec.yaml');
}
var major = int.parse(versionMatch.group(1)!);
var minor = int.parse(versionMatch.group(2)!);
var patch = int.parse(versionMatch.group(3)!);
var code = int.parse(versionMatch.group(4)!);
2021-02-04 21:34:40 +01:00
2021-02-05 00:04:14 +01:00
switch (versionBumpType) {
2021-02-04 21:34:40 +01:00
case 'patch':
patch++;
break;
case 'minor':
patch = 0;
minor++;
break;
case 'major':
patch = 0;
minor = 0;
major++;
break;
}
code++;
2021-04-09 11:41:11 +02:00
return Version(major, minor, patch, code);
2021-02-05 00:04:14 +01:00
}
Future<void> updatePubspec(Version version) async {
final pubspecFile = File('pubspec.yaml');
final pubspecContents = await pubspecFile.readAsString();
2021-02-04 21:34:40 +01:00
2021-02-05 00:04:14 +01:00
confirm('Version looks good? $version');
final updatedPubspec =
pubspecContents.replaceFirst(RegExp('version: .+'), 'version: $version');
2021-02-04 21:34:40 +01:00
await pubspecFile.writeAsString(updatedPubspec);
2021-02-05 00:04:14 +01:00
}
2021-02-04 22:07:41 +01:00
2021-02-05 00:04:14 +01:00
Future<void> updateChangelog(Version version) async {
2021-02-04 22:07:41 +01:00
final changelogFile = File('CHANGELOG.md');
final changelogContents = await changelogFile.readAsString();
2021-02-05 00:04:14 +01:00
var currentChangelog =
2021-02-04 22:07:41 +01:00
RegExp(r'^## Unreleased$.+?^##[^#]', multiLine: true, dotAll: true)
.stringMatch(changelogContents);
2021-04-09 11:41:11 +02:00
if (currentChangelog == null) {
printError('No changelog found');
}
2021-02-05 00:04:14 +01:00
currentChangelog = currentChangelog.substring(0, currentChangelog.length - 4);
2021-02-04 22:07:41 +01:00
final date = DateTime.now();
final dateString =
'${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
2021-02-05 00:04:14 +01:00
currentChangelog = currentChangelog.replaceFirst(
'Unreleased', 'v${version.toStringNoCode()} - $dateString');
2021-02-04 22:07:41 +01:00
2021-02-05 00:04:14 +01:00
confirm('Changelog looks good?\n$currentChangelog\n');
2021-02-04 22:07:41 +01:00
2021-02-05 00:04:14 +01:00
await changelogFile.writeAsString(changelogContents.replaceFirst(
'Unreleased', 'v${version.toStringNoCode()} - $dateString'));
2021-02-04 22:07:41 +01:00
2021-02-05 00:04:14 +01:00
await File('fastlane/metadata/android/en-US/changelogs/${version.code}.txt')
.writeAsString(currentChangelog.split('\n').skip(2).join('\n'));
}
Future<void> runGitCommands(Version version) async {
2021-02-04 22:07:41 +01:00
stdout.write('Running git add... ');
await Process.run('git', [
'add',
'CHANGELOG.md',
'pubspec.yaml',
2021-02-05 00:04:14 +01:00
'fastlane/metadata/android/en-US/changelogs/${version.code}.txt'
2021-02-04 22:07:41 +01:00
]);
print('done');
2021-02-05 00:04:14 +01:00
stdout.write('Running git commit... ');
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');
2021-02-04 21:34:40 +01:00
}