Skip to content

Commit

Permalink
Add release script
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianMong committed Jan 30, 2024
1 parent 447c6d5 commit 32341a2
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
pubspec.lock
.vscode/
/build/
config/
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 1.0.4

# [1.0.5] (UnReleased)

- Fixed issue related to auto scroll to initial duration for day
Expand Down
9 changes: 7 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: calendar_view
description: A Flutter package allows you to easily implement all calendar UI and calendar event functionality.
version: 1.0.4
homepage: https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view
issue_tracker: https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues?q=is%3Aissue+is%3Aopen+label%3Abug
homepage: https://github.com/AfriDoctor/flutter_calendar_view
publish_to: 'https://dart.cloudsmith.io/afridoctor/flutter_calendar_view'

environment:
sdk: ">=2.12.0 <4.0.0"
Expand All @@ -14,7 +14,12 @@ dependencies:
timezone: ^0.9.2

dev_dependencies:
dcli: ^3.3.2
flutter_test:
sdk: flutter
http: ^1.2.0
process_run: ^0.14.1+3
yaml: ^3.1.2
yaml_writer: ^2.0.0

flutter:
137 changes: 137 additions & 0 deletions scripts/release.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#! /usr/bin/env dcli

import 'dart:convert';
import 'dart:io';
import 'package:dcli/dcli.dart';
import 'package:http/http.dart' as http;
import 'package:yaml/yaml.dart';
import 'package:yaml_writer/yaml_writer.dart';
import 'package:process_run/process_run.dart' as process;

const List<String> releaseTypes = ["major", "minor", "patch"];

void main(List<String> args) async {
// ignore: avoid_print
print("🚀 Let's release a new version !\n");

try {
final String currentVersion = await getCurrentPackageVersion();

String? releaseType;
if (args.isEmpty || !releaseTypes.contains(args[0])) {
releaseType = getReleaseType(currentVersion);
} else {
releaseType = args[0];
}

final String newVersion = getNewVersion(currentVersion, releaseType);
// ignore: avoid_print
print("New ${releaseType.toUpperCase()} release : $newVersion");
await updateVersion(newVersion);
await commitNewVersion(releaseType, newVersion);
} catch (e) {
printerr(e.toString());
}
}

String getReleaseType(String currentVersion) {
return menu<String>(
'Please select a release type:',
options: releaseTypes,
format: (String item) =>
"$item (${getNewVersion(currentVersion, item)})".toUpperCase(),
defaultOption: releaseTypes[2],
);
}

Future<String> getCurrentPackageVersion() async {
File configFile = File('config/cloudsmith_config.json');
if (!await configFile.exists()) {
throw Exception("Configuration file not found.");
}

String configString = await configFile.readAsString();
Map<String, dynamic> config = jsonDecode(configString);

final String owner = config['OWNER']!;
final String repo = config['REPO']!;
final String packageFormat = config['PACKAGE_FORMAT']!;
final String packageName = config['PACKAGE_NAME']!;
final String apiKey = config['CLOUDSMITH_API_KEY']!;

final response = await http.get(
Uri.https("api.cloudsmith.io",
"/v1/badges/version/$owner/$repo/$packageFormat/$packageName/package_version/package_identifiers"),
headers: {"X-Api-Key": apiKey},
);
if (response.statusCode != 200) {
throw Exception("Error when getting the current version of $owner/$repo");
}

final String version = json.decode(response.body)["version"];
// ignore: avoid_print
print("Current version: $version");

return version;
}

String getNewVersion(String currentVersion, String releaseType) {
List<int?> versionNumbers =
currentVersion.split('.').map<int?>((e) => int.tryParse(e)).toList();

// update version number by release type
switch (releaseType) {
case 'major':
versionNumbers[0] = versionNumbers[0]! + 1;
versionNumbers[1] = 0;
versionNumbers[2] = 0;
break;
case 'minor':
versionNumbers[1] = versionNumbers[1]! + 1;
versionNumbers[2] = 0;
break;
default: // patch
versionNumbers[2] = versionNumbers[2]! + 1;
break;
}

return versionNumbers.join(".");
}

Future<void> updateVersion(String newVersion) async {
final File changeLog = File('CHANGELOG.md');
final File pubspec = File("pubspec.yaml");

// get files content as string
String changeLogContent = await changeLog.readAsString();
String pubspecContent = await pubspec.readAsString();

// update package version in pubspec.yaml
Map map = Map<String, dynamic>.from(loadYaml(pubspecContent));
map["version"] = newVersion;

// update package version in CHANGELOG.MD
List<String> changeLogContentList = changeLogContent.split('\n');
changeLogContentList[0] = "## $newVersion";

// write update
await pubspec.writeAsString(YAMLWriter().write(map));
await changeLog.writeAsString(changeLogContentList.join("\n"));
}

Future<void> commitNewVersion(String releaseType, String newVersion) async {
// Commit the changes
bool commit = confirm("Do you want to commit the changes ?");
if (commit) {
await process.run("git add .");
await process.run("git commit -m \"$releaseType release: $newVersion\"");
// Add tag version
await process.run("git tag v$newVersion");
}

// Push the changes
if (commit && confirm("Do you want to push the changes as well ?")) {
await process.run("git push");
await process.run("git push --tags");
}
}

0 comments on commit 32341a2

Please sign in to comment.