aigu 1.0.3 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3358a66e5995d3ce9e7a14d145d244b34f0a64a5
4
- data.tar.gz: 1f343aa8b7122a03690a613d1bb9f2d40f8f5087
3
+ metadata.gz: 65716065f73197d5e63e316e39fd0ef3e0b50a58
4
+ data.tar.gz: 4af655d2c2f9cded6aeab7a340344acbc7cc29b2
5
5
  SHA512:
6
- metadata.gz: d3553f93ffb7e1f9873563ce6b9cf75daf67bcd63d04da78517730e7b9634fb82a4bc88b6e50ee9984ad7d92759f25286b9fc059d31b85d691779c2fdb0e639d
7
- data.tar.gz: 6eb458257db71b5d04a1dfcca7ae7220d76ed34eab818bf55e214a17229a8463c09d390e35abe80d05a67cc6ab33e6683d2ea506345c750e25404e26b2fe5a2f
6
+ metadata.gz: 949b6e813c31babe552a050238e179f18b4ae93358edc4f6abdb711daa95691eaa489bdf2476bd83fec366e02d5c10eb609ae79476745d8c2cef3983286cbc53
7
+ data.tar.gz: 72bbd501d493e26378b08443eeb9e8ac5044642cb925909ec9bc92555629b3e61533d2de7a62d22af6728d5d39862a9720a0ea7632aaa6470a4e09e221a0b3cb
data/README.md CHANGED
@@ -28,14 +28,15 @@ $ aigu <export-command> --locale=fr --input-directory=config/locales --output-fi
28
28
  ```
29
29
  #### Commands
30
30
 
31
- | Command | File format |
32
- |--------------------|---------------------------|
33
- | `rails-export` | Rails YAML |
34
- | `android-export` | Android XML |
35
- | `core-export` | Java Enum Core |
36
- | `ios-export` | iOS strings & stringsdict |
37
- | `ember-export` | Ember.js JavaScript |
38
- | `ember-pod-export` | Ember.js pods JSON |
31
+ | Command | File format |
32
+ |-----------------------|----------------------------|
33
+ | `rails-export` | Rails YAML |
34
+ | `android-export` | Android XML |
35
+ | `core-export` | Java Enum Core |
36
+ | `ios-export` | iOS strings & stringsdict |
37
+ | `ember-export` | Ember.js JavaScript |
38
+ | `ember-pod-export ` | Ember.js pods JSON |
39
+ | `ember-engine-export` | Ember.js Engine JavaScript |
39
40
 
40
41
  #### Options
41
42
 
@@ -57,14 +58,15 @@ $ aigu <import-command> --locale=fr --input-file=file-from-accent.json --output-
57
58
 
58
59
  #### Commands
59
60
 
60
- | Command | File format |
61
- |--------------------|---------------------------|
62
- | `rails-import` | Rails YAML |
63
- | `android-import` | Android XML |
64
- | `core-import` | Java Enum Core |
65
- | `ios-import` | iOS strings & stringsdict |
66
- | `ember-import` | Ember.js JavaScript |
67
- | `ember-pod-import` | Ember.js pods JSON |
61
+ | Command | File format |
62
+ |-----------------------|----------------------------|
63
+ | `rails-import` | Rails YAML |
64
+ | `android-import` | Android XML |
65
+ | `core-import` | Java Enum Core |
66
+ | `ios-import` | iOS strings & stringsdict |
67
+ | `ember-import` | Ember.js JavaScript |
68
+ | `ember-pod-import` | Ember.js pods JSON |
69
+ | `ember-engine-import` | Ember.js Engine JavaScript |
68
70
 
69
71
  #### Options
70
72
 
@@ -26,6 +26,8 @@ require 'aigu/ember_exporter'
26
26
  require 'aigu/ember_importer'
27
27
  require 'aigu/ember_pod_exporter'
28
28
  require 'aigu/ember_pod_importer'
29
+ require 'aigu/ember_engine_exporter'
30
+ require 'aigu/ember_engine_importer'
29
31
 
30
32
  module Aigu
31
33
  end
@@ -12,7 +12,9 @@ module Aigu
12
12
  'ember-import' => 'EmberImporter',
13
13
  'ember-export' => 'EmberExporter',
14
14
  'ember-pod-import' => 'EmberPodImporter',
15
- 'ember-pod-export' => 'EmberPodExporter'
15
+ 'ember-pod-export' => 'EmberPodExporter',
16
+ 'ember-engine-import' => 'EmberEngineImporter',
17
+ 'ember-engine-export' => 'EmberEngineExporter'
16
18
  }.freeze
17
19
 
18
20
  def initialize(env, argv)
@@ -0,0 +1,76 @@
1
+ module Aigu
2
+ class EmberEngineExporter < Exporter
3
+ protected
4
+
5
+ def build_output
6
+ @output = build_output_from_global_translations(@input_directory, @locale)
7
+ @output.merge! build_output_from_engines(@input_directory, @locale)
8
+ @output
9
+ end
10
+
11
+ def build_output_from_global_translations(input_directory, locale)
12
+ global_directory = File.join(input_directory, 'app')
13
+ pattern = File.join(global_directory, 'locales', locale, 'translations.js')
14
+ parse_file(pattern, input_directory)
15
+ end
16
+
17
+ def build_output_from_engines(input_directory, locale)
18
+ engines_directory = File.join(input_directory, 'lib')
19
+ pattern = File.join(engines_directory, '**', 'locales', locale, 'translations.js')
20
+ parse_file(pattern, input_directory)
21
+ end
22
+
23
+ def parse_file(pattern, directory)
24
+ Dir[pattern].reduce({}) do |memo, file|
25
+ content = read_or_ignore(file, directory)
26
+
27
+ return nil unless content
28
+
29
+ base_key = file.gsub(/^#{directory}/, '').gsub(/^\//, '').gsub(/[a-z]+\/translations\.js$/, '__LOCALE__/translations.js|')
30
+ content = flattenize_hash(content, base_key)
31
+ memo.merge! content
32
+ end
33
+ end
34
+
35
+ def write_json_file
36
+ file_path = @output_file
37
+ puts "Generating #{file_path}"
38
+ FileUtils.mkdir_p(File.dirname(file_path))
39
+
40
+ File.open(file_path, 'w+') do |file|
41
+ file << @output.to_json
42
+ end
43
+ end
44
+
45
+ def read_or_ignore(file, directory)
46
+ filepath = file.gsub(/\A#{directory}\//, '')
47
+
48
+ if ignored_filepath?(filepath)
49
+ puts "Ignoring #{filepath}"
50
+ false
51
+ else
52
+ puts "Processing #{filepath}"
53
+ content = File.read(file).gsub(/^export default (.*);$/m, '\1')
54
+ JSON.parse(content)
55
+
56
+ end
57
+ end
58
+
59
+ def flattenize_hash(hash, base_key = '')
60
+ if hash.is_a?(Hash)
61
+ hash.reduce({}) do |memo, (key, value)|
62
+ new_base_key = [base_key, key].join('.').gsub(/\|\.+/, '|')
63
+ memo.merge! flattenize_hash(value, new_base_key)
64
+ end
65
+ else
66
+ { base_key.gsub(/^\./, '') => hash }
67
+ end
68
+ end
69
+
70
+ def ignored_filepath?(filepath)
71
+ @ignore && @ignore.any? do |pattern|
72
+ File.fnmatch(pattern, filepath, File::FNM_PATHNAME | File::FNM_DOTMATCH)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,58 @@
1
+ module Aigu
2
+ class EmberEngineImporter < Importer
3
+ def process!
4
+ puts "Generating JSON files in `#{@output_directory}` based on Accent-generated `#{@input_file}` file"
5
+ puts '---'
6
+
7
+ object = sorted_object_from_json(@input_file)
8
+ blob = build_blob(object)
9
+ write_json_files(blob, @output_directory)
10
+
11
+ puts '---'
12
+ puts 'Done'
13
+ end
14
+
15
+ protected
16
+
17
+ def sorted_object_from_json(input_file)
18
+ json = File.read(input_file)
19
+ JSON.parse(json).sort_with_keys
20
+ end
21
+
22
+ def write_json_files(blob, output_directory)
23
+ blob.each_pair do |file_name, translations|
24
+ file_path = File.join(output_directory, file_name.gsub('__LOCALE__', @locale))
25
+ puts "Generating #{file_path}"
26
+ FileUtils.mkdir_p(File.dirname(file_path))
27
+
28
+ javascript_object = JSON.pretty_generate(translations)
29
+ content = "export default #{javascript_object};\n"
30
+
31
+ File.open(file_path, 'w+') do |file|
32
+ file << content
33
+ end
34
+ end
35
+ end
36
+
37
+ def build_blob(object)
38
+ blob = Hash.recursive
39
+
40
+ object.each_pair do |key, value|
41
+ filename, flat_key = key.split('|')
42
+
43
+ parts = flat_key.split('.')
44
+ hash = blob[filename]
45
+
46
+ parts.each_with_index do |part, index|
47
+ if index + 1 < parts.length
48
+ hash = hash[part]
49
+ else
50
+ hash[part] = value
51
+ end
52
+ end
53
+ end
54
+
55
+ blob
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Aigu
2
- VERSION = '1.0.3'
2
+ VERSION = '1.1'
3
3
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::EmberEngineExporter do
4
+ describe :build_output do
5
+ let(:input_directory) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'ember_engine_files') }
6
+ let(:exporter) { Aigu::EmberEngineExporter.new(:'input-directory' => input_directory, locale: 'en') }
7
+ let(:output) { exporter.send(:build_output) }
8
+
9
+ let(:expected_output) do
10
+ {
11
+ 'app/locales/__LOCALE__/translations.js|hello' => 'Hello from global.hello',
12
+ 'lib/engine-a/addon/locales/__LOCALE__/translations.js|hello.world' => 'Hello world from nested engine-a',
13
+ 'lib/engine-b/addon/locales/__LOCALE__/translations.js|hello.world' => 'Hello world from nested engine-b'
14
+ }
15
+ end
16
+
17
+ it { expect(output).to eql expected_output }
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::EmberEngineImporter do
4
+ describe :build_blob do
5
+ let(:importer) { Aigu::EmberEngineImporter.new }
6
+ let(:blob) { importer.send(:build_blob, content) }
7
+
8
+ let(:content) do
9
+ {
10
+ 'app/locales/__LOCALE__/translations.js|hello' => 'Hello from global.hello',
11
+ 'lib/engine-a/addon/locales/__LOCALE__/translations.js|hello.world' => 'Hello world from nested engine-a',
12
+ 'lib/engine-b/addon/locales/__LOCALE__/translations.js|hello.world' => 'Hello world from nested engine-b'
13
+ }
14
+ end
15
+
16
+ let(:expected_blob) do
17
+ {
18
+ 'app/locales/__LOCALE__/translations.js' => {
19
+ 'hello' => 'Hello from global.hello'
20
+ },
21
+ 'lib/engine-a/addon/locales/__LOCALE__/translations.js' => {
22
+ 'hello' => {
23
+ 'world' => 'Hello world from nested engine-a'
24
+ }
25
+ },
26
+ 'lib/engine-b/addon/locales/__LOCALE__/translations.js' => {
27
+ 'hello' => {
28
+ 'world' => 'Hello world from nested engine-b'
29
+ }
30
+ }
31
+ }
32
+ end
33
+
34
+ it { expect(blob).to eql expected_blob }
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": "Hello from global.hello"
5
+ };
@@ -0,0 +1,5 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": "Bonjour de global.hello"
5
+ };
@@ -0,0 +1,7 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": {
5
+ "world": "Hello world from nested engine-a"
6
+ }
7
+ };
@@ -0,0 +1,7 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": {
5
+ "world": "Hello World de l'engine-a"
6
+ }
7
+ };
@@ -0,0 +1,7 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": {
5
+ "world": "Hello world from nested engine-b"
6
+ }
7
+ };
@@ -0,0 +1,7 @@
1
+ /* eslint-disable quotes, quote-props */
2
+
3
+ export default {
4
+ "hello": {
5
+ "world": "Hello World de l'engine-b"
6
+ }
7
+ };
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aigu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-27 00:00:00.000000000 Z
12
+ date: 2017-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -123,6 +123,8 @@ files:
123
123
  - lib/aigu/cli.rb
