locraft 1.6.3 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b133e3c4682a07fe867920b1563bf10138aaa46a
4
- data.tar.gz: 9e12e2fc6da98cebe44c12d292e6a0b07170de6c
3
+ metadata.gz: c3a26853e9771037414db3fbc19b3b7f66e5d7b1
4
+ data.tar.gz: 875eff16bec1b32a9aab847690b5d0938c25c924
5
5
  SHA512:
6
- metadata.gz: 3ba68b5cae89e6acc8853ec89e0b0ad6c8340fe375bda63f4acd6b612e6291cb6d5feff273cb7d1e0abdc5436ef9c64abd93d9e0f2e14ffd3d33e29e7f6e38b9
7
- data.tar.gz: 9b2a6f3e51c10469a501cf50a69187e76f74a00996507883a234c2d83084470758d7068c1b79940028c80d12326b828b498f85a65679ff8aa67c9bbe4859b419
6
+ metadata.gz: 7a38009a9f27c52a9282ba41b506b1705a45f3c700df9e3c767bd1b5bf8caa0403a22da0e13436c662dc40d1d1141786373f4ead5e9e20f4904e0faea85814a7
7
+ data.tar.gz: dd476e7d00b6774875b42eec7a1c6acce98baf22555436395cbe7486da41077fca2f4f8a3858cbc886cef4ab33d93fc7472c7b33b99a240890581f3fee787d59
@@ -1,5 +1,5 @@
1
- require_relative 'config'
2
- require_relative 'localization'
1
+ require_relative 'model/config'
2
+ require_relative 'model/localization'
3
3
  require 'csv'
4
4
 
5
5
  module Locraft
@@ -1,8 +1,5 @@
1
- require_relative 'config'
1
+ require_relative 'model/config'
2
2
  require_relative 'csv_parser'
3
- require_relative 'generators/info_plist_generator'
4
- require_relative 'generators/constants_generator'
5
- require_relative 'generators/strings_generator'
6
3
  require_relative 'google_drive_wrapper'
7
4
 
8
5
  module Locraft
@@ -19,28 +16,12 @@ module Locraft
19
16
  warn 'extract ERROR: no localizations given'
20
17
  else
21
18
  generate_strings(localizations_hash)
22
- generate_info_plist(localizations_hash)
23
- generate_constant(localizations_hash)
24
19
  end
25
20
  end
26
21
 
27
22
  def generate_strings(localizations_hash)
28
- return if @config.strings_destination.nil?
29
- StringsGenerator.new(@config, localizations_hash).generate
30
- end
31
-
32
- def generate_info_plist(localizations_hash)
33
- return if @config.info_plist_destination.nil?
34
- InfoPlistGenerator.new(@config, localizations_hash).generate
35
- end
36
-
37
- def generate_constant(localizations_hash)
38
- return if @config.macro_destination.nil?
39
- localizations = localizations_hash[@config.default_lang]
40
- if @config.dev_lang.equal? OBJC
41
- ObjcConstantsGenerator.new(@config, localizations).generate
42
- else
43
- SwiftConstantsGenerator.new(@config, localizations).generate
23
+ @config.strings_generators.each do |generator|
24
+ generator.generate(localizations_hash)
44
25
  end
45
26
  end
46
27
  end
@@ -1,19 +1,12 @@
1
- require_relative '../config'
1
+ require_relative '../model/config'
2
+ require_relative '../utils'
3
+ require_relative 'generator'
2
4
  require 'fileutils'
3
5
 
4
6
  module Locraft
5
- class ConstantsGenerator
7
+ class ConstantsGenerator < Generator
6
8
  include FileUtils
7
9
 
8
- def initialize(config, localizations)
9
- @config = config
10
- @localizations = localizations
11
- end
12
-
13
- def generate
14
- raise NotImplementedError
15
- end
16
-
17
10
  def write_file_content(file, content)
18
11
  File.open(file, 'w+') do |f|
19
12
  f.puts files_header + "\n"
@@ -25,8 +18,8 @@ module Locraft
25
18
  @config.dev_prefix + @config.macro_file
26
19
  end
27
20
 
28
- def constants_keys
29
- @localizations.select(&:valid?).map do |l|
21
+ def constants_keys_with(localizations)
22
+ localizations.select(&:valid?).map do |l|
30
23
  { key: l.key, const: constant_from(l.key) }
31
24
  end
32
25
  end
@@ -36,11 +29,7 @@ module Locraft
36
29
  end
37
30
 
38
31
  def files_header
