Class: MGit::Manifest

Inherits:
Object
  • Object
show all
Includes:
Internal, Linter
Defined in:
lib/m-git/manifest.rb,
lib/m-git/manifest/linter.rb,
lib/m-git/manifest/internal.rb,
lib/m-git/manifest/light_repo.rb,
lib/m-git/manifest/cache_manager.rb,
lib/m-git/manifest/light_repo_generator.rb

Overview

Sample: ‘m-git/manifest/manifest.sample’

Defined Under Namespace

Modules: Internal, Linter Classes: CacheManager, LightRepo, LightRepoGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal

#__setup, #__simple_setup

Methods included from Linter

#lint_light_repos!, #lint_local_manifest_path, #lint_manifest_path, #lint_raw_json!

Instance Attribute Details

#cache_pathObject (readonly)

String

缓存文件路径



35
36
37
# File 'lib/m-git/manifest.rb', line 35

def cache_path
  @cache_path
end

#configObject (readonly)

Hash

配置内容



16
17
18
# File 'lib/m-git/manifest.rb', line 16

def config
  @config
end

#config_repoObject (readonly)

LightRepo

包含配置表的配置仓库



29
30
31
# File 'lib/m-git/manifest.rb', line 29

def config_repo
  @config_repo
end

#hash_sha1Object (readonly)

String

配置表内容哈希

attr_reader :config_hash



20
21
22
# File 'lib/m-git/manifest.rb', line 20

def hash_sha1
  @hash_sha1
end

#light_reposObject (readonly)

Array<LightRepo>

与config中‘repositories’字段对应的Manifest::LightRepo对象数组,包含git仓库和非git仓库



23
24
25
# File 'lib/m-git/manifest.rb', line 23

def light_repos
  @light_repos
end

#pathObject (readonly)

String

配置文件路径



32
33
34
# File 'lib/m-git/manifest.rb', line 32

def path
  @path
end

#previous_configObject (readonly)

Hash

上次改动生成的配置缓存



38
39
40
# File 'lib/m-git/manifest.rb', line 38

def previous_config
  @previous_config
end

#previous_extra_light_reposObject (readonly)

Array<LightRepo>

类型同light_repos,表示上次操作,但本次未操作的仓库



26
27
28
# File 'lib/m-git/manifest.rb', line 26

def previous_extra_light_repos
  @previous_extra_light_repos
end

Class Method Details

.generate_light_repos(root, mgit_managed_only: false, only_exist: false, exclude_dummy: false) ⇒ Array<LightRepo>

在mgit根目录中搜索配置文件并根据配置文件生成对应LightRepo对象。

!!!!!

请勿随意修改该接口

Parameters:

  • root (String)

    mgit工作区根目录

  • mgit_managed_only (Boolean) (defaults to: false)

    default: false,是否只获取mgit管理的git仓库

  • only_exist (Boolean) (defaults to: false)

    default: false,是否只获取实际存在的仓库

  • exclude_dummy (Boolean) (defaults to: false)

    default: false,是否排除dummy仓库

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/m-git/manifest.rb', line 54

def self.generate_light_repos(root, mgit_managed_only: false, only_exist:false, exclude_dummy:false)
  config_path = File.join(root, Constants::PROJECT_DIR[:source_config])
  config = self.parse(config_path)

  if mgit_managed_only
    repos = config.repo_list
  elsif exclude_dummy
    repos = config.light_repos.select { |repo| !repo.dummy }
  else
    repos = config.light_repos
  end

  if only_exist
    existing_repos = []
    repos.each { |repo|
      abs_path = repo.abs_dest(root)
      # 被管理的仓库验证是否是git仓库,不被管理的仓库只验证文件夹是否存在
      if (repo.mgit_excluded && Dir.exist?(abs_path)) || (!repo.mgit_excluded && Repo.is_git_repo?(abs_path))
        existing_repos.push(repo)
      end
    }
    repos = existing_repos
  end

  return repos
end

.parse(config_path, strict_mode: true) ⇒ Manifest

全流程校验,除了字段合法性校验外,还包含缓存生成/校验,local配置文件校验/合并

