Class: MGit::Config

Inherits:
BaseCommand show all
Defined in:
lib/m-git/command/config.rb

Overview

eg: mgit config -s key ‘value’

Constant Summary collapse

OPT_LIST =
{
  :create_local          =>  '--create-local',
  :create_local_s        =>  '-c',
  :update_manifest       =>  '--update-manifest',
  :update_manifest_s     =>  '-m',
  :update_local          =>  '--update-local',
  :update_local_s        =>  '-u',
  :list                  =>  '--list',
  :list_s                =>  '-l',
  :set                   =>  '--set',
  :set_s                 =>  '-s'
}.freeze

Constants inherited from BaseCommand

BaseCommand::HIGH_PRIORITY_OPT_LIST, BaseCommand::SELECTABLE_OPT_LIST

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#all_repos, cmd, #exec_light_repos, #generate_config_repo, inherited, #initialize, #locked_repos, #run

Constructor Details

This class inherits a constructor from MGit::BaseCommand

Class Method Details

.descriptionObject



194
195
196
# File 'lib/m-git/command/config.rb', line 194

def self.description
  "用于更新多仓库配置信息。"
end

.usageObject



198
199
200
# File 'lib/m-git/command/config.rb', line 198

def self.usage
  "mgit config [-s <config_key> <config_value>] [-l]\nmgit config [(-m|-u) <path_to_manifest> | -c <dir_contains_local>]\nmgit config [-h]"
end

Instance Method Details

#create_local_config(dir) ⇒ Object

新建本地配置表软链接



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/m-git/command/config.rb', line 138

def create_local_config(dir)
  path = Utils.expand_path(File.join(dir, Constants::CONFIG_FILE_NAME[:local_manifest]))
  if File.exist?(path) && !Output.continue_with_user_remind?("本地配置文件\"#{path}\"已经存在,是否覆盖?")
    Output.puts_cancel_message
    return
  end

  FileUtils.mkdir_p(dir)
  file = File.new(path, 'w')
  if !file.nil?
    file.write(Template.default_template)
    file.close
  end

  Utils.link(path, File.join(Workspace.root, Constants::PROJECT_DIR[:source_config], Constants::CONFIG_FILE_NAME[:local_manifest]))
  Output.puts_success_message("本地配置文件生成完毕:#{path}")
end

#dump_configObject

列出所有配置



157
158
159
160
161
162
163
# File 'lib/m-git/command/config.rb', line 157

def dump_config
  begin
    MGitConfig.dump_config(Workspace.root)
  rescue Error => e
    Foundation.help!(e.msg)
  end
end

#enable_short_basic_optionObject

允许使用短命令



190
191
192
# File 'lib/m-git/command/config.rb', line 190

def enable_short_basic_option
  true
end

#execute(argv) ⇒ Object




73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/m-git/command/config.rb', line 73

def execute(argv)
  argv.enumerate_valid_opts { |opt|
    if opt.key == OPT_LIST[:update_manifest]
      update_mgit_config(opt.value)
      return
    elsif opt.key == OPT_LIST[:update_local]
      update_local_config(opt.value)
      return
    elsif opt.key == OPT_LIST[:create_local]
      dir = opt.value
      if opt.value.is_a?(TrueClass)
        Workspace.setup_config
        if Workspace.config.config_repo.nil?
          Foundation.help!("未找到配置仓库,请为参数\"--create-local\"或\"-c\"指定一个具体文件夹目录并重试!")
        else
          dir = Workspace.config.config_repo.abs_dest(Workspace.root)
        end
      end
      create_local_config(dir)
      return
    elsif opt.key == OPT_LIST[:list]
      dump_config
    elsif opt.key == OPT_LIST[:set]
      set_config(opt.value)
    end
  }
end

#optionsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/m-git/command/config.rb', line 24

def options
  return [
      ARGV::Opt.new(OPT_LIST[:update_manifest],
                    short_key:OPT_LIST[:update_manifest_s],
                    info:"该指令用于更新mgit所使用的配置文件,如:\"mgit config -m <new_path>/manifest.json\"。",
                    type: :string),
      ARGV::Opt.new(OPT_LIST[:update_local],
                    short_key:OPT_LIST[:update_local_s],
                    info:"该指令用于更新mgit所使用的本地配置文件,如:\"mgit config -u <new_path>/local_manifest.json\"。",
                    type: :string),
      ARGV::Opt.new(OPT_LIST[:create_local],
                    short_key:OPT_LIST[:create_local_s],
                    info:"在指定目录下创建本地配置文件,若目录不存在则自动创建。如执行:\"mgit config -c /a/b/c\",则生成本地配置文件:\"/a/b/c/local_manifest.json\"。如果未传入值,如:\"mgit config -c\",那么若配置仓库存在的话,会在配置仓库中创建本地配置文件。",
                    type: :string),
      ARGV::Opt.new(OPT_LIST[:list],
                    short_key:OPT_LIST[:list_s],
                    info:"列出当前MGit所有配置,无参数,如:\"mgit config -l\"。",
                    type: :boolean),
      ARGV::Opt.new(OPT_LIST[:set],
                    short_key:OPT_LIST[:set_s],
                    info:"对MGit进行配置,遵守格式:\"mgit config -s <key> <value>\",如:\"mgit config -s maxconcurrentcount 5\"。")
  ].concat(super)
