Class: Stringer::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/stringer/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(locale, options = {}) ⇒ Processor

The Processor will be doing the genstringing part of the operation.

Possible options:

- file_folder  : Folder where `genstrings` should look for
                 .m files. It searches this recursively.
- genstrings   : Location of the genstrings command
- lproj_parent : Path to folder containing the `<locale>.lproj`


14
15
16
17
18
19
20
# File 'lib/stringer/processor.rb', line 14

def initialize(locale, options = {})
  options       = default_options.merge(options)
  @locale       = locale
  @files_folder = options[:files_folder]
  @genstrings   = options[:genstrings]
  @lproj_parent = options[:lproj_parent]
end

Instance Method Details

#default_optionsObject

Some sensible defaults in a hash



23
24
25
26
27
28
29
# File 'lib/stringer/processor.rb', line 23

def default_options
  {
    :lproj_parent => dir_path_for_dir_with_same_name_as_parent_dir,
    :files_folder => dir_path_for_dir_with_same_name_as_parent_dir,
    :genstrings => "/usr/bin/genstrings"
  }
end

#dir_path_for_dir_with_same_name_as_parent_dirObject

If standard Xcode template is used, this is the dir where most files will be. For a project created with name “Something”

Something:
  Something:
    en.lproj/
    *.m

Returns the path



40
41
42
43
44
# File 'lib/stringer/processor.rb', line 40

def dir_path_for_dir_with_same_name_as_parent_dir
  current_dir_path = File.dirname(__FILE__)
  current_dir_name = File.basename(current_dir_path)
  File.join(current_dir_path, current_dir_name)
end

#filesObject

Returns an array of all files needed to be processed.



51
52
53
# File 'lib/stringer/processor.rb', line 51

def files
  `find #{@files_folder} -name \*.m`.split("\n")
end

#genstrings_commandObject



46
47
48
# File 'lib/stringer/processor.rb', line 46

def genstrings_command
  "#{@genstrings} -q -o #{lproj_folder} #{files.join(" ")}"
end

#log(message) ⇒ Object



75
76
77
# File 'lib/stringer/processor.rb', line 75

def log(message)
  puts message
end

#lproj_folderObject



67
68
69
# File 'lib/stringer/processor.rb', line 67

def lproj_folder
  File.join(@lproj_parent, "#{@locale}.lproj")
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stringer/processor.rb', line 55

def run
  log("Generating #{@locale}.lproj")
  original_file = StringsFile.new(strings_file_path)
  if system(genstrings_command)
    new_file = StringsFile.new(strings_file_path)
    added_keys, removed_keys = original_file.apply(new_file)
    show_changes(added_keys, "Added")
    show_changes(removed_keys, "Removed")
  end
  original_file.write!
end

#show_changes(keys, string) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/stringer/processor.rb', line 79

def show_changes(keys, string)
  number = keys.count
  if number == 1
    string = " - #{string} #{number} key"
  else
    string = " - #{string} #{number} keys"
  end
  string << " (#{keys.join(";")[0..50]}...)" unless number == 0
  log string
end

#strings_file_pathObject



71
72
73
# File 'lib/stringer/processor.rb', line 71

def strings_file_path
  "#{lproj_folder}/Localizable.strings"
end