Module: Pod::Private::PrivateCache

Defined in:
lib/cocoapods-modularization/private/private_cache.rb

Defined Under Namespace

Classes: BranchCache

Constant Summary collapse

@@root_path =
Pathname.new(File.expand_path('~')) + 'Projects/Pods'
@@path_map =
'.path_map.json'
@@repo_map =
'.repo_map.json'
@@path_map_path =
@@root_path + @@path_map
@@repo_map_path =
@@root_path + @@repo_map
@@binary_repo_key =
'binary'
@@source_repo_key =
'source'

Class Method Summary collapse

Class Method Details

.binary_repo_nameObject



52
53
54
55
56
57
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 52

def binary_repo_name
  repo_map = cached_repo_map
  binary_map = repo_map[@@binary_repo_key]
  return nil unless binary_map.kind_of?(Hash)
  binary_map.keys.first
end

.binary_repo_urlObject



66
67
68
69
70
71
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 66

def binary_repo_url
  repo_map = cached_repo_map
  binary_map = repo_map[@@binary_repo_key]
  return nil unless binary_map.kind_of?(Hash)
  binary_map.values.first
end

.cache_branch(branch_caches) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 84

def cache_branch(branch_caches)
  make_branch_cache_workspace_if_needed
  branch_hash = Hash.new

  branch_caches.each do |e|
    branch_hash[e.key] = {Meta::MetaConstants.branch_key => e.branch, Meta::MetaConstants.git_key => e.git}
  end

  File.open(Meta::MetaConstants.branch_cache_path, "w") { |io| io << branch_hash.to_yaml }
end

.cache_repo(repo_name, repo_url, is_binary) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 39

def cache_repo(repo_name, repo_url, is_binary)
  make_workspace
  repo_map = cached_repo_map

  if is_binary
    repo_map[@@binary_repo_key] = Hash[repo_name => repo_url]
  else
    repo_map[@@source_repo_key] = Hash[repo_name => repo_url]
  end

  File.open(@@repo_map_path, 'w') { |io| io << repo_map.to_json }
end

.cached_branch_hashObject



95
96
97
98
99
100
101
102
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 95

def cached_branch_hash
  unless File.exists?(Meta::MetaConstants.branch_cache_path)
    return Hash.new
  end

  yaml = YAML.load_file(Meta::MetaConstants.branch_cache_path)
  yaml
end

.read_local_path(repo_name) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 177

def read_local_path(repo_name)
  return "" unless File.exists?(@@path_map_path)
  content = File.open(@@path_map_path)
  json = JSON.load(content)
  local_path = json[repo_name] if json.kind_of?(Hash)
  return local_path if local_path.kind_of?(String)

  # 如果不存在, 先在search_path中寻找
  local_path = locate_spec_in_search_path(repo_name)
  if local_path.kind_of?(String)
    write_path_into_cache(repo_name, local_path)
    return local_path 
  end
end

.root_pathObject



80
81
82
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 80

def root_path
  @@root_path
end

.set_search_path(search_path) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 104

def set_search_path(search_path)
  make_workspace()
  content = File.open(@@path_map_path)
  json = JSON.load(content)

  map = Hash.new
  if json.kind_of?(JSON)
    map = json.to_hash
  end
  map['search_path'] = search_path

  File.open(@@path_map_path, "w") { |io| io.puts map.to_json }
end

.source_repo_nameObject



59
60
61
62
63
64
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 59

def source_repo_name
  repo_map = cached_repo_map
  source_map = repo_map[@@source_repo_key]
  return nil unless source_map.kind_of?(Hash)
  source_map.keys.first
end

.source_repo_urlObject



73
74
75
76
77
78
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 73

def source_repo_url
  repo_map = cached_repo_map
  source_map = repo_map[@@source_repo_key]
  return nil unless source_map.kind_of?(Hash)
  source_map.values.first
end

.start(repo_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 118

def start(repo_name)
  current_pwd = Dir.pwd

  # 1.检查repo_name合法性, 是否在repos中存在
  repos_path = locate_repos_path
  raise "#{repo_name} not found in configured source, run 'pod mod config' or 'pod repo uppdate' at first" unless repos_path

  # 2.读取@@path_map缓存,是否有repo_name对应的pod的path存在
  make_workspace()
  # 2.1 如果repo_name对应的path存在, 返回他
  local_path = read_local_path(repo_name)
  return local_path if local_path.kind_of?(String)

  ## 2.3 使用repos的地址clone到@@root中并在@@path_map中写入缓存地址
  version = Meta::MetaAccessor.version(repo_name)
  version_path = Pathname.new(repos_path) + repo_name
  raise "#{version}不存在, run `pod repo update #{File.basename(repos_path)}`" unless Dir.entries(version_path).include?(version)

  repo_spec_path = version_path + "#{version}/#{repo_name}.podspec"
  raise "#{repo_spec_path} 不存在, 不是一个合法的podspec" unless File.exists?(repo_spec_path)

  ## 切换workspace
  FileUtils.cd(@@root_path)
  ## 判断repo_name是否存在,如果存在,删除他,可能是一些临时文件
  if File.exists?(repo_name)
    UI.puts '清空workspace'
    FileUtils.rm_rf(repo_name)
  end

  git_clone_command = package_clone_command(repo_spec_path, repo_name)
  UI.puts "clone #{repo_name} into #{@@root_path}"
  system_build(git_clone_command)

  ## 下载完成之后,把路径写入缓存
  local_path = "#{@@root_path}/#{repo_name}"
  write_path_into_cache(repo_name, path)

  ## 结束后,重置pwd
  FileUtils.cd(current_pwd)

  return local_path
end

.write_path_into_cache(repo_name, path) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cocoapods-modularization/private/private_cache.rb', line 161

def write_path_into_cache(repo_name, path)
  content = File.open(@@path_map_path)
  json = JSON.load(content)

  map = Hash.new
  if json.kind_of?(JSON)
    map = json.to_hash
  end
  map[repo_name] = path

  # path的上一级作为search_path
  map['search_path'] = Pathname.new(path).parent

  File.open(@@path_map_path, "w") { |io| io.puts map.to_json }
end