filegen 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -3
- data/Rakefile +3 -1
- data/config/rubocop-enabled.yml +2 -2
- data/features/step_definitions.rb +1 -1
- data/filegen.gemspec +0 -2
- data/lib/filegen.rb +3 -1
- data/lib/filegen/data_sources/environment.rb +10 -8
- data/lib/filegen/data_sources/yaml.rb +12 -5
- data/lib/filegen/options.rb +1 -1
- data/lib/filegen/rubygen.rb +29 -0
- data/lib/filegen/ui.rb +1 -1
- data/lib/filegen/version.rb +1 -1
- data/spec/data_sources/environment_spec.rb +8 -1
- data/spec/data_sources/yaml_spec.rb +10 -0
- data/spec/ruby_gen_spec.rb +36 -0
- data/spec/rubygen_spec.rb +21 -0
- data/spec/spec_helper.rb +5 -0
- metadata +14 -19
data/README.md
CHANGED
@@ -26,7 +26,7 @@ Or install it yourself as:
|
|
26
26
|
This gem ships with an executable called `filegen`. It makes the power of ERB
|
27
27
|
available on the commandline.
|
28
28
|
|
29
|
-
## Usage
|
29
|
+
## Usage on Commandline
|
30
30
|
|
31
31
|
### General advice
|
32
32
|
|
@@ -127,6 +127,48 @@ And get the following result.
|
|
127
127
|
Hello my name is: Karl
|
128
128
|
```
|
129
129
|
|
130
|
+
## Usage within ruby
|
131
|
+
|
132
|
+
### Hash with Symbol-Keys
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
require 'filegen'
|
136
|
+
require 'stringio'
|
137
|
+
|
138
|
+
template = <<-EOS
|
139
|
+
Hi <%= lookup('name') %>
|
140
|
+
EOS
|
141
|
+
|
142
|
+
data = {
|
143
|
+
name: 'Karl'
|
144
|
+
}
|
145
|
+
|
146
|
+
generator = Filegen::Rubygen.new
|
147
|
+
puts generator.run(template, data)
|
148
|
+
|
149
|
+
# => Hi Karl
|
150
|
+
```
|
151
|
+
|
152
|
+
### Hash with String-Keys
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
require 'filegen'
|
156
|
+
require 'stringio'
|
157
|
+
|
158
|
+
template = <<-EOS
|
159
|
+
Hi <%= lookup('name') %>
|
160
|
+
EOS
|
161
|
+
|
162
|
+
data = {
|
163
|
+
'name' => 'Karl'
|
164
|
+
}
|
165
|
+
|
166
|
+
generator = Filegen::Rubygen.new
|
167
|
+
puts generator.run(template, data)
|
168
|
+
|
169
|
+
# => Hi Karl
|
170
|
+
```
|
171
|
+
|
130
172
|
## Use Cases
|
131
173
|
|
132
174
|
### RPM packaging
|
@@ -162,8 +204,7 @@ chmod +x <wrapper>
|
|
162
204
|
|
163
205
|
## Future
|
164
206
|
|
165
|
-
* Maybe I will add additional data sources.
|
166
|
-
[Moneta](https://github.com/minad/moneta)-gem for possible candidates.
|
207
|
+
* Maybe I will add additional data sources.
|
167
208
|
|
168
209
|
## Contributing
|
169
210
|
|
data/Rakefile
CHANGED
data/config/rubocop-enabled.yml
CHANGED
data/filegen.gemspec
CHANGED
data/lib/filegen.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'erb'
|
3
|
-
require 'moneta'
|
4
3
|
require 'optparse'
|
5
4
|
require 'forwardable'
|
6
5
|
require 'ostruct'
|
7
6
|
require 'logger'
|
7
|
+
require 'active_support/hash_with_indifferent_access'
|
8
|
+
require 'stringio'
|
8
9
|
|
9
10
|
require 'filegen/version'
|
10
11
|
require 'filegen/ui'
|
@@ -15,6 +16,7 @@ require 'filegen/erb_generator'
|
|
15
16
|
require 'filegen/data_source_builder'
|
16
17
|
require 'filegen/data_sources/environment'
|
17
18
|
require 'filegen/data_sources/yaml'
|
19
|
+
require 'filegen/rubygen'
|
18
20
|
require 'filegen/exceptions'
|
19
21
|
|
20
22
|
# Main class
|
@@ -1,20 +1,22 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Filegen
|
3
|
+
# Data Sources
|
3
4
|
module DataSources
|
4
5
|
# Data source which makes environment variables available
|
5
6
|
class Environment
|
6
|
-
|
7
|
+
private
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
attr_reader :source
|
10
|
+
|
11
|
+
public
|
11
12
|
|
12
13
|
# Create data source
|
13
14
|
def initialize
|
14
|
-
@source =
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
@source = HashWithIndifferentAccess.new(ENV.to_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def fetch(key, default_value = nil)
|
19
|
+
source[key.to_sym] || source[key.to_s] || default_value
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -1,17 +1,24 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Filegen
|
3
|
+
# Data Sources
|
3
4
|
module DataSources
|
4
5
|
# Data source which makes yaml files available
|
5
6
|
class Yaml
|
6
|
-
|
7
|
+
private
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
attr_reader :source
|
10
|
+
|
11
|
+
public
|
11
12
|
|
12
13
|
# Create data source
|
13
14
|
def initialize(file)
|
14
|
-
@source =
|
15
|
+
@source = HashWithIndifferentAccess.new(Psych.load_file(file))
|
16
|
+
rescue
|
17
|
+
@source = HashWithIndifferentAccess.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def fetch(key, default_value = nil)
|
21
|
+
source[key.to_sym] || source[key.to_s] || default_value
|
15
22
|
end
|
16
23
|
end
|
17
24
|
end
|
data/lib/filegen/options.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Filegen
|
3
|
+
# Generator for ruby code
|
4
|
+
class Rubygen
|
5
|
+
# Generate text
|
6
|
+
#
|
7
|
+
# @param [String] source
|
8
|
+
# The template used
|
9
|
+
# @param [Hash] data
|
10
|
+
# The data used within template evaluation
|
11
|
+
# @return [String]
|
12
|
+
# The evaluated template
|
13
|
+
def run(source, data)
|
14
|
+
result = StringIO.new
|
15
|
+
source = StringIO.new(source)
|
16
|
+
|
17
|
+
begin
|
18
|
+
generator = ErbGenerator.new(Data.new([HashWithIndifferentAccess.new(data)]))
|
19
|
+
generator.compile(source, result)
|
20
|
+
rescue RuntimeError => e
|
21
|
+
Filegen::Ui.error e.message
|
22
|
+
rescue Exceptions::ErbTemplateHasSyntaxErrors => e
|
23
|
+
Filegen::Ui.error "Syntax error in ERB-Template: \n" + e.message
|
24
|
+
end
|
25
|
+
|
26
|
+
result.string
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/filegen/ui.rb
CHANGED
data/lib/filegen/version.rb
CHANGED
@@ -3,13 +3,20 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe DataSources::Environment do
|
5
5
|
context '#fetch' do
|
6
|
-
it 'retrieves environment variables' do
|
6
|
+
it 'retrieves environment variables as string' do
|
7
7
|
in_environment 'MY_NAME' => 'Karl' do
|
8
8
|
source = DataSources::Environment.new
|
9
9
|
expect(source.fetch('MY_NAME')).to eq('Karl')
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
it 'retrieves environment variables as symbol' do
|
14
|
+
in_environment 'MY_NAME' => 'Karl' do
|
15
|
+
source = DataSources::Environment.new
|
16
|
+
expect(source.fetch(:MY_NAME)).to eq('Karl')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
13
20
|
it 'returns nil if variable cannot be found' do
|
14
21
|
in_environment 'MY_NAME' => 'Karl' do
|
15
22
|
source = DataSources::Environment.new
|
@@ -13,6 +13,16 @@ describe DataSources::Yaml do
|
|
13
13
|
expect(source.fetch('MY_NAME')).to eq('Karl')
|
14
14
|
end
|
15
15
|
|
16
|
+
it 'retrieves symbol variables from yaml file' do
|
17
|
+
file = create_file('blub.yaml', <<-EOF.strip_heredoc
|
18
|
+
---
|
19
|
+
:MY_NAME: Karl
|
20
|
+
EOF
|
21
|
+
)
|
22
|
+
source = DataSources::Yaml.new(file)
|
23
|
+
expect(source.fetch('MY_NAME')).to eq('Karl')
|
24
|
+
end
|
25
|
+
|
16
26
|
it 'returns nil if variable cannot be found' do
|
17
27
|
file = create_file('blub.yaml', <<-EOF.strip_heredoc
|
18
28
|
---
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Rubygen do
|
5
|
+
context '#execute' do
|
6
|
+
it 'generates output based on hash with symbol keys' do
|
7
|
+
template = <<-EOS.strip_heredoc
|
8
|
+
<%= lookup('name') %>
|
9
|
+
EOS
|
10
|
+
|
11
|
+
data = {
|
12
|
+
name: 'karl'
|
13
|
+
}
|
14
|
+
|
15
|
+
generator = Rubygen.new
|
16
|
+
result = generator.run(template, data)
|
17
|
+
|
18
|
+
expect(result).to include('karl')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'generates output based on hash with string keys' do
|
22
|
+
template = <<-EOS.strip_heredoc
|
23
|
+
<%= lookup('name') %>
|
24
|
+
EOS
|
25
|
+
|
26
|
+
data = {
|
27
|
+
'name' => 'karl'
|
28
|
+
}
|
29
|
+
|
30
|
+
generator = Rubygen.new
|
31
|
+
result = generator.run(template, data)
|
32
|
+
|
33
|
+
expect(result).to include('karl')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Rubygen do
|
5
|
+
context '#execute' do
|
6
|
+
it 'generates output' do
|
7
|
+
template = <<-EOS.strip_heredoc
|
8
|
+
<%= lookup('name') %>
|
9
|
+
EOS
|
10
|
+
|
11
|
+
data = {
|
12
|
+
name: 'karl'
|
13
|
+
}
|
14
|
+
|
15
|
+
generator = Rubygen.new
|
16
|
+
result = generator.run(template, data)
|
17
|
+
|
18
|
+
expect(result).to include('karl')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,11 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
3
3
|
|
4
4
|
require 'simplecov'
|
5
5
|
SimpleCov.command_name 'rspec'
|
6
|
+
SimpleCov.start
|
7
|
+
|
8
|
+
# Pull in all of the gems including those in the `test` group
|
9
|
+
require 'bundler'
|
10
|
+
Bundler.require :default, :test, :development
|
6
11
|
|
7
12
|
require 'filegen'
|
8
13
|
include Filegen
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filegen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: moneta
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
12
|
+
date: 2014-03-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
30
14
|
description: Evaluate erb templates
|
31
15
|
email:
|
32
16
|
- dg1@vrnetze.de
|
@@ -67,6 +51,7 @@ files:
|
|
67
51
|
- lib/filegen/erb_generator.rb
|
68
52
|
- lib/filegen/exceptions.rb
|
69
53
|
- lib/filegen/options.rb
|
54
|
+
- lib/filegen/rubygen.rb
|
70
55
|
- lib/filegen/runner.rb
|
71
56
|
- lib/filegen/ui.rb
|
72
57
|
- lib/filegen/version.rb
|
@@ -78,6 +63,8 @@ files:
|
|
78
63
|
- spec/data_spec.rb
|
79
64
|
- spec/erb_generator_spec.rb
|
80
65
|
- spec/options_spec.rb
|
66
|
+
- spec/ruby_gen_spec.rb
|
67
|
+
- spec/rubygen_spec.rb
|
81
68
|
- spec/spec_helper.rb
|
82
69
|
- spec/support/config.rb
|
83
70
|
- spec/support/debugging.rb
|
@@ -97,12 +84,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
84
|
- - ! '>='
|
98
85
|
- !ruby/object:Gem::Version
|
99
86
|
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: 1267357899602957187
|
100
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
91
|
none: false
|
102
92
|
requirements:
|
103
93
|
- - ! '>='
|
104
94
|
- !ruby/object:Gem::Version
|
105
95
|
version: '0'
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: 1267357899602957187
|
106
99
|
requirements: []
|
107
100
|
rubyforge_project:
|
108
101
|
rubygems_version: 1.8.23
|
@@ -122,6 +115,8 @@ test_files:
|
|
122
115
|
- spec/data_spec.rb
|
123
116
|
- spec/erb_generator_spec.rb
|
124
117
|
- spec/options_spec.rb
|
118
|
+
- spec/ruby_gen_spec.rb
|
119
|
+
- spec/rubygen_spec.rb
|
125
120
|
- spec/spec_helper.rb
|
126
121
|
- spec/support/config.rb
|
127
122
|
- spec/support/debugging.rb
|