From d22f82ebb0c67434a886d7c4d0272e69042d76b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eloy=20Dur=C3=A1n?= Date: Mon, 14 Oct 2013 13:52:32 +0200 Subject: [PATCH] [xcassets] Move iOS/OS X specific behavior back to configs. --- lib/motion/project/builder.rb | 49 +++++------------------ lib/motion/project/template/ios/config.rb | 12 ++++++ lib/motion/project/template/osx/config.rb | 9 +++++ lib/motion/project/xcode_config.rb | 24 +++++++++++ 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/lib/motion/project/builder.rb b/lib/motion/project/builder.rb index 3839a0fd..102a2f5c 100644 --- a/lib/motion/project/builder.rb +++ b/lib/motion/project/builder.rb @@ -337,29 +337,12 @@ EOS preserve_resources = [] # Compile Asset Catalog bundles. - xcassets_bundles = [] - config.resources_dirs.each do |dir| - if File.exist?(dir) - xcassets_bundles.concat(Dir.glob(File.expand_path(File.join(dir, '*.xcassets')))) - end - end - unless xcassets_bundles.empty? - ios = config.deploy_platform == 'iPhoneOS' - - app_icons_asset_bundles = xcassets_bundles.map { |b| Dir.glob(File.join(b, '*.appiconset')) }.flatten - if app_icons_asset_bundles.size > 1 - App.warn "Found #{app_icons_asset_bundles.size} app icon sets across all " \ - "xcasset bundles. Only the first one (alphabetically) " \ - "will be used." - end - if app_icons_asset_bundle = app_icons_asset_bundles.sort.first - preserve_resources = ios ? config.icons : [config.icon] - app_icon_name = File.basename(app_icons_asset_bundle, '.appiconset') - # TODO We can potentially use the plist output to identify the icon - # names for the config, instead of parsing the JSON. This also - # guards against the Contents.json format changing. - app_icons_info_plist = ios ? File.expand_path(File.join(build_dir, 'AppIcon.plist')) : '/dev/null' - app_icons_options = "--output-partial-info-plist \"#{app_icons_info_plist}\" --app-icon \"#{app_icon_name}\"" + assets_bundles = config.assets_bundles + unless assets_bundles.empty? + app_icons_asset_bundle = config.app_icons_asset_bundle + if app_icons_asset_bundle + app_icons_options = "--output-partial-info-plist \"#{config.app_icons_info_plist_path(platform)}\" " \ + "--app-icon \"#{config.app_icon_name_from_asset_bundle}\"" end app_resources_dir = File.expand_path(config.app_resources_dir(platform)) @@ -369,7 +352,7 @@ EOS "--minimum-deployment-target #{config.deployment_target} " \ "#{Array(config.device_family).map { |d| "--target-device #{d}" }.join(' ')} " \ "#{app_icons_options} " \ - "--compress-pngs --compile \"#{app_resources_dir}\" \"#{xcassets_bundles.join('" "')}\"" + "--compress-pngs --compile \"#{app_resources_dir}\" \"#{assets_bundles.join('" "')}\"" # TODO should be quiet normally puts cmd actool_output = `#{cmd}`.strip @@ -386,23 +369,11 @@ EOS unless actool_compiled_files App.fail 'TODO' end - actool_compiled_files.pop + # Remove the partial Info.plist line. + actool_compiled_files.pop if app_icons_asset_bundle preserve_resources.concat(actool_compiled_files) - # Extract the app icon file(s) name(s) and assign it to the config. - if app_icons_asset_bundle - if ios - app_icons_info_plist_content = `/usr/libexec/PlistBuddy -c 'Print :CFBundleIcons:CFBundlePrimaryIcon:CFBundleIconFiles' "#{app_icons_info_plist}"`.strip - lines = app_icons_info_plist_content.split("\n") - if lines.size < 2 - App.fail 'TODO' - end - config.icons = lines[1..-2] - else - # On Mac there is only ever one icns file. - config.icon = app_icon_name - end - end + config.configure_app_icons_from_asset_bundle(platform) if app_icons_asset_bundle end # Compile CoreData Model resources and SpriteKit atlas files. diff --git a/lib/motion/project/template/ios/config.rb b/lib/motion/project/template/ios/config.rb index c5ab5833..6741194b 100644 --- a/lib/motion/project/template/ios/config.rb +++ b/lib/motion/project/template/ios/config.rb @@ -57,6 +57,18 @@ module Motion; module Project; end end + def app_icons_info_plist_path(platform) + File.expand_path(File.join(versionized_build_dir(platform), 'AppIcon.plist')) + end + + def configure_app_icons_from_asset_bundle(platform) + path = app_icons_info_plist_path(platform) + if File.exist?(path) + content = `/usr/libexec/PlistBuddy -c 'Print :CFBundleIcons:CFBundlePrimaryIcon:CFBundleIconFiles' "#{path}"`.strip + self.icons = content.split("\n")[1..-2] + end + end + def validate # icons if !(icons.is_a?(Array) and icons.all? { |x| x.is_a?(String) }) diff --git a/lib/motion/project/template/osx/config.rb b/lib/motion/project/template/osx/config.rb index 481dea49..20963a54 100644 --- a/lib/motion/project/template/osx/config.rb +++ b/lib/motion/project/template/osx/config.rb @@ -68,6 +68,15 @@ module Motion; module Project; archs end + def app_icons_info_plist_path(platform) + '/dev/null' + end + + # On OS X only one file is output. E.g. NAME.icns + def configure_app_icons_from_asset_bundle(platform) + self.icon = app_icon_name_from_asset_bundle + end + def locate_compiler(platform, *execs) execs.each do |exec| cc = File.join('/usr/bin', exec) diff --git a/lib/motion/project/xcode_config.rb b/lib/motion/project/xcode_config.rb index e943ca39..f1eb29cf 100644 --- a/lib/motion/project/xcode_config.rb +++ b/lib/motion/project/xcode_config.rb @@ -322,5 +322,29 @@ EOS path = File.join(xcode_dir, 'usr/bin/TextureAtlas') File.exist?(path) ? path : nil end + + def assets_bundles + xcassets_bundles = [] + resources_dirs.each do |dir| + if File.exist?(dir) + xcassets_bundles.concat(Dir.glob(File.expand_path(File.join(dir, '*.xcassets')))) + end + end + xcassets_bundles + end + + def app_icons_asset_bundle + app_icons_asset_bundles = assets_bundles.map { |b| Dir.glob(File.join(b, '*.appiconset')) }.flatten + if app_icons_asset_bundles.size > 1 + App.warn "Found #{app_icons_asset_bundles.size} app icon sets across all " \ + "xcasset bundles. Only the first one (alphabetically) " \ + "will be used." + end + app_icons_asset_bundles.sort.first + end + + def app_icon_name_from_asset_bundle + File.basename(app_icons_asset_bundle, '.appiconset') + end end end; end