lokale 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: 36f4dfe95a8d5ab359a7e8e8533a0870316bbb6b
4
+ data.tar.gz: 3d3fa77a8ab9dbef6d5a1b65f2ef06b0e6ba83f8
5
+ SHA512:
6
+ metadata.gz: 329c3243dd0410f237bb55a650f102c10d132cf37089f6f177b51894604fd43dc62a8fe5535240f0d3f6aa9962a1ee0069f9b7bd022920e2a1d19b7813097328
7
+ data.tar.gz: 95eb3e99e7a0356f8efce8b51f78c37453f41dd6f3f6e786009344293247b83a4909fcc9698789a67d63414bcac80160a97766f84870f332dd4c73beeff54d73
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lokale.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Lokale
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lokale`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lokale'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lokale
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lokale.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lokale"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/lokale ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lokale'
4
+
5
+ macros = [
6
+ Lokale::Macro.new("NSLocalizedString", /NSLocalizedString\("(.+?)",\s*comment:\s*"(.*?)"\)/),
7
+ Lokale::Macro.new("PluralString", /String.localizedPlural\("(.+?)"/),
8
+ #LocalizationMacro.new("ObjC String", /NSLocalizedString\("(.*)",\s*(.*)\)/),
9
+ #LocalizationMacro.new("ObjC Table String", /NSLocalizedStringFromTableInBundle\((.*?),/)
10
+ ]
11
+
12
+ puts Dir.pwd
13
+ agent = Lokale::Agent.new(Dir.pwd, macros)
14
+ reporter = Lokale::Reporter.new(agent)
15
+ reporter.print_summary
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Lokale
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lokale.rb ADDED
@@ -0,0 +1,280 @@
1
+ # require "lokale/version"
2
+ require "set"
3
+
4
+ class String
5
+ def rpadded(count=20)
6
+ "%-#{count}.#{count}s" % self
7
+ end
8
+
9
+ def lpadded(count=20)
10
+ "%#{count}.#{count}s" % self
11
+ end
12
+
13
+ def localization_file?
14
+ File.directory?(self) == false &&
15
+ (self =~ /\/Pods\//) == nil &&
16
+ (self =~ /\.bundle\//) == nil &&
17
+ (self =~ /\/(.{1,8})\.lproj\//)
18
+ end
19
+
20
+ def source_file?
21
+ (File.directory?(self) == false) && (self =~ /\/Pods\//).nil? && ((self =~ /\.(swift|h|m)$/) != nil)
22
+ end
23
+ end
24
+
25
+ module Lokale
26
+
27
+ class LocalizedString
28
+ attr_accessor :key, :str, :note, :target
29
+
30
+ def initialize(key, str, note, target)
31
+ @key = key; @str = str; @note = note; @target = target
32
+ end
33
+
34
+ def self.strings_from_file(file_path, lang)
35
+ regex = /(?:\/* (.+) *\/.*\n)?"(.+)" *= *"(.+)";/
36
+ File.read(file_path).scan(regex).map { |m| LocalizedString.new(m[1], m[2], m[0], lang) }
37
+ end
38
+ end
39
+
40
+ #
41
+
42
+ class LocalizationFile
43
+ attr_reader :path, :lang, :name, :type
44
+ def initialize(file_path)
45
+ @path = file_path
46
+
47
+ File.basename(file_path) =~ /^(.+)\.([^\.]*?)$/
48
+ @name = $1
49
+ @type = $2
50
+
51
+ file_path =~ /\/(.{1,8})\.lproj\//
52
+ @lang = $1
53
+ end
54
+
55
+ def self.try_to_read(file_path)
56
+ return nil unless file_path.localization_file?
57
+ LocalizationFile.new(file_path)
58
+ end
59
+
60
+ def inspect
61
+ "<#{@lang}/#{full_name}>"
62
+ end
63
+
64
+ def full_name
65
+ "#{@name}.#{@type}"
66
+ end
67
+
68
+ def parsed
69
+ return @parsed unless @parsed.nil?
70
+ @parsed = case type
71
+ when "strings" then LocalizedString.strings_from_file(@path, @lang)
72
+ when "stringsdict" then []
73
+ else nil
74
+ end
75
+ end
76
+
77
+ def repeats
78
+ return [] if @parsed.nil?
79
+ keys = @parsed.map(&:key)
80
+ uniq_keys = keys.to_set
81
+
82
+ keys.delete_if do |k|
83
+ if uniq_keys.include? k
84
+ uniq_keys.delete k
85
+ true
86
+ end
87
+ end
88
+ end
89
+
90
+ def keys
91
+ return nil if parsed.nil?
92
+ parsed.map(&:key)
93
+ end
94
+ end
95
+
96
+ #
97
+
98
+ class Macro
99
+ attr_reader :regex, :found_strings, :name
100
+
101
+ def initialize(name, regex)
102
+ @name = name
103
+ @regex = regex
104
+ clear_calls
105
+ end
106
+
107
+ def clear_calls
108
+ @found_strings = Hash.new { |h, k| h[k] = 0 }
109
+ end
110
+
111
+ def read_from(file)
112
+ file.scan(@regex) { |m| @found_strings[m] += 1 }
113
+ end
114
+
115
+ def uniq_count
116
+ @found_strings.size
117
+ end
118
+
119
+ def total_count
120
+ @found_strings.values.reduce(:+) || 0
121
+ end
122
+ end
123
+
124
+
125
+ #
126
+ #
127
+ #
128
+ #
129
+
130
+ class Agent
131
+ attr_reader :proj_path, :macros, :lfiles, :sfiles_proceeded
132
+
133
+ def initialize(proj_path, macros)
134
+ @proj_path = proj_path
135
+ @macros = macros
136
+
137
+ get_localization_files
138
+ find_all_localization_calls
139
+ end
140
+
141
+ def proj_files
142
+ Dir.glob("#{@proj_path}/**/**")
143
+ end
144
+
145
+ def get_localization_files
146
+ return @lfiles unless @lfiles.nil?
147
+ @lfiles = proj_files.map { |file| LocalizationFile.try_to_read(file) }.compact
148
+ end
149
+
150
+ def find_all_localization_calls
151
+ @macros.each { |m| m.clear_calls }
152
+
153
+ @sfiles_proceeded = 0
154
+ proj_files.each do |file|
155
+ next unless file.source_file?
156
+
157
+ file_content = File.read(file)
158
+ @macros.each { |macro| macro.read_from file_content }
159
+ @sfiles_proceeded += 1
160
+ end
161
+ end
162
+ end
163
+
164
+ #
165
+
166
+ class Reporter
167
+ def initialize(agent)
168
+ @agent = agent
169
+ end
170
+
171
+ def print_summary
172
+ print_macro_calls_summary
173
+ print_macro_table
174
+ print_files_table
175
+ print_repeats_report
176
+ print_diferences_report
177
+ end
178
+
179
+ def print_macro_calls_summary
180
+ total_macro_calls = @agent.macros.map(&:total_count).reduce(:+)
181
+ uniq_macro_calls = @agent.macros.map(&:uniq_count).reduce(:+)
182
+ puts "Found #{total_macro_calls} localization macro calls in #{@agent.sfiles_proceeded} files."
183
+ puts "Uniq macro calls: #{uniq_macro_calls}"
184
+ puts
185
+ end
186
+
187
+
188
+ def print_files_table
189
+ languages = @agent.lfiles.map { |e| e.lang }.to_set.to_a
190
+ files = @agent.lfiles.map { |e| e.full_name }.to_set.to_a
191
+
192
+ puts "Found #{@agent.lfiles.size} localized files for #{languages.size} languages."
193
+
194
+ description_header = "[*]".rpadded(36)
195
+ languages.each { |l| description_header << l.rpadded(8) }
196
+ puts description_header
197
+
198
+ files.each do |f|
199
+ description_string = f.rpadded(36)
200
+ languages.each do |l|
201
+ lfile = @agent.lfiles.select { |lf| lf.full_name == f && lf.lang == l }
202
+ description_string << (lfile.empty? ? "-" : lfile[0].parsed.nil? ? "*" : "#{lfile[0].parsed.size}").rpadded(8)
203
+ end
204
+ puts description_string
205
+ end
206
+ puts
207
+ end
208
+
209
+ def print_macro_table
210
+ @agent.macros.each do |macro|
211
+ puts "#{macro.name}:".rpadded(24) + "total: #{macro.total_count}".rpadded(16) + "uniq: #{macro.uniq_count}"
212
+ end
213
+ puts
214
+ end
215
+
216
+ def print_repeats_report
217
+ repeats_repot = ""
218
+ @agent.lfiles.each do |lf|
219
+ repeats = lf.repeats
220
+ next if repeats.count.zero?
221
+ repeats_repot << "#{lf.lang}/#{lf.full_name} repeats:\n"
222
+ repeats_repot << repeats.join("\n")
223
+ repeats_repot << "\n"
224
+ end
225
+
226
+ if repeats_repot.empty?
227
+ puts "Repeats not found."
228
+ puts
229
+ else
230
+ puts repeats_repot
231
+ puts
232
+ end
233
+ end
234
+
235
+ def print_diferences_report
236
+ diferences_repot = ""
237
+
238
+ @agent.lfiles.group_by { |f| f.full_name }.each do |file_name, files|
239
+ base_lang = files.select { |f| f.lang == "Base" }.first.nil? ? "en" : "Base"
240
+ files = files.select { |f| f.lang != base_lang }
241
+ all_keys = files.map(&:keys).compact.map(&:to_set)
242
+ next if all_keys.empty?
243
+ shared_keys = all_keys.reduce(:&)
244
+ all_keys.map! { |ks| ks - shared_keys }
245
+ next if all_keys.map(&:length).reduce(:+).zero?
246
+
247
+ diferences_repot << "Excess keys in file \"#{file_name}\":\n"
248
+ all_keys.zip(files) do |excess_keys, lfile|
249
+ next if excess_keys.size.zero?
250
+ diferences_repot << "* #{lfile.lang}: #{excess_keys.size} key(s).\n"
251
+ excess_keys.each { |k| diferences_repot << "#{k}\n" }
252
+ end
253
+ diferences_repot << "\n"
254
+ end
255
+
256
+ if diferences_repot.empty?
257
+ puts "Localization files are full."
258
+ puts
259
+ else
260
+ puts "Localization files are not full."
261
+ puts
262
+ puts diferences_repot
263
+ puts
264
+ end
265
+ end
266
+ end
267
+ end
268
+
269
+
270
+
271
+ # macros = [
272
+ # Lokale::Macro.new("NSLocalizedString", /NSLocalizedString\("(.+?)",\s*comment:\s*"(.*?)"\)/),
273
+ # Lokale::Macro.new("PluralString", /String.localizedPlural\("(.+?)"/),
274
+ # #LocalizationMacro.new("ObjC String", /NSLocalizedString\("(.*)",\s*(.*)\)/),
275
+ # #LocalizationMacro.new("ObjC Table String", /NSLocalizedStringFromTableInBundle\((.*?),/)
276
+ # ]
277
+
278
+ # agent = Lokale::Agent.new("/Users/crysberry/Documents/hand2hand", macros)
279
+ # reporter = Lokale::Reporter.new(agent)
280
+ # reporter.print_summary
data/lokale.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lokale/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lokale"
8
+ spec.version = Lokale::VERSION
9
+ spec.authors = ["Anton Onizhuk"]
10
+ spec.email = ["anton.onizhuk@gmail.com"]
11
+
12
+ spec.summary = %q{Lokale is a small tool that inspects localization of Xcode projects.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "bin"
30
+ spec.executables = ["lokale"]
31
+ spec.require_paths = ["lib"]
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lokale
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Onizhuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write a longer description or delete this line.
14
+ email:
15
+ - anton.onizhuk@gmail.com
16
+ executables:
17
+ - lokale
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/lokale
28
+ - bin/setup
29
+ - lib/lokale.rb
30
+ - lib/lokale/version.rb
31
+ - lokale.gemspec
32
+ homepage:
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.0.14.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Lokale is a small tool that inspects localization of Xcode projects.
56
+ test_files: []