Parameters:

  • config_path (String)

    配置文件本地路径

  • strict_mode (Boolean) (defaults to: true)

    default: true 如果为true,出错直接报错退出,如果为false,出错抛出异常(MGit::Error)

Returns:



89
90
91
92
93
# File 'lib/m-git/manifest.rb', line 89

def self.parse(config_path, strict_mode:true)
  config = Manifest.new
  config.__setup(config_path, strict_mode)
  return config
end

.simple_parse(config_content, strict_mode: true) ⇒ Manifest

简单校验,仅校验配置内容,无缓存生成/校验,无local配置文件校验/合并

Parameters:

  • config_content (String/Hash)

    配置表json字符串或字典(key值需为String)

  • strict_mode (Boolean) (defaults to: true)

    default: true 如果为true,出错直接报错退出,如果为false,出错抛出异常(MGit::Error)

Returns:



103
104
105
106
107
# File 'lib/m-git/manifest.rb', line 103

def self.simple_parse(config_content, strict_mode:true)
  config = Manifest.new
  config.__simple_setup(config_content, strict_mode)
  return config
end

Instance Method Details

#global_config(key) ⇒ Object

获取全局配置

Parameters:

  • key (Symbol)

    配置字段【符号】

Returns:

  • (Object)

    对应配置字段的值,可能是字符串,也可能是字典



141
142
143
144
145
146
# File 'lib/m-git/manifest.rb', line 141

def global_config(key)
  key_str = Constants::CONFIG_KEY[key]
  value = config[key_str]
  terminate!("无法获取多仓库配置必需字段\"#{key}\"的值!") if value.nil? && Constants::REQUIRED_CONFIG_KEY.include?(key_str)
  value
end

#repo_list(selection: nil, exclusion: nil, all: false) ⇒ Array<LightRepo>

返回所有git仓库列表

Parameters:

  • selection (Array<String>) (defaults to: nil)

    default: nil,需要筛选的仓库名数组

  • exclusion (Array<String>) (defaults to: nil)

    default: nil,需要排除的仓库名数组

  • all (Boolean) (defaults to: false)

    default: false 若指定为true,则忽略mgit_excluded的字段值,只要remote url存在(即有对应远程仓库),则选取

Returns:

  • (Array<LightRepo>)

    被mgit管理的仓库生成的LightRepo数组



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/m-git/manifest.rb', line 120

def repo_list(selection: nil, exclusion: nil, all:false)
  list = []
  light_repos.each { |light_repo|
    if !light_repo.mgit_excluded || (all && !light_repo.url.nil?)
      # 选取指定仓库
      if (selection.nil? && exclusion.nil?) ||
        (!selection.nil? && selection.is_a?(Array) && selection.any? { |e| e.downcase == light_repo.name.downcase }) ||
        (!exclusion.nil? && exclusion.is_a?(Array) && !exclusion.any? { |e| e.downcase == light_repo.name.downcase })
        list.push(light_repo)
      end
    end
  }
  list
end

#terminate!(msg, type: nil) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/m-git/manifest.rb', line 170

def terminate!(msg, type:nil)
  if @strict_mode
    Foundation.help!(msg)
  else
    raise Error.new(msg, type:type)
  end
end

#update_cache_with_content(root, config_content) ⇒ Object

更新缓存

Parameters:

  • root (String)

    多仓库根目录

  • config_content (Hash)

    配置Hash



154
155
156
157
158
159
# File 'lib/m-git/manifest.rb', line 154

def update_cache_with_content(root, config_content)
  config_dir = File.join(root, Constants::PROJECT_DIR[:source_config])
  cache_path = File.join(config_dir, Constants::CONFIG_FILE_NAME[:manifest_cache])
  sha1 = __generate_hash_sha1(config_content.to_json)
  CacheManager.save_to_cache(cache_path, sha1, config_content)
end

#update_previous_extra_light_repos(root) ⇒ Object

更新(对比上次操作)冗余的轻量仓库对象

Parameters:

  • root (String)

    多仓库根目录



165
166
167
168
# File 'lib/m-git/manifest.rb', line 165

def update_previous_extra_light_repos(root)
  cache_path = File.join(root, Constants::PROJECT_DIR[:source_config],  Constants::CONFIG_FILE_NAME[:manifest_cache])
  __load_cache(cache_path)
end