ios-asset-updater 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ios_asset_updater.rb +142 -0
  3. metadata +71 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 61878be297afccc12ef7bdcdf8ddfe38f3edda04db59258d5f581cefaca7db05
4
+ data.tar.gz: ae817cf275cf1cfb1fcde656b5ea781c9f3e166b5998f8ce9cd2f3b3b271b1e2
5
+ SHA512:
6
+ metadata.gz: e541a159629f6dd8d624cbd1ca46cad70a3b7374444d7c2697f151117b1d4e7606d02b6c7f167056a76feaa0ca000e7ddcdaa6afbe1efe19247915b726b3a01e
7
+ data.tar.gz: 99fc1b98a55883af60d85415bee59710a09b37c6c270bd123b52b7eac94f608c983329e99248705e4187bd1381020dcb534bf9ddcda2d52760a5136640e05124
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+ require 'colorize'
6
+
7
+ # Provides a way to update iOS Assets with new ones
8
+ class IOSAssetUpdater
9
+ # Copies image files (.jpg, .png) from a source, and update corresponding
10
+ # assets in an iOS project folder.
11
+ # Names have to match exactly for the update to take place, but the file
12
+ # extensions can be different.
13
+ # If necessary, Contents.json is updated to reflect the changes.
14
+ # Params:
15
+ # +source+:: Source folder containing images
16
+ # +destination+:: Destination folder, tipically an iOS project
17
+ def self.update(source, destination)
18
+ if source.empty? || destination.empty?
19
+ log('You must provide source and destination folders'.light_red)
20
+ return false
21
+ end
22
+
23
+ log("Searching image assets (png,jpg) in #{source}".light_blue)
24
+
25
+ source_files = files_in_dir(source)
26
+ source_hash = create_hash(source_files)
27
+
28
+ destination_files = files_in_dir(destination)
29
+ destination_hash = create_hash(destination_files)
30
+
31
+ not_found = []
32
+
33
+ source_files.each do |source_file_path|
34
+ source_file = source_hash[source_file_path]
35
+
36
+ destination_file_path = find_path(source_file[:name], destination_files)
37
+
38
+ if destination_file_path.nil? || destination_file_path.empty?
39
+ not_found.push(source_file_path)
40
+ next
41
+ end
42
+
43
+ destination_file = destination_hash[destination_file_path]
44
+
45
+ copy(source_file_path, destination_file[:dir])
46
+
47
+ if extension_equal?(source_file[:basename], destination_file[:basename])
48
+ log("#{'File extensions are equal ->'.light_green} "\
49
+ 'No need for updating Contents.json')
50
+ next
51
+ end
52
+
53
+ update_contents_json(source_file, destination_file)
54
+
55
+ delete(destination_file_path)
56
+ end
57
+
58
+ log_not_found(not_found)
59
+
60
+ return true
61
+ end
62
+
63
+ def self.files_in_dir(dir)
64
+ Dir["#{dir}/**/*.{jpg,png}"]
65
+ end
66
+
67
+ def self.create_hash(files)
68
+ new_hash = {}
69
+ files.each do |file|
70
+ properties = {}
71
+ properties[:extension] = File.extname(file)
72
+ properties[:basename] = File.basename(file)
73
+ properties[:name] = File.basename(file, properties[:extension])
74
+ properties[:dir] = File.dirname(file)
75
+ properties[:contents_path] = "#{properties[:dir]}/Contents.json"
76
+
77
+ new_hash[file] = properties
78
+ end
79
+ new_hash
80
+ end
81
+
82
+ def self.find_path(search_file_name, destination_files)
83
+ destination_files.find do |file|
84
+ file.include?("#{search_file_name}.jpg") ||
85
+ file.include?("#{search_file_name}.png")
86
+ end
87
+ end
88
+
89
+ def self.copy(source, destination)
90
+ log_separator
91
+ log("#{'Copying ->'.light_green} #{source}")
92
+ FileUtils.cp(source, destination)
93
+ end
94
+
95
+ def self.extension_equal?(source_file_path, destination_file_path)
96
+ source_file_path_basename = File.basename(source_file_path)
97
+ destination_file_path_basename = File.basename(destination_file_path)
98
+
99
+ source_file_path_basename.eql?(destination_file_path_basename)
100
+ end
101
+
102
+ def self.update_contents_json(source_file, destination_file)
103
+ log('File extensions are different. Updating Contents.json'.light_green)
104
+
105
+ contents_json_file = File.read(destination_file[:contents_path])
106
+ contents_json = JSON.parse(contents_json_file)
107
+
108
+ image_json = contents_json['images'].find do |content|
109
+ content['filename'].match(destination_file[:basename])
110
+ end
111
+
112
+ image_json['filename'] = source_file[:basename]
113
+
114
+ File.open(destination_file[:contents_path], 'w') do |f|
115
+ f.write(JSON.pretty_generate(contents_json))
116
+ end
117
+ end
118
+
119
+ def self.delete(path)
120
+ File.delete(path)
121
+ log("Deleted outdated file -> #{path}".light_red)
122
+ end
123
+
124
+ def self.log_not_found(not_found)
125
+ return if not_found.nil? || not_found.empty?
126
+
127
+ log("The following #{not_found.length} files were not found:".yellow)
128
+ not_found.each do |not_found_path|
129
+ log("▸ #{not_found_path}")
130
+ end
131
+ end
132
+
133
+ def self.log_separator
134
+ log('---'.cyan)
135
+ end
136
+
137
+ def self.log(msg)
138
+ time = Time.new
139
+ prefix = "[#{time.strftime('%H:%M:%S')}]: "
140
+ puts("#{prefix}#{msg}")
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ios-asset-updater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Vitoria
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.83.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.83.0
41
+ description: Update iOS Assets in an easy and quick way
42
+ email: contact@marcelovitoria.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/ios_asset_updater.rb
48
+ homepage: https://rubygems.org/gems/ios-asset-updater
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.0.8
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: iOS Asset Updater
71
+ test_files: []