babel_diff 1.0.3 → 1.1.0

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: 636cfb635813720ebceed48ded1eea465486bcde
4
- data.tar.gz: a645857700b111c53dd4f467e87e9cd66a247e97
3
+ metadata.gz: 1f45964490450ee1b8fd1d63721bfe461c49ea69
4
+ data.tar.gz: 043788f850c467122cd044e7f21c515861197a6e
5
5
  SHA512:
6
- metadata.gz: db1ff545db9294c18c2261482b90922cc71f800806a8fef30463a53af59718c54d3d71087cc78d0db887614e1ab4f874a88fec221922b6f9e706194dc70c0f0c
7
- data.tar.gz: ee0c4a2297c52df41a54e851afeb89bfa42b9cce3c6d6e4086d1e4abab70b130be44c1f4a1beedcecaa83f1dbfe02c7efbc3b3ef72bed39b06e3a2897ee6d3e3
6
+ metadata.gz: a0692f831fb000c55c1c57862481ac1856fcfe14b8a96e68aa79a63589a9dfc84c183ce03c0fd4bb77819d4faf075cda267adef73f920c015d81db8dd94f7a81
7
+ data.tar.gz: 973394a45b36855c46fb240937864dbf17e17176a1ac7989b4c4a2466195a48b78ddfcf18d1aa2540601b349499bb563220011d010005dd898e61845c23f6eac
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
- spec.executables << "babel_diff"
16
+ spec.executables << "babel_diff" << "babel_import"
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.8"
@@ -1,4 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'babel_diff'
3
3
 
4
- BabelDiff.run
4
+ help = <<-HELP
5
+ usages:
6
+
7
+ babel_import <import_dir> <phrase_dir> [defaults to 'config/locales/']
8
+
9
+ Imports all phrase files in the <import_dir> and merges them with the phrase files in <phrase_dir>.
10
+ Existing keys will be overwritten.
11
+
12
+ babel_diff <phrase_file_path> [defaults to 'config/locales/phrase.en.yml']
13
+
14
+ Produces two files containing all updates and all additions to the given phrase file located at <phrase_file_path> since babel_diff last ran.
15
+
16
+ HELP
17
+
18
+ case ARGV.first
19
+ when "--help"
20
+ puts help
21
+ exit 0
22
+ when nil
23
+ BabelDiff.generate_diffs
24
+ else
25
+ BabelDiff.generate_diffs(ARGV[0])
26
+ end
27
+
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ require 'babel_diff'
3
+
4
+ help = <<-HELP
5
+ usages:
6
+
7
+ babel_import <import_dir> <phrase_dir> [defaults to 'config/locales/']
8
+
9
+ Imports all phrase files in the <import_dir> and merges them with the phrase files in <phrase_dir>.
10
+ Existing keys will be overwritten.
11
+
12
+ babel_diff <phrase_file_path> [defaults to 'config/locales/phrase.en.yml']
13
+
14
+ Produces two files containing all updates and all additions to the given phrase file located at <phrase_file_path> since babel_diff last ran.
15
+
16
+ HELP
17
+
18
+
19
+ case ARGV.first
20
+ when nil
21
+ puts help
22
+ exit 1
23
+ when "--help"
24
+ puts help
25
+ exit 0
26
+ else
27
+ if ARGV[1]
28
+ BabelDiff.import_translations(ARGV[0], ARGV[1])
29
+ else
30
+ BabelDiff.import_translations(ARGV[0])
31
+ end
32
+ end
33
+
34
+
@@ -1,9 +1,12 @@
1
1
  require "babel_diff/version"
2
2
  require "babel_diff/file_handler"
3
+ require "babel_diff/import_file_handler"
3
4
  require "babel_diff/yaml_differ"
5
+ require "babel_diff/yaml_merger"
6
+ require "babel_diff/hash_flattener"
4
7
 
5
8
  module BabelDiff
6
- def self.run(current_version_path = "config/locales/phrase.en.yml")
9
+ def self.generate_diffs(current_version_path = "config/locales/phrase.en.yml")
7
10
  handler = FileHandler.new(current_version_path)
8
11
 
9
12
  yaml_differ = YamlDiffer.new(handler.current_version, handler.previous_version)
@@ -15,4 +18,13 @@ module BabelDiff
15
18
  handler.version_files
16
19
  end
17
20
 
21
+ def self.import_translations(import_directory, phrase_directory = "config/locales/")
22
+ handler = ImportFileHandler.new(import_directory, phrase_directory)
23
+ handler.phrases.each do |language,files|
24
+ phrase, import = files
25
+ yaml_merger = YamlMerger.new(phrase, import)
26
+ handler.update_phrase(language,yaml_merger.merged_yaml)
27
+ end
28
+ end
29
+
18
30
  end
