Class: XCRes::ResourcesAnalyzer::BaseResourcesAnalyzer

Inherits:
Analyzer
  • Object
show all
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.

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

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

#basename_without_ext

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

Parameters:

  • image_files (Array<String>)
  • options (Hash) (defaults to: {})

    see #build_section_data

Returns:

  • (Hash{String => Pathname})


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, options={}
  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, options)
end

#build_section_data(file_paths, options = {}) ⇒ Hash{String => Pathname}

Build a keys to paths mapping

Parameters:

  • file_paths (Array<Pathname>)

    the file paths, which will be the values of the mapping

  • options (Hash) (defaults to: {})

    valid options are:

    • Array<Symbol>

      :use_basename

      can contain :key and :path

Returns:

  • (Hash{String => Pathname})


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, options={}
  options = {
    use_basename: [],
    path_without_ext: false,
  }.merge options

  # Transform image file paths to keys
  keys_to_paths = {}
  for path in file_paths
    basename = File.basename(path)
    key = key_from_path(options[:use_basename].include?(:key) ? basename : path.to_s)
    transformed_path = options[:use_basename].include?(:path) ? basename : path
    if options[: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

Parameters:

  • file_paths (Array<Pathname>)

    the file paths to filter

Returns:

  • (Array<String>)

    the filtered file paths



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

Parameters:

  • dir (Pathname)

    the directory

Returns:

  • (Array<Pathname>)

    the file paths relative to the given dir



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

Parameters:

  • file_paths (Array<Pathname>)

    the list of files

Returns:

  • (Array<Pathname>)

    the filtered list



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

Parameters:

  • path (String)

    the path to the resource

Returns:

  • (String)


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