class MGit::BaseCommand

Constants

HIGH_PRIORITY_OPT_LIST
SELECTABLE_OPT_LIST

注意使用时跟指令自身设置的参数对比,避免冲突

Public Class Methods

cmd() click to toggle source

当前命令行的命令比如checkout / status /..

# File lib/m-git/base_command.rb, line 14
def self.cmd
  name.split('::').last.downcase
end
inherited(sub_klass) click to toggle source
# File lib/m-git/base_command.rb, line 9
def self.inherited(sub_klass)
  CommandManager.register_command(sub_klass.cmd, sub_klass)
end
new(argv) click to toggle source

初始化

@param argv [ARGV::Opt] 输入参数对象

# File lib/m-git/base_command.rb, line 45
def initialize(argv)
  # 指令解析
  setup_argv(argv)
  process_highest_priority_option(argv)
  validate(argv)
  @argv = argv
end

Private Class Methods

description() click to toggle source

可覆写该方法,返回该指令的描述

# File lib/m-git/base_command.rb, line 184
def self.description
end
usage() click to toggle source

可覆写该方法,返回该指令的用法

# File lib/m-git/base_command.rb, line 188
def self.usage
end

Public Instance Methods

all_repos(except_config:false) click to toggle source
# File lib/m-git/base_command.rb, line 67
def all_repos(except_config:false)
  Workspace.all_repos(except_config: except_config)
end
exec_light_repos() click to toggle source
# File lib/m-git/base_command.rb, line 75
def exec_light_repos
  Workspace.exec_light_repos
end
generate_config_repo() click to toggle source
# File lib/m-git/base_command.rb, line 79
def generate_config_repo
  Workspace.generate_config_repo
end
locked_repos() click to toggle source
# File lib/m-git/base_command.rb, line 71
def locked_repos
  Workspace.locked_repos
end
run() click to toggle source
# File lib/m-git/base_command.rb, line 55
def run
  begin
    __config_repo_filter
    pre_exec
    execute(@argv)
    post_exec
  rescue SystemExit, Interrupt
    did_interrupt
  end
end

Private Instance Methods

__config_repo_filter() click to toggle source

# File lib/m-git/base_command.rb, line 84
def __config_repo_filter
  cfg = Workspace.filter_config
  cfg.include_lock = !@argv.opt(SELECTABLE_OPT_LIST[:include_lock]).nil? || include_lock_by_default
  cfg.select_repos = @argv.opt(SELECTABLE_OPT_LIST[:mrepo])
  cfg.exclude_repos = @argv.opt(SELECTABLE_OPT_LIST[:exclude_mrepo])
  cfg.auto_exec = @argv.opt_list.did_set_opt?(HIGH_PRIORITY_OPT_LIST[:auto_exec])
end
did_interrupt() click to toggle source

【子类按需覆写修改实现】按下ctrl+c后调用

# File lib/m-git/base_command.rb, line 180
def did_interrupt
end
enable_abort_operation() click to toggle source

【子类按需覆写修改返回值】是否添加‘–abort’参数

# File lib/m-git/base_command.rb, line 175
def enable_abort_operation
  false
end
enable_auto_execution() click to toggle source

【子类按需覆写修改返回值】返回true表示可以支持自动执行

# File lib/m-git/base_command.rb, line 145
def enable_auto_execution
  false
end
enable_continue_operation() click to toggle source

【子类按需覆写修改返回值】是否添加‘–continue’参数,要在指令中自行控制中间态操作

# File lib/m-git/base_command.rb, line 170
def enable_continue_operation
  false
end
enable_lock_operation() click to toggle source

【子类按需覆写修改返回值】是否添加操作lock仓库的选项(如‘–include-lock’)

# File lib/m-git/base_command.rb, line 160
def enable_lock_operation
  false
end
enable_repo_selection() click to toggle source

【子类按需覆写修改返回值】返回true表示可以支持“–mrepo”等可选选项

# File lib/m-git/base_command.rb, line 150
def enable_repo_selection
  false
end
enable_short_basic_option() click to toggle source

【子类按需覆写修改返回值】是否使默认添加选项(如‘–help’)和可选添加的选项(如‘–mrepo’)支持短指令

# File lib/m-git/base_command.rb, line 155
def enable_short_basic_option
  false
end
execute(argv) click to toggle source
# File lib/m-git/base_command.rb, line 194
def execute(argv)
  Foundation.help!("请覆写父类方法: \"execute(argv)\"")
end
include_lock_by_default() click to toggle source

【子类按需覆写修改返回值】是否自动将lock仓库加入到操作集中

# File lib/m-git/base_command.rb, line 165
def include_lock_by_default
  false
end
mgit_try_to_abort?() click to toggle source
# File lib/m-git/base_command.rb, line 259
def mgit_try_to_abort?
  @argv.opt_list.did_set_opt?(SELECTABLE_OPT_LIST[:abort])
end
mgit_try_to_continue?() click to toggle source

mgit是否输入–continue希望继续上次操作

# File lib/m-git/base_command.rb, line 255
def mgit_try_to_continue?
  @argv.opt_list.did_set_opt?(SELECTABLE_OPT_LIST[:continue])