39
- resource_file('macro_files_header.txt')
40
- end
41
-
42
- def resource_file(file)
43
- File.read(File.expand_path("../../resources/#{file}", __FILE__))
32
+ Utils.resource_file('macro_files_header.txt')
44
33
  end
45
34
  end
46
35
  end
@@ -0,0 +1,16 @@
1
+ module Locraft
2
+ class Generator
3
+ def initialize(config)
4
+ @config = config
5
+ end
6
+
7
+ def valid?
8
+ raise NotImplementedError
9
+ end
10
+
11
+ # @param [{ String => [Localization]}] _ # localizations hash like #{ 'Lang' => [array of localizations] }
12
+ def generate(_)
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+ end
@@ -1,32 +1,25 @@
1
- require_relative '../config'
2
- require_relative '../localization'
3
- require 'fileutils'
1
+ require_relative 'strings_generator'
4
2
 
5
3
  module Locraft
6
- class InfoPlistGenerator
7
- include FileUtils
4
+ class InfoPlistGenerator < StringsGenerator
5
+ def valid?
6
+ !@config.relative_plist_destination.nil?
7
+ end
8
+
9
+ def destination_dir_for(lang)
10
+ "#{@config.relative_plist_destination}/#{lang}.lproj"
11
+ end
8
12
 
9
- def initialize(config, localizations_hash)
10
- @config = config
11
- @localizations_hash = localizations_hash
13
+ def valid_localization?(l)
14
+ l.valid? && l.key =~ /^(NS|CF).*/
12
15
  end
13
16
 
14
- def generate
15
- @config.langs.each do |lang, val|
16
- localizations = @localizations_hash[lang]
17
- destination_dir = "#{@config.relative_plist_destination}/#{val}.lproj"
18
- file = "#{destination_dir}/#{@config.info_plist_file}"
19
- mkdir_p destination_dir unless Dir.exist?(destination_dir)
20
- File.open(file, 'w+') do |f|
21
- f.puts info_plist_with(localizations)
22
- end
23
- puts "info plist generated: [#{file}]"
24
- end
17
+ def strings_file
18
+ @config.info_plist_file
25
19
  end
26
20
 
27
- def info_plist_with(localizations)
28
- filter = /(NS|CF).*/
29
- localizations.select { |l| l.key =~ filter }.map(&:to_line).join("\n")
21
+ def broadcast_file_generation(f)
22
+ puts "info plist strings file generated: [#{f}]"
30
23
  end
31
24
  end
32
25
  end
@@ -5,23 +5,28 @@ module Locraft
5
5
  OBJC_CONST_TEMPLATE_H = 'extern NSString *const __nonnull %<const>s;'.freeze
6
6
  OBJC_CONST_TEMPLATE_M = 'NSString *const %<const>s = @"%<key>s";'.freeze
7
7
 
8
- def generate
8
+ def valid?
9
+ !@config.relative_macro_destination.nil?
10
+ end
11
+
12
+ def generate(localizations_hash)
13
+ localizations = localizations_hash[@config.default_lang]
9
14
  destination_dir = @config.relative_macro_destination
10
15
  mkdir_p destination_dir unless Dir.exist?(destination_dir)
11
- write_file_content("#{destination_dir}/#{files_name}.h", h_file)
12
- write_file_content("#{destination_dir}/#{files_name}.m", m_file)
16
+ write_file_content("#{destination_dir}/#{files_name}.h", h_file(localizations))
17
+ write_file_content("#{destination_dir}/#{files_name}.m", m_file(localizations))
13
18
  puts 'constants files was created for [objc] language'
14
19
  end
15
20
 
