Class: XCRes::ResourcesAnalyzer::BaseResourcesAnalyzer
- Defined in:
- lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb
Overview
A BaseResourcesAnalyzer
scans the project for resources, which should be included in the output file.
Direct Known Subclasses
BundleResourcesAnalyzer, LooseResourcesAnalyzer, XCAssetsAnalyzer
Constant Summary collapse
- FILTER_WORDS =
['icon', 'image']
Instance Attribute Summary
Attributes inherited from Analyzer
#exclude_file_patterns, #logger, #options, #sections, #target
Instance Method Summary collapse
-
#build_images_section_data(image_file_paths, options = {}) ⇒ Hash{String => Pathname}
Build a section for image resources.
-
#build_section_data(file_paths, options = {}) ⇒ Hash{String => Pathname}
Build a keys to paths mapping.
-
#filter_device_specific_image_paths(file_paths) ⇒ Array<String>
Filter out device scale and idiom specific images (retina, ipad), but ensure the base exist once.
-
#find_files_in_dir(dir) ⇒ Array<Pathname>
Get a list of all files in a directory.
-
#find_image_files(file_paths) ⇒ Array<Pathname>
Find image files in a given list of file paths.
-
#key_from_path(path) ⇒ String
Derive a key from a resource path.
Methods inherited from Analyzer
#analyze, #filter_exclusions, #find_file_refs_by_extname, #initialize, #is_file_ref_included_in_application_target?, #new_section, #project, #resources_files
Methods included from FileHelper
Constructor Details
This class inherits a constructor from XCRes::Analyzer
Instance Method Details
#build_images_section_data(image_file_paths, options = {}) ⇒ Hash{String => Pathname}
Build a section for image resources
42 43 44 45 46 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 42 def build_images_section_data image_file_paths, ={} image_file_paths = filter_exclusions(image_file_paths) image_file_paths = filter_device_specific_image_paths(image_file_paths) build_section_data(image_file_paths, ) end |
#build_section_data(file_paths, options = {}) ⇒ Hash{String => Pathname}
Build a keys to paths mapping
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 87 def build_section_data file_paths, ={} = { use_basename: [], path_without_ext: false, }.merge # Transform image file paths to keys keys_to_paths = {} for path in file_paths basename = File.basename(path) key = key_from_path([:use_basename].include?(:key) ? basename : path.to_s) transformed_path = [:use_basename].include?(:path) ? basename : path if [:path_without_ext] transformed_path = transformed_path.to_s.sub /#{File.extname(path)}$/, '' end keys_to_paths[key] = transformed_path.to_s end keys_to_paths end |
#filter_device_specific_image_paths(file_paths) ⇒ Array<String>
Filter out device scale and idiom specific images (retina, ipad), but ensure the base exist once
57 58 59 60 61 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 57 def filter_device_specific_image_paths file_paths file_paths.map do |path| path.to_s.gsub /(@2x)?(~(iphone|ipad))?(?=\.\w+$)/, '' end.to_set.to_a end |
#find_files_in_dir(dir) ⇒ Array<Pathname>
Get a list of all files in a directory
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 22 def find_files_in_dir dir unless dir.exist? warn "Can't find files in dir %s as it doesn't exist!", dir.relative_path_from(project.path + '..').to_s return [] end Dir.chdir dir do Dir['**/*'].map { |path| Pathname(path) } end end |
#find_image_files(file_paths) ⇒ Array<Pathname>
Find image files in a given list of file paths
71 72 73 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 71 def find_image_files file_paths file_paths.select { |path| path.to_s.match /\.(png|jpe?g|gif)$/ } end |
#key_from_path(path) ⇒ String
Derive a key from a resource path
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/xcres/analyzer/resources_analyzer/base_resources_analyzer.rb', line 115 def key_from_path path key = path.to_s # Get rid of the file extension key = key.sub /#{File.extname(path)}$/, '' # Graphical assets tend to contain words, which you want to strip. # Because we want to list the words to ignore only in one variant, # we have to ensure that the icon name is prepared for that, without # loosing word separation if camel case was used. key = key.underscore.downcase for filter_word in FILTER_WORDS do key.gsub! filter_word, '' end # Remove unnecessary underscores key = key.gsub(/^_*|_*$|(_)_+/, '\1') return key end |