Class: IOSAssetUpdater
- Inherits:
-
Object
- Object
- IOSAssetUpdater
- Defined in:
- lib/ios_asset_updater.rb
Overview
Provides a way to update iOS Assets with new ones
Class Method Summary collapse
- .copy(source, destination) ⇒ Object
- .create_hash(files) ⇒ Object
- .delete(path) ⇒ Object
- .extension_equal?(source_file_path, destination_file_path) ⇒ Boolean
- .files_in_dir(dir) ⇒ Object
- .find_path(search_file_name, destination_files) ⇒ Object
- .log(msg) ⇒ Object
- .log_not_found(not_found) ⇒ Object
- .log_separator ⇒ Object
-
.update(source, destination) ⇒ Object
Copies image files (.jpg, .png) from a source, and update corresponding assets in an iOS project folder.
- .update_contents_json(source_file, destination_file) ⇒ Object
Class Method Details
.copy(source, destination) ⇒ Object
89 90 91 92 93 |
# File 'lib/ios_asset_updater.rb', line 89 def self.copy(source, destination) log_separator log("#{'Copying ->'.light_green} #{source}") FileUtils.cp(source, destination) end |
.create_hash(files) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ios_asset_updater.rb', line 67 def self.create_hash(files) new_hash = {} files.each do |file| properties = {} properties[:extension] = File.extname(file) properties[:basename] = File.basename(file) properties[:name] = File.basename(file, properties[:extension]) properties[:dir] = File.dirname(file) properties[:contents_path] = "#{properties[:dir]}/Contents.json" new_hash[file] = properties end new_hash end |
.delete(path) ⇒ Object
119 120 121 122 |
# File 'lib/ios_asset_updater.rb', line 119 def self.delete(path) File.delete(path) log("Deleted outdated file -> #{path}".light_red) end |
.extension_equal?(source_file_path, destination_file_path) ⇒ Boolean
95 96 97 98 99 100 |
# File 'lib/ios_asset_updater.rb', line 95 def self.extension_equal?(source_file_path, destination_file_path) source_file_path_basename = File.basename(source_file_path) destination_file_path_basename = File.basename(destination_file_path) source_file_path_basename.eql?(destination_file_path_basename) end |
.files_in_dir(dir) ⇒ Object
63 64 65 |
# File 'lib/ios_asset_updater.rb', line 63 def self.files_in_dir(dir) Dir["#{dir}/**/*.{jpg,png}"] end |
.find_path(search_file_name, destination_files) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/ios_asset_updater.rb', line 82 def self.find_path(search_file_name, destination_files) destination_files.find do |file| file.include?("#{search_file_name}.jpg") || file.include?("#{search_file_name}.png") end end |
.log(msg) ⇒ Object
137 138 139 140 141 |
# File 'lib/ios_asset_updater.rb', line 137 def self.log(msg) time = Time.new prefix = "[#{time.strftime('%H:%M:%S')}]: " puts("#{prefix}#{msg}") end |
.log_not_found(not_found) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/ios_asset_updater.rb', line 124 def self.log_not_found(not_found) return if not_found.nil? || not_found.empty? log("The following #{not_found.length} files were not found:".yellow) not_found.each do |not_found_path| log("▸ #{not_found_path}") end end |
.log_separator ⇒ Object
133 134 135 |
# File 'lib/ios_asset_updater.rb', line 133 def self.log_separator log('---'.cyan) end |
.update(source, destination) ⇒ Object
Copies image files (.jpg, .png) from a source, and update corresponding assets in an iOS project folder. Names have to match exactly for the update to take place, but the file extensions can be different. If necessary, Contents.json is updated to reflect the changes. Params:
source
-
Source folder containing images
destination
-
Destination folder, tipically an iOS project
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ios_asset_updater.rb', line 17 def self.update(source, destination) if source.empty? || destination.empty? log('You must provide source and destination folders'.light_red) return false end log("Searching image assets (png,jpg) in #{source}".light_blue) source_files = files_in_dir(source) source_hash = create_hash(source_files) destination_files = files_in_dir(destination) destination_hash = create_hash(destination_files) not_found = [] source_files.each do |source_file_path| source_file = source_hash[source_file_path] destination_file_path = find_path(source_file[:name], destination_files) if destination_file_path.nil? || destination_file_path.empty? not_found.push(source_file_path) next end destination_file = destination_hash[destination_file_path] copy(source_file_path, destination_file[:dir]) if extension_equal?(source_file[:basename], destination_file[:basename]) log("#{'File extensions are equal ->'.light_green} "\ 'No need for updating Contents.json') next end update_contents_json(source_file, destination_file) delete(destination_file_path) end log_not_found(not_found) return true end |
.update_contents_json(source_file, destination_file) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ios_asset_updater.rb', line 102 def self.update_contents_json(source_file, destination_file) log('File extensions are different. Updating Contents.json'.light_green) contents_json_file = File.read(destination_file[:contents_path]) contents_json = JSON.parse(contents_json_file) image_json = contents_json['images'].find do |content| content['filename'].match(destination_file[:basename]) end image_json['filename'] = source_file[:basename] File.open(destination_file[:contents_path], 'w') do |f| f.write(JSON.pretty_generate(contents_json)) end end |