Skip to content

Commit

Permalink
fix fluttercommunity#550 and inconstant implementation of reading & w…
Browse files Browse the repository at this point in the history
…riting files.
  • Loading branch information
vanlooverenkoen authored and mwhobrey committed Aug 22, 2024
1 parent 006cb1e commit a252ba8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 45 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ linter:
- avoid_types_as_parameter_names
- avoid_unused_constructor_parameters
- avoid_void_async
- unawaited_futures
- await_only_futures
- camel_case_types
- cancel_subscriptions
Expand Down
44 changes: 20 additions & 24 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ void createMipmapXmlFile(
);
}

mipmapXmlFile.create(recursive: true).then((File adaptiveIconFile) {
adaptiveIconFile.writeAsString(
xml_template.mipmapXmlFile.replaceAll('{{CONTENT}}', xmlContent),
);
});
mipmapXmlFile.createSync(recursive: true);
mipmapXmlFile.writeAsStringSync(
xml_template.mipmapXmlFile.replaceAll('{{CONTENT}}', xmlContent),
);
}

/// Retrieves the colors.xml file for the project.
Expand Down Expand Up @@ -249,13 +248,10 @@ void _createAdaptiveBackgrounds(

/// Creates a colors.xml file if it was missing from android/app/src/main/res/values/colors.xml
void createNewColorsFile(String backgroundColor, String? flavor) {
File(constants.androidColorsFile(flavor))
.create(recursive: true)
.then((File colorsFile) {
colorsFile.writeAsString(xml_template.colorsXml).then((File file) {
updateColorsFile(colorsFile, backgroundColor);
});
});
final colorsFile = File(constants.androidColorsFile(flavor));
colorsFile.createSync(recursive: true);
colorsFile.writeAsStringSync(xml_template.colorsXml);
updateColorsFile(colorsFile, backgroundColor);
}

/// Updates the colors.xml with the new adaptive launcher icon color
Expand Down Expand Up @@ -296,14 +292,14 @@ void overwriteExistingIcons(
String? flavor,
) {
final Image newFile = utils.createResizedImage(template.size, image);
File(
final file = File(
constants.androidResFolder(flavor) +
template.directoryName +
'/' +
filename,
).create(recursive: true).then((File file) {
file.writeAsBytesSync(encodePng(newFile));
});
);
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(newFile));
}

/// Saves new launcher icons to the project, keeping the old launcher icons.
Expand All @@ -316,30 +312,30 @@ void _saveNewImages(
String? flavor,
) {
final Image newFile = utils.createResizedImage(template.size, image);
File(
final file = File(
constants.androidResFolder(flavor) +
template.directoryName +
'/' +
iconFilePath,
).create(recursive: true).then((File file) {
file.writeAsBytesSync(encodePng(newFile));
});
);
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(newFile));
}

