Class: XCRes::BuildCommand

Inherits:
ProjectCommand show all
Includes:
FileHelper
Defined in:
lib/xcres/command/build_command.rb

Overview

The BuildCommand builds the resources index files.

Instance Method Summary collapse

Methods included from FileHelper

#basename_without_ext

Methods inherited from ProjectCommand

#application_targets, #discover_xcodeproj_file_path!, #discover_xcodeproj_file_path_in_dir!, #find_xcodeproj, inherit_parameters!, #native_targets, #project, #target

Methods inherited from Command

#configure_logger, #logger, #run

Instance Method Details

#build {|builder| ... } ⇒ Object

Yields:

  • (builder)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/xcres/command/build_command.rb', line 73

def build
  # Prepare builder
  builder = XCRes::ResourcesBuilder.new
  builder.output_path = output_path
  builder.logger = logger
  builder.documented = documented?
  builder.swift = swift?
  builder.resources_constant_name = resources_constant_name

  yield builder

  # Write the files, if needed
  builder.build
end

#derive_resources_constant_nameObject



46
47
48
49
50
51
52
53
# File 'lib/xcres/command/build_command.rb', line 46

def derive_resources_constant_name
  # Fall back to `basename OUTPUT_PATH` or 'R' if both are not given
  if output_path != nil && !File.directory?(output_path)
    basename_without_ext(output_path)
  else
    'R'
  end
end

#executeObject



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
# File 'lib/xcres/command/build_command.rb', line 17

def execute
  super

  # Derive the name for the resources constant file
  self.resources_constant_name ||= derive_resources_constant_name

  # Locate output path
  self.output_path = locate_output_path

  build do |builder|
    analyzer = XCRes::AggregateAnalyzer.new(target)
    analyzer.exclude_file_patterns = exclude_file_patterns
    analyzer.logger = logger
    analyzer.add_with_class(XCRes::ResourcesAggregateAnalyzer, shorten_keys: true)
    analyzer.add_with_class(XCRes::StringsAnalyzer, default_language: default_language)
    sections = analyzer.analyze

    sections.each do |section|
      builder.add_section section.name, section.items, section.options
    end
  end

  if swift?
    success 'Successfully updated: %s', "#{output_path}.swift"
  else
    success 'Successfully updated: %s', "#{output_path}.h"
  end
end

#locate_output_pathObject



69
70
71
# File 'lib/xcres/command/build_command.rb', line 69

def locate_output_path
  output_dir.realdirpath + resources_constant_name
end

#output_dirObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xcres/command/build_command.rb', line 55

def output_dir
  if output_path.nil?
    # Use project dir, if no output path was set
    Pathname(xcodeproj_file_path) + '..'
  else
    path = Pathname(output_path)
    if path.directory?
      path
    else
      path + '..'
    end
  end
end