end

#post_execObject



66
67
68
69
70
# File 'lib/m-git/command/config.rb', line 66

def post_exec
  # 打点结束
  duration = MGit::DurationRecorder.end
  MGit::Loger.info("~~~ #{@argv.absolute_cmd}, 耗时:#{duration} s ~~~")
end

#pre_execObject

— 覆写前后hook,不需要预设操作 —



57
58
59
60
61
62
63
64
# File 'lib/m-git/command/config.rb', line 57

def pre_exec
  # 开始计时
  MGit::DurationRecorder.start
  Workspace.setup_multi_repo_root
  # 配置log
  MGit::Loger.config(Workspace.root)
  MGit::Loger.info("~~~ #{@argv.absolute_cmd} ~~~")
end

#set_config(key_value_arr) ⇒ Object

设置配置



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/m-git/command/config.rb', line 166

def set_config(key_value_arr)
  key = key_value_arr.first
  value = key_value_arr.last
  begin
    MGitConfig.update(Workspace.root) { |config|
      if MGitConfig::CONFIG_KEY.keys.include?(key.to_sym)
        valid_value = MGitConfig.to_suitable_value_for_key(Workspace.root, key, value)
        if !valid_value.nil?
          config[key] = valid_value
        else
          type = MGitConfig::CONFIG_KEY[key.to_sym][:type]
          Foundation.help!("#{value}不匹配类型:#{type},请重试。")
        end
      else
        Foundation.help!("非法key值:#{key}。使用mgit config -l查看所有可配置字段。")
      end
    }
    Output.puts_success_message("配置成功!")
  rescue Error => e
    Foundation.help!(e.msg)
  end
end

#update_local_config(config_path) ⇒ Object

更新本地配置表软链接



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/m-git/command/config.rb', line 124

def update_local_config(config_path)
  config_path = Utils.expand_path(config_path)
  Utils.execute_under_dir("#{File.join(Workspace.root, Constants::PROJECT_DIR[:source_config])}") {
    mgit_managed_local_config_link_path = File.join(Dir.pwd, Constants::CONFIG_FILE_NAME[:local_manifest])
    # 在.mgit/source-config文件夹下创建原始本地配置文件的软连接
    if config_path != mgit_managed_local_config_link_path
      Utils.link(config_path, mgit_managed_local_config_link_path)
    end

    Output.puts_success_message("本地配置文件更新完毕!")
  }
end

#update_mgit_config(config_path) ⇒ Object

更新配置表软链接



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/m-git/command/config.rb', line 102

def update_mgit_config(config_path)
  config = Manifest.parse(Utils.expand_path(config_path))
  Utils.execute_under_dir("#{File.join(Workspace.root, Constants::PROJECT_DIR[:source_config])}") {
    mgit_managed_config_link_path = File.join(Dir.pwd, Constants::CONFIG_FILE_NAME[:manifest])
    mgit_managed_config_cache_path = File.join(Dir.pwd, Constants::CONFIG_FILE_NAME[:manifest_cache])

    # 在.mgit/source-config文件夹下创建原始配置文件的软连接
    if config.path != mgit_managed_config_link_path
      Utils.link(config.path, mgit_managed_config_link_path)
    end

    # 将配置缓存移动到.mgit/source-config文件夹下
    if config.cache_path != mgit_managed_config_cache_path
      FileUtils.rm_f(mgit_managed_config_cache_path) if File.exist?(mgit_managed_config_cache_path)
      FileUtils.mv(config.cache_path, Dir.pwd)
    end

    Output.puts_success_message("配置文件更新完毕!")
  }
end

#validate(argv) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/m-git/command/config.rb', line 48

def validate(argv)
  Foundation.help!("输入非法参数:#{argv.git_opts}。请通过\"mgit #{argv.cmd} --help\"查看用法。") if argv.git_opts.length > 0

  if set_kv = argv.opt(OPT_LIST[:set])
    Foundation.help!("参数#{OPT_LIST[:set]}格式错误,只需传入key和value两个值!") if set_kv.value.count != 2
  end
end