Skip to content

Commit

Permalink
generate Contents.json for mac app icons
Browse files Browse the repository at this point in the history
  • Loading branch information
qixotic committed Aug 9, 2022
1 parent 26cc728 commit 45452ae
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 145 deletions.
2 changes: 1 addition & 1 deletion example/flavor_app/icons_launcher-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ icons_launcher:
image_path: 'assets/ic_logo_border.png'
favicon_path: 'assets/ic_logo_round.png'
macos:
enable: false
enable: true
image_path: 'assets/ic_logo_border.png'
windows:
enable: false
Expand Down
2 changes: 1 addition & 1 deletion example/flavor_app/icons_launcher-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ icons_launcher:
image_path: 'assets/ic_logo_border.png'
favicon_path: 'assets/ic_logo_round.png'
macos:
enable: false
enable: true
image_path: 'assets/ic_logo_border.png'
windows:
enable: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
{
"images" : [
"images": [
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "app_icon_16.png",
"scale" : "1x"
"filename": "app_icon_16.png",
"idiom": "mac",
"scale": "1x",
"size": "16x16"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "app_icon_32.png",
"scale" : "2x"
"filename": "app_icon_32.png",
"idiom": "mac",
"scale": "2x",
"size": "16x16"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "app_icon_32.png",
"scale" : "1x"
"filename": "app_icon_32.png",
"idiom": "mac",
"scale": "1x",
"size": "32x32"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "app_icon_64.png",
"scale" : "2x"
"filename": "app_icon_64.png",
"idiom": "mac",
"scale": "2x",
"size": "32x32"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "app_icon_128.png",
"scale" : "1x"
"filename": "app_icon_128.png",
"idiom": "mac",
"scale": "1x",
"size": "128x128"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "app_icon_256.png",
"scale" : "2x"
"filename": "app_icon_256.png",
"idiom": "mac",
"scale": "2x",
"size": "128x128"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "app_icon_256.png",
"scale" : "1x"
"filename": "app_icon_256.png",
"idiom": "mac",
"scale": "1x",
"size": "256x256"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "app_icon_512.png",
"scale" : "2x"
"filename": "app_icon_512.png",
"idiom": "mac",
"scale": "2x",
"size": "256x256"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "app_icon_512.png",
"scale" : "1x"
"filename": "app_icon_512.png",
"idiom": "mac",
"scale": "1x",
"size": "512x512"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "app_icon_1024.png",
"scale" : "2x"
"filename": "app_icon_1024.png",
"idiom": "mac",
"scale": "2x",
"size": "512x512"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"info": {
"author": "icons_launcher",
"version": 1
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions example/flavor_app/macos/Runner/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
2 changes: 1 addition & 1 deletion example/simple_app/icons_launcher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ icons_launcher:
image_path: 'assets/ic_logo_border.png'
favicon_path: 'assets/ic_logo_round.png'
macos:
enable: false
enable: true
image_path: 'assets/ic_logo_border.png'
windows:
enable: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
{
"images" : [
"images": [
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "app_icon_16.png",
"scale" : "1x"
"filename": "app_icon_16.png",
"idiom": "mac",
"scale": "1x",
"size": "16x16"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "app_icon_32.png",
"scale" : "2x"
"filename": "app_icon_32.png",
"idiom": "mac",
"scale": "2x",
"size": "16x16"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "app_icon_32.png",
"scale" : "1x"
"filename": "app_icon_32.png",
"idiom": "mac",
"scale": "1x",
"size": "32x32"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "app_icon_64.png",
"scale" : "2x"
"filename": "app_icon_64.png",
"idiom": "mac",
"scale": "2x",
"size": "32x32"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "app_icon_128.png",
"scale" : "1x"
"filename": "app_icon_128.png",
"idiom": "mac",
"scale": "1x",
"size": "128x128"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "app_icon_256.png",
"scale" : "2x"
"filename": "app_icon_256.png",
"idiom": "mac",
"scale": "2x",
"size": "128x128"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "app_icon_256.png",
"scale" : "1x"
"filename": "app_icon_256.png",
"idiom": "mac",
"scale": "1x",
"size": "256x256"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "app_icon_512.png",
"scale" : "2x"
"filename": "app_icon_512.png",
"idiom": "mac",
"scale": "2x",
"size": "256x256"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "app_icon_512.png",
"scale" : "1x"
"filename": "app_icon_512.png",
"idiom": "mac",
"scale": "1x",
"size": "512x512"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "app_icon_1024.png",
"scale" : "2x"
"filename": "app_icon_1024.png",
"idiom": "mac",
"scale": "2x",
"size": "512x512"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"info": {
"author": "icons_launcher",
"version": 1
}
}
}
2 changes: 0 additions & 2 deletions lib/cli_commands.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
library icons_launcher_cli;

import 'dart:convert';

import 'package:icons_launcher/utils/cli_logger.dart';
import 'package:icons_launcher/utils/constants.dart';
import 'package:icons_launcher/utils/icon.dart';
Expand Down
34 changes: 6 additions & 28 deletions lib/src/ios.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
part of icons_launcher_cli;

/// Wrapper to allow generation of the Contents.json file used by the Asset
/// Catalog "App Icon Type":
/// https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/AppIconType.html
class IosContents {
/// Provide a list of [images] to be created in your Asset set.
const IosContents({required this.images});

/// The meta data for each asset file to create.
final List<IosIconTemplate> images;

/// For use with a [JsonEncoder] to generate this Asset's Contents.json file.
Map<String, dynamic> toJson() => <String, dynamic>{
'images': images.map((image) => image.toJson()).toList(),
'info': {'author': 'icons_launcher', 'version': 1}
};
}

/// Generates a list of IosIconTemplates according to the parameters:
///
/// [size] is the image size @1x scale to use in pixels.
/// [sizeName] if not provided will use the naming scheme: '\<size>x\<size>',
/// where \<size> is the rounded [size] value.
/// [scales] is an array of integers indicating the different scales for which
/// we will generate images.
/// [scales] are the different size multiples for which we will generate images.
/// [idiom] is the device family name to use within the Asset Catalog's
/// description of each icon generated.
List<IosIconTemplate> _createIosTemplates(
Expand Down Expand Up @@ -85,14 +67,17 @@ void _createIosIcons({required String imagePath}) {

final filenames = <String>{};
for (final template in iosIcons) {
// Multiple idioms can use the same file, so check if we already created it.
// Multiple icon variants can use the same file, so check if we already
// created it.
if (filenames.contains(template.filename) == false) {
filenames.add(template.filename);
_saveImageIos(template, image);
}
}

_saveContentsJson(IosContents(images: iosIcons));
AppleAppIconType(
images: iosIcons, assetPath: _flavorHelper.iOSAssetsAppIconFolder)
.saveContentsJson();

CliLogger.success('Generated app icon images', level: CliLoggerLevel.two);
}
Expand All @@ -103,10 +88,3 @@ void _saveImageIos(IosIconTemplate template, Icon image) {
'${_flavorHelper.iOSAssetsAppIconFolder}${template.filename}';
image.saveResizedPng(template.scaledSize, filePath);
}

void _saveContentsJson(IosContents contents) {
final filePath = '${_flavorHelper.iOSAssetsAppIconFolder}Contents.json';
final file = File(filePath);
const encoder = JsonEncoder.withIndent(' ');
file.writeAsStringSync(encoder.convert(contents), flush: true);
}
43 changes: 32 additions & 11 deletions lib/src/macos.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
part of icons_launcher_cli;

/// Generates templates according to the parameters:
///
/// [size] is the image size @1x scale to use in pixels.
/// [scales] are the different size multiples for which we will generate images.
List<MacOSIconTemplate> _createMacTemplates(
{required int size, required List<int> scales}) {
final templates = <MacOSIconTemplate>[];
for (int scale in scales) {
templates.add(MacOSIconTemplate(
sizeName: '${size}x$size',
scaledSize: size * scale,
scale: scale,
idiom: 'mac'));
}
return templates;
}

/// Start create macos icons
void _createMacOSIcons({required String imagePath}) {
CliLogger.info('Creating macOS icons...');
Expand All @@ -12,25 +29,29 @@ void _createMacOSIcons({required String imagePath}) {
}

final macosIcons = <MacOSIconTemplate>[
MacOSIconTemplate(name: '_16', size: 16),
MacOSIconTemplate(name: '_32', size: 32),
MacOSIconTemplate(name: '_64', size: 64),
MacOSIconTemplate(name: '_128', size: 128),
MacOSIconTemplate(name: '_256', size: 256),
MacOSIconTemplate(name: '_512', size: 512),
MacOSIconTemplate(name: '_1024', size: 1024),
..._createMacTemplates(size: 16, scales: [1, 2]),
..._createMacTemplates(size: 32, scales: [1, 2]),
..._createMacTemplates(size: 128, scales: [1, 2]),
..._createMacTemplates(size: 256, scales: [1, 2]),
..._createMacTemplates(size: 512, scales: [1, 2]),
];

final filenames = <String>{};
for (final template in macosIcons) {
_saveImageMacOS(template, image);
if (filenames.contains(template.filename) == false) {
filenames.add(template.filename);
_saveImageMacOS(template, image);
}
}

AppleAppIconType(images: macosIcons, assetPath: MACOS_DEFAULT_APP_ICON_DIR)
.saveContentsJson();

CliLogger.success('Generated app icon images', level: CliLoggerLevel.two);
}

/// Save macos image
void _saveImageMacOS(MacOSIconTemplate template, Icon image) {
final filePath =
'$MACOS_DEFAULT_APP_ICON_DIR$MACOS_DEFAULT_ICON_NAME${template.name}.png';
image.saveResizedPng(template.size, filePath);
final filePath = '$MACOS_DEFAULT_APP_ICON_DIR${template.filename}';
image.saveResizedPng(template.scaledSize, filePath);
}

0 comments on commit 45452ae

Please sign in to comment.