2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2017-08-17 22:00:37 +05:30
|
|
|
module Entry
|
2016-06-16 23:09:34 +05:30
|
|
|
##
|
|
|
|
# This mixin is responsible for adding DSL, which purpose is to
|
|
|
|
# simplifly process of adding child nodes.
|
|
|
|
#
|
|
|
|
# This can be used only if parent node is a configuration entry that
|
|
|
|
# holds a hash as a configuration value, for example:
|
|
|
|
#
|
|
|
|
# job:
|
|
|
|
# script: ...
|
|
|
|
# artifacts: ...
|
|
|
|
#
|
|
|
|
module Configurable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
included do
|
2018-03-17 18:26:18 +05:30
|
|
|
include Validatable
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
validations do
|
|
|
|
validates :config, type: Hash
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def compose!(deps = nil)
|
|
|
|
return unless valid?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
self.class.nodes.each do |key, factory|
|
|
|
|
factory
|
2018-03-17 18:26:18 +05:30
|
|
|
.value(config[key])
|
2016-09-13 17:45:13 +05:30
|
|
|
.with(key: key, parent: self)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
entries[key] = factory.create!
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
entries.each_value do |entry|
|
2016-09-29 09:46:39 +05:30
|
|
|
entry.compose!(deps)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2016-08-24 12:49:21 +05:30
|
|
|
def nodes
|
|
|
|
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
private
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def entry(key, entry, metadata)
|
|
|
|
factory = Entry::Factory.new(entry)
|
2016-06-16 23:09:34 +05:30
|
|
|
.with(description: metadata[:description])
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
(@nodes ||= {}).merge!(key.to_sym => factory)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def helpers(*nodes)
|
|
|
|
nodes.each do |symbol|
|
|
|
|
define_method("#{symbol}_defined?") do
|
2018-03-17 18:26:18 +05:30
|
|
|
entries[symbol]&.specified?
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
define_method("#{symbol}_value") do
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless entries[symbol] && entries[symbol].valid?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
entries[symbol].value
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|