Migrate scripts to nullsafety

This commit is contained in:
shilangyu 2021-04-09 11:41:11 +02:00
parent 83235534f5
commit 1dc131cd29
4 changed files with 36 additions and 30 deletions

View File

@ -13,8 +13,8 @@ void confirm(String message) {
}
}
void printError(String message, {bool shouldExit = true}) {
Never printError(String message) {
stderr.writeln('\x1B[31m$message\x1B[0m');
if (shouldExit) exit(1);
exit(1);
}

View File

@ -12,7 +12,7 @@ Future<void> main(List<String> args) async {
final keys = strings.keys.where((key) => !key.startsWith('@')).toSet();
final keysWithoutVariables = keys.where((key) {
final metadata = strings['@$key'] as Map<String, dynamic>;
final placeholders = metadata['placeholders'] as Map<String, dynamic>;
final placeholders = metadata['placeholders'] as Map<String, dynamic>?;
return placeholders?.isEmpty ?? true;
}).toSet();

View File

@ -10,17 +10,17 @@ import 'gen_l10n_from_string.dart' as gen;
// ignore: camel_case_types
class _ {
final String key;
final String rename;
final String? rename;
/// make all letters except the first one lower case
final bool decapitalize;
final bool toLowerCase;
/// arb format for the placeholder
final String format;
final String? format;
/// arb type for the placeholder
final String type;
final String? type;
const _(
this.key, {
@ -281,18 +281,20 @@ void portStrings(
}
}
final baseTranslations = lemmyTranslations[baseLanguage]!;
for (final migrate in toMigrate) {
if (!lemmyTranslations[baseLanguage].containsKey(migrate.key)) {
if (!baseTranslations.containsKey(migrate.key)) {
printError('"${migrate.key}" does not exist in $repoName');
}
if (lemmurTranslations[baseLanguage].containsKey(migrate.renamedKey) &&
if (lemmurTranslations[baseLanguage]!.containsKey(migrate.renamedKey) &&
!force) {
confirm('"${migrate.key}" already exists in lemmur, overwrite?');
}
final variableName = RegExp(r'{{([\w_]+)}|')
.firstMatch(lemmyTranslations[baseLanguage][migrate.key])
.firstMatch(baseTranslations[migrate.key]!)
?.group(1);
final metadata = <String, dynamic>{
@ -306,19 +308,17 @@ void portStrings(
};
// ignore: omit_local_variable_types
String Function(Map<String, String> translations) transformer =
(translations) => translations[migrate.key];
(translations) => translations[migrate.key]!;
// check if it has a plural form
if (lemmyTranslations[baseLanguage].containsKey('${migrate.key}_plural')) {
if (baseTranslations.containsKey('${migrate.key}_plural')) {
transformer = (translations) {
if (translations[migrate.key] == null) return null;
final fixedVariables = translations[migrate.key]
final fixedVariables = translations[migrate.key]!
.replaceAll('{{$variableName}}', '{$variableName}');
final pluralForm = () {
if (translations.containsKey('${migrate.key}_plural')) {
return translations['${migrate.key}_plural']
return translations['${migrate.key}_plural']!
.replaceAll('{{$variableName}}', '{$variableName}');
}
@ -337,11 +337,11 @@ void portStrings(
final language = trans.key;
final strings = trans.value;
lemmurTranslations[language][migrate.renamedKey] = transformer(strings);
lemmurTranslations[language]![migrate.renamedKey] = transformer(strings);
}
lemmurTranslations[baseLanguage][migrate.renamedKey] =
migrate.transform(transformer(lemmyTranslations[baseLanguage]));
lemmurTranslations[baseLanguage]['@${migrate.renamedKey}'] = metadata;
lemmurTranslations[baseLanguage]![migrate.renamedKey] =
migrate.transform(transformer(baseTranslations));
lemmurTranslations[baseLanguage]!['@${migrate.renamedKey}'] = metadata;
}
}
@ -359,7 +359,7 @@ Future<void> save(Map<String, Map<String, dynamic>> lemmurTranslations) async {
}
}
for (final rem in toRemove) {
lemmurTranslations[rem[0]].remove(rem[1]);
lemmurTranslations[rem[0]]?.remove(rem[1]);
}
for (final language in lemmurTranslations.keys) {

View File

@ -32,7 +32,8 @@ Future<void> assertNoStagedGit() async {
}
class Version {
int major, minor, patch, code;
final int major, minor, patch, code;
Version(this.major, this.minor, this.patch, this.code);
String toString() => '$major.$minor.$patch+$code';
String toStringNoCode() => '$major.$minor.$patch';
}
@ -44,10 +45,14 @@ Future<Version> bumpedVersion(String versionBumpType) async {
final versionMatch = RegExp(r'version: (\d+)\.(\d+)\.(\d+)\+(\d+)')
.firstMatch(pubspecContents);
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));
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)!);
switch (versionBumpType) {
case 'patch':
@ -65,11 +70,7 @@ Future<Version> bumpedVersion(String versionBumpType) async {
}
code++;
return Version()
..major = major
..minor = minor
..patch = patch
..code = code;
return Version(major, minor, patch, code);
}
Future<void> updatePubspec(Version version) async {
@ -90,6 +91,11 @@ Future<void> updateChangelog(Version version) async {
var currentChangelog =
RegExp(r'^## Unreleased$.+?^##[^#]', multiLine: true, dotAll: true)
.stringMatch(changelogContents);
if (currentChangelog == null) {
printError('No changelog found');
}
currentChangelog = currentChangelog.substring(0, currentChangelog.length - 4);
final date = DateTime.now();