filegen 0.2.1 → 0.2.2
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/.rubocop.yml +1 -0
- data/CHANGELOG.md +5 -0
- data/features/error_handling.feature +28 -0
- data/features/evaluate_template.feature +0 -15
- data/lib/filegen.rb +2 -0
- data/lib/filegen/data.rb +1 -1
- data/lib/filegen/data_source_builder.rb +1 -1
- data/lib/filegen/exceptions.rb +10 -1
- data/lib/filegen/options.rb +23 -4
- data/lib/filegen/runner.rb +7 -6
- data/lib/filegen/ui.rb +38 -0
- data/lib/filegen/version.rb +1 -1
- data/spec/data_source_builder_spec.rb +1 -1
- data/spec/options_spec.rb +4 -6
- metadata +4 -1
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
Feature: Error handling
|
2
|
+
|
3
|
+
As a user
|
4
|
+
I want to get good error message
|
5
|
+
In order to fix my mistakes
|
6
|
+
|
7
|
+
Scenario: Missing file name
|
8
|
+
When I run `filegen`
|
9
|
+
Then the output should contain:
|
10
|
+
"""
|
11
|
+
Template file name is missing.
|
12
|
+
"""
|
13
|
+
|
14
|
+
Scenario: Non existing file
|
15
|
+
When I run `filegen template1.erb`
|
16
|
+
Then the stderr should contain:
|
17
|
+
"""
|
18
|
+
File "template1.erb" does not exist
|
19
|
+
"""
|
20
|
+
|
21
|
+
Scenario: Non erb file
|
22
|
+
Given an empty file named "template1.abc"
|
23
|
+
When I run `filegen template1.abc`
|
24
|
+
Then the stderr should contain:
|
25
|
+
"""
|
26
|
+
File "template1.abc" is not a valid erb template: file ending erb
|
27
|
+
"""
|
28
|
+
|
@@ -29,21 +29,6 @@ Feature: Evaluate Template
|
|
29
29
|
Hello Karl
|
30
30
|
"""
|
31
31
|
|
32
|
-
Scenario: Non existing file
|
33
|
-
When I run `filegen template1.erb`
|
34
|
-
Then the stderr should contain:
|
35
|
-
"""
|
36
|
-
File "template1.erb" does not exist
|
37
|
-
"""
|
38
|
-
|
39
|
-
Scenario: Non erb file
|
40
|
-
Given an empty file named "template1.abc"
|
41
|
-
When I run `filegen template1.abc`
|
42
|
-
Then the stderr should contain:
|
43
|
-
"""
|
44
|
-
File "template1.abc" is not a valid erb template: file ending erb
|
45
|
-
"""
|
46
|
-
|
47
32
|
Scenario: YAML file as input (short)
|
48
33
|
Given a file named "template.erb" with:
|
49
34
|
"""
|
data/lib/filegen.rb
CHANGED
data/lib/filegen/data.rb
CHANGED
@@ -43,7 +43,7 @@ module Filegen
|
|
43
43
|
|
44
44
|
def validate_data_sources
|
45
45
|
invalid_data_sources = chosen_data_sources - allowed_data_sources
|
46
|
-
fail Exceptions::
|
46
|
+
fail Exceptions::DataSourcesAreInvalid, "Unknown data source#{invalid_data_sources.size > 1 ? 's' : ''} \"#{invalid_data_sources.join(', ')}\" found." unless invalid_data_sources.empty?
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
data/lib/filegen/exceptions.rb
CHANGED
@@ -3,6 +3,15 @@ module Filegen
|
|
3
3
|
# Exceptions
|
4
4
|
module Exceptions
|
5
5
|
# raised if order arguments are invalid
|
6
|
-
class
|
6
|
+
class DataSourcesAreInvalid < RuntimeError; end
|
7
|
+
|
8
|
+
# raised if one forgot to tell the script the template name
|
9
|
+
class TemplateNameIsMissing < RuntimeError; end
|
10
|
+
|
11
|
+
# raised if template given on commandline does not exist
|
12
|
+
class TemplateDoesNotExist < RuntimeError; end
|
13
|
+
|
14
|
+
# raised if one uses an invalid template name (missing erb)
|
15
|
+
class TemplateNameIsInvalid < RuntimeError; end
|
7
16
|
end
|
8
17
|
end
|
data/lib/filegen/options.rb
CHANGED
@@ -14,6 +14,8 @@ module Filegen
|
|
14
14
|
# The array which contains the commandline arguments
|
15
15
|
def initialize(argv)
|
16
16
|
@params = parse_options(argv)
|
17
|
+
|
18
|
+
validate_params
|
17
19
|
end
|
18
20
|
|
19
21
|
# Source template
|
@@ -21,8 +23,6 @@ module Filegen
|
|
21
23
|
# @return [File]
|
22
24
|
# Returns a file handle for the template
|
23
25
|
def source
|
24
|
-
validate_source
|
25
|
-
|
26
26
|
File.new(params.template)
|
27
27
|
end
|
28
28
|
|
@@ -44,10 +44,20 @@ module Filegen
|
|
44
44
|
|
45
45
|
private
|
46
46
|
|
47
|
+
def validate_params
|
48
|
+
validate_source
|
49
|
+
end
|
50
|
+
|
51
|
+
# rubocop:disable MethodLength
|
47
52
|
def parse_options(argv)
|
48
53
|
params = OpenStruct.new
|
49
54
|
parser = OptionParser.new
|
50
55
|
|
56
|
+
parser.banner = 'Usage: filegen [options] <template>'
|
57
|
+
|
58
|
+
parser.separator ''
|
59
|
+
parser.separator 'Specific options:'
|
60
|
+
|
51
61
|
params.data_sources = [:env]
|
52
62
|
params.data_source_builders = {}
|
53
63
|
params.data_source_builders[:env] = DataSources::Environment.new
|
@@ -62,6 +72,9 @@ module Filegen
|
|
62
72
|
params.data_sources = l.map(&:to_sym)
|
63
73
|
end
|
64
74
|
|
75
|
+
parser.separator ''
|
76
|
+
parser.separator 'Common options:'
|
77
|
+
|
65
78
|
parser.on_tail('-h', '--help', 'Show this message') do
|
66
79
|
$stderr.puts parser
|
67
80
|
exit
|
@@ -76,10 +89,16 @@ module Filegen
|
|
76
89
|
|
77
90
|
params
|
78
91
|
end
|
92
|
+
# rubocop:enable MethodLength
|
79
93
|
|
80
94
|
def validate_source
|
81
|
-
fail
|
82
|
-
fail "File \"#{params.template}\"
|
95
|
+
fail Exceptions::TemplateNameIsMissing, 'Template file name is missing.' if empty?
|
96
|
+
fail Exceptions::TemplateDoesNotExist, "File \"#{params.template}\" does not exist" unless exists?
|
97
|
+
fail Exceptions::TemplateNameIsInvalid, "File \"#{params.template}\" is not a valid erb template: file ending erb" unless erb_template?
|
98
|
+
end
|
99
|
+
|
100
|
+
def empty?
|
101
|
+
params.template.nil?
|
83
102
|
end
|
84
103
|
|
85
104
|
def exists?
|
data/lib/filegen/runner.rb
CHANGED
@@ -4,7 +4,7 @@ module Filegen
|
|
4
4
|
class Runner
|
5
5
|
private
|
6
6
|
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :argv
|
8
8
|
|
9
9
|
public
|
10
10
|
|
@@ -20,19 +20,20 @@ module Filegen
|
|
20
20
|
# @param [Kernel] kernel
|
21
21
|
# Kernel class
|
22
22
|
def initialize(argv, stdin = $stdin, stdout = $stdout, stderr = $stderr, kernel = Kernel)
|
23
|
-
$stdin, $stdout,
|
24
|
-
|
25
|
-
@options = Options.new(argv)
|
26
|
-
@generator = ErbGenerator.new(Data.new(@options.data_sources))
|
23
|
+
@argv, $stdin, $stdout, Ui.logger, @kernel = argv, stdin, stdout, stderr, kernel
|
27
24
|
end
|
28
25
|
|
29
26
|
# Execute runner
|
30
27
|
def execute!
|
31
28
|
begin
|
29
|
+
options = Options.new(argv)
|
30
|
+
|
31
|
+
generator = ErbGenerator.new(Data.new(options.data_sources))
|
32
32
|
generator.compile(options.source, options.destination)
|
33
|
+
|
33
34
|
exitstatus = 0
|
34
35
|
rescue RuntimeError => e
|
35
|
-
|
36
|
+
Filegen::Ui.error e.message
|
36
37
|
exitstatus = 1
|
37
38
|
end
|
38
39
|
|
data/lib/filegen/ui.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Filegen
|
3
|
+
# Methods for ui
|
4
|
+
module Ui
|
5
|
+
@logger = ::Logger.new($stderr)
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_reader :logger
|
9
|
+
|
10
|
+
def formated_logger
|
11
|
+
logger.formatter = proc { |severity, datetime, _, msg|
|
12
|
+
sprintf("%s %s: %s\n", datetime, severity, msg)
|
13
|
+
}
|
14
|
+
|
15
|
+
logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def logger=(output = $stderr)
|
19
|
+
@logger = ::Logger.new(output)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Output warnings
|
23
|
+
def warning(*args)
|
24
|
+
formated_logger.warn(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Output messages
|
28
|
+
def message(*args)
|
29
|
+
formated_logger.info(*args)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Output errors
|
33
|
+
def error(*args)
|
34
|
+
formated_logger.error(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/filegen/version.rb
CHANGED
@@ -21,7 +21,7 @@ describe DataSourceBuilder do
|
|
21
21
|
allow(params).to receive(:data_source_builders).and_return({})
|
22
22
|
allow(params).to receive(:yaml_file).and_return('')
|
23
23
|
|
24
|
-
expect { DataSourceBuilder.new(params) }.to raise_error Exceptions::
|
24
|
+
expect { DataSourceBuilder.new(params) }.to raise_error Exceptions::DataSourcesAreInvalid
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'supports order of data sources: env first' do
|
data/spec/options_spec.rb
CHANGED
@@ -4,7 +4,8 @@ require 'spec_helper'
|
|
4
4
|
describe Options do
|
5
5
|
context '#destination' do
|
6
6
|
it 'returns an io handle' do
|
7
|
-
|
7
|
+
template_file = create_file('template.erb')
|
8
|
+
options = Options.new([template_file])
|
8
9
|
expect(options.destination).to respond_to(:puts)
|
9
10
|
end
|
10
11
|
end
|
@@ -18,15 +19,12 @@ describe Options do
|
|
18
19
|
end
|
19
20
|
|
20
21
|
it 'raises an exception if file does not exist' do
|
21
|
-
|
22
|
-
expect { options.source }.to raise_error RuntimeError
|
22
|
+
expect { Options.new(['template_file']) }.to raise_error Exceptions::TemplateDoesNotExist
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'raises an exception if file does not have a erb-extension' do
|
26
26
|
template_file = create_file('template')
|
27
|
-
|
28
|
-
options = Options.new([template_file])
|
29
|
-
expect { options.source }.to raise_error RuntimeError
|
27
|
+
expect { Options.new([template_file]) }.to raise_error Exceptions::TemplateNameIsInvalid
|
30
28
|
end
|
31
29
|
end
|
32
30
|
end
|
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.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- config/rubocop-disabled.yml
|
53
53
|
- config/rubocop-enabled.yml
|
54
54
|
- cucumber.yml
|
55
|
+
- features/error_handling.feature
|
55
56
|
- features/evaluate_template.feature
|
56
57
|
- features/output_meta_data.feature
|
57
58
|
- features/step_definitions.rb
|
@@ -67,6 +68,7 @@ files:
|
|
67
68
|
- lib/filegen/exceptions.rb
|
68
69
|
- lib/filegen/options.rb
|
69
70
|
- lib/filegen/runner.rb
|
71
|
+
- lib/filegen/ui.rb
|
70
72
|
- lib/filegen/version.rb
|
71
73
|
- rubocop-todo.yml
|
72
74
|
- scripts/terminal
|
@@ -109,6 +111,7 @@ specification_version: 3
|
|
109
111
|
summary: This helper takes an erb template and provides access to environment variables
|
110
112
|
and a yaml file within that template
|
111
113
|
test_files:
|
114
|
+
- features/error_handling.feature
|
112
115
|
- features/evaluate_template.feature
|
113
116
|
- features/output_meta_data.feature
|
114
117
|
- features/step_definitions.rb
|