124
124
  - lib/aigu/core_exporter.rb
125
125
  - lib/aigu/core_importer.rb
126
+ - lib/aigu/ember_engine_exporter.rb
127
+ - lib/aigu/ember_engine_importer.rb
126
128
  - lib/aigu/ember_exporter.rb
127
129
  - lib/aigu/ember_importer.rb
128
130
  - lib/aigu/ember_pod_exporter.rb
@@ -139,6 +141,8 @@ files:
139
141
  - spec/aigu/android_importer_spec.rb
140
142
  - spec/aigu/core_exporter_spec.rb
141
143
  - spec/aigu/core_importer_spec.rb
144
+ - spec/aigu/ember_engine_exporter_spec.rb
145
+ - spec/aigu/ember_engine_importer_spec.rb
142
146
  - spec/aigu/ember_exporter_spec.rb
143
147
  - spec/aigu/ember_importer_spec.rb
144
148
  - spec/aigu/ember_pod_exporter_spec.rb
@@ -147,6 +151,12 @@ files:
147
151
  - spec/aigu/ios_importer_spec.rb
148
152
  - spec/aigu/rails_exporter_spec.rb
149
153
  - spec/aigu/rails_importer_spec.rb
154
+ - spec/fixtures/ember_engine_files/app/locales/en/translations.js
155
+ - spec/fixtures/ember_engine_files/app/locales/fr/translations.js
156
+ - spec/fixtures/ember_engine_files/lib/engine-a/addon/locales/en/translations.js
157
+ - spec/fixtures/ember_engine_files/lib/engine-a/addon/locales/fr/translations.js
158
+ - spec/fixtures/ember_engine_files/lib/engine-b/addon/locales/en/translations.js
159
+ - spec/fixtures/ember_engine_files/lib/engine-b/addon/locales/fr/translations.js
150
160
  - spec/fixtures/ember_pod_files/app/locales/en/translations.general.json
