fastlane-plugin-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
+ SHA1:
3
+ metadata.gz: b9abc2c961cdbb0dfb43d435da11531ce84445d6
4
+ data.tar.gz: 7e560ea153fb1b19fd10da4c506592e5c8de9513
5
+ SHA512:
6
+ metadata.gz: 374ea985d1bfe90ac4b9d3490979781657d3b41ae735af02e1d4a51f7e8ef9b1823868187ba475fc8aae3561de611662d8ee199b86b85135959bd748a8f1a285
7
+ data.tar.gz: 705afb57d00e194c0e957876069cfdb39f2fbb1aa29741d5fe38a1fd8bae01a547420706d57a35723ecc2f432507b2805adfc075a0cfa49c68b5ee2783d70660
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Wolfgang Lutz <wlut@num42.de>
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
+ # 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-localize)
4
+
5
+ ## Getting Started
6
+
7
+ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-localize`, add it to your project by running:
8
+
9
+ ```bash
10
+ fastlane add_plugin localize
11
+ ```
12
+
13
+ ## About localize
14
+
15
+ Searches the code for extractable strings and allows interactive extraction to .strings file.
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,16 @@
1
+ require 'fastlane/plugin/localize/version'
2
+
3
+ module Fastlane
4
+ module Localize
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::Localize.all_classes.each do |current|
15
+ require current
16
+ end
@@ -0,0 +1,102 @@
1
+ require 'fastlane/action'
2
+ require_relative '../helper/localize_helper'
3
+
4
+ module Fastlane
5
+ module Actions
6
+ class LocalizeAction < Action
7
+ def self.run(params)
8
+ project = Helper::LocalizeHelper.getProject(params)
9
+ target = Helper::LocalizeHelper.getTarget(project)
10
+
11
+ files = Helper::LocalizeHelper.codeFiles(target, params)
12
+
13
+ matches = files.flat_map do |file|
14
+ Helper::LocalizeHelper.stringsFromFile(file)
15
+ end
16
+
17
+
18
+ matchHash = {}
19
+
20
+ matches.each do |match|
21
+ matchHash[match[0]] = [] unless matchHash[match[0]] != nil
22
+ matchHash[match[0]] << match
23
+ end
24
+
25
+
26
+ whitelist = Helper::LocalizeHelper.getWhitelist
27
+
28
+ matchHash.keys
29
+ .map { |match|
30
+ match.gsub(/^\\"/, "\"").gsub(/\\"$/, "\"")
31
+ }
32
+ .reject { |match|
33
+ whitelist.include? match
34
+ }
35
+ .each { |match|
36
+
37
+ files.flat_map do |file|
38
+ Helper::LocalizeHelper.showStringOccurrencesFromFile(file, match)
39
+ end
40
+
41
+ if UI.confirm "Extract #{match}?"
42
+ key = UI.input "Enter key for localization:"
43
+ Helper::LocalizeHelper.localize_string(match, key, files, params)
44
+ else
45
+ Helper::LocalizeHelper.addToWhitelist(match)
46
+ end
47
+ }
48
+ end
49
+
50
+ def self.description
51
+ "Searches the code for extractable strings and allows interactive extraction to .strings file."
52
+ end
53
+
54
+ def self.authors
55
+ ["Wolfgang Lutz"]
56
+ end
57
+
58
+ def self.return_value
59
+ end
60
+
61
+ def self.details
62
+ "Searches the code for extractable strings and allows interactive extraction to .strings file:"
63
+ "- Whitelists non-extracted strings"
64
+ "- Support for NSLocalizedString and Swiftgen"
65
+ end
66
+
67
+ def self.available_options
68
+ [
69
+ FastlaneCore::ConfigItem.new(key: :localize_project,
70
+ env_name: "FL_LOCALIZE_PROJECT", # The name of the environment variable
71
+ description: "The project to localize", # a short description of this parameter
72
+ is_string: true, # true: verifies the input is a string, false: every kind of value
73
+ default_value: Helper::LocalizeHelper.getProject(nil).path # the default value if the user didn't provide one
74
+ ),
75
+ FastlaneCore::ConfigItem.new(key: :use_swiftgen,
76
+ env_name: "FL_LOCALIZE_USE_SWIFTGEN", # The name of the environment variable
77
+ description: "Localize using Swiftgens L10n ", # a short description of this parameter
78
+ is_string: false, # true: verifies the input is a string, false: every kind of value
79
+ default_value: false # the default value if the user didn't provide one
80
+ ),
81
+ FastlaneCore::ConfigItem.new(key: :strings_file,
82
+ env_name: "FL_LOCALIZE_STRINGS_FILE",
83
+ description: "The stringsfile to write to",
84
+ is_string: true, # true: verifies the input is a string, false: every kind of value
85
+ default_value: "Localizable.strings" # the default value if the user didn't provide one,
86
+ ),
87
+ FastlaneCore::ConfigItem.new(key: :file_filter,
88
+ env_name: "FL_LOCALIZE_FILE_FILTER",
89
+ description: "Filter strings for file paths to ignore, separated by commas",
90
+ optional: true,
91
+ is_string: false
92
+ )
93
+ ]
94
+ end
95
+
96
+ def self.is_supported?(platform)
97
+ [:ios, :mac].include?(platform)
98
+ true
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,150 @@
1
+ require 'fastlane_core/ui/ui'
2
+
3
+ module Fastlane
4
+ UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
5
+
6
+ module Helper
7
+ class LocalizeHelper
8
+ # class methods that you define here become available in your action
9
+ # as `Helper::LocalizeHelper.your_method`
10
+ #
11
+
12
+
13
+ def self.getProject(options)
14
+ projectPath = nil
15
+
16
+ unless options.nil?
17
+ projectPath = options[:localize_project]
18
+ end
19
+
20
+ if projectPath.nil?
21
+ projectPath = ENV["PROJECT_FILE_PATH"]
22
+ end
23
+
24
+ if projectPath.nil?
25
+ projectPath = Dir.entries('.').select { |f| f.end_with?(".xcodeproj") }.first
26
+ end
27
+
28
+ if projectPath.nil?
29
+ fail "no xcodeproject found"
30
+ end
31
+
32
+ project = Xcodeproj::Project.open(projectPath)
33
+
34
+ project
35
+ end
36
+
37
+ def self.getTarget(project)
38
+ targetName = nil
39
+
40
+ # if options[:localize_target].nil?
41
+ # targetName = ENV["TARGETNAME"]
42
+ # end
43
+
44
+ if targetName.nil?
45
+ targetName = project.targets.first.name
46
+ end
47
+
48
+ if targetName.nil?
49
+ fail "no target found"
50
+ end
51
+
52
+ target = project.targets.select { |target| target.name.eql? targetName }.first
53
+
54
+ if target.nil?
55
+ fail 'no target'
56
+ end
57
+
58
+ target
59
+ end
60
+
61
+ def self.codeFiles(target, options)
62
+
63
+ filter = options[:file_filter].split(",")
64
+
65
+ codeFiles = target.source_build_phase.files.to_a.map do |pbx_build_file|
66
+ pbx_build_file.file_ref.real_path.to_s
67
+ end.select do |path|
68
+ path.end_with?(".swift")
69
+ end.select do |path|
70
+ bool = true
71
+
72
+ filter.each { |filter|
73
+ if path.include? filter
74
+ bool = false
75
+ end
76
+ }
77
+
78
+ next bool
79
+ end.select do |path|
80
+ File.exists?(path)
81
+ end
82
+
83
+ codeFiles
84
+ end
85
+
86
+ def self.stringsFromFile(file)
87
+
88
+ strings = File.readlines(file).to_s.enum_for(:scan,
89
+ /(?<!\#imageLiteral\(resourceName:|\#imageLiteral\(resourceName: |NSLocalizedString\()\\"[^\\"\r\n]*\\"/
90
+ ).flat_map {Regexp.last_match}
91
+ return strings
92
+ end
93
+
94
+ def self.showStringOccurrencesFromFile(file, string)
95
+
96
+ line_array = File.readlines(file)
97
+
98
+ File.open(file).each_with_index { |line, index|
99
+ if line =~ /(?<!\#imageLiteral\(resourceName:|\#imageLiteral\(resourceName: |NSLocalizedString\()#{string}/
100
+ then
101
+ hits = line_array[index-2 .. index-1] + [line_array[index].green] + line_array[index+1 .. index+2]
102
+ UI.message "In file #{file} (line #{index}): \n\n#{hits.join()}"
103
+ end
104
+ }
105
+ end
106
+
107
+ def self.whitelistFilename
108
+ "fastlane/.fastlane_localization_whitelist"
109
+ end
110
+
111
+ def self.addToWhitelist(string)
112
+ open(whitelistFilename, 'a') { |f|
113
+ f.puts string
114
+ }
115
+ UI.message "Added \"#{string}\" to #{whitelistFilename}"
116
+ end
117
+
118
+ def self.getWhitelist
119
+ if File.exist?(whitelistFilename)
120
+ return File.readlines(whitelistFilename).map(&:strip).map { |s| s.gsub(/^\\"/, "\"").gsub(/\\"$/, "\"") }
121
+ else
122
+ return []
123
+ end
124
+ end
125
+
126
+ def self.localize_string(string, key, files, params)
127
+ open(params[:strings_file], 'a') { |f|
128
+ f.puts "\"#{key}\" = #{string};"
129
+ }
130
+ files.each do |file_name|
131
+ text = File.read(file_name)
132
+
133
+ new_contents = text.gsub(string, replacementString(key, params))
134
+
135
+ File.open(file_name, "w") {|file| file.puts new_contents }
136
+ end
137
+
138
+ UI.message "Replaced \"#{string}\" with \"#{replacementString(key, params)}\""
139
+ end
140
+
141
+ def self.replacementString(key, params)
142
+ if params[:use_swiftgen]
143
+ return "L10n.#{key.gsub("." , "_").split("_").inject { |m, p| m + p.capitalize }}"
144
+ else
145
+ return "NSLocalizedString(\"#{key}\")"
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,5 @@
1
+ module Fastlane
2
+ module Localize
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlane-plugin-localize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wolfgang Lutz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-19 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.81.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.81.0
139
+ description:
140
+ email: wlut@num42.de
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - LICENSE
146
+ - README.md
147
+ - lib/fastlane/plugin/localize.rb
148
+ - lib/fastlane/plugin/localize/actions/localize_action.rb
149
+ - lib/fastlane/plugin/localize/helper/localize_helper.rb
150
+ - lib/fastlane/plugin/localize/version.rb
151
+ homepage: https://github.com/num42/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.5.2.1
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Searches the code for extractable strings and allows interactive extraction
175
+ to .strings file.
176
+ test_files: []