locraft 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c903b35d6cf323493448686440a20c5ca7c8d16f
4
+ data.tar.gz: cabed8f85f718e14031f71f3904971c85a257714
5
+ SHA512:
6
+ metadata.gz: 17012bb71cdaa75ced47c652c56339698c0e9575b54fba2773394a5d24c882709303c48ef2748ba5c68ff1a720362aabffbafde808eedf9101ec77b6c988f691
7
+ data.tar.gz: 520473245aac8c6eb64d68c840b8dac4be92b010a08fb4dc5bfcb9fb4a18b503cd32f83cf3f5f19adbc7dbbf93569222b43c481d216592b99ea5cc24efd4a0f4
data/bin/locraft ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../../lib/locraft', __FILE__)
4
+ Locraft::Runner.start(ARGV)
@@ -0,0 +1,65 @@
1
+ module Locraft
2
+
3
+ STRINGS_EXTENSION = '.strings'
4
+ SWIFT = 'swift'
5
+ OBJC = 'objc'
6
+
7
+ class Config
8
+
9
+ attr_accessor :langs
10
+ attr_accessor :default_lang
11
+ attr_accessor :destination_dir
12
+ attr_accessor :gdoc_file
13
+ attr_accessor :gdoc_sheet
14
+ attr_accessor :gdoc_keys_column
15
+ attr_accessor :dev_lang
16
+ attr_accessor :dev_prefix
17
+ attr_accessor :macro_file
18
+ attr_accessor :macro_destination_dir
19
+ attr_accessor :strings_basename
20
+ attr_accessor :info_plist_basename
21
+
22
+ # fill automatically in load_from method
23
+ attr_accessor :from_file
24
+
25
+ def initialize
26
+ self.info_plist_basename = 'InfoPlist'
27
+ self.strings_basename = 'Localizable'
28
+ self.destination_dir = './Localizations'
29
+ self.default_lang = 'English'
30
+ self.gdoc_sheet = 0
31
+ self.gdoc_keys_column = 0
32
+ self.dev_lang = OBJC
33
+ self.dev_prefix = 'XYZ'
34
+ self.macro_file = 'LocalizedConstants'
35
+ self.macro_destination_dir = './'
36
+ self.langs = {
37
+ 'English' => 'en',
38
+ 'Russian' => 'ru'
39
+ }
40
+ yield self if block_given?
41
+ end
42
+
43
+ def self.load_from(file)
44
+ return unless file || File.file?(file)
45
+ begin
46
+ config = eval(File.read(file), binding, 'config loading problem')
47
+ config.from_file = file
48
+ return config if Config === config
49
+
50
+ warn %Q([#{file}] isn't a Locraft::Config, but #{config.class}.)
51
+ rescue SyntaxError, Exception => e
52
+ warn %Q(Invalid config in [#{file}]: #{e})
53
+ end
54
+ end
55
+
56
+ def relative_destination_dir
57
+ File.expand_path('../' + self.destination_dir, self.from_file)
58
+ end
59
+
60
+ def relative_macro_destination_dir
61
+ File.expand_path('../' + self.macro_destination_dir, self.from_file)
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,77 @@
1
+ require_relative 'config'
2
+
3
+ module Locraft
4
+ class ConstantsGenerator
5
+
6
+ OBJC_CONST_TEMPLATE_H = 'extern NSString *const __nonnull %{const};'
7
+ OBJC_CONST_TEMPLATE_M = 'NSString *const %{const} = @"%{key}";'
8
+ SWIFT_CONST_TEMPLATE = 'let %{const}: String = @"%{key}"'
9
+
10
+ def initialize(config)
11
+ @config = config
12
+ end
13
+
14
+ def generate
15
+ if @config.dev_lang.equal?(OBJC)
16
+ self.write_to(%Q{#{@config.relative_macro_destination_dir}/#{self.files_name}.h}, self.h_file)
17
+ self.write_to(%Q{#{@config.relative_macro_destination_dir}/#{self.files_name}.m}, self.m_file)
18
+ else
19
+ self.write_to(%Q{#{@config.relative_macro_destination_dir}/#{self.files_name}.swift}, self.swift_file)
20
+ end
21
+
22
+ puts %(constants files was created for [#{@config.dev_lang}] language)
23
+ end
24
+
25
+ def strings_lines
26
+ File.readlines(self.strings_file).select { |l| l =~ %r(".*"\s*=\s*".*";) }
27
+ end
28
+
29
+ def strings_file
30
+ to_add = %Q(#{@config.langs.values.first}.lproj/#{@config.strings_basename}#{STRINGS_EXTENSION})
31
+ File.join(@config.relative_destination_dir, to_add)
32
+ end
33
+
34
+ def write_to(file, content)
35
+ File.open(file, 'w+') do |f|
36
+ f.puts self.files_header + "\n"
37
+ f.puts content
38
+ end
39
+ end
40
+
41
+ def files_header
42
+ File.read(File.expand_path('../resources/constants_files_header.txt', __FILE__))
43
+ end
44
+
45
+ def files_name
46
+ @config.dev_prefix + @config.macro_file
47
+ end
48
+
49
+ def m_file
50
+ file = %Q{#import "#{self.files_name}.h"\n}
51
+ file + self.constants_keys.map { |d| OBJC_CONST_TEMPLATE_M % d }.join("\n")
52
+ end
53
+
54
+ def h_file
55
+ file = %Q{@import Foundation;\n}
56
+ file + self.constants_keys.map { |d| OBJC_CONST_TEMPLATE_H % d }.join("\n")
57
+ end
58
+
59
+ def swift_file
60
+ file = %Q{import Foundation\n}
61
+ file + self.constants_keys.map { |d| SWIFT_CONST_TEMPLATE % d }.join("\n")
62
+ end
63
+
64
+ def constants_keys
65
+ self.strings_lines.map do |l|
66
+ key = l.match(%r("(.*)"\s*=))[1]
67
+ { :key => key, :const => self.constant_from(key) }
68
+ end
69
+ end
70
+
71
+ def constant_from(key)
72
+ inside = key.split('.').map { |s| s.capitalize }.join
73
+ %Q{k#{@config.dev_prefix}#{inside}Localized}
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'config'
2
+ require_relative 'info_plist_generator'
3
+ require_relative 'constants_generator'
4
+ require 'babelish'
5
+
6
+ module Locraft
7
+ class Extractor
8
+
9
+ def initialize(config_file)
10
+ @config = Config.load_from(config_file)
11
+ end
12
+
13
+ def extract
14
+ # generate localized strings files
15
+ extract_strings
16
+
17
+ # generate .plist files
18
+ InfoPlistGenerator.new(@config).generate
19
+
20
+ # generate constants
21
+ ConstantsGenerator.new(@config).generate
22
+ end
23
+
24
+ def extract_strings
25
+ langs = ''
26
+ @config.langs.each { |k,v| langs += %Q('#{k}:#{v}' ) }
27
+ arg1 = %Q(--filename='#{@config.gdoc_file}')
28
+ arg2 = %Q(--langs=#{langs})
29
+ arg3 = %Q(--default-lang='#{@config.default_lang}')
30
+ arg4 = %Q(--keys-column=#{@config.gdoc_keys_column})
31
+ arg5 = %Q(--output-basenames=#{@config.strings_basename})
32
+ arg6 = %Q(--sheet=#{@config.gdoc_sheet})
33
+ arg7 = %Q(--output-dir='#{@config.relative_destination_dir}')
34
+ system("babelish csv2strings #{arg1} #{arg2} #{arg3} #{arg4} #{arg5} #{arg6} #{arg7} --fetch")
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'config'
2
+
3
+ module Locraft
4
+ class InfoPlistGenerator
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def generate
11
+ dir = @config.relative_destination_dir
12
+ folders = Dir.entries(dir).select { |d| d =~ %r(.+(.lproj)) }.map { |d| File.join(dir, d) }
13
+ folders.each { |f| generate_info_plist_in_lproj(f) }
14
+ end
15
+
16
+ def generate_info_plist_in_lproj(folder)
17
+ localizable_file = File.join(folder, %Q(#{@config.strings_basename}#{STRINGS_EXTENSION}))
18
+ info_file = File.join(folder, %Q(#{@config.info_plist_basename}#{STRINGS_EXTENSION}))
19
+ File.write(info_file, info_plist_from(localizable_file))
20
+
21
+ puts %Q(- #{info_file} created)
22
+ end
23
+
24
+ def info_plist_from(localizable_file)
25
+ filter = %r("(NS|CF).*"\s*=\s*".*";)
26
+ File.readlines(localizable_file).select { |l| l =~ filter }.join("\n")
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ module Locraft
2
+
3
+ SAMPLE_CONFIG_FILE = './locraft.config'
4
+
5
+ class Initializer
6
+
7
+ def self.init
8
+ config = File.read(File.expand_path('../resources/sample_locraft.config', __FILE__))
9
+ if File.file? SAMPLE_CONFIG_FILE
10
+ warn 'ERROR: Initialization failed. There is already exists some locraft.config.'
11
+ else
12
+ File.write(SAMPLE_CONFIG_FILE, config)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright © 2016 Sroik. All rights reserved.
3
+ //
4
+ // This file is autogenerated using 'locraft'.
5
+ // read more here https://github.com/dt34m/locraft
6
+ //
@@ -0,0 +1,26 @@
1
+ Locraft::Config.new do |conf|
2
+ ############
3
+ # @!required
4
+ ############
5
+ conf.gdoc_file = 'google doc file name here'
6
+
7
+ ############
8
+ # @!optional
9
+ ############
10
+ conf.langs = {
11
+ 'English' => 'en',
12
+ 'Russian' => 'ru'
13
+ } # default: {'English' => 'en', 'Russian' => 'ru'}
14
+
15
+ conf.destination_dir = './Localizations' # strings destination directory related to this file, default: './Localizations'
16
+ conf.default_lang = 'English' # default: 'English'
17
+ conf.gdoc_sheet = 0 # default: 0
18
+ conf.gdoc_keys_column = 0 # default: 0
19
+
20
+ conf.dev_prefix = 'XYZ' # default: 'XYZ'
21
+ conf.dev_lang = OBJC # OBJC or SWIFT, default: OBJC
22
+ conf.macro_file = 'LocalizedConstants' # default: 'LocalizedConstants'
23
+ conf.macro_destination_dir = './' # default: './'
24
+ conf.strings_basename = 'Localizable' # default: 'Localizable'
25
+ conf.info_plist_basename = 'InfoPlist' # default: 'InfoPlist'
26
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'initializer'
2
+ require_relative 'extractor'
3
+ require 'thor'
4
+
5
+ module Locraft
6
+ class Runner < Thor
7
+
8
+ desc 'init', 'create sample locraft.config'
9
+ def init
10
+ Initializer.init
11
+ end
12
+
13
+ desc 'extract', 'extract strings from google drive and parse them into {.strings, .plist} files. Then create constants'
14
+ option :config, :default => SAMPLE_CONFIG_FILE , :aliases => '-c', :type => :string, :desc => 'configuration file'
15
+ def extract
16
+ config_file = options[:config]
17
+ if File.file? config_file
18
+ extractor = Extractor.new(options[:config])
19
+ extractor.extract
20
+ else
21
+ warn %Q(Error: there is no such a file '#{config_file}'.)
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Locraft
2
+ VERSION = '1.0.0'
3
+ end
data/lib/locraft.rb ADDED
@@ -0,0 +1,4 @@
1
+ require_relative 'locraft/version'
2
+ require_relative 'locraft/config'
3
+ require_relative 'locraft/initializer'
4
+ require_relative 'locraft/runner'
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locraft
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sroik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: babelish
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
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: rake
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: test-unit
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
+ description: application localizator
84
+ email:
85
+ - vasili.kazhanouski@gmail.com
86
+ executables:
87
+ - locraft
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/locraft
92
+ - lib/locraft.rb
93
+ - lib/locraft/config.rb
94
+ - lib/locraft/constants_generator.rb
95
+ - lib/locraft/extractor.rb
96
+ - lib/locraft/info_plist_generator.rb
97
+ - lib/locraft/initializer.rb
98
+ - lib/locraft/resources/constants_files_header.txt
99
+ - lib/locraft/resources/sample_locraft.config
100
+ - lib/locraft/runner.rb
101
+ - lib/locraft/version.rb
102
+ homepage: https://github.com/dt34m/locraft
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.6.8
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: locraft
126
+ test_files: []