carioca 2.0.4 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +54 -0
- data/Gemfile +3 -4
- data/Gemfile.lock +36 -2
- data/Rakefile +40 -2
- data/VERSION +1 -0
- data/bin/console +3 -3
- data/carioca.gemspec +29 -29
- data/lib/carioca/configuration.rb +60 -57
- data/lib/carioca/constants.rb +52 -53
- data/lib/carioca/container.rb +8 -10
- data/lib/carioca/dependencies.rb +2 -0
- data/lib/carioca/helpers.rb +39 -41
- data/lib/carioca/mixin.rb +10 -12
- data/lib/carioca/rake/manage.rb +5 -5
- data/lib/carioca/registry.rb +95 -86
- data/lib/carioca/registry_file.rb +54 -57
- data/lib/carioca/services/config.rb +122 -127
- data/lib/carioca/services/debug.rb +45 -52
- data/lib/carioca/services/i18n.rb +15 -16
- data/lib/carioca/services/init.rb +3 -2
- data/lib/carioca/services/output.rb +183 -183
- data/lib/carioca/validator.rb +44 -45
- data/lib/carioca/version.rb +10 -0
- data/lib/carioca.rb +2 -5
- data/samples/Rakefile +2 -0
- data/samples/test.rb +73 -86
- metadata +96 -22
@@ -1,189 +1,189 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
1
3
|
module Carioca
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
4
|
+
module Services
|
5
|
+
module Output
|
6
|
+
module FormatsMapping
|
7
|
+
COLORS = {
|
8
|
+
unknown: :red,
|
9
|
+
fatal: :red,
|
10
|
+
error: :red,
|
11
|
+
ko: :yellow,
|
12
|
+
warn: :yellow,
|
13
|
+
item: :white,
|
14
|
+
arrow: :white,
|
15
|
+
sending: :white,
|
16
|
+
calling: :white,
|
17
|
+
scheduling: :white,
|
18
|
+
trigger: :white,
|
19
|
+
receive: :white,
|
20
|
+
info: :cyan,
|
21
|
+
ok: :green,
|
22
|
+
success: :green,
|
23
|
+
debug: :magenta,
|
24
|
+
flat: :white
|
25
|
+
}.freeze
|
26
|
+
|
27
|
+
EMOJI = {
|
28
|
+
unknown: { value: "\u{1F4A5}", alt: '[!!]', text: '(UNKNOWN)' },
|
29
|
+
fatal: { value: "\u{26D4}", alt: '[!!]', text: '(FATAL)' },
|
30
|
+
error: { value: "\u{1F6AB}", alt: '[!]', text: '(ERROR)' },
|
31
|
+
ko: { value: "\u{1F44E}", alt: '[-]', text: '(KO)' },
|
32
|
+
warn: { value: "\u{26A0}", alt: '[/!\]', text: '(WARNING)' },
|
33
|
+
info: { value: "\u{2139}", alt: '[i]', text: '(INFO)' },
|
34
|
+
item: { value: " \u{1F539}", alt: '', text: ' *' },
|
35
|
+
arrow: { value: " \u{27A1}", alt: '', text: ' =>' },
|
36
|
+
calling: { value: "\u{1F4DE}", alt: '[C]', text: '(CALLING)' },
|
37
|
+
scheduling: { value: "\u{23F2}", alt: '[S]', text: '{SCHEDULING})' },
|
38
|
+
trigger: { value: "\u{23F0}", alt: '[T]', text: '(TRIGGER)' },
|
39
|
+
sending: { value: "\u{1F4E4}", alt: '[>]', text: '(SENDING)' },
|
40
|
+
receive: { value: "\u{1F4E5}", alt: '[<]', text: '(RECEIVE)' },
|
41
|
+
ok: { value: "\u{1F44D}", alt: '[+]', text: '(OK)' },
|
42
|
+
success: { value: "\u{1F4AA}", alt: '[+]', text: '(SUCCESS)' },
|
43
|
+
debug: { value: "\u{1F41B}", alt: '[D]', text: '(DEBUG)' },
|
44
|
+
flat: { value: '', alt: '' },
|
45
|
+
skip: { value: 'U{23E9}', alt: '[I]', text: '(SKIPPED)'}
|
46
|
+
}.freeze
|
47
|
+
LEVELS = %i[debug info warn error fatal unknown].freeze
|
48
|
+
ALIAS = {
|
49
|
+
flat: :info,
|
50
|
+
item: :info,
|
51
|
+
ok: :info,
|
52
|
+
ko: :error,
|
53
|
+
trigger: :info,
|
54
|
+
scheduling: :info,
|
55
|
+
arrow: :info,
|
56
|
+
sending: :info,
|
57
|
+
calling: :info,
|
58
|
+
receive: :info,
|
59
|
+
success: :info,
|
60
|
+
skipped: :info
|
61
|
+
|
62
|
+
}.freeze
|
63
|
+
end
|
64
|
+
|
65
|
+
class Provider
|
66
|
+
include FormatsMapping
|
67
|
+
|
68
|
+
attr_accessor :mode, :emoji, :color
|
69
|
+
|
70
|
+
@@alias = ALIAS.dup
|
71
|
+
@@colors = COLORS.dup
|
72
|
+
@@emoji = EMOJI.dup
|
73
|
+
|
74
|
+
MODE = %i[mono dual log].freeze
|
75
|
+
|
76
|
+
LEVELS.each do |method|
|
77
|
+
define_method(method) do |message, session = nil, source = 'Carioca->Output'|
|
78
|
+
display(level: method, message: message, session: session, source: source)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
@@alias.each_key do |method|
|
82
|
+
define_method(method) do |message, session = nil, source = 'Carioca->Output'|
|
83
|
+
display(level: method, message: message, session: session, source: source)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def map_color(color:, analias:)
|
88
|
+
raise 'Color must be a Symbol' unless color.instance_of?(Symbol)
|
89
|
+
raise "Missing alias : #{analias}" unless LEVELS.include? analias
|
90
|
+
|
91
|
+
@@alias[analias] = color
|
92
|
+
end
|
93
|
+
|
94
|
+
def map_emoji(emoji:, analias:)
|
95
|
+
raise 'Emoji must be a String' unless color.instance_of?(String)
|
96
|
+
raise "Missing alias : #{analias}" unless LEVELS.include? analias
|
97
|
+
|
98
|
+
@@alias[analias] = emoji
|
99
|
+
end
|
100
|
+
|
101
|
+
def add_alias(newalias:, level:)
|
102
|
+
raise 'Alias must be a Symbol' unless newalias.instance_of?(Symbol)
|
103
|
+
raise "Bad Level : #{level}" unless LEVELS.include? level
|
104
|
+
|
105
|
+
self.class.define_method(newalias) do |message, session = nil|
|
106
|
+
display({ level: newalias, message: message, session: session })
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# constructor
|
111
|
+
def initialize(level: :debug, mode: :mono, emoji: true, colors: true)
|
112
|
+
registry = Carioca::Registry.get
|
113
|
+
@logger = registry.get_service name: :logger
|
114
|
+
@i18n = registry.get_service name: :i18n
|
115
|
+
@debug = Carioca::Registry.config.debug?
|
116
|
+
self.level = level
|
117
|
+
@mode = mode
|
118
|
+
@emoji = check_unicode_term ? emoji : false
|
119
|
+
@color = colors
|
120
|
+
set = []
|
121
|
+
set.push mode
|
122
|
+
set.push :emoji if @emoji
|
123
|
+
set.push :colors if @color
|
124
|
+
@logger.debug('Carioca->Output') { @i18n.t('output.load.context', confset: set.to_s) } if @debug
|
125
|
+
raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
|
126
|
+
end
|
127
|
+
|
128
|
+
# build a session number
|
129
|
+
# @return [String] Session number
|
130
|
+
def generate_session
|
131
|
+
"#{Time.now.to_i}#{rand(999)}"
|
132
|
+
end
|
133
|
+
|
134
|
+
# getter for the current level
|
135
|
+
# @return [Symbol] level
|
136
|
+
def level
|
137
|
+
@active_levels.first
|
138
|
+
end
|
139
|
+
|
140
|
+
# virtual setter for level, set the current level
|
141
|
+
# @raise a badLevel in case of bad level
|
142
|
+
# @param [Symbol] level
|
143
|
+
def level=(level)
|
144
|
+
raise "Bad Level : #{level}" unless LEVELS.include? level
|
145
|
+
|
146
|
+
@active_levels = LEVELS.dup
|
147
|
+
@active_levels.shift(LEVELS.index(level))
|
148
|
+
end
|
149
|
+
|
150
|
+
# check if unicode must be used with term ENV
|
151
|
+
# @return [Boolean]
|
152
|
+
def check_unicode_term
|
153
|
+
return false unless ENV.include? 'TERM'
|
154
|
+
|
155
|
+
ENV.values_at('LC_ALL', 'LC_CTYPE',
|
156
|
+
'LANG').compact.first.include?('UTF-8') && ENV.values_at('TERM').first.include?('xterm')
|
157
|
+
end
|
158
|
+
|
159
|
+
# abstract method for log wrapper
|
160
|
+
# @param [Hash] params
|
161
|
+
# @option params [Symbol] :level, a valid level in LEVELS or ALIAS
|
162
|
+
# @option params [String] :message text
|
163
|
+
# @option params [String] :session an id/timestamp of session
|
164
|
+
def display(level:, message:, session:, source:)
|
165
|
+
message << " (#{session})" if session
|
166
|
+
save = message.dup
|
167
|
+
target_level = @@alias.keys.include?(level) ? @@alias[level] : level
|
168
|
+
if @active_levels.include? target_level
|
169
|
+
if @color
|
170
|
+
pastel = ::Pastel.new
|
171
|
+
message = pastel.send @@colors[level], message
|
60
172
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@@alias = ALIAS.dup
|
67
|
-
@@colors = COLORS.dup
|
68
|
-
@@emoji = EMOJI.dup
|
69
|
-
|
70
|
-
MODE = [:mono, :dual]
|
71
|
-
|
72
|
-
LEVELS.each do |method|
|
73
|
-
define_method(method) do |message, session = '', source = 'Carioca->Output'|
|
74
|
-
self.display(level: method, message: message, session: session, source: source)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
@@alias.keys.each do |method|
|
78
|
-
define_method(method) do |message, session = '',source = 'Carioca->Output'|
|
79
|
-
self.display( level: method, message: message, session: session, source: source)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def map_color(color: , analias: )
|
84
|
-
raise "Color must be a Symbol" unless color.class == Symbol
|
85
|
-
raise "Missing alias : #{analias}" unless LEVELS.include? analias
|
86
|
-
@@alias[analias] = color
|
87
|
-
end
|
88
|
-
|
89
|
-
def map_emoji(emoji: , analias: )
|
90
|
-
raise "Emoji must be a String" unless color.class == String
|
91
|
-
raise "Missing alias : #{analias}" unless LEVELS.include? analias
|
92
|
-
@@alias[analias] = emoji
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
|
-
def add_alias(newalias:, level:)
|
97
|
-
raise "Alias must be a Symbol" unless newalias.class == Symbol
|
98
|
-
raise "Bad Level : #{level}" unless LEVELS.include? level
|
99
|
-
self.class.define_method(newalias) do |message, session = ''|
|
100
|
-
self.display({ level: newalias, message: message, session: session})
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
# constructor
|
106
|
-
def initialize(level: :debug, mode: :mono , emoji: true, colors: true)
|
107
|
-
registry = Carioca::Registry.get
|
108
|
-
@logger = registry.get_service name: :logger
|
109
|
-
@i18n = registry.get_service name: :i18n
|
110
|
-
@debug = Carioca::Registry.config.debug?
|
111
|
-
self.level = level
|
112
|
-
@mode = mode
|
113
|
-
@emoji = (check_unicode_term)? emoji : false
|
114
|
-
@color = colors
|
115
|
-
set = []; set.push mode; set.push :emoji if @emoji ; set.push :colors if @color
|
116
|
-
@logger.debug("Carioca->Output") { @i18n.t('output.load.context', confset: set.to_s ) } if @debug
|
117
|
-
raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
|
118
|
-
end
|
119
|
-
|
120
|
-
# build a session number
|
121
|
-
# @return [String] Session number
|
122
|
-
def get_session
|
123
|
-
return "#{Time.now.to_i.to_s}#{rand(999)}"
|
124
|
-
end
|
125
|
-
|
126
|
-
# getter for the current level
|
127
|
-
# @return [Symbol] level
|
128
|
-
def level
|
129
|
-
return @active_levels.first
|
130
|
-
end
|
131
|
-
|
132
|
-
# virtual setter for level, set the current level
|
133
|
-
# @raise a badLevel in case of bad level
|
134
|
-
# @param [Symbol] level
|
135
|
-
def level=(level)
|
136
|
-
raise "Bad Level : #{level}" unless LEVELS.include? level
|
137
|
-
@active_levels = LEVELS.dup
|
138
|
-
@active_levels.shift(LEVELS.index(level))
|
139
|
-
end
|
140
|
-
|
141
|
-
# check if unicode must be used with term ENV
|
142
|
-
# @return [Boolean]
|
143
|
-
def check_unicode_term
|
144
|
-
return false unless ENV.include? "TERM"
|
145
|
-
if ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.first.include?("UTF-8") and ENV.values_at('TERM').first.include? "xterm" then
|
146
|
-
return true
|
147
|
-
else
|
148
|
-
return false
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
|
153
|
-
# abstract method for log wrapper
|
154
|
-
# @param [Hash] params
|
155
|
-
# @option params [Symbol] :level, a valid level in LEVELS or ALIAS
|
156
|
-
# @option params [String] :message text
|
157
|
-
def display(level: , message: , session:, source:)
|
158
|
-
save = message.dup
|
159
|
-
target_level = (@@alias.keys.include? level)? @@alias[level] : level
|
160
|
-
if @active_levels.include? target_level then
|
161
|
-
if @color then
|
162
|
-
pastel = ::Pastel.new
|
163
|
-
message = pastel.send @@colors[level], message
|
164
|
-
end
|
165
|
-
if @@emoji.include? level
|
166
|
-
pattern = (@emoji)? @@emoji[level][:value] : @@emoji[level][:alt]
|
167
|
-
pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include? :text and !@emoji
|
168
|
-
message = pattern + " " + message unless pattern.empty?
|
169
|
-
end
|
170
|
-
if @mode == :dual
|
171
|
-
|
172
|
-
pattern = @@emoji[level][:alt]
|
173
|
-
unless LEVELS.include? level
|
174
|
-
save = "#{@@emoji[level][:text]} #{save}" if @@emoji[level].include? :text
|
175
|
-
end
|
176
|
-
block = Proc::new {save}
|
177
|
-
@logger.send target_level, source, &block
|
178
|
-
end
|
179
|
-
puts message
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
173
|
+
if @@emoji.include? level
|
174
|
+
pattern = @emoji ? @@emoji[level][:value] : @@emoji[level][:alt]
|
175
|
+
pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include?(:text) && !@emoji
|
176
|
+
message = "#{pattern} #{message}" unless pattern.empty?
|
183
177
|
end
|
184
|
-
|
185
|
-
|
178
|
+
if @mode == :dual
|
179
|
+
save = "#{@@emoji[level][:text]} #{save}" if !LEVELS.include?(level) && (@@emoji[level].include? :text)
|
180
|
+
block = proc { save }
|
181
|
+
@logger.send target_level, source, &block
|
182
|
+
end
|
183
|
+
puts message if @mode == :mono or @mode == :dual
|
184
|
+
end
|
186
185
|
end
|
186
|
+
end
|
187
187
|
end
|
188
|
+
end
|
188
189
|
end
|
189
|
-
|
data/lib/carioca/validator.rb
CHANGED
@@ -1,49 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Carioca
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
private
|
22
|
-
def validate_not_builtins
|
23
|
-
|
24
|
-
raise "Builtins reserved name #{@service.to_s}" if BUILTINS.keys.include? @service
|
25
|
-
end
|
26
|
-
|
27
|
-
def validate_mandatories
|
28
|
-
SERVICES_MANDATORY_SPECS.keys.each do |spec|
|
29
|
-
raise "Key : :#{spec} is mandatory in a service definition" unless @definition.include? spec
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def validate_full_and_type
|
34
|
-
@definition.each do |spec,value|
|
35
|
-
raise "Key : :#{spec} is not allowed in a service definition" unless SERVICES_FULL_LIST_SPECS.include? spec
|
36
|
-
raise "key : #{spec} must be a : #{SERVICES_FULL_LIST_SPECS[spec].to_s}" unless value.class == SERVICES_FULL_LIST_SPECS[spec]
|
37
|
-
if SERVICES_SPECS_DETAIL.include? spec then
|
38
|
-
raise "key : #{spec} must be in : #{SERVICES_SPECS_DETAIL[spec].to_s}" unless SERVICES_SPECS_DETAIL[spec].include? value
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def fill_content
|
44
|
-
@definition[:description] = @service.to_s unless @definition.include? :description
|
45
|
-
end
|
4
|
+
module Services
|
5
|
+
class Validator
|
6
|
+
attr_reader :definition
|
7
|
+
|
8
|
+
include Carioca::Constants
|
9
|
+
|
10
|
+
def initialize(definition:, service:)
|
11
|
+
@definition = definition
|
12
|
+
@service = service
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate!
|
16
|
+
validate_mandatories
|
17
|
+
validate_full_and_type
|
18
|
+
validate_not_builtins
|
19
|
+
fill_content
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
46
23
|
|
24
|
+
def validate_not_builtins
|
25
|
+
raise "Builtins reserved name #{@service}" if BUILTINS.keys.include? @service
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_mandatories
|
29
|
+
SERVICES_MANDATORY_SPECS.each_key do |spec|
|
30
|
+
raise "Key : :#{spec} is mandatory in a service definition" unless @definition.include? spec
|
47
31
|
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate_full_and_type
|
35
|
+
@definition.each do |spec, value|
|
36
|
+
raise "Key : :#{spec} is not allowed in a service definition" unless SERVICES_FULL_LIST_SPECS.include? spec
|
37
|
+
raise "key : #{spec} must be a : #{SERVICES_FULL_LIST_SPECS[spec]}" unless value.instance_of?(SERVICES_FULL_LIST_SPECS[spec])
|
38
|
+
|
39
|
+
raise "key : #{spec} must be in : #{SERVICES_SPECS_DETAIL[spec]}" if SERVICES_SPECS_DETAIL.include?(spec) && !(SERVICES_SPECS_DETAIL[spec].include? value)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def fill_content
|
44
|
+
@definition[:description] = @service.to_s unless @definition.include? :description
|
45
|
+
end
|
48
46
|
end
|
49
|
-
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/carioca.rb
CHANGED
data/samples/Rakefile
CHANGED