2018-12-23 12:14:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
2017-09-10 17:25:29 +05:30
|
|
|
module QuickActions
|
2016-09-13 17:45:13 +05:30
|
|
|
class CommandDefinition
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_accessor :name, :aliases, :description, :explanation, :params,
|
2018-12-23 12:14:25 +05:30
|
|
|
:condition_block, :parse_params_block, :action_block, :warning
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def initialize(name, attributes = {})
|
|
|
|
@name = name
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@aliases = attributes[:aliases] || []
|
|
|
|
@description = attributes[:description] || ''
|
2018-12-23 12:14:25 +05:30
|
|
|
@warning = attributes[:warning] || ''
|
2017-08-17 22:00:37 +05:30
|
|
|
@explanation = attributes[:explanation] || ''
|
|
|
|
@params = attributes[:params] || []
|
2016-09-13 17:45:13 +05:30
|
|
|
@condition_block = attributes[:condition_block]
|
2017-08-17 22:00:37 +05:30
|
|
|
@parse_params_block = attributes[:parse_params_block]
|
|
|
|
@action_block = attributes[:action_block]
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def all_names
|
|
|
|
[name, *aliases]
|
|
|
|
end
|
|
|
|
|
|
|
|
def noop?
|
|
|
|
action_block.nil?
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def available?(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
return true unless condition_block
|
|
|
|
|
|
|
|
context.instance_exec(&condition_block)
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def explain(context, arg)
|
|
|
|
return unless available?(context)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
message = if explanation.respond_to?(:call)
|
|
|
|
execute_block(explanation, context, arg)
|
|
|
|
else
|
|
|
|
explanation
|
|
|
|
end
|
|
|
|
|
|
|
|
warning.empty? ? message : "#{message} (#{warning})"
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def execute(context, arg)
|
|
|
|
return if noop? || !available?(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
execute_block(action_block, context, arg)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def to_h(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
desc = description
|
|
|
|
if desc.respond_to?(:call)
|
|
|
|
desc = context.instance_exec(&desc) rescue ''
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
prms = params
|
|
|
|
if prms.respond_to?(:call)
|
|
|
|
prms = Array(context.instance_exec(&prms)) rescue params
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
aliases: aliases,
|
|
|
|
description: desc,
|
2018-12-23 12:14:25 +05:30
|
|
|
warning: warning,
|
2017-08-17 22:00:37 +05:30
|
|
|
params: prms
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_block(block, context, arg)
|
|
|
|
if arg.present?
|
|
|
|
parsed = parse_params(arg, context)
|
|
|
|
context.instance_exec(parsed, &block)
|
|
|
|
elsif block.arity == 0
|
|
|
|
context.instance_exec(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_params(arg, context)
|
|
|
|
return arg unless parse_params_block
|
|
|
|
|
|
|
|
context.instance_exec(arg, &parse_params_block)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|