end
options() click to toggle source
# File lib/m-git/base_command.rb, line 202
def options
  opts = [
      ARGV::Opt.new(HIGH_PRIORITY_OPT_LIST[:help],
                    short_key:(HIGH_PRIORITY_OPT_LIST[:help_s] if enable_short_basic_option),
                    info:"显示帮助。",
                    type: :boolean)
  ]

  opts.push(
      ARGV::Opt.new(HIGH_PRIORITY_OPT_LIST[:auto_exec],
                    info:'指定该参数会跳过所有交互场景,并自动选择需要的操作执行。该参数主要用于脚本调用mgit进行自动化操作,日常RD开发不应当使用。',
                    type: :boolean)
  ) if enable_auto_execution

  opts.push(
      ARGV::Opt.new(SELECTABLE_OPT_LIST[:mrepo],
      short_key:(SELECTABLE_OPT_LIST[:mrepo_s] if enable_short_basic_option),
      info:'指定需要执行该指令的仓库,可指定一个或多个,空格隔开,大小写均可,如:"--mrepo boxapp BBAAccount",若缺省则对所有仓库执行指令。'),
      ARGV::Opt.new(SELECTABLE_OPT_LIST[:exclude_mrepo],
      short_key:(SELECTABLE_OPT_LIST[:exclude_mrepo_s] if enable_short_basic_option),
      info:'指定不需要执行该指令的仓库,可指定一个或多个,空格隔开,大小写均可,如:"--el-mrepo boxapp BBAAccount",若缺省则对所有仓库执行指令。与"--mrepo"同时指定时无效。')
  ) if enable_repo_selection

  opts.push(
      ARGV::Opt.new(SELECTABLE_OPT_LIST[:include_lock], info:'指定该参数意味着同时也操作lock仓库。',
                    type: :boolean)
  ) if enable_lock_operation

  opts.push(
      ARGV::Opt.new(SELECTABLE_OPT_LIST[:continue],
                    info:'MGit自定义参数,仅在操作多仓库过程中出现问题停止,执行状态进入中间态后可用。该参数只能单独使用,解决问题后可执行"mgit <cmd> --continue"继续操作其余仓库。',
                    type: :boolean)
  ) if enable_continue_operation

  opts.push(
      ARGV::Opt.new(SELECTABLE_OPT_LIST[:abort],
                    info:'MGit自定义参数,仅在操作多仓库过程中出现问题停止,执行状态进入中间态后可用。该参数用于清除操作中间态,且只能单独使用:"mgit <cmd> --abort"。',
                    type: :boolean)
  ) if enable_abort_operation

  opts
end
post_exec() click to toggle source

此处有post hook,覆写时需要最后调用super, 特殊指令除外

# File lib/m-git/base_command.rb, line 136
def post_exec
  # 执行后置hook
  HooksManager.execute_mgit_post_hook(@argv.cmd, @argv.pure_opts, Workspace.exec_light_repos)
  # 打点结束
  duration = MGit::DurationRecorder.end
  MGit::Loger.info("~~~ #{@argv.absolute_cmd}, 耗时:#{duration} s ~~~")
end
pre_exec() click to toggle source
# File lib/m-git/base_command.rb, line 115
def pre_exec
  # 开始计时
  MGit::DurationRecorder.start
  # 配置根目录
  Workspace.setup_multi_repo_root

  # 配置log
  MGit::Loger.config(Workspace.root)
  MGit::Loger.info("~~~ #{@argv.absolute_cmd} ~~~")

  # 执行前置hook
  HooksManager.execute_mgit_pre_hook(@argv.cmd, @argv.pure_opts)

  # 解析配置文件
  Workspace.setup_config

  # 校验实体仓库
  Workspace.setup_all_repos
end
process_highest_priority_option(argv) click to toggle source

处理最高优指令

# File lib/m-git/base_command.rb, line 104
def process_highest_priority_option(argv)
  if argv.opt_list.did_set_opt?(HIGH_PRIORITY_OPT_LIST[:help])
    usage(argv)
    exit
  end
end
revise_option_value(opt) click to toggle source

解析参数并更新参数值到列表中

# File lib/m-git/base_command.rb, line 246
def revise_option_value(opt)
end
setup_argv(argv) click to toggle source
# File lib/m-git/base_command.rb, line 93
def setup_argv(argv)
  argv.register_opts(options)
  argv.resolve!

  argv.opt_list.opts.each do |opt|
    next if opt.empty?
    revise_option_value(opt)
  end
end
usage(argv) click to toggle source

显示指令使用信息

# File lib/m-git/base_command.rb, line 264
def usage(argv)
  puts "#{Output.blue_message("[指令说明]")}\n#{self.class.description}"
  puts "\n#{Output.blue_message("[指令格式]")}\n#{self.class.usage}"
  argv.show_info
end
validate(argv) click to toggle source

— 可选覆写(若该指令不带任何自定义参数,则无须覆写) — 判断是否有必须参数漏传或数据格式不正确

# File lib/m-git/base_command.rb, line 251
def validate(argv)
end