Class: Pixab::MboxAdd

Inherits:
Object
  • Object
show all
Defined in:
lib/mbox/MboxAdd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_manager) ⇒ MboxAdd

Returns a new instance of MboxAdd.



12
13
14
15
# File 'lib/mbox/MboxAdd.rb', line 12

def initialize(repo_manager)
  @repo_manager = repo_manager
  @default_branch = AirBrushProjectInfo::DEFAULT_BRANCH
end

Instance Attribute Details

#default_branchObject (readonly)

Returns the value of attribute default_branch.



10
11
12
# File 'lib/mbox/MboxAdd.rb', line 10

def default_branch
  @default_branch
end

#repo_managerObject (readonly)

Returns the value of attribute repo_manager.



10
11
12
# File 'lib/mbox/MboxAdd.rb', line 10

def repo_manager
  @repo_manager
end

Instance Method Details

#add_repo(commands) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mbox/MboxAdd.rb', line 49

def add_repo(commands)
  return if commands.empty?

  execute_commad = "mbox add"
  commands.each do |command|
    execute_commad += " #{command}"
  end

  stdout, status = Open3.capture2(execute_commad)
  if status.success?
    pull_lfs_files_if_needed(commands[0])
  end
end

#get_all_sub_repo_namesObject

获取所有的子仓名



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mbox/MboxAdd.rb', line 64

def get_all_sub_repo_names
  podfile_path = "#{@repo_manager.root_path}/#{@repo_manager.main_repo_name}/#{AirBrushProjectInfo::COMPONENT_FILE_PATH}" 
  podfile_content = File.read(podfile_path)
  matches = podfile_content.match(/def pix_ab_component(.*?)end/m)
  return [] if matches.nil?
  podfile_content = matches[0]
  reg = /'([^']+).+:commit => '(.+)'/
  matches = podfile_content.scan(reg)
  repo_names = matches.map do |match|
    match[0]
  end
  repo_names - @repo_manager.sub_repo_names
end

#pull_lfs_files_if_needed(repo_name) ⇒ Object

如果使用了lfs,则拉取lfs文件



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mbox/MboxAdd.rb', line 79

def pull_lfs_files_if_needed(repo_name)
  # 使用此方法切换路径时可以避免在控制台打印路径,并在执行完成后会切换回原路径
  FileUtils.cd("#{@repo_manager.root_path}/#{repo_name}") do
    break unless File.exist?(".gitattributes")

    file_content = File.read(".gitattributes")
    break unless file_content.include?("filter=lfs")

    Open3.capture3("mbox git hooks --disable")
    `git lfs pull`
    Open3.capture3("mbox git hooks --enable")
  end
end

#run(commands) ⇒ Object



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
# File 'lib/mbox/MboxAdd.rb', line 17

def run(commands)
  commands.each_index do |index|
    command = commands[index]
    case command
    when "--default-branch"
      @default_branch = commands[index + 1]
      commands.delete_at(index + 1)
      commands.delete_at(index)
    end
  end

  if commands.empty?
    repo_names = get_all_sub_repo_names
    if !repo_names.empty?
      selected_item_names = Utilities.display_choose_list(repo_names, nil, "仓库", "请选择需要添加的仓库:", nil, nil, true)
      selected_item_names.each do |repo_name|
        add_repo([repo_name.strip, @default_branch])
      end
    end
  elsif commands[0] == "--all"
    repo_names = get_all_sub_repo_names
    repo_names.each do |repo_name|
      add_repo([repo_name, @default_branch])
    end
  else
    add_repo(commands)
  end

  system("mbox status")

end