From d59eaf46afddad4bb63874450866e7500dba0a9f Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Tue, 28 Oct 2014 15:38:41 +0800 Subject: [PATCH] Modify project plist for decorated icons during build time --- lib/ios_build_kit/tasks/decorate_icon.rb | 26 ++++++++++++++++++---- lib/ios_build_kit/tasks/xcode_build.rb | 8 +++++++ lib/ios_build_kit/utils/plist_pal.rb | 28 ++++++++++++++++++++---- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/lib/ios_build_kit/tasks/decorate_icon.rb b/lib/ios_build_kit/tasks/decorate_icon.rb index a80bd8f..cd0f38e 100644 --- a/lib/ios_build_kit/tasks/decorate_icon.rb +++ b/lib/ios_build_kit/tasks/decorate_icon.rb @@ -22,6 +22,7 @@ def initialize(attributes = {}) def run! decorate_icons! + update_plist_icons! complete_task! end @@ -49,12 +50,32 @@ def icon_files_to_decorate to_decorate end + def decorated_icon_path icon_path + original_img_filename = File.basename icon_path + new_img_filename = "Decorated-" + original_img_filename + icon_path.gsub(original_img_filename, new_img_filename) + end + def decorate_icons! icon_files_to_decorate.each do |img_path| @decorated_icons << create_decorated_version_of(img_path) end end + def update_plist_icons! + backup_plist = @runner.config.info_plist.gsub(/(\.plist)$/i, '.orig\1') + FileUtils.remove_file backup_plist, true + FileUtils.cp @runner.config.info_plist, backup_plist + @runner.store[:backup_plist] = backup_plist + + icons = BuildKit::Utilities::PlistPal.read_array_value_in_plist( + @runner.config.info_plist, "CFBundleIcons:CFBundlePrimaryIcon:CFBundleIconFiles") + + decorated_icons = icons.map{|icon| decorated_icon_path(icon)} + BuildKit::Utilities::PlistPal.write_array_value_in_plist( + @runner.config.info_plist, "CFBundleIcons:CFBundlePrimaryIcon:CFBundleIconFiles", decorated_icons) + end + def create_decorated_version_of icon_path original = Magick::ImageList.new icon_path decorated_icon = original.copy @@ -84,10 +105,7 @@ def create_decorated_version_of icon_path self.font_weight = annotation_params[:font_weight] end - original_img_filename = File.basename icon_path - new_img_filename = "Decorated-" + original_img_filename - new_img_full_path = icon_path.gsub(original_img_filename, new_img_filename) - + new_img_full_path = decorated_icon_path(icon_path) decorated_icon.write(new_img_full_path) new_img_full_path diff --git a/lib/ios_build_kit/tasks/xcode_build.rb b/lib/ios_build_kit/tasks/xcode_build.rb index a8c54b2..0bd2420 100644 --- a/lib/ios_build_kit/tasks/xcode_build.rb +++ b/lib/ios_build_kit/tasks/xcode_build.rb @@ -19,6 +19,7 @@ def run! run_command! "clean" if @task_options[:clean] run_command! "build" create_build_directory unless File.exists?(@config.absolute_build_dir) + cleanup_build_assets! complete_task! end @@ -57,6 +58,13 @@ def build_succeeded? @output.include? "EXIT CODE: 0" end + def cleanup_build_assets! + if @runner.has_completed_task? :decorate_icon + backup_plist = @runner.store[:backup_plist] + FileUtils.mv backup_plist, @runner.config.info_plist, :force => true + end + end + def complete_task! @runner.store[:xcode_build_succeeded] = build_succeeded? message = (build_succeeded?) ? "xcode_build completed, project built successfully" : "xcode_build completed, but the project failed to build" diff --git a/lib/ios_build_kit/utils/plist_pal.rb b/lib/ios_build_kit/utils/plist_pal.rb index 527b252..8c32f5a 100644 --- a/lib/ios_build_kit/utils/plist_pal.rb +++ b/lib/ios_build_kit/utils/plist_pal.rb @@ -10,16 +10,36 @@ def self.read_value_in_plist plist, key %x[/usr/libexec/PlistBuddy -c "Print #{key}" \"#{plist}\"] end + def self.read_array_value_in_plist plist, key + array_value = [] + index = 0 + loop do + value = %x[/usr/libexec/PlistBuddy -c "Print #{key}:#{index}" \"#{plist}\" 2>/dev/null] + break if !$?.success? || value.nil? + array_value.push value.strip + index += 1 + end + return array_value + end + def self.write_value_in_plist plist, key, value plist_buddy_command = "\"Set :#{key} #{value}\" \"#{plist}\"" system "/usr/libexec/PlistBuddy -c #{plist_buddy_command}" end + def self.write_array_value_in_plist plist, key, array_value + system "/usr/libexec/PlistBuddy -c \"Delete #{key}\" \"#{plist}\"" + system "/usr/libexec/PlistBuddy -c \"Add #{key} array\" \"#{plist}\"" + array_value.each_with_index do |value, index| + system "/usr/libexec/PlistBuddy -c \"Add #{key}:#{index} string #{value}\" \"#{plist}\"" + end + end + def self.brute_replace_in_plist plist, value, new_value - original = File.read plist - updated = original.gsub value, new_value - File.open(plist, "w") { |file| file.write updated } - end + original = File.read plist + updated = original.gsub value, new_value + File.open(plist, "w") { |file| file.write updated } + end end