Skip to content

Instantly share code, notes, and snippets.

@long1eu
Created January 24, 2019 14:08
Show Gist options
  • Save long1eu/e3c1dad4989869739231ee9a6ede38e0 to your computer and use it in GitHub Desktop.
Save long1eu/e3c1dad4989869739231ee9a6ede38e0 to your computer and use it in GitHub Desktop.
// File created by
// Lung Razvan <long1eu>
// on 2019-01-01
import 'dart:io';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';
void main() {
final String pwd = Directory.current.path;
final String source = '$pwd/pubspec.yaml';
String yaml = File(source).readAsStringSync();
final YamlMap doc = loadYaml(yaml, sourceUrl: source);
final Version version = Version.parse(doc['version']);
int versionCode = doc['versionCode'];
versionCode += 1;
print(versionCode);
String versionName;
if (version.patch >= 99) {
versionName = version.nextBreaking.toString();
} else {
versionName = version.nextPatch.toString();
}
yaml = yaml.replaceAll(RegExp('version: (.+)'), 'version: $versionName');
yaml = yaml.replaceAll(RegExp('versionCode: (.+)'), 'versionCode: $versionCode');
File(source).writeAsStringSync(yaml);
stdout.writeln(versionName);
updateIos(pwd, versionName, versionCode);
updateAndroid(pwd, versionName, versionCode);
updateFlutter(pwd, versionName, versionCode);
}
void updateIos(String pwd, String versionName, int versionCode) {
final File releaseConfigFile = File('$pwd/ios/Flutter/Release.xcconfig');
final File debugConfigFile = File('$pwd/ios/Flutter/Debug.xcconfig');
String releaseConfig = releaseConfigFile.readAsStringSync();
String debugConfig = debugConfigFile.readAsStringSync();
releaseConfig = updateField(releaseConfig, 'FLUTTER_BUILD_NUMBER', '$versionCode');
releaseConfig = updateField(releaseConfig, 'FLUTTER_BUILD_NAME', versionName);
debugConfig = updateField(debugConfig, 'FLUTTER_BUILD_NUMBER', '$versionCode');
debugConfig = updateField(debugConfig, 'FLUTTER_BUILD_NAME', versionName);
releaseConfigFile.writeAsStringSync(releaseConfig);
debugConfigFile.writeAsStringSync(debugConfig);
}
String updateField(String data, String field, String value) {
if (!data.contains(field)) {
data += '\n$field=$value';
} else {
data = data.replaceAll(
RegExp('$field=(.+)'),
'$field=$value',
);
}
return data;
}
void updateAndroid(String pwd, String versionName, int versionCode) {
final File gradleFile = File('$pwd/android/app/build.gradle');
String gradle = gradleFile.readAsStringSync();
gradle = gradle.replaceAll(RegExp(r"flutterVersionCode = '(\d+)'"), "flutterVersionCode = '$versionCode'");
gradle = gradle.replaceAll(RegExp(r"flutterVersionName = '(.+?)'"), "flutterVersionName = '$versionName'");
gradleFile.writeAsStringSync(gradle);
}
void updateFlutter(String pwd, String versionName, int versionCode) {
final File versionFile = File('$pwd/lib/generated/version.dart');
versionFile.writeAsStringSync('''class Version {
static const String versionName = \'$versionName\';
static const int versionCode = $versionCode;
}
''');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment