fedux_org-stdlib 0.6.47 → 0.6.48
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/fedux_org_stdlib/file_template.rb +1 -1
- data/lib/fedux_org_stdlib/template_directory.rb +197 -0
- data/lib/fedux_org_stdlib/template_directory/exceptions.rb +21 -0
- data/lib/fedux_org_stdlib/version.rb +1 -1
- data/spec/template_directory_spec.rb +139 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83b6bcc48108fd4ffbf7271496c08875e9fd8b38
|
4
|
+
data.tar.gz: 90370b2ddcdad236c1fe53915cdafa781c4aea3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73b8bbf86f9aa7c5be2e757882af824c831d3cbfa9f2400dfa8cbe14b8b607f711491843b9a65c16709358ec3c9a7dec1bd37ee56047d7ee65b2d09a6b7a0dee
|
7
|
+
data.tar.gz: f564ed4e1960a030b77a0006b393af5e7ec966ba87fd46a162e03470bbb7c8df0930a4d757b3361757c5a0a53a95205aaf433b3b2dfd02d8def8c6966a30bf74
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'fedux_org_stdlib/require_files'
|
3
3
|
require 'fedux_org_stdlib/file_template/exceptions'
|
4
|
-
require 'fedux_org_stdlib/core_ext/array'
|
4
|
+
require 'fedux_org_stdlib/core_ext/array/list'
|
5
5
|
require 'fedux_org_stdlib/logging/logger'
|
6
6
|
require_library %w{ json active_support/core_ext/string/inflections }
|
7
7
|
|
@@ -0,0 +1,197 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'fedux_org_stdlib/require_files'
|
3
|
+
require 'fedux_org_stdlib/template_directory/exceptions'
|
4
|
+
require 'fedux_org_stdlib/core_ext/array/list'
|
5
|
+
require 'fedux_org_stdlib/logging/logger'
|
6
|
+
require_library %w{ json active_support/core_ext/string/inflections }
|
7
|
+
|
8
|
+
module FeduxOrgStdlib
|
9
|
+
# This class makes a template file available as an object. You can use
|
10
|
+
# whatever template language you prefer. It's up to you to compile the
|
11
|
+
# template with a suitable template parser.
|
12
|
+
#
|
13
|
+
# By default it will look for a suitable template file in the given order:
|
14
|
+
#
|
15
|
+
# 1. <current working directory>/templates/<template_directory>
|
16
|
+
# 2. $HOME/.config/<application_name>/templates/<template_directory>
|
17
|
+
# 3. $HOME/.<application_name>/templates/<template_directory>
|
18
|
+
# 4. /etc/<application_name>/templates/<template_directory>
|
19
|
+
#
|
20
|
+
# Please keep in mind
|
21
|
+
#
|
22
|
+
# * application_name: Module of your class, e.g. "MyApplication" becomes
|
23
|
+
# "my_application"
|
24
|
+
# * template_directory: Plural name of your class and "TemplateDirectory" strip
|
25
|
+
# off, e.g "ClientTemplateDirectory" becomes "clients.d"
|
26
|
+
#
|
27
|
+
# Most conventions defined by me are implemented as separate methods. If one convention
|
28
|
+
# is not suitable for your use case, just overwrite the method.
|
29
|
+
#
|
30
|
+
# If you prefer to use a different path to the template file or name of the
|
31
|
+
# template file one of the following methods needs to be overwritten:
|
32
|
+
#
|
33
|
+
# * template_directory
|
34
|
+
# * template_directory_basename
|
35
|
+
# * application_name
|
36
|
+
#
|
37
|
+
# If you want the class to look for your template file at a different place
|
38
|
+
# overwrite the following method
|
39
|
+
#
|
40
|
+
# * allowed_template_directory_paths
|
41
|
+
#
|
42
|
+
# Below you find some examples for the usage of the class:
|
43
|
+
#
|
44
|
+
# @example Create template with one writer and reader
|
45
|
+
# module MyApplication
|
46
|
+
# class ClientTemplateDirectory < FileTemplate
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
class TemplateDirectory
|
50
|
+
|
51
|
+
attr_reader :working_directory, :logger, :directory, :template_files
|
52
|
+
|
53
|
+
# Create a new instance of template
|
54
|
+
#
|
55
|
+
# It tries to find a suitable template directory. If it doesn't find one
|
56
|
+
# the template is empty
|
57
|
+
#
|
58
|
+
# @param [String] directory
|
59
|
+
# Path where template directory is stored.
|
60
|
+
#
|
61
|
+
# @raise [Exceptions::TemplateFileNotReadable]
|
62
|
+
# If an avaiable template directory could not be read by the template engine
|
63
|
+
#
|
64
|
+
# @return [AppTemplate]
|
65
|
+
# The template instance. If the resulting data structure created by the
|
66
|
+
# template_engine does not respond to `:[]` an empty template object will be
|
67
|
+
# created.
|
68
|
+
def initialize(
|
69
|
+
directory: nil,
|
70
|
+
logger: FeduxOrgStdlib::Logging::Logger.new,
|
71
|
+
working_directory: Dir.getwd,
|
72
|
+
output_directory: nil
|
73
|
+
)
|
74
|
+
@logger = logger
|
75
|
+
@working_directory = working_directory
|
76
|
+
|
77
|
+
@directory ||= available_template_directory
|
78
|
+
|
79
|
+
fail Exceptions::NoTemplateDirectoryFound, "No template directory found at #{allowed_template_directory_paths.to_list}, therefor I'm stop working as there are methods which depend on an available template directory path." unless @directory
|
80
|
+
|
81
|
+
begin
|
82
|
+
@template_files = Dir.glob(File.join(@directory, '**', '*')).keep_if { |o| FileTest.file? o }
|
83
|
+
rescue StandardError => e
|
84
|
+
fail Exceptions::TemplateDirectoryNotReadable, JSON.dump(message: e.message, file: @directory)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Return the path to the preferred template file
|
89
|
+
# @return [String]
|
90
|
+
# The path to the preferred template file
|
91
|
+
def preferred_template_directory
|
92
|
+
allowed_template_directory_paths[1]
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# The name of the template file
|
98
|
+
#
|
99
|
+
# @return [String]
|
100
|
+
# The name of the template file. It defaults to `<template_directory_basename>.yaml`. If
|
101
|
+
# you want to use a different file name you need to overwrite this
|
102
|
+
# method.
|
103
|
+
def template_directory
|
104
|
+
"#{template_directory_basename}#{template_directory_suffix}"
|
105
|
+
end
|
106
|
+
|
107
|
+
# The suffix of the template file
|
108
|
+
#
|
109
|
+
# @return [String]
|
110
|
+
# The suffix of the template file
|
111
|
+
def template_directory_suffix
|
112
|
+
'.d'
|
113
|
+
end
|
114
|
+
|
115
|
+
# The base name of the template
|
116
|
+
#
|
117
|
+
# @return [String]
|
118
|
+
# This one returns the base name of the template file (without the file
|
119
|
+
# extension). It uses the class name of the template class
|
120
|
+
#
|
121
|
+
# @example Determine the base name of the template
|
122
|
+
#
|
123
|
+
# class ClientTemplate; end
|
124
|
+
#
|
125
|
+
# This will result in `client` as base name for the template file.
|
126
|
+
def template_directory_basename
|
127
|
+
unless (name = class_name.sub(/TemplateDirectory/, '').underscore.pluralize).blank?
|
128
|
+
return name
|
129
|
+
end
|
130
|
+
|
131
|
+
fail Exceptions::ClassNameIsMissing, JSON.dump(klass: class_name)
|
132
|
+
end
|
133
|
+
|
134
|
+
# The name of your application
|
135
|
+
#
|
136
|
+
# @return [String]
|
137
|
+
# This will strip of the class part of fully qualified class name and
|
138
|
+
# converted it to a path.
|
139
|
+
#
|
140
|
+
# @example Determine application name
|
141
|
+
#
|
142
|
+
# class MyApplication::MyTemplate; end
|
143
|
+
#
|
144
|
+
# This will be converted to
|
145
|
+
#
|
146
|
+
# my_application
|
147
|
+
def application_name
|
148
|
+
module_name.underscore
|
149
|
+
end
|
150
|
+
|
151
|
+
# The paths where to look for the template file
|
152
|
+
#
|
153
|
+
# @return [Array]
|
154
|
+
# A list of paths where the template object should look for its template
|
155
|
+
# file.
|
156
|
+
def allowed_template_directory_paths
|
157
|
+
paths = []
|
158
|
+
|
159
|
+
paths << resolve_path(working_directory, 'templates', template_directory)
|
160
|
+
paths << resolve_path('~', '.config', application_name, 'templates', template_directory)
|
161
|
+
paths << resolve_path('~', format('.%s', application_name), 'templates', template_directory)
|
162
|
+
paths << resolve_path('/etc', application_name, 'templates', template_directory)
|
163
|
+
paths << resolve_path(fallback_template_directory, template_directory) if fallback_template_directory
|
164
|
+
|
165
|
+
paths
|
166
|
+
end
|
167
|
+
|
168
|
+
def resolve_path(*path)
|
169
|
+
::File.expand_path(::File.join(*path))
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
# Use this path as fall back path
|
174
|
+
def fallback_template_directory; end
|
175
|
+
|
176
|
+
def class_name
|
177
|
+
self.class.name.to_s.demodulize
|
178
|
+
end
|
179
|
+
|
180
|
+
def module_name
|
181
|
+
self.class.to_s.deconstantize
|
182
|
+
end
|
183
|
+
|
184
|
+
def available_template_directory
|
185
|
+
allowed_template_directory_paths.each do |p|
|
186
|
+
object = Dir.glob("#{p}/**/*").keep_if { |o| ::FileTest.file?(o) }.first
|
187
|
+
|
188
|
+
next if object.blank?
|
189
|
+
return p if object
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def self.reserved_key_words
|
194
|
+
(methods | instance_methods | private_methods | private_instance_methods ) - (Class.methods | Class.private_methods ) | [:to_s]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module FeduxOrgStdlib
|
3
|
+
class TemplateDirectory
|
4
|
+
module Exceptions
|
5
|
+
# Found config file is not readable
|
6
|
+
class TemplateDirectoryNotReadable < StandardError; end
|
7
|
+
|
8
|
+
# No allowed config file could be found
|
9
|
+
class NoTemplateDirectoryFound < StandardError; end
|
10
|
+
|
11
|
+
# If no module is given on class
|
12
|
+
class NamespaceIsMissing < StandardError; end
|
13
|
+
|
14
|
+
# If no class name is present
|
15
|
+
class ClassNameIsMissing < StandardError; end
|
16
|
+
|
17
|
+
# If one tries to define an option name which is forbbiden
|
18
|
+
class OptionNameForbidden < StandardError; end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'fedux_org_stdlib/template_directory'
|
4
|
+
|
5
|
+
RSpec.describe TemplateDirectory do
|
6
|
+
|
7
|
+
context '#preferred_template_directory' do
|
8
|
+
it 'has a default template file which is the preferred place to store the template' do
|
9
|
+
with_environment 'HOME' => working_directory do
|
10
|
+
create_file '.config/my_application/templates/tests.d/test.tt'
|
11
|
+
|
12
|
+
template_klass = Class.new(TemplateDirectory) do
|
13
|
+
def class_name
|
14
|
+
'TestTemplateDirectory'
|
15
|
+
end
|
16
|
+
|
17
|
+
def module_name
|
18
|
+
'MyApplication'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
template = template_klass.new
|
23
|
+
expect(template.preferred_template_directory).to eq File.expand_path('~/.config/my_application/templates/tests.d')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'works with nested module names' do
|
28
|
+
with_environment 'HOME' => working_directory do
|
29
|
+
create_file '.config/my_application/my_sub/templates/tests.d/test.tt'
|
30
|
+
|
31
|
+
template_klass = Class.new(TemplateDirectory) do
|
32
|
+
def class_name
|
33
|
+
'TestTemplateDirectory'
|
34
|
+
end
|
35
|
+
|
36
|
+
def module_name
|
37
|
+
'MyApplication::MySub'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
template = template_klass.new
|
42
|
+
expect(template.preferred_template_directory).to eq File.expand_path('~/.config/my_application/my_sub/templates/tests.d')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'template files' do
|
48
|
+
it 'looks at ~/.config/my_application/templates/tests.d/test.tt' do
|
49
|
+
with_environment 'HOME' => working_directory do
|
50
|
+
file = create_file '.config/my_application/templates/tests.d/test.tt', <<-EOS.strip_heredoc
|
51
|
+
<%= hello world %>
|
52
|
+
EOS
|
53
|
+
|
54
|
+
template_klass = Class.new(TemplateDirectory) do
|
55
|
+
def class_name
|
56
|
+
'TestTemplateDirectory'
|
57
|
+
end
|
58
|
+
|
59
|
+
def module_name
|
60
|
+
'MyApplication'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
template = template_klass.new
|
65
|
+
expect(template.template_files).to include file
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'looks at ~/.my_application/templates/tests.d/test.tt' do
|
70
|
+
with_environment 'HOME' => working_directory do
|
71
|
+
file = create_file '.my_application/templates/tests.d/test.tt', <<-EOS.strip_heredoc
|
72
|
+
<%= hello world %>
|
73
|
+
EOS
|
74
|
+
|
75
|
+
template_klass = Class.new(TemplateDirectory) do
|
76
|
+
def class_name
|
77
|
+
'TestTemplateDirectory'
|
78
|
+
end
|
79
|
+
|
80
|
+
def module_name
|
81
|
+
'MyApplication'
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
template = template_klass.new
|
86
|
+
expect(template.template_files).to include file
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'returns templates in subdirectories as well' do
|
91
|
+
with_environment 'HOME' => working_directory do
|
92
|
+
file = create_file '.my_application/templates/tests.d/blub/test.tt', <<-EOS.strip_heredoc
|
93
|
+
<%= hello world %>
|
94
|
+
EOS
|
95
|
+
|
96
|
+
template_klass = Class.new(TemplateDirectory) do
|
97
|
+
def class_name
|
98
|
+
'TestTemplateDirectory'
|
99
|
+
end
|
100
|
+
|
101
|
+
def module_name
|
102
|
+
'MyApplication'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
template = template_klass.new
|
107
|
+
expect(template.template_files).to include file
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context '#template_files' do
|
113
|
+
it 'returns template_files in template directory' do
|
114
|
+
with_environment 'HOME' => working_directory do
|
115
|
+
file1 = create_file '.my_application/templates/tests.d/test1.tt', <<-EOS.strip_heredoc
|
116
|
+
<%= hello world %>
|
117
|
+
EOS
|
118
|
+
|
119
|
+
file2 = create_file '.my_application/templates/tests.d/test2.tt', <<-EOS.strip_heredoc
|
120
|
+
<%= hello world %>
|
121
|
+
EOS
|
122
|
+
|
123
|
+
template_klass = Class.new(TemplateDirectory) do
|
124
|
+
def class_name
|
125
|
+
'TestTemplateDirectory'
|
126
|
+
end
|
127
|
+
|
128
|
+
def module_name
|
129
|
+
'MyApplication'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
template = template_klass.new
|
134
|
+
expect(template.template_files).to include file1
|
135
|
+
expect(template.template_files).to include file2
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fedux_org-stdlib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.48
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Meyer
|
@@ -126,6 +126,8 @@ files:
|
|
126
126
|
- lib/fedux_org_stdlib/require_files.rb
|
127
127
|
- lib/fedux_org_stdlib/shell_language_detector.rb
|
128
128
|
- lib/fedux_org_stdlib/shell_language_detector/language.rb
|
129
|
+
- lib/fedux_org_stdlib/template_directory.rb
|
130
|
+
- lib/fedux_org_stdlib/template_directory/exceptions.rb
|
129
131
|
- lib/fedux_org_stdlib/version.rb
|
130
132
|
- lib/fedux_org_stdlib/version_management/exceptions.rb
|
131
133
|
- lib/fedux_org_stdlib/version_management/library_builder.rb
|
@@ -179,6 +181,7 @@ files:
|
|
179
181
|
- spec/support/reporting.rb
|
180
182
|
- spec/support/rspec.rb
|
181
183
|
- spec/support/string.rb
|
184
|
+
- spec/template_directory_spec.rb
|
182
185
|
- spec/template_file_spec.rb
|
183
186
|
- spec/version_management/library_builder_spec.rb
|
184
187
|
- spec/version_management/ruby_library_spec.rb
|
@@ -251,6 +254,7 @@ test_files:
|
|
251
254
|
- spec/support/reporting.rb
|
252
255
|
- spec/support/rspec.rb
|
253
256
|
- spec/support/string.rb
|
257
|
+
- spec/template_directory_spec.rb
|
254
258
|
- spec/template_file_spec.rb
|
255
259
|
- spec/version_management/library_builder_spec.rb
|
256
260
|
- spec/version_management/ruby_library_spec.rb
|