151
161
  - spec/fixtures/ember_pod_files/app/locales/fr/translations.general.json
152
162
  - spec/fixtures/ember_pod_files/app/pods/component-a/nested-component-a/translations.en.json
@@ -187,6 +197,8 @@ test_files:
187
197
  - spec/aigu/android_importer_spec.rb
188
198
  - spec/aigu/core_exporter_spec.rb
189
199
  - spec/aigu/core_importer_spec.rb
200
+ - spec/aigu/ember_engine_exporter_spec.rb
201
+ - spec/aigu/ember_engine_importer_spec.rb
190
202
  - spec/aigu/ember_exporter_spec.rb
191
203
  - spec/aigu/ember_importer_spec.rb
192
204
  - spec/aigu/ember_pod_exporter_spec.rb
@@ -195,6 +207,12 @@ test_files:
195
207
  - spec/aigu/ios_importer_spec.rb
196
208
  - spec/aigu/rails_exporter_spec.rb
197
209
  - spec/aigu/rails_importer_spec.rb
210
+ - spec/fixtures/ember_engine_files/app/locales/en/translations.js
211
+ - spec/fixtures/ember_engine_files/app/locales/fr/translations.js
212
+ - spec/fixtures/ember_engine_files/lib/engine-a/addon/locales/en/translations.js
213
+ - spec/fixtures/ember_engine_files/lib/engine-a/addon/locales/fr/translations.js
214
+ - spec/fixtures/ember_engine_files/lib/engine-b/addon/locales/en/translations.js
215
+ - spec/fixtures/ember_engine_files/lib/engine-b/addon/locales/fr/translations.js
198
216
  - spec/fixtures/ember_pod_files/app/locales/en/translations.general.json
199
217
  - spec/fixtures/ember_pod_files/app/locales/fr/translations.general.json
200
218
  - spec/fixtures/ember_pod_files/app/pods/component-a/nested-component-a/translations.en.json