/// Updates the line which specifies the launcher icon within the AndroidManifest.xml
/// with the new icon name (only if it has changed)
///
/// Note: default iconName = "ic_launcher"
Future<void> overwriteAndroidManifestWithNewLauncherIcon(
void overwriteAndroidManifestWithNewLauncherIcon(
String iconName,
File androidManifestFile,
) async {
) {
// we do not use `file.readAsLinesSync()` here because that always gets rid of the last empty newline
final List<String> oldManifestLines =
(await androidManifestFile.readAsString()).split('\n');
androidManifestFile.readAsStringSync().split('\n');
final List<String> transformedLines =
_transformAndroidManifestWithNewLauncherIcon(oldManifestLines, iconName);
await androidManifestFile.writeAsString(transformedLines.join('\n'));
androidManifestFile.writeAsStringSync(transformedLines.join('\n'));
}

/// Updates only the line containing android:icon with the specified iconName
Expand Down
21 changes: 9 additions & 12 deletions lib/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ void overwriteDefaultIcons(IosIconTemplate template, Image image) {
void saveNewIcons(IosIconTemplate template, Image image, String newIconName) {
final String newIconFolder = iosAssetFolder + newIconName + '.appiconset/';
final Image newFile = createResizedImage(template, image);
File(newIconFolder + newIconName + template.name + '.png')
.create(recursive: true)
.then((File file) {
file.writeAsBytesSync(encodePng(newFile));
});
final file = File(newIconFolder + newIconName + template.name + '.png');
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(newFile));
}

/// create resized icon image
Expand All @@ -152,7 +150,7 @@ Image createResizedImage(IosIconTemplate template, Image image) {
/// Change the iOS launcher icon
Future<void> changeIosLauncherIcon(String iconName, String? flavor) async {
final File iOSConfigFile = File(iosConfigFile);
final List<String> lines = await iOSConfigFile.readAsLines();
final List<String> lines = iOSConfigFile.readAsLinesSync();

bool onConfigurationSection = false;
String? currentConfig;
Expand Down Expand Up @@ -180,18 +178,17 @@ Future<void> changeIosLauncherIcon(String iconName, String? flavor) async {
}

final String entireFile = '${lines.join('\n')}\n';
await iOSConfigFile.writeAsString(entireFile);
iOSConfigFile.writeAsStringSync(entireFile);
}

/// Create the Contents.json file
void modifyContentsFile(String newIconName) {
final String newIconFolder =
iosAssetFolder + newIconName + '.appiconset/Contents.json';
File(newIconFolder).create(recursive: true).then((File contentsJsonFile) {
final String contentsFileContent =
generateContentsFileAsString(newIconName);
contentsJsonFile.writeAsString(contentsFileContent);
});
final contentsJsonFile = File(newIconFolder);
contentsJsonFile.createSync(recursive: true);
final String contentsFileContent = generateContentsFileAsString(newIconName);
contentsJsonFile.writeAsStringSync(contentsFileContent);
}

String generateContentsFileAsString(String newIconName) {
Expand Down
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Future<void> createIconsFromArguments(List<String> arguments) async {
);
}
try {
await createIconsFromConfig(
createIconsFromConfig(
flutterLauncherIconsConfigs,
logger,
prefixPath,
Expand All @@ -109,7 +109,7 @@ Future<void> createIconsFromArguments(List<String> arguments) async {
'No configuration found for $flavor flavor.',
);
}
await createIconsFromConfig(
createIconsFromConfig(
flutterLauncherIconsConfigs,
logger,
prefixPath,
Expand All @@ -125,12 +125,12 @@ Future<void> createIconsFromArguments(List<String> arguments) async {
}
}

Future<void> createIconsFromConfig(
void createIconsFromConfig(
Config flutterConfigs,
FLILogger logger,
String prefixPath, [
String? flavor,
]) async {
]) {
if (!flutterConfigs.hasPlatformConfig) {
throw const InvalidConfigException(errorMissingPlatform);
}
Expand Down
10 changes: 5 additions & 5 deletions test/android_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void main() {

await withTempFile('AndroidManifest.xml', (File androidManifestFile) async {
androidManifestFile.writeAsStringSync(inputManifest);
await android.overwriteAndroidManifestWithNewLauncherIcon(
android.overwriteAndroidManifestWithNewLauncherIcon(
'ic_other_icon_name',
androidManifestFile,
);
Expand All @@ -89,7 +89,7 @@ void main() {

await withTempFile('AndroidManifest.xml', (File androidManifestFile) async {
androidManifestFile.writeAsStringSync(inputManifest);
await android.overwriteAndroidManifestWithNewLauncherIcon(
android.overwriteAndroidManifestWithNewLauncherIcon(
'ic_launcher',
androidManifestFile,
);
Expand All @@ -106,7 +106,7 @@ void main() {

await withTempFile('AndroidManifest.xml', (File androidManifestFile) async {
androidManifestFile.writeAsStringSync(inputManifest);
await android.overwriteAndroidManifestWithNewLauncherIcon(
android.overwriteAndroidManifestWithNewLauncherIcon(
'ic_launcher',
androidManifestFile,
);
Expand All @@ -124,7 +124,7 @@ void main() {

await withTempFile('AndroidManifest.xml', (File androidManifestFile) async {
androidManifestFile.writeAsStringSync(inputManifest);
await android.overwriteAndroidManifestWithNewLauncherIcon(
android.overwriteAndroidManifestWithNewLauncherIcon(
'ic_launcher',
androidManifestFile,
);
Expand All @@ -142,7 +142,7 @@ void main() {

await withTempFile('AndroidManifest.xml', (File androidManifestFile) async {
androidManifestFile.writeAsStringSync(inputManifest);
await android.overwriteAndroidManifestWithNewLauncherIcon(
android.overwriteAndroidManifestWithNewLauncherIcon(
'ic_launcher',
androidManifestFile,
);
Expand Down

0 comments on commit a252ba8

Please sign in to comment.