filegen 0.0.1 → 0.1.1
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.
- data/.gitignore +1 -0
- data/.rdebugrc +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +10 -0
- data/.simplecov +9 -0
- data/.travis.yml +7 -0
- data/.yardopts +6 -0
- data/Gemfile +30 -0
- data/{LICENSE.txt → LICENSE.md} +1 -1
- data/README.DEVELOPER.md +28 -0
- data/README.md +106 -7
- data/Rakefile +60 -1
- data/bin/filegen +1 -1
- data/config/rubocop-disabled.yml +0 -0
- data/config/rubocop-enabled.yml +140 -0
- data/cucumber.yml +2 -0
- data/features/evaluate_template.feature +134 -0
- data/features/output_meta_data.feature +20 -0
- data/features/step_definitions.rb +9 -0
- data/features/support/env.rb +20 -0
- data/filegen.gemspec +8 -9
- data/lib/filegen.rb +11 -1
- data/lib/filegen/data.rb +48 -0
- data/lib/filegen/data_source_builder.rb +49 -0
- data/lib/filegen/data_sources.rb +5 -0
- data/lib/filegen/data_sources/environment.rb +21 -0
- data/lib/filegen/data_sources/yaml.rb +18 -0
- data/lib/filegen/erb_generator.rb +21 -6
- data/lib/filegen/exceptions.rb +8 -0
- data/lib/filegen/options.rb +77 -6
- data/lib/filegen/runner.rb +29 -9
- data/lib/filegen/version.rb +2 -1
- data/rubocop-todo.yml +144 -0
- data/scripts/terminal +12 -0
- data/spec/data_source_builder_spec.rb +53 -0
- data/spec/data_sources/environment_spec.rb +20 -0
- data/spec/data_sources/yaml_spec.rb +31 -0
- data/spec/data_spec.rb +60 -0
- data/spec/erb_generator_spec.rb +31 -0
- data/spec/options_spec.rb +32 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/config.rb +6 -0
- data/spec/support/debugging.rb +5 -0
- data/spec/support/environment.rb +7 -0
- data/spec/support/filesystem.rb +11 -0
- data/spec/support/here_doc.rb +2 -0
- metadata +58 -25
- data/lib/filegen/env.rb +0 -14
data/lib/filegen/runner.rb
CHANGED
@@ -1,22 +1,42 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Filegen
|
3
|
+
# Commandline from end
|
3
4
|
class Runner
|
5
|
+
private
|
6
|
+
|
4
7
|
attr_reader :options, :generator
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
public
|
10
|
+
|
11
|
+
# Create runner
|
12
|
+
# @param [Array] argv
|
13
|
+
# Commandline arguments
|
14
|
+
# @param [IO] stdin
|
15
|
+
# Stdin
|
16
|
+
# @param [IO] stdout
|
17
|
+
# Stdout
|
18
|
+
# @param [IO] stderr
|
19
|
+
# Stderr
|
20
|
+
# @param [Kernel] kernel
|
21
|
+
# Kernel class
|
22
|
+
def initialize(argv, stdin = $stdin, stdout = $stdout, stderr = $stderr, kernel = Kernel)
|
23
|
+
$stdin, $stdout, $stderr, @kernel = stdin, stdout, stderr, kernel
|
24
|
+
|
25
|
+
@options = Options.new(argv)
|
26
|
+
@generator = ErbGenerator.new(Data.new(@options.data_sources))
|
9
27
|
end
|
10
28
|
|
11
|
-
|
29
|
+
# Execute runner
|
30
|
+
def execute!
|
12
31
|
begin
|
13
|
-
generator.compile(options.source,options.destination)
|
14
|
-
|
15
|
-
|
16
|
-
|
32
|
+
generator.compile(options.source, options.destination)
|
33
|
+
exitstatus = 0
|
34
|
+
rescue RuntimeError => e
|
35
|
+
$stderr.puts e.message
|
36
|
+
exitstatus = 1
|
17
37
|
end
|
18
38
|
|
19
|
-
exit
|
39
|
+
@kernel.exit(exitstatus)
|
20
40
|
end
|
21
41
|
end
|
22
42
|
end
|
data/lib/filegen/version.rb
CHANGED
data/rubocop-todo.yml
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`.
|
2
|
+
# The point is for the user to remove these configuration records
|
3
|
+
# one by one as the offences are removed from the code base.
|
4
|
+
|
5
|
+
AccessModifierIndentation:
|
6
|
+
Enabled: true
|
7
|
+
|
8
|
+
Alias:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
AlignParameters:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Blocks:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
BracesAroundHashParameters:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
CaseEquality:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
CharacterLiteral:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
ClassVars:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
CollectionMethods:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
ColonMethodCall:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Documentation:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
EmptyLinesAroundAccessModifier:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
EmptyLinesAroundBody:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Encoding:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Eval:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
FinalNewline:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
HashSyntax:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
IfUnlessModifier:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
IndentationWidth:
|
60
|
+
Enabled: false
|
61
|
+
|
62
|
+
Lambda:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
LeadingCommentSpace:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
LineLength:
|
69
|
+
Max: 188
|
70
|
+
|
71
|
+
MethodLength:
|
72
|
+
Max: 23
|
73
|
+
|
74
|
+
ParenthesesAroundCondition:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
PerlBackrefs:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
RaiseArgs:
|
81
|
+
Enabled: false
|
82
|
+
|
83
|
+
RedundantSelf:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
RegexpLiteral:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
Semicolon:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
SignalException:
|
93
|
+
Enabled: false
|
94
|
+
|
95
|
+
SingleLineBlockParams:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
SpaceAfterComma:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
SpaceAfterControlKeyword:
|
102
|
+
Enabled: false
|
103
|
+
|
104
|
+
SpaceAfterNot:
|
105
|
+
Enabled: false
|
106
|
+
|
107
|
+
SpaceAroundBlockBraces:
|
108
|
+
Enabled: false
|
109
|
+
|
110
|
+
SpaceAroundEqualsInParameterDefault:
|
111
|
+
Enabled: false
|
112
|
+
|
113
|
+
SpaceAroundOperators:
|
114
|
+
Enabled: false
|
115
|
+
|
116
|
+
SpaceInsideBrackets:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
SpaceInsideHashLiteralBraces:
|
120
|
+
Enabled: false
|
121
|
+
|
122
|
+
SpaceInsideParens:
|
123
|
+
Enabled: false
|
124
|
+
|
125
|
+
SpecialGlobalVars:
|
126
|
+
Enabled: false
|
127
|
+
|
128
|
+
StringLiterals:
|
129
|
+
Enabled: false
|
130
|
+
|
131
|
+
TrailingBlankLines:
|
132
|
+
Enabled: false
|
133
|
+
|
134
|
+
TrailingWhitespace:
|
135
|
+
Enabled: false
|
136
|
+
|
137
|
+
TrivialAccessors:
|
138
|
+
Enabled: false
|
139
|
+
|
140
|
+
UselessAssignment:
|
141
|
+
Enabled: false
|
142
|
+
|
143
|
+
WordArray:
|
144
|
+
Enabled: false
|
data/scripts/terminal
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'tmrb'
|
5
|
+
|
6
|
+
tmux = TmRb::Multiplexer.new
|
7
|
+
# creates an empty window
|
8
|
+
tmux.new_session(session_name: 'filegen')
|
9
|
+
|
10
|
+
tmux.new_window(name: 'code', command: 'vim -c :NERDTree lib')
|
11
|
+
tmux.new_window(name: 'spec', command: 'vim -c :NERDTree spec')
|
12
|
+
tmux.start
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe DataSourceBuilder do
|
5
|
+
context '#sources' do
|
6
|
+
it 'returns data sources' do
|
7
|
+
yaml_builder = double('yaml_builder')
|
8
|
+
env_builder = double('env_builder')
|
9
|
+
|
10
|
+
params = double('params')
|
11
|
+
allow(params).to receive(:data_sources).and_return([:yaml, :env])
|
12
|
+
allow(params).to receive(:data_source_builders).and_return(yaml: yaml_builder, env: env_builder)
|
13
|
+
allow(params).to receive(:yaml_file).and_return('')
|
14
|
+
|
15
|
+
expect { DataSourceBuilder.new(params) }.not_to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises an exception on invalid input' do
|
19
|
+
params = double('params')
|
20
|
+
allow(params).to receive(:data_sources).and_return([:yaml, 'garbage'])
|
21
|
+
allow(params).to receive(:data_source_builders).and_return({})
|
22
|
+
allow(params).to receive(:yaml_file).and_return('')
|
23
|
+
|
24
|
+
expect { DataSourceBuilder.new(params) }.to raise_error Exceptions::InvalidDataSources
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'supports order of data sources: env first' do
|
28
|
+
yaml_builder = double('yaml_builder')
|
29
|
+
env_builder = double('env_builder')
|
30
|
+
|
31
|
+
params = double('params')
|
32
|
+
allow(params).to receive(:data_sources).and_return([:env, :yaml])
|
33
|
+
allow(params).to receive(:data_source_builders).and_return(yaml: yaml_builder, env: env_builder)
|
34
|
+
allow(params).to receive(:yaml_file).and_return('')
|
35
|
+
|
36
|
+
builder = DataSourceBuilder.new(params)
|
37
|
+
expect(builder.sources.first).to be(env_builder)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'supports order of data sources: yaml first' do
|
41
|
+
yaml_builder = double('yaml_builder')
|
42
|
+
env_builder = double('env_builder')
|
43
|
+
|
44
|
+
params = double('params')
|
45
|
+
allow(params).to receive(:data_sources).and_return([:yaml, :env])
|
46
|
+
allow(params).to receive(:data_source_builders).and_return(yaml: yaml_builder, env: env_builder)
|
47
|
+
allow(params).to receive(:yaml_file).and_return('')
|
48
|
+
|
49
|
+
builder = DataSourceBuilder.new(params)
|
50
|
+
expect(builder.sources.first).to be yaml_builder
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe DataSources::Environment do
|
5
|
+
context '#fetch' do
|
6
|
+
it 'retrieves environment variables' do
|
7
|
+
in_environment 'MY_NAME' => 'Karl' do
|
8
|
+
source = DataSources::Environment.new
|
9
|
+
expect(source.fetch('MY_NAME')).to eq('Karl')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns nil if variable cannot be found' do
|
14
|
+
in_environment 'MY_NAME' => 'Karl' do
|
15
|
+
source = DataSources::Environment.new
|
16
|
+
expect(source.fetch('NOT_MY_NAME')).to be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe DataSources::Yaml do
|
5
|
+
context '#fetch' do
|
6
|
+
it 'retrieves variables from yaml file' do
|
7
|
+
file = create_file('blub.yaml', <<-EOF.strip_heredoc
|
8
|
+
---
|
9
|
+
MY_NAME: Karl
|
10
|
+
EOF
|
11
|
+
)
|
12
|
+
source = DataSources::Yaml.new(file)
|
13
|
+
expect(source.fetch('MY_NAME')).to eq('Karl')
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns nil if variable cannot be found' 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('NOT_MY_NAME')).to be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'returns nil if file cannot be found' do
|
27
|
+
source = DataSources::Yaml.new('not valid file')
|
28
|
+
expect(source.fetch('NOT_MY_NAME')).to be_nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/data_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Filegen::Data do
|
5
|
+
context '#instance_binding' do
|
6
|
+
it 'let you variables' do
|
7
|
+
source = double('DataSource')
|
8
|
+
expect(source).to receive(:fetch).with('MY_NAME').and_return('Karl')
|
9
|
+
|
10
|
+
data = Filegen::Data.new(source)
|
11
|
+
# rubocop:disable Eval
|
12
|
+
result = eval('lookup("MY_NAME")', data.instance_binding)
|
13
|
+
# rubocop:enable Eval
|
14
|
+
expect(result).to eq('Karl')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context '#lookup' do
|
19
|
+
it 'provides access to variables' do
|
20
|
+
source = double('DataSource')
|
21
|
+
expect(source).to receive(:fetch).with('MY_NAME').and_return('Karl')
|
22
|
+
|
23
|
+
data = Filegen::Data.new(source)
|
24
|
+
expect(data.lookup('MY_NAME')).to eq('Karl')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'supports multiple data sources: first has match' do
|
28
|
+
source1 = double('DataSource1')
|
29
|
+
expect(source1).to receive(:fetch).with('MY_NAME').and_return('Egon')
|
30
|
+
|
31
|
+
source2 = double('DataSource2')
|
32
|
+
expect(source2).not_to receive(:fetch)
|
33
|
+
|
34
|
+
data = Filegen::Data.new([source1, source2])
|
35
|
+
expect(data.lookup('MY_NAME')).to eq('Egon')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'supports multiple data sources: last has match' do
|
39
|
+
source1 = double('DataSource1')
|
40
|
+
expect(source1).to receive(:fetch).with('MY_NAME').and_return(nil)
|
41
|
+
|
42
|
+
source2 = double('DataSource2')
|
43
|
+
expect(source2).to receive(:fetch).with('MY_NAME').and_return('Egon')
|
44
|
+
|
45
|
+
data = Filegen::Data.new([source1, source2])
|
46
|
+
expect(data.lookup('MY_NAME')).to eq('Egon')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'returns "" if none matches' do
|
50
|
+
source1 = double('DataSource1')
|
51
|
+
expect(source1).to receive(:fetch).with('MY_NAME').and_return(nil)
|
52
|
+
|
53
|
+
source2 = double('DataSource2')
|
54
|
+
expect(source2).to receive(:fetch).with('MY_NAME').and_return(nil)
|
55
|
+
|
56
|
+
data = Filegen::Data.new([source1, source2])
|
57
|
+
expect(data.lookup('MY_NAME')).to eq('')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ErbGenerator do
|
5
|
+
context '#compile' do
|
6
|
+
it 'needs a source and a target' do
|
7
|
+
data = double('binding')
|
8
|
+
allow(data).to receive(:instance_binding) do
|
9
|
+
Class.new do
|
10
|
+
def name
|
11
|
+
'Karl'
|
12
|
+
end
|
13
|
+
|
14
|
+
def instance_binding
|
15
|
+
binding
|
16
|
+
end
|
17
|
+
end.new.instance_binding
|
18
|
+
end
|
19
|
+
|
20
|
+
source = double('source')
|
21
|
+
allow(source).to receive(:encoding)
|
22
|
+
allow(source).to receive(:read).and_return('Hello <%= name %>')
|
23
|
+
|
24
|
+
destination = double('destination')
|
25
|
+
expect(destination).to receive(:puts).with('Hello Karl')
|
26
|
+
|
27
|
+
gen = ErbGenerator.new(data)
|
28
|
+
gen.compile(source, destination)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Options do
|
5
|
+
context '#destination' do
|
6
|
+
it 'returns an io handle' do
|
7
|
+
options = Options.new([])
|
8
|
+
expect(options.destination).to respond_to(:puts)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context '#source' do
|
13
|
+
it 'returns an io handle' do
|
14
|
+
template_file = create_file('template.erb')
|
15
|
+
|
16
|
+
options = Options.new([template_file])
|
17
|
+
expect(options.source).to respond_to(:read)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises an exception if file does not exist' do
|
21
|
+
options = Options.new(['template_file'])
|
22
|
+
expect { options.source }.to raise_error RuntimeError
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'raises an exception if file does not have a erb-extension' do
|
26
|
+
template_file = create_file('template')
|
27
|
+
|
28
|
+
options = Options.new([template_file])
|
29
|
+
expect { options.source }.to raise_error RuntimeError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|