Class: XCRes::Analyzer

Inherits:
Object
  • Object
show all
Includes:
FileHelper
Defined in:
lib/xcres/analyzer/analyzer.rb

Overview

An Analyzer scans the project for references, which should be included in the output file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileHelper

#basename_without_ext

Constructor Details

#initialize(target = nil, options = {}) ⇒ Analyzer

Initialize a new analyzer

Parameters:

  • target (PBXNativeTarget) (defaults to: nil)

    see target.

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

    see subclasses.



43
44
45
46
47
48
# File 'lib/xcres/analyzer/analyzer.rb', line 43

def initialize(target=nil, options={})
  @target = target
  @sections = []
  @exclude_file_patterns = []
  @options = options
end

Instance Attribute Details

#exclude_file_patternsArray<String>

Returns the exclude file patterns.

Returns:

  • (Array<String>)

    the exclude file patterns



27
28
29
# File 'lib/xcres/analyzer/analyzer.rb', line 27

def exclude_file_patterns
  @exclude_file_patterns
end

#loggerLogger

Returns the logger.

Returns:



31
32
33
# File 'lib/xcres/analyzer/analyzer.rb', line 31

def logger
  @logger
end

#optionsHash

Returns the options passed to the sections.

Returns:

  • (Hash)

    the options passed to the sections



23
24
25
# File 'lib/xcres/analyzer/analyzer.rb', line 23

def options
  @options
end

#sectionsArray<Section> (readonly)

Returns the built sections.

Returns:

  • (Array<Section>)

    the built sections



19
20
21
# File 'lib/xcres/analyzer/analyzer.rb', line 19

def sections
  @sections
end

#targetPBXNativeTarget (readonly)

Returns the application target of the #project to analyze.

Returns:

  • (PBXNativeTarget)

    the application target of the #project to analyze.



15
16
17
# File 'lib/xcres/analyzer/analyzer.rb', line 15

def target
  @target
end

Instance Method Details

#analyzeArray<Section>

Analyze the project

Returns:

  • (Array<Section>)

    the built sections



55
56
57
# File 'lib/xcres/analyzer/analyzer.rb', line 55

def analyze
  @sections = @sections.compact.reject { |s| s.items.nil? || s.items.empty? }
end

#filter_exclusions(file_paths) ⇒ Object

Apply the configured exclude file patterns to a list of files

Parameters:

  • file_paths (Array<Pathname>)

    the list of files to filter

  • the (Array<Pathname>)

    filtered list of files



92
93
94
95
96
# File 'lib/xcres/analyzer/analyzer.rb', line 92

def filter_exclusions file_paths
  file_paths.reject do |path|
    exclude_file_patterns.any? { |pattern| File.fnmatch("#{pattern}", path) || File.fnmatch("**/#{pattern}", path) }
  end
end

#find_file_refs_by_extname(extname) ⇒ Array<PBXFileReference>

Discover all references to files with a specific extension in project, which belong to a resources build phase of an application target.

Parameters:

  • extname (String)

    the extname, which contains a leading dot e.g.: ‘.bundle’, ‘.strings’

Returns:

  • (Array<PBXFileReference>)


107
108
109
110
111
112
# File 'lib/xcres/analyzer/analyzer.rb', line 107

def find_file_refs_by_extname(extname)
  project.files.select do |file_ref|
    File.extname(file_ref.path) == extname \
    && is_file_ref_included_in_application_target?(file_ref)
  end
end

#is_file_ref_included_in_application_target?(file_ref) ⇒ Bool

Checks if a file ref is included in any resources build phase of any of the application targets of the #project.

Parameters:

  • file_ref (PBXFileReference)

    the file to search for

Returns:

  • (Bool)


122
123
124
# File 'lib/xcres/analyzer/analyzer.rb', line 122

def is_file_ref_included_in_application_target?(file_ref)
  resources_files.include?(file_ref)
end

#new_section(name, data, options = {}) ⇒ XCRes::Section

Create a new Section.

Parameters:

  • name (String)

    see Section#name

  • items (Hash)

    see Section#items

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

    see Section#options

Returns:



80
81
82
# File 'lib/xcres/analyzer/analyzer.rb', line 80

def new_section(name, data, options={})
  XCRes::Section.new(name, data, self.options.merge(options))
end

#projectXcodeproj::Project

Return the Xcode project to analyze

Returns:

  • (Xcodeproj::Project)


63
64
65
# File 'lib/xcres/analyzer/analyzer.rb', line 63

def project
  target.project
end

#resources_filesArray<PBXFileReference>

Find files in resources build phases of application targets

Returns:

  • (Array<PBXFileReference>)


130
131
132
133
134
135
136
137
138
# File 'lib/xcres/analyzer/analyzer.rb', line 130

def resources_files
  target.resources_build_phase.files.map do |build_file|
    if build_file.file_ref.is_a?(Xcodeproj::Project::Object::PBXGroup)
      build_file.file_ref.recursive_children
    else
      [build_file.file_ref]
    end
  end.flatten.compact
end