fastlane-plugin-google_sheet_localize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa84006d7058d1cc9649efb0b0c8f7c540976c22621c9e07c5d1d9d3aa87d5df
4
+ data.tar.gz: 9ebb44a052366f2274aaf1434063b0136f2b56e6c36f9a3527258a2bdcf4d893
5
+ SHA512:
6
+ metadata.gz: f8079ccd415ce7c7277251915966b7a5da501b9e2619f1dad4c8c777a234de0ca9543b88b6aa8597bc51fa6d816e7086c8a5119a8804f1a4015b5aa253bb11f5
7
+ data.tar.gz: 3bd3dd582afe25d12b26a6c34ea72c3f3362323debd7c5eca0045f4d01e57b9284f5def274260a5e5f519bfdab23dbdfcc5a199b55f761e7c6386ca3155ef141
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Mario Hahn <mario_hahn@me.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # google_sheet_localize plugin
2
+
3
+ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-google_sheet_localize)
4
+
5
+ ## Getting Started
6
+
7
+ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-google_sheet_localize`, add it to your project by running:
8
+
9
+ ```bash
10
+ fastlane add_plugin google_sheet_localize
11
+ ```
12
+
13
+ ## About google_sheet_localize
14
+
15
+ Creates .strings files for iOS and strings.xml files for Android
16
+
17
+ **Note to author:** Add a more detailed description about this plugin here. If your plugin contains multiple actions, make sure to mention them here.
18
+
19
+ ## Example
20
+
21
+ Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin. Try it by cloning the repo, running `fastlane install_plugins` and `bundle exec fastlane test`.
22
+
23
+ **Note to author:** Please set up a sample project to make it easy for users to explore what your plugin does. Provide everything that is necessary to try out the plugin in this project (including a sample Xcode/Android project if necessary)
24
+
25
+ ## Run tests for this plugin
26
+
27
+ To run both the tests, and code style validation, run
28
+
29
+ ```
30
+ rake
31
+ ```
32
+
33
+ To automatically fix many of the styling issues, use
34
+ ```
35
+ rubocop -a
36
+ ```
37
+
38
+ ## Issues and Feedback
39
+
40
+ For any other issues and feedback about this plugin, please submit it to this repository.
41
+
42
+ ## Troubleshooting
43
+
44
+ If you have trouble using plugins, check out the [Plugins Troubleshooting](https://docs.fastlane.tools/plugins/plugins-troubleshooting/) guide.
45
+
46
+ ## Using _fastlane_ Plugins
47
+
48
+ For more information about how the `fastlane` plugin system works, check out the [Plugins documentation](https://docs.fastlane.tools/plugins/create-plugin/).
49
+
50
+ ## About _fastlane_
51
+
52
+ _fastlane_ is the easiest way to automate beta deployments and releases for your iOS and Android apps. To learn more, check out [fastlane.tools](https://fastlane.tools).
@@ -0,0 +1,295 @@
1
+ require 'fastlane/action'
2
+ require_relative '../helper/google_sheet_localize_helper'
3
+
4
+ module Fastlane
5
+ module Actions
6
+ class GoogleSheetLocalizeAction < Action
7
+ def self.run(params)
8
+
9
+ session = GoogleDrive::Session.from_service_account_key(params[:service_account_path])
10
+ spreadsheet_id = "https://docs.google.com/spreadsheets/d/#{params[:sheet_id]}"
11
+ tabs = params[:tabs]
12
+ plaform = params[:platform]
13
+
14
+ spreadsheet = session.spreadsheet_by_url(spreadsheet_id)
15
+
16
+ # Get the first worksheet
17
+ worksheet = spreadsheet.worksheets.first
18
+
19
+ languages = numberOfLanguages(worksheet)
20
+
21
+ result = []
22
+
23
+ for i in 2..1+languages
24
+ title = worksheet.rows[0][i]
25
+
26
+ language = {
27
+ 'language' => title,
28
+ 'items' => []
29
+ }
30
+
31
+ filterdWorksheets = []
32
+
33
+ if tabs.count == 0
34
+ filterdWorksheets = spreadsheet.worksheets
35
+ else
36
+ filterdWorksheets = spreadsheet.worksheets.select { |item| tabs.include?(item.title) }
37
+ end
38
+
39
+ filterdWorksheets.each { |worksheet|
40
+ contentRows = worksheet.rows.drop(1)
41
+ language['items'].concat(generateJSONObject(contentRows, i))
42
+ }
43
+
44
+ result.push(language)
45
+
46
+ end
47
+ createFiles(result, platform, path)
48
+ end
49
+
50
+ def generateJSONObject(contentRows, index)
51
+ result = Array.new
52
+ for i in 0..contentRows.count - 1
53
+ item = generateSingleObject(contentRows[i], index)
54
+
55
+ if item[:identifierIos] != "" && item[:identifierAndroid] != ""
56
+ result.push(item)
57
+ end
58
+ end
59
+
60
+ return result
61
+
62
+ end
63
+
64
+ def writeToJSONFile(languages)
65
+ File.open("output.json","w") do |f|
66
+ f.write(JSON.pretty_generate(languages))
67
+ end
68
+ end
69
+
70
+ def generateSingleObject(row, column)
71
+ identifierIos = row[0]
72
+ identifierAndroid = row[1]
73
+
74
+ text = row[column]
75
+ comment = row.last
76
+
77
+ object = { 'identifierIos' => identifierIos,
78
+ 'identifierAndroid' => identifierAndroid,
79
+ 'text' => text,
80
+ 'comment' => comment
81
+ }
82
+
83
+ return object
84
+
85
+ end
86
+
87
+ def createFiles(languages, platform, destinationPath)
88
+ languages.each { |language| createFileForLanguage(language, platform, destinationPath) }
89
+
90
+ if platform == "ios"
91
+
92
+ swiftFilename = "Localization.swift"
93
+ swiftFilepath = "#{destinationPath}/#{swiftFilename}"
94
+
95
+ #removes all identififiers with the label Not Required
96
+ filteredItems = languages[0]["items"].select { |item| item['identifierIos'] != "NR" }
97
+
98
+ File.open(swiftFilepath, "w") do |f|
99
+ f.write("import Foundation\n\n\npublic struct Localization {\n")
100
+ filteredItems.each { |item|
101
+
102
+ identifier = item['identifierIos']
103
+
104
+ values = identifier.dup.gsub('.', ' ').split(" ")
105
+
106
+ constantName = ""
107
+
108
+ values.each_with_index do |item, index|
109
+ if index == 0
110
+ constantName += item.downcase
111
+ else
112
+ constantName += item.capitalize
113
+ end
114
+ end
115
+
116
+ text = mapInvalidPlaceholder(item['text'])
117
+
118
+ arguments = findArgumentsInText(text)
119
+
120
+ puts "Arguments"
121
+ puts arguments.count
122
+
123
+ if arguments.count == 0
124
+ f.write("\n\t///Sheet comment: #{item['comment']}\n\tpublic static let #{constantName} = localized(identifier: \"#{identifier}\")\n")
125
+ else
126
+ f.write(createiOSFunction(constantName, identifier, arguments, item['comment']))
127
+ end
128
+ }
129
+ f.write("\n}")
130
+ f.write(createiOSFileEndString())
131
+ end
132
+
133
+ end
134
+ end
135
+
136
+ def createFileForLanguage(language, platform, destinationPath)
137
+ if platform == "ios"
138
+
139
+ swiftFilename = "Localization.swift"
140
+ swiftFilepath = "#{destinationPath}/#{swiftFilename}"
141
+
142
+ #removes all identififiers with the label Not Required
143
+ filteredItems = language["items"].select { |item| item['identifierIos'] != "NR" }
144
+
145
+ filename = "Localizable.strings"
146
+ filepath = "#{destinationPath}/#{language['language']}.lproj/#{filename}"
147
+ FileUtils.mkdir_p language['language']
148
+ File.open(filepath, "w") do |f|
149
+ filteredItems.each { |item|
150
+
151
+ text = mapInvalidPlaceholder(item['text'])
152
+
153
+ f.write("//#{item['comment']}\n\"#{item['identifierIos']}\" = \"#{text}\";\n")
154
+ }
155
+ end
156
+ end
157
+
158
+ if platform == "android"
159
+ FileUtils.mkdir_p "values-#{language['language']}"
160
+ File.open("values-#{language['language']}/strings.xml", "w") do |f|
161
+ f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
162
+ f.write("<resources>\n")
163
+ language["items"].each { |item|
164
+ f.write("\t<!--#{item['comment']}-->\n\t<string name=\"#{item['identifierAndroid']}\"><![CDATA[#{item['text']}]]></string>\n")
165
+ }
166
+ f.write("</resources>\n")
167
+ end
168
+ end
169
+ end
170
+
171
+ def createiOSFileEndString()
172
+ return "\n\nextension Localization {\n\tprivate static func localized(identifier key: String, _ args: CVarArg...) -> String {\n\t\tlet format = NSLocalizedString(key, tableName: nil, bundle: Bundle.main, comment: \"\")\n\n\t\tguard !args.isEmpty else { return format }\n\n\t\treturn String(format: format, locale: Locale.current, arguments: args)\n\t}\n}"
173
+ end
174
+
175
+ def createiOSFunction(constantName, identifier, arguments, comment)
176
+ functionTitle = "\n\t///Sheet comment: #{comment}\n\tpublic static func #{constantName}("
177
+
178
+ arguments.each_with_index do |item, index|
179
+ puts item
180
+ functionTitle = functionTitle + "_ arg#{index}: #{item[:type]}"
181
+ if index < arguments.count - 1
182
+ functionTitle = functionTitle + ", "
183
+ else
184
+ functionTitle = functionTitle + ") -> String {\n"
185
+ end
186
+ end
187
+ functionTitle = functionTitle + "\t\treturn localized(identifier: \"#{identifier}\", "
188
+ arguments.each_with_index do |item, index|
189
+ functionTitle = functionTitle + "arg#{index}"
190
+ if index < arguments.count - 1
191
+ functionTitle = functionTitle + ", "
192
+ else
193
+ functionTitle = functionTitle + ")\n\t}"
194
+ end
195
+ end
196
+
197
+ return functionTitle
198
+ end
199
+
200
+ def findArgumentsInText(text)
201
+ result = Array.new
202
+ filtered = mapInvalidPlaceholder(text)
203
+
204
+ puts filtered
205
+
206
+ stringIndexes = (0 ... filtered.length).find_all { |i| filtered[i,2] == '%@' }
207
+ intIndexes = (0 ... filtered.length).find_all { |i| filtered[i,2] == '%d' }
208
+ floatIndexes = (0 ... filtered.length).find_all { |i| filtered[i,2] == '%f' }
209
+ doubleIndexes = (0 ... filtered.length).find_all { |i| filtered[i,3] == '%ld' }
210
+
211
+ if stringIndexes.count > 0
212
+ result = result.concat(stringIndexes.map { |e| { "index": e, "type": "String" }})
213
+ end
214
+
215
+ if intIndexes.count > 0
216
+ result = result.concat(intIndexes.map { |e| { "index": e, "type": "Int" }})
217
+ end
218
+
219
+ if floatIndexes.count > 0
220
+ result = result.concat(floatIndexes.map { |e| { "index": e, "type": "Float" }})
221
+ end
222
+
223
+ if doubleIndexes.count > 0
224
+ result = result.concat(doubleIndexes.map { |e| { "index": e, "type": "Double" }})
225
+ end
226
+
227
+ puts result
228
+ result = result.sort { |a, b| a[:index] <=> b[:index] }
229
+ puts result
230
+
231
+ puts "count"
232
+ puts result.count
233
+
234
+
235
+ return result
236
+ end
237
+
238
+ def mapInvalidPlaceholder(text)
239
+ filtered = text.gsub('%s', '%@')
240
+ return filtered
241
+ end
242
+
243
+ def numberOfLanguages(worksheet)
244
+ i = worksheet.num_cols
245
+ return i - 3
246
+ end
247
+
248
+ def self.description
249
+ "Creates .strings files for iOS and strings.xml files for Android"
250
+ end
251
+
252
+ def self.authors
253
+ ["Mario Hahn"]
254
+ end
255
+
256
+ def self.return_value
257
+ # If your method provides a return value, you can describe here what it does
258
+ end
259
+
260
+ def self.details
261
+ # Optional:
262
+ "Creates .strings files for iOS and strings.xml files for Android. The localization is mananged on a google sheet."
263
+ end
264
+
265
+ def self.available_options
266
+ [
267
+ FastlaneCore::ConfigItem.new(key: :service_account_path,
268
+ env_name: "SERVICE_ACCOUNT_PATH",
269
+ description: "Credentials path",
270
+ optional: false,
271
+ type: String),
272
+ FastlaneCore::ConfigItem.new(key: :sheet_id,
273
+ env_name: "SHEET_ID",
274
+ description: "Your Google-spreadsheet id",
275
+ optional: false,
276
+ type: String),
277
+ FastlaneCore::ConfigItem.new(key: :platform,
278
+ env_name: "PLATFORM",
279
+ description: "Plaform, ios or android",
280
+ optional: "ios",
281
+ type: String),
282
+ FastlaneCore::ConfigItem.new(key: :tabs,
283
+ env_name: "TABS",
284
+ description: "Array of all Google Sheet Tabs",
285
+ optional: false,
286
+ type: Array)
287
+ ]
288
+ end
289
+
290
+ def self.is_supported?(platform)
291
+ [:ios, :mac, :android].include?(platform)
292
+ end
293
+ end
294
+ end
295
+ end
@@ -0,0 +1,11 @@
1
+ require 'fastlane_core/ui/ui'
2
+ require 'google_drive'
3
+
4
+ module Fastlane
5
+ UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
6
+
7
+ module Helper
8
+ class GoogleSheetLocalizeHelper
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Fastlane
2
+ module GoogleSheetLocalize
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'fastlane/plugin/google_sheet_localize/version'
2
+
3
+ module Fastlane
4
+ module GoogleSheetLocalize
5
+ # Return all .rb files inside the "actions" and "helper" directory
6
+ def self.all_classes
7
+ Dir[File.expand_path('**/{actions,helper}/*.rb', File.dirname(__FILE__))]
8
+ end
9
+ end
10
+ end
11
+
12
+ # By default we want to import all available actions and helpers
13
+ # A plugin can contain any number of actions and plugins
14
+ Fastlane::GoogleSheetLocalize.all_classes.each do |current|
15
+ require current
16
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlane-plugin-google_sheet_localize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mario Hahn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.49.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.49.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-require_tools
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: fastlane
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.111.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 2.111.0
139
+ description:
140
+ email: mario_hahn@me.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - LICENSE
146
+ - README.md
147
+ - lib/fastlane/plugin/google_sheet_localize.rb
148
+ - lib/fastlane/plugin/google_sheet_localize/actions/google_sheet_localize_action.rb
149
+ - lib/fastlane/plugin/google_sheet_localize/helper/google_sheet_localize_helper.rb
150
+ - lib/fastlane/plugin/google_sheet_localize/version.rb
151
+ homepage: https://github.com/tailoredmedia/fastlane-plugin-localize
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.7.7
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Creates .strings files for iOS and strings.xml files for Android
175
+ test_files: []