16
- def m_file
21
+ def m_file(localizations)
17
22
  content = %(#import "#{files_name}.h"\n\n)
18
- content += constants_keys.map { |d| OBJC_CONST_TEMPLATE_M % d }.join("\n")
19
- resource_file('objc_m_macro_template.txt') % content
23
+ content += constants_keys_with(localizations).map { |d| OBJC_CONST_TEMPLATE_M % d }.join("\n")
24
+ Utils.resource_file('objc_m_macro_template.txt') % content
20
25
  end
21
26
 
22
- def h_file
23
- content = constants_keys.map { |d| OBJC_CONST_TEMPLATE_H % d }.join("\n")
24
- resource_file('objc_h_macro_template.txt') % content
27
+ def h_file(localizations)
28
+ content = constants_keys_with(localizations).map { |d| OBJC_CONST_TEMPLATE_H % d }.join("\n")
29
+ Utils.resource_file('objc_h_macro_template.txt') % content
25
30
  end
26
31
 
27
32
  def constant_from(key)
@@ -1,31 +1,58 @@
1
- require_relative '../config'
2
- require_relative '../localization'
1
+ require_relative '../model/config'
2
+ require_relative '../model/localization'
3
+ require_relative '../utils'
4
+ require_relative 'generator'
3
5
  require 'fileutils'
4
6
 
5
7
  module Locraft
6
- class StringsGenerator
8
+ class StringsGenerator < Generator
7
9
  include FileUtils
8
10
 
9
- def initialize(config, localizations_hash)
10
- @config = config
11
- @localizations_hash = localizations_hash
11
+ def valid?
12
+ !@config.relative_strings_destination.nil?
12
13
  end
13
14
 
14
- def generate
15
- @config.langs.each do |lang, val|
16
- localizations = @localizations_hash[lang]
17
- destination_dir = "#{@config.relative_strings_destination}/#{val}.lproj"
18
- file = "#{destination_dir}/#{@config.strings_file}"
15
+ def generate(localizations_hash)
16
+ @config.langs.each do |lang, lang_code|
17
+ localizations = localizations_hash[lang].select { |l| valid_localization?(l) }
18
+ destination_dir = destination_dir_for(lang_code)
19
+ file = "#{destination_dir}/#{strings_file}"
19
20
  mkdir_p destination_dir unless Dir.exist?(destination_dir)
20
21
  File.open(file, 'w+') do |f|
21
- f.puts strings_file_with(localizations)
22
+ f.puts file_template % strings_file_content(localizations)
22
23
  end
23
- puts "strings file generated: [#{file}]"
24
+ broadcast_file_generation(file)
24
25
  end
25
26
  end
26
27
 
27
- def strings_file_with(localizations)
28
- localizations.select(&:valid?).map(&:to_line).join("\n")
28
+ def broadcast_file_generation(f)
29
+ puts "strings file generated: [#{f}]"
30
+ end
31
+
32
+ def valid_localization?(l)
33
+ l.valid?
34
+ end
35
+
36
+ def file_template
37
+ Utils.resource_file('strings_template.txt')
38
+ end
39
+
40
+ def destination_dir_for(lang)
41
+ "#{@config.relative_strings_destination}/#{lang}.lproj"
42
+ end
43
+
44
+ def strings_file
45
+ @config.strings_file
46
+ end
47
+
48
+ def strings_file_content(localizations)
49
+ localizations.select(&:valid?).map { |l| strings_line_with(l) }.join("\n")
50
+ end
51
+
52
+ def strings_line_with(localization)
53
+ content = ''
54
+ content += "/* #{localization.comment} */" unless localization.comment.nil?
55
+ content + %(\n"#{localization.key}" = "#{localization.strict_value}";\n)
29
56
  end
30
57
  end
31
58
  end
@@ -2,16 +2,27 @@ require_relative 'constants_generator'
2
2
 
3
3
  module Locraft
4
4
  class SwiftConstantsGenerator < ConstantsGenerator
5
- def generate
5
+ def valid?
6
+ !@config.relative_macro_destination.nil?
7
+ end
8
+
9
+ def generate(localizations_hash)
10
+ if @config.relative_plist_destination.nil?
11
+ warn 'Skipping objc constants generation. No destination path provided.'
12
+ return
13
+ end
14
+
15
+ localizations = localizations_hash[@config.default_lang]
6
16
  destination_dir = @config.relative_macro_destination
17
+ file_content = file_content(localizations)
7
18
  mkdir_p destination_dir unless Dir.exist?(destination_dir)
8
- write_file_content("#{destination_dir}/#{files_name}.swift", file)
19
+ write_file_content("#{destination_dir}/#{files_name}.swift", file_content)
9
20
  puts 'constants files was created for [swift] language'
10
21
  end
11
22
 
12
- def file
13
- content = constants_keys.map { |d| const_template % d }.join("\n")
14
- resource_file('swift_macro_template.txt') % content
23
+ def file_content(localizations)
24
+ content = constants_keys_with(localizations).map { |d| const_template % d }.join("\n")
25
+ Utils.resource_file('swift_macro_template.txt') % content
15
26
  end
16
27
 
17
28
  def const_template
@@ -0,0 +1,22 @@
1
+ require_relative 'strings_generator'
2
+
3
+ module Locraft
4
+ class XMLStringsGenerator < StringsGenerator
5
+ def file_template
6
+ Utils.resource_file('xml_strings_template.txt')
7
+ end
8
+
9
+ def destination_dir_for(lang)
10
+ "#{@config.relative_strings_destination}/values-#{lang}"
11
+ end
12
+
13
+ def strings_line_with(localization)
14
+ xml_value = localization.strict_value.gsub('%@', '%s')
15
+ %(<string name="#{localization.key}">#{xml_value}</string>\n)
16
+ end
17
+
18
+ def broadcast_file_generation(f)
19
+ puts "xml strings file generated: [#{f}]"
20
+ end
21
+ end
22
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'config'
1
+ require_relative 'model/config'
2
2
  require 'google_drive'
3
3
  require 'fileutils'
4
4
 
@@ -1,24 +1,25 @@
1
1
  module Locraft
2
- STRINGS_EXTENSION = '.strings'.freeze
2
+ IOS = 'iOS'.freeze
3
+ ANDROID = 'android'.freeze
4
+
3
5
  SWIFT = 'swift'.freeze
4
6
  OBJC = 'objc'.freeze
7
+ JAVA = 'java'.freeze
8
+
9
+ STRINGS_EXT = '.strings'.freeze
10
+ XML_EXT = '.xml'.freeze
5
11
 
6
12
  class Config
13
+ attr_accessor :platform
7
14
  attr_accessor :gdoc_file
8
- attr_accessor :info_plist_destination
9
- attr_accessor :macro_destination
10
15
  attr_accessor :strings_destination
16
+ attr_accessor :strings_basename
11
17
  attr_accessor :langs
12
18
  attr_accessor :default_lang
13
19
  attr_accessor :gdoc_sheet
14
20
  attr_accessor :gdoc_keys_column
15
21
  attr_accessor :gdoc_comments_column
16
- attr_accessor :dev_lang
17
- attr_accessor :dev_prefix
18
22
  attr_accessor :keys_map
19
- attr_accessor :macro_file
20
- attr_accessor :strings_basename
21
- attr_accessor :info_plist_basename
22
23
 
23
24
  # fill automatically in load_from method
24
25
  attr_accessor :from_file
@@ -28,12 +29,8 @@ module Locraft
28
29
  self.gdoc_sheet = 0
29
30
  self.gdoc_keys_column = 'Keys'
30
31
  self.gdoc_comments_column = 'Comments'
31
- self.dev_lang = OBJC
32
- self.dev_prefix = 'XYZ'
33
32
  self.keys_map = {}
34
- self.macro_file = 'LocalizedConstants'
35
33
  self.strings_basename = 'Localizable'
36
- self.info_plist_basename = 'InfoPlist'
37
34
  self.langs = {
38
35
  'English' => 'en',
39
36
  'Russian' => 'ru'
@@ -62,20 +59,83 @@ module Locraft
62
59
  File.expand_path('../' + strings_destination, from_file)
63
60
  end
64
61
 
62
+ def strings_file
63
+ raise NotImplementedError
64
+ end
65
+
66
+ def strings_generators
67
+ raise NotImplementedError
68
+ end
69
+ end
70
+
71
+ class IOSConfig < Config
72
+ attr_accessor :info_plist_destination
73
+ attr_accessor :info_plist_basename
74
+ attr_accessor :macro_destination
75
+ attr_accessor :dev_lang
76
+ attr_accessor :dev_prefix
77
+ attr_accessor :keys_map
78
+ attr_accessor :macro_file
79
+
80
+ def initialize
81
+ super()
82
+ self.platform = IOS
83
+ self.dev_lang = OBJC
84
+ self.dev_prefix = 'XYZ'
85
+ self.macro_file = 'LocalizedConstants'
86
+ self.info_plist_basename = 'InfoPlist'
87
+ yield self if block_given?
88
+ end
89
+
65
90
  def relative_plist_destination
91
+ return nil if info_plist_destination.nil?
66
92
  File.expand_path('../' + info_plist_destination, from_file)
67
93
  end
68
94
 
69
95
  def relative_macro_destination
96
+ return nil if macro_destination.nil?
70
97
  File.expand_path('../' + macro_destination, from_file)
71
98
  end
72
99
 
73
100
  def strings_file
74
- "#{strings_basename}#{STRINGS_EXTENSION}"
101
+ "#{strings_basename}#{STRINGS_EXT}"
75
102
  end
76
103
 
77
104
  def info_plist_file
78
- "#{info_plist_basename}#{STRINGS_EXTENSION}"
105
+ "#{info_plist_basename}#{STRINGS_EXT}"
106
+ end
107
+
108
+ def strings_generators
109
+ [StringsGenerator.new(self), info_plist_generator, constants_generator].select(&:valid?)
110
+ end
111
+
112
+ def info_plist_generator
113
+ InfoPlistGenerator.new(self)
114
+ end
115
+
116
+ def constants_generator
117
+ if dev_lang == SWIFT
118
+ SwiftConstantsGenerator.new(self)
119
+ elsif dev_lang == OBJC
120
+ ObjcConstantsGenerator.new(self)
121
+ end
122
+ end
123
+ end
124
+
125
+ class AndroidConfig < Config
126
+ def initialize
127
+ super()
128
+ self.platform = ANDROID
129
+ self.strings_basename = 'strings'
130
+ yield self if block_given?
131
+ end
132
+
133
+ def strings_file
134
+ "#{strings_basename}#{XML_EXT}"
135
+ end
136
+
137
+ def strings_generators
138
+ [XMLStringsGenerator.new(self)]
79
139
  end
80
140
  end
81
141
  end
@@ -19,13 +19,8 @@ module Locraft
19
19
  !(key.nil? or value.nil?)
20
20
  end
21
21
 
22
- def to_line
23
- content = ''
24
- if valid?
25
- content += "/* #{comment} */" unless comment.nil?
26
- content += %(\n"#{key}" = "#{value.gsub("\n", "\\n") }";\n)
27
- end
28
- content
22
+ def strict_value
23
+ value.gsub("\n", "\\n")
29
24
  end
30
25
  end
31
26
  end
@@ -1,4 +1,4 @@
1
- Locraft::Config.new do |conf|
1
+ Locraft::IOSConfig.new do |conf|
2
2
  ############
3
3
  # @!required
4
4
  ############
@@ -0,0 +1,3 @@
1
+ <resources>
2
+ %s
3
+ </resources>
@@ -0,0 +1,9 @@
1
+ require 'fileutils'
2
+
3
+ module Locraft
4
+ module Utils
5
+ def self.resource_file(file)
6
+ File.read(File.expand_path("../resources/#{file}", __FILE__))
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Locraft
2
- VERSION = '1.6.3'.freeze
2
+ VERSION = '1.6.4'.freeze
3
3
  end
data/lib/locraft.rb CHANGED
@@ -1,5 +1,5 @@
1
- require_relative 'locraft/config'
2
- require_relative 'locraft/localization'
1
+ require_relative 'locraft/model/config'
2
+ require_relative 'locraft/model/localization'
3
3
  require_relative 'locraft/initializer'
4
4
  require_relative 'locraft/runner'
5
5
  require_relative 'locraft/extractor'
@@ -8,3 +8,4 @@ require_relative 'locraft/generators/constants_generator'
8
8
  require_relative 'locraft/generators/swift_constants_generator'
9
9
  require_relative 'locraft/generators/objc_constants_generator'
10
10
  require_relative 'locraft/generators/strings_generator'
11
+ require_relative 'locraft/generators/xml_strings_generator'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locraft
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.3
4
+ version: 1.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - sroik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-20 00:00:00.000000000 Z
11
+ date: 2018-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google_drive
@@ -118,23 +118,28 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - bin/locraft
120
120
  - lib/locraft.rb
121
- - lib/locraft/config.rb
122
121
  - lib/locraft/csv_parser.rb
123
122
  - lib/locraft/extractor.rb
124
123
  - lib/locraft/generators/constants_generator.rb
124
+ - lib/locraft/generators/generator.rb
125
125
  - lib/locraft/generators/info_plist_generator.rb
126
126
  - lib/locraft/generators/objc_constants_generator.rb
127
127
  - lib/locraft/generators/strings_generator.rb
128
128
  - lib/locraft/generators/swift_constants_generator.rb
129
+ - lib/locraft/generators/xml_strings_generator.rb
129
130
  - lib/locraft/google_drive_wrapper.rb
130
131
  - lib/locraft/initializer.rb
131
- - lib/locraft/localization.rb
132
+ - lib/locraft/model/config.rb
133
+ - lib/locraft/model/localization.rb
132
134
  - lib/locraft/resources/macro_files_header.txt
133
135
  - lib/locraft/resources/objc_h_macro_template.txt
134
136
  - lib/locraft/resources/objc_m_macro_template.txt
135
137
  - lib/locraft/resources/sample_locraft.config
138
+ - lib/locraft/resources/strings_template.txt
136
139
  - lib/locraft/resources/swift_macro_template.txt
140
+ - lib/locraft/resources/xml_strings_template.txt
137
141
  - lib/locraft/runner.rb
142
+ - lib/locraft/utils.rb
138
143
  - lib/locraft/version.rb
139
144
  homepage: https://github.com/app-craft/locraft
140
145
  licenses: