erb_asterisk 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/erb_asterisk/arg_parser.rb +2 -1
- data/lib/erb_asterisk/file_cache.rb +27 -0
- data/lib/erb_asterisk/inclusion.rb +4 -1
- data/lib/erb_asterisk/log.rb +20 -0
- data/lib/erb_asterisk/render.rb +2 -0
- data/lib/erb_asterisk/utils.rb +4 -1
- data/lib/erb_asterisk/version.rb +1 -1
- data/lib/erb_asterisk/yields.rb +9 -0
- data/lib/erb_asterisk.rb +29 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcf4a610f1430f15b08437cc1e1bb8b09f31bcbc
|
4
|
+
data.tar.gz: f1f6ee972dfb99dd839b51bc1016e692ab1c3eb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5fe4467d64751959d1951d01a62cc897e61b81f0f51bc1fda1cf379299e07b39ab89bc594a31cf17712046cfdedbce49caa42b665c05429cc7ca5fbb349b3a8
|
7
|
+
data.tar.gz: e75e9c4e19cd4ac6e792b4d5ade0ee0f337e235c6aadc0bb25225fda637cb9bd6b83d35ef761fd4ae8a42ad9a92af843dc15c45f9224458439d47b8cd1872918
|
@@ -7,8 +7,9 @@ module ErbAsterisk
|
|
7
7
|
Slop.parse do |o|
|
8
8
|
o.string '-t', '--templates',
|
9
9
|
'set templates path (e.g.: ~/.erb_asterisk)'
|
10
|
+
o.bool '-v', '--verbose', 'enable verbose mode'
|
10
11
|
|
11
|
-
o.on '
|
12
|
+
o.on '--version', 'print the version' do
|
12
13
|
puts "#{VERSION}"
|
13
14
|
exit
|
14
15
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ErbAsterisk
|
2
|
+
module FileCache
|
3
|
+
def file_read(file_name)
|
4
|
+
content = @file_cache[file_name]
|
5
|
+
if content
|
6
|
+
log_debug('cache', 3)
|
7
|
+
return content
|
8
|
+
end
|
9
|
+
|
10
|
+
content = File.read(file_name)
|
11
|
+
@file_cache[file_name] = content
|
12
|
+
|
13
|
+
log_debug('disk', 3)
|
14
|
+
|
15
|
+
content
|
16
|
+
end
|
17
|
+
|
18
|
+
def file_exist?(file_name)
|
19
|
+
return true if @file_cache.key?(file_name)
|
20
|
+
File.exist?(file_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def file_cache_init
|
24
|
+
@file_cache = {}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -11,10 +11,13 @@ module ErbAsterisk
|
|
11
11
|
|
12
12
|
current_conf_file = TOPLEVEL_BINDING.local_variable_get(:current_conf_file)
|
13
13
|
unless arr.index { |i| i[:file] == current_conf_file }.nil?
|
14
|
-
|
14
|
+
log_warn(
|
15
|
+
"Skip #{current_conf_file} duplicate inclusion to #{file_name}")
|
15
16
|
return
|
16
17
|
end
|
17
18
|
|
19
|
+
log_debug("include_to: #{current_conf_file}, #{file_name}, #{args}", 2)
|
20
|
+
|
18
21
|
arr << { file: current_conf_file, priority: args[:priority] }
|
19
22
|
"; Included to \"#{file_name}\""
|
20
23
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module ErbAsterisk
|
4
|
+
module Log
|
5
|
+
private
|
6
|
+
|
7
|
+
def log_init(verbose)
|
8
|
+
@log = Logger.new(STDOUT)
|
9
|
+
@log.level = verbose ? Logger::DEBUG : Logger::WARN
|
10
|
+
end
|
11
|
+
|
12
|
+
def log_debug(msg, level = 0)
|
13
|
+
@log.debug { "#{' ' * level}#{msg}" }
|
14
|
+
end
|
15
|
+
|
16
|
+
def log_warn(msg)
|
17
|
+
@log.warn { msg }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/erb_asterisk/render.rb
CHANGED
data/lib/erb_asterisk/utils.rb
CHANGED
@@ -6,9 +6,12 @@ module ErbAsterisk
|
|
6
6
|
# LongExtension1234! -> Lo[n]gE[x]te[n]sio[n]1234[!]
|
7
7
|
#
|
8
8
|
def escape_exten(exten)
|
9
|
-
exten.each_char.reduce('') do |s, c|
|
9
|
+
result = exten.each_char.reduce('') do |s, c|
|
10
10
|
s << (%w(x z n . !).include?(c.downcase) ? "[#{c}]" : c)
|
11
11
|
end
|
12
|
+
|
13
|
+
log_debug("escape_exten: '#{exten}' => '#{result}'", 2)
|
14
|
+
result
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
data/lib/erb_asterisk/version.rb
CHANGED
data/lib/erb_asterisk/yields.rb
CHANGED
@@ -4,6 +4,8 @@ module ErbAsterisk
|
|
4
4
|
def apply_line_to(tag, line, args = {})
|
5
5
|
default_args!(args)
|
6
6
|
apply_to_yields(:line, tag, line, args[:priority])
|
7
|
+
|
8
|
+
log_debug("apply_line_to: :#{tag}, #{line}, #{args}", 2)
|
7
9
|
"; Applied \"#{line}\" to :#{tag}"
|
8
10
|
end
|
9
11
|
|
@@ -12,12 +14,16 @@ module ErbAsterisk
|
|
12
14
|
default_args!(args)
|
13
15
|
old_output = @erb_output
|
14
16
|
@erb_output = ''
|
17
|
+
|
15
18
|
apply_to_yields(:block, tag, yield, args[:priority])
|
19
|
+
|
20
|
+
log_debug("content_for: :#{tag}, #{args}", 2)
|
16
21
|
@erb_output = old_output
|
17
22
|
end
|
18
23
|
|
19
24
|
# Define place where put apply_line_to
|
20
25
|
def yield_here(tag)
|
26
|
+
@yield_here_occured = true
|
21
27
|
"<%= yield_actual :#{tag} %>"
|
22
28
|
end
|
23
29
|
|
@@ -34,6 +40,9 @@ module ErbAsterisk
|
|
34
40
|
|
35
41
|
def output_yield(tag)
|
36
42
|
a = @yields[tag]
|
43
|
+
|
44
|
+
log_debug("yield_here: :#{tag}, size=#{a.size}", 2)
|
45
|
+
|
37
46
|
a = a.sort_by { |i| -i[:priority] }
|
38
47
|
result = a.reduce('') do |s, i|
|
39
48
|
s << "; priority: #{i[:priority]}\n" if i[:priority] != 0
|
data/lib/erb_asterisk.rb
CHANGED
@@ -6,12 +6,16 @@ require 'erb_asterisk/render'
|
|
6
6
|
require 'erb_asterisk/inclusion'
|
7
7
|
require 'erb_asterisk/yields'
|
8
8
|
require 'erb_asterisk/utils'
|
9
|
+
require 'erb_asterisk/log'
|
10
|
+
require 'erb_asterisk/file_cache'
|
9
11
|
|
10
12
|
module ErbAsterisk
|
11
13
|
include Render
|
12
14
|
include Inclusion
|
13
15
|
include Yields
|
14
16
|
include Utils
|
17
|
+
include Log
|
18
|
+
include FileCache
|
15
19
|
|
16
20
|
def execute(opts)
|
17
21
|
init_instance(opts)
|
@@ -21,7 +25,6 @@ module ErbAsterisk
|
|
21
25
|
@templates_path = "#{root}templates".freeze
|
22
26
|
|
23
27
|
render_files(root)
|
24
|
-
export_includes(root)
|
25
28
|
end
|
26
29
|
|
27
30
|
private
|
@@ -35,6 +38,9 @@ module ErbAsterisk
|
|
35
38
|
@templates_path = ''
|
36
39
|
@yields = {}
|
37
40
|
|
41
|
+
log_init(opts[:verbose])
|
42
|
+
file_cache_init
|
43
|
+
|
38
44
|
user_path = opts[:templates].nil? ? '~/.erb_asterisk' : opts[:templates]
|
39
45
|
@user_templates = File.expand_path("#{user_path}/templates")
|
40
46
|
end
|
@@ -55,9 +61,14 @@ module ErbAsterisk
|
|
55
61
|
# It does two round of rendering because of apply_line_to and yield_here.
|
56
62
|
# First round accumulates apply_line_to declarations and converts
|
57
63
|
# yield_here to yield_actual.
|
64
|
+
log_debug('FIRST ROUND:')
|
58
65
|
render_erbs(erbs)
|
66
|
+
log_debug('')
|
67
|
+
|
59
68
|
# Second round replaces yield_actual with accumulated apply_line_to.
|
69
|
+
log_debug('SECOND ROUND:')
|
60
70
|
render_erbs(erbs)
|
71
|
+
log_debug('')
|
61
72
|
|
62
73
|
save_erbs(erbs)
|
63
74
|
export_includes(root)
|
@@ -80,9 +91,16 @@ module ErbAsterisk
|
|
80
91
|
|
81
92
|
def render_erbs(erbs)
|
82
93
|
erbs.each do |file, value|
|
94
|
+
# Skip on second round all erbs without yield_here method
|
95
|
+
next if value[:skip]
|
96
|
+
|
83
97
|
# Declare global variable with current erb file name for include_to method:
|
84
98
|
TOPLEVEL_BINDING.local_variable_set(:current_conf_file, value[:config])
|
85
|
-
|
99
|
+
log_debug("ERB: #{file}", 1)
|
100
|
+
|
101
|
+
@yield_here_occured = false
|
102
|
+
value[:content] = new_erb(value[:content]).result
|
103
|
+
value[:skip] = true unless @yield_here_occured
|
86
104
|
end
|
87
105
|
end
|
88
106
|
|
@@ -99,16 +117,23 @@ module ErbAsterisk
|
|
99
117
|
end
|
100
118
|
|
101
119
|
File.write("#{root}#{include_file}", result)
|
120
|
+
log_debug("export_includes: #{include_file}")
|
102
121
|
end
|
103
122
|
end
|
104
123
|
|
105
124
|
def read_template(template)
|
106
125
|
file_name = "#{template}.erb"
|
107
126
|
project_template = "#{@templates_path}/#{file_name}"
|
108
|
-
|
127
|
+
if file_exist?(project_template)
|
128
|
+
log_debug("read_template: #{project_template}", 2)
|
129
|
+
return file_read(project_template)
|
130
|
+
end
|
109
131
|
|
110
132
|
user_template = "#{@user_templates}/#{file_name}"
|
111
|
-
|
133
|
+
if file_exist?(user_template)
|
134
|
+
log_debug("read_template: #{user_template}", 2)
|
135
|
+
return file_read(user_template)
|
136
|
+
end
|
112
137
|
|
113
138
|
raise "Template not found: #{template}"
|
114
139
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb_asterisk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artem Baikuzin
|
@@ -113,7 +113,9 @@ files:
|
|
113
113
|
- exe/erb_asterisk
|
114
114
|
- lib/erb_asterisk.rb
|
115
115
|
- lib/erb_asterisk/arg_parser.rb
|
116
|
+
- lib/erb_asterisk/file_cache.rb
|
116
117
|
- lib/erb_asterisk/inclusion.rb
|
118
|
+
- lib/erb_asterisk/log.rb
|
117
119
|
- lib/erb_asterisk/render.rb
|
118
120
|
- lib/erb_asterisk/utils.rb
|
119
121
|
- lib/erb_asterisk/version.rb
|