Class: XCRes::ResourcesBuilder

Inherits:
FileBuilder show all
Includes:
FileHelper
Defined in:
lib/xcres/builder/resources_builder.rb

Constant Summary collapse

<<EOS
// generated by xcres
//
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// See https://github.com/mrackwitz/xcres for more info.
//
EOS
OBJC_COMPILER_KEYWORDS =
%w{
  auto break case char const continue default do double else enum extern float
  for goto if inline int long register restrict return short signed sizeof
  static struct switch typedef union unsigned void volatile while
}
SWIFT_COMPILER_KEYWORDS =
%w{
  class deinit enum extension func import init internal let operator private
  protocol public static struct subscript typealias var break case continue
  default do else fallthrough for if in return switch where while as
  dynamicType false is nil self Self super true
}
SWIFT_EXTENSIONS =
<<EOS
public extension %s.Strings {
    public var localizedValue: String {
        return NSLocalizedString(self.rawValue,
                                 bundle: NSBundle(forClass: R.self),
                                 comment: "")
    }
}
EOS

Instance Attribute Summary collapse

Attributes inherited from FileBuilder

#logger, #output_path

Instance Method Summary collapse

Methods included from FileHelper

#basename_without_ext

Methods inherited from FileBuilder

#build_contents, #prepare_output_path!, #write_file, #write_file_eventually

Constructor Details

#initializeResourcesBuilder

Initialize a new instance



61
62
63
64
65
# File 'lib/xcres/builder/resources_builder.rb', line 61

def initialize
  @sections = {}
  self.documented = true
  self.swift = false
end

Instance Attribute Details

#documentedBool Also known as: documented?

Returns whether the generated resources constant should contain inline documentation for each key, true by default.

Returns:

  • (Bool)

    whether the generated resources constant should contain inline documentation for each key, true by default



46
47
48
# File 'lib/xcres/builder/resources_builder.rb', line 46

def documented
  @documented
end

#resources_constant_nameString

Extract resource name from #output_path, if not customized

Returns:

  • (String)


41
42
43
# File 'lib/xcres/builder/resources_builder.rb', line 41

def resources_constant_name
  @resources_constant_name
end

#sectionsHash{String => {String => String}} (readonly)

Returns the sections, which will been written to the built files.

Returns:

  • (Hash{String => {String => String}})

    the sections, which will been written to the built files



57
58
59
# File 'lib/xcres/builder/resources_builder.rb', line 57

def sections
  @sections
end

#swiftBool Also known as: swift?

Returns whether Swift code should be generated, Objective-C used if false, false by default.

Returns:

  • (Bool)

    whether Swift code should be generated, Objective-C used if false, false by default



52
53
54
# File 'lib/xcres/builder/resources_builder.rb', line 52

def swift
  @swift
end

Instance Method Details

#add_section(name, items, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/xcres/builder/resources_builder.rb', line 75

def add_section name, items, options = {}
  raise ArgumentError.new 'No items given!' if items.nil?

  transformed_items = {}

  for key, value in items
    transformed_key = transform_key key, options

    # Skip invalid key names
    if transformed_key.length == 0
      logger.warn "Skip invalid key: '%s'. (Was transformed to empty text)", key
      next
    end

    # Skip compiler keywords
    compiler_keywords = swift? ? SWIFT_COMPILER_KEYWORDS : OBJC_COMPILER_KEYWORDS
    if compiler_keywords.include? transformed_key
      logger.warn "Skip invalid key: '%s'. (Was transformed to keyword '%s')", key, transformed_key
      next
    end

    transformed_items[transformed_key] = value
  end

  @sections[name] = transformed_items
end

#buildObject



102
103
104
105
106
107
108
109
110
# File 'lib/xcres/builder/resources_builder.rb', line 102

def build
  super

  if swift?
    build_and_write_swift
  else
    build_and_write_objc
  end
end