yamljam 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/yamljam +1 -1
- data/lib/yamljam.rb +1 -1
- data/lib/yamljam/jammer.rb +52 -0
- data/lib/yamljam/version.rb +1 -1
- data/yamljam.gemspec +1 -1
- metadata +20 -7
- data/lib/yamljam/concatenator.rb +0 -41
data/bin/yamljam
CHANGED
@@ -7,7 +7,7 @@ if !ARGV[0] || ARGV[0] == "--help"
|
|
7
7
|
puts "Creates a <directory>.yml file containing the keys from all yml files found in <directory>. Does not work recursively yet!"
|
8
8
|
else
|
9
9
|
begin
|
10
|
-
Yamljam::Jammer.new.jam(
|
10
|
+
Yamljam::Jammer.new.jam(ARGV[0])
|
11
11
|
rescue Exception => ex
|
12
12
|
puts "failed due to exception: #{ex.class}! message was #{ex.message}"
|
13
13
|
end
|
data/lib/yamljam.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Yamljam
|
2
|
+
class Jammer
|
3
|
+
def merged_hash
|
4
|
+
@hash ||= {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def write_output(output_hashmap, output_file)
|
8
|
+
begin
|
9
|
+
File.open(output_file, 'w') do |f|
|
10
|
+
f.puts("# Generated by Yamljam. Do not directly edit this file.")
|
11
|
+
f.write(output_hashmap.to_yaml)
|
12
|
+
end
|
13
|
+
rescue Exception => ex # if it's any solace I cringed when I wrote that
|
14
|
+
puts "hit exception when writing output file! #{ex.class} #{ex.message} #{ex.backtrace}"
|
15
|
+
raise ex
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def merge_files(input_files, namespace)
|
20
|
+
{namespace => input_files.map{|input_file| make_hashmap(input_file)}.reduce({}, &:merge)}
|
21
|
+
end
|
22
|
+
|
23
|
+
def make_hashmap(input_file)
|
24
|
+
base_hashmap = YAML::load_file(input_file)
|
25
|
+
end
|
26
|
+
|
27
|
+
def jam(input_directory_name)
|
28
|
+
write_output(jam_recursive(input_directory_name), input_directory_name + ".yml")
|
29
|
+
end
|
30
|
+
|
31
|
+
def jam_recursive(input_directory_name)
|
32
|
+
Dir.chdir(input_directory_name) do
|
33
|
+
directories = Dir.entries('.').reject{|entry| !File.directory?(entry) || entry == '.' || entry == '..'}
|
34
|
+
yaml_files = Dir.entries('.').reject{|entry| File.directory?(entry) || !is_yaml?(entry)}
|
35
|
+
merged_yaml = merge_files(yaml_files, input_directory_name)
|
36
|
+
more_yamls = directories.map{|directory| jam_recursive(directory)}
|
37
|
+
|
38
|
+
total_merged_yaml = {input_directory_name => more_yamls.reduce(merged_yaml[input_directory_name], &:merge)}
|
39
|
+
return total_merged_yaml
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def name_of_dotdot
|
44
|
+
# is there seriously not a better way to do this?
|
45
|
+
Dir.chdir(".."){File.split(Dir.getwd).last}
|
46
|
+
end
|
47
|
+
|
48
|
+
def is_yaml?(filename)
|
49
|
+
%w[yml yaml].include?(filename.split(".").last)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/yamljam/version.rb
CHANGED
data/yamljam.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yamljam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Bill Abney
|
@@ -15,9 +15,22 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-06-
|
19
|
-
dependencies:
|
20
|
-
|
18
|
+
date: 2012-06-13 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
21
34
|
description: concatenates multiple yaml files from a subdirectory into one yaml file. Useful for splitting up and re-joining en.yml
|
22
35
|
email:
|
23
36
|
- bill.abney@gmail.com
|
@@ -34,7 +47,7 @@ files:
|
|
34
47
|
- Rakefile
|
35
48
|
- bin/yamljam
|
36
49
|
- lib/yamljam.rb
|
37
|
-
- lib/yamljam/
|
50
|
+
- lib/yamljam/jammer.rb
|
38
51
|
- lib/yamljam/version.rb
|
39
52
|
- yamljam.gemspec
|
40
53
|
homepage: ""
|
data/lib/yamljam/concatenator.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
module Yamljam
|
2
|
-
class Jammer
|
3
|
-
def concatenate(input_files, output_file)
|
4
|
-
output_hashmap = merge_files(input_files)
|
5
|
-
write_output(output_hashmap, output_file)
|
6
|
-
end
|
7
|
-
|
8
|
-
def write_output(output_hashmap, output_file)
|
9
|
-
begin
|
10
|
-
File.open(output_file, 'w') do |f|
|
11
|
-
f.puts("# Generated by Yamljam. Do not directly edit this file.")
|
12
|
-
f.write(output_hashmap.to_yaml)
|
13
|
-
end
|
14
|
-
rescue Exception => ex # if it's any solace I cringed when I wrote that
|
15
|
-
puts "hit exception when writing output file! #{ex.class} #{ex.message} #{ex.backtrace}"
|
16
|
-
raise ex
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def merge_files(input_files)
|
21
|
-
{@namespace => input_files.map{|input_file| make_hashmap(input_file)}.reduce({}, &:merge)}
|
22
|
-
end
|
23
|
-
|
24
|
-
def make_hashmap(input_file)
|
25
|
-
base_hashmap = YAML::load_file(input_file)
|
26
|
-
end
|
27
|
-
|
28
|
-
def jam(input_directory_path, namespace)
|
29
|
-
# input directory will contain #{namespace}.yml (output file, which will be blown away)
|
30
|
-
# and a directory called #{namespace}. All files in the #{namespace} directory will be
|
31
|
-
# assumed to be yaml input files if they end in .yml or .yaml, and ignored otherwise.
|
32
|
-
@namespace = namespace
|
33
|
-
input_directory_path ||= Dir.getwd
|
34
|
-
input_file_dir = File.join(input_directory_path, namespace)
|
35
|
-
Dir.chdir(input_file_dir) do
|
36
|
-
input_files = Dir.glob("*.yaml") + Dir.glob("*.yml")
|
37
|
-
concatenate(input_files, File.join("..", namespace + ".yml"))
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|