@@ -0,0 +1,32 @@
1
+ module BabelDiff
2
+ class HashFlattener < Struct.new(:hash)
3
+ def flat_hash
4
+ @_flat_hash ||= {}
5
+ end
6
+
7
+ def flatten(current_hash = hash, keys = [])
8
+ current_hash.each do |key, value|
9
+ new_keys = keys.dup << key
10
+ if value.is_a? Hash
11
+ flatten(value, new_keys)
12
+ else
13
+ flat_hash[new_keys.join(".")] = value
14
+ end
15
+ end
16
+ flat_hash
17
+ end
18
+
19
+ def unflatten
20
+ {}.tap do |unflattened_hash|
21
+ hash.each do |k,v|
22
+ keys = k.split(".")
23
+ current_hash = unflattened_hash
24
+
25
+ keys[0...-1].each { |key| current_hash = current_hash[key] ||= {} }
26
+
27
+ current_hash[keys.last] = v
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,52 @@
1
+ require 'pry'
2
+ module BabelDiff
3
+ class ImportFileHandler < Struct.new(:import_directory, :phrase_directory)
4
+
5
+ def phrases
6
+ phrase_files = Dir.glob(phrase_directory + '/*.yml').map {|f| PhraseFile.new(f) }
7
+ import_files = Dir.glob(import_directory + '/*/*.yml').map {|f| ImportFile.new(f) }
8
+
9
+ matched_files = {}
10
+
11
+ phrase_files.each do |phrase|
12
+ if matched_import = import_files.detect {|i| i.language == phrase.language }
13
+ matched_files[phrase.language] = [phrase.contents, matched_import.contents]
14
+ end
15
+ end
16
+
17
+ matched_files
18
+ end
19
+
20
+ def update_phrase(language, contents)
21
+ File.open(phrase_directory + "/phrase.#{language}.yml", "w+") do |file|
22
+ file.write(contents)
23
+ end
24
+ end
25
+
26
+ class PhraseFile < Struct.new(:path)
27
+
28
+ def language
29
+ path.split("/").last.match(/phrase.(.*).yml/)[1]
30
+ end
31
+
32
+ def contents
33
+ File.read(path)
34
+ end
35
+
36
+ end
37
+
38
+ class ImportFile < Struct.new(:path)
39
+
40
+ def language
41
+ path.split("/")[-2]
42
+ end
43
+
44
+ def contents
45
+ File.read(path)
46
+ end
47
+
48
+ end
49
+
50
+
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module BabelDiff
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'yaml'
2
- require 'pry'
3
2
 
4
3
  module BabelDiff
5
4
  class YamlDiffer < Struct.new(:current_version, :previous_version)
@@ -7,14 +6,14 @@ module BabelDiff
7
6
  process_difference unless @processed
8
7
  @processed = true
9
8
 
10
- unflatten(updates_hash).to_yaml
9
+ HashFlattener.new(updates_hash).unflatten.to_yaml
11
10
  end
12
11
 
13
12
  def additions
14
13
  process_difference unless @processed
15
14
  @processed = true
16
15
 
17
- unflatten(additions_hash).to_yaml
16
+ HashFlattener.new(additions_hash).unflatten.to_yaml
18
17
  end
19
18
 
20
19
  def process_difference
@@ -30,22 +29,8 @@ module BabelDiff
30
29
  end
31
30
  end
32
31
 
33
-
34
32
  private
35
33
 
36
- def unflatten(hash)
37
- {}.tap do |unflattened_hash|
38
- hash.each do |k,v|
39
- keys = k.split(".")
40
- current_hash = unflattened_hash
41
-
42
- keys[0...-1].each { |key| current_hash = current_hash[key] ||= {} }
43
-
44
- current_hash[keys.last] = v
45
- end
46
- end
47
- end
48
-
49
34
  def updates_hash
50
35
  @updated_hash ||= {}
51
36
  end
@@ -63,23 +48,4 @@ module BabelDiff
63
48
  end
64
49
  end
65
50
 
66
- class HashFlattener < Struct.new(:hash)
67
-
68
- def flatten(current_hash = hash, keys = [])
69
- current_hash.each do |key, value|
70
- new_keys = keys.dup << key
71
- if value.is_a? Hash
72
- flatten(value, new_keys)
73
- else
74
- flat_hash[new_keys.join(".")] = value
75
- end
76
- end
77
-
78
- flat_hash
79
- end
80
-
81
- def flat_hash
82
- @_flat_hash ||= {}
83
- end
84
- end
85
51
  end
@@ -0,0 +1,20 @@
1
+ module BabelDiff
2
+ class YamlMerger < Struct.new(:original_contents, :flattened_contents_to_merge)
3
+
4
+ def merged_yaml
5
+ flat_original = HashFlattener.new(original_hash).flatten
6
+
7
+ flat_original.merge!(contents_to_merge)
8
+ HashFlattener.new(flat_original).unflatten.to_yaml
9
+ end
10
+
11
+ def original_hash
12
+ YAML.load(original_contents)
13
+ end
14
+
15
+ def contents_to_merge
16
+ YAML.load(flattened_contents_to_merge)
17
+ end
18
+
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babel_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan McGarvey
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-11 00:00:00.000000000 Z
12
+ date: 2015-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -60,6 +60,7 @@ email:
60
60
  - devs@gust.com
61
61
  executables:
62
62
  - babel_diff
63
+ - babel_import
63
64
  extensions: []
64
65
  extra_rdoc_files: []
65
66
  files:
@@ -75,12 +76,16 @@ files:
75
76
  - Rakefile
76
77
  - babel_diff.gemspec
77
78
  - bin/babel_diff
79
+ - bin/babel_import
78
80
  - bin/console
79
81
  - bin/setup
80
82
  - lib/babel_diff.rb
81
83
  - lib/babel_diff/file_handler.rb
84
+ - lib/babel_diff/hash_flattener.rb
85
+ - lib/babel_diff/import_file_handler.rb
82
86
  - lib/babel_diff/version.rb
83
87
  - lib/babel_diff/yaml_differ.rb
88
+ - lib/babel_diff/yaml_merger.rb
84
89
  homepage: https://github.com/zephyr-dev/babel_diff
85
90
  licenses:
86
91
  - MIT