Class: Fastlane::Helper::LocalizeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/localize/helper/localize_helper.rb

Class Method Summary collapse

Class Method Details

.addToWhitelist(string) ⇒ Object



126
127
128
129
130
131
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 126

def self.addToWhitelist(string)
  open(whitelistFilename, "a") { |f|
    f.puts string
  }
  UI.message "Added \"#{string}\" to #{whitelistFilename}"
end

.codeFiles(target, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 77

def self.codeFiles(target, options)
  if options[:file_filter].nil?
    filter = []
  else
    filter = (options[:file_filter]).split(",")
  end

  codeFiles = target.source_build_phase.files.to_a.map do |pbx_build_file|
    pbx_build_file.file_ref.real_path.to_s
  end.select do |path|
    path.end_with?(".swift")
  end.select do |path|
    bool = true

    filter.each { |filter|
      if path.include? filter
        bool = false
      end
    }

    next bool
  end.select do |path|
    File.exists?(path)
  end

  codeFiles
end

.getProject(options) ⇒ Object



34
35
36
37
38
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 34

def self.getProject(options)
  project = Xcodeproj::Project.open(self.getProjectPath(options))

  project
end

.getProjectPath(options) ⇒ Object

class methods that you define here become available in your action as ‘Helper::LocalizeHelper.your_method`



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 12

def self.getProjectPath(options)
  projectPath = nil

  unless options.nil?
    projectPath = options[:localize_project]
  end

  if projectPath.nil?
    projectPath = ENV["PROJECT_FILE_PATH"]
  end

  if projectPath.nil?
    projectPath = Dir.entries(".").select { |f| f.end_with?(".xcodeproj") }.first
  end

  if projectPath.nil?
    fail "no xcodeproject found"
  end

  projectPath
end

.getTarget(options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 64

def self.getTarget(options)
  project = self.getProject(options)
  targetName = self.getTargetName(options)

  target = project.targets.select { |target| target.name.eql? targetName }.first

  if target.nil?
    fail "no target"
  end

  target
end

.getTargetName(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 40

def self.getTargetName(options)
  project = self.getProject(options)

  targetName = nil

  unless options.nil?
    targetName = options[:localize_target]
  end

  if targetName.nil?
    targetName = ENV["TARGET_NAME"]
  end

  if targetName.nil?
    targetName = project.targets.map { |target| target.name.to_s }.first
  end

  if targetName.nil?
    fail "no target found"
  end

  targetName
end

.getWhitelistObject



133
134
135
136
137
138
139
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 133

def self.getWhitelist
  if File.exist?(whitelistFilename)
    return File.readlines(whitelistFilename).map(&:strip).map { |s| s.gsub(/^\\"/, "\"").gsub(/\\"$/, "\"") }
  else
    return []
  end
end

.localize_string(string, key, files, params) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 141

def self.localize_string(string, key, files, params)
  open(params[:strings_file], "a") { |f|
    f.puts "\"#{key}\" = #{string};"
  }
  files.each do |file_name|
    text = File.read(file_name)

    new_contents = text.gsub(string, replacementString(key, params))

    File.open(file_name, "w") { |file| file.puts new_contents }
  end

  UI.message "Replaced \"#{string}\" with \"#{replacementString(key, params)}\""
end

.replacementString(key, params) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 156

def self.replacementString(key, params)
  if params[:use_swiftgen]
    swiftgenKey = key.gsub(".", "_").split("_").inject { |m, p|
      letters = p.split("")
      letters.first.upcase!
      p2 = letters.join
      m + p2
    }

    return "L10n.#{swiftgenKey}"
  else
    return "NSLocalizedString(\"#{key}\")"
  end
end

.showStringOccurrencesFromFile(file, string) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 111

def self.showStringOccurrencesFromFile(file, string)
  line_array = File.readlines(file)

  File.open(file).each_with_index { |line, index|
    if line =~ /(?<!\#imageLiteral\(resourceName:|\#imageLiteral\(resourceName: |NSLocalizedString\()#{Regexp.escape(string)}/
      hits = line_array[index - 2..index - 1] + [line_array[index].green] + line_array[index + 1..index + 2]
      UI.message "In file #{file} (line #{index}): \n\n#{hits.join()}"
    end
  }
end

.stringsFromFile(file) ⇒ Object



105
106
107
108
109
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 105

def self.stringsFromFile(file)
  strings = File.readlines(file).to_s.enum_for(:scan,
                                               /(?<!\#imageLiteral\(resourceName:|\#imageLiteral\(resourceName: |NSLocalizedString\()\\"[^\\"\r\n]*\\"/).flat_map { Regexp.last_match }
  return strings
end

.whitelistFilenameObject



122
123
124
# File 'lib/fastlane/plugin/localize/helper/localize_helper.rb', line 122

def self.whitelistFilename
  "fastlane/.fastlane_localization_whitelist"
end