Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Modify project plist for decorated icons during build time #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/ios_build_kit/tasks/decorate_icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(attributes = {})

def run!
decorate_icons!
update_plist_icons!
complete_task!
end

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/ios_build_kit/tasks/xcode_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
28 changes: 24 additions & 4 deletions lib/ios_build_kit/utils/plist_pal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down