Class: LgPodPlugin::LGitUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/lg_pod_plugin/git_util.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ LGitUtil

Returns a new instance of LGitUtil.



14
15
16
17
18
19
20
21
# File 'lib/lg_pod_plugin/git_util.rb', line 14

def initialize(name, options = {})
  self.name = name
  self.git = options[:git]
  self.tag = options[:tag]
  self.path = options[:path]
  self.branch = options[:branch]
  self.commit = options[:commit]
end

Class Method Details

.git_ls_remote_refs(git, branch, tag, commit) ⇒ Object

获取最新的一条 commit 信息



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/lg_pod_plugin/git_util.rb', line 294

def self.git_ls_remote_refs(git, branch, tag, commit)
  if branch
    LgPodPlugin.log_yellow "git ls-remote #{git} #{branch}"
    result = %x(git ls-remote #{git} #{branch})
    new_commit = result.split(" ").first if result
    return [branch, new_commit]
  elsif tag
    LgPodPlugin.log_yellow "git ls-remote #{git}"
    ls = Git.ls_remote(git, :head => true)
    map = ls["tags"]
    keys = map.keys
    idx = keys.index("#{tag}")
    unless idx
      return [nil, nil]
    end
    key = keys[idx]
    new_commit = map[key][:sha]
    return [nil, new_commit]
  else
    if commit
      return nil, commit
    else
      LgPodPlugin.log_yellow "git ls-remote #{git}"
      ls = Git.ls_remote(git, :head => true)
      find_commit = ls["head"][:sha]
      ls["branches"].each do |key, value|
        sha = value[:sha]
        next if sha != find_commit
        return [key, find_commit]
      end
      return nil, find_commit
    end
  end

end

Instance Method Details

#git_clone_by_branch(path, temp_name) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/lg_pod_plugin/git_util.rb', line 204

def git_clone_by_branch(path, temp_name)
  if self.git && self.branch
    LgPodPlugin.log_blue "git clone --depth=1 --branch #{self.branch} #{self.git}"
    system("git clone --depth=1 -b #{self.branch} #{self.git} #{temp_name}")
  else
    LgPodPlugin.log_blue "git clone --depth=1 #{self.git}"
    system("git clone --depth=1 #{self.git} #{temp_name}")
  end
  path.join(temp_name)
end

#git_clone_by_commit(path, temp_name) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'lib/lg_pod_plugin/git_util.rb', line 221

def git_clone_by_commit(path, temp_name)
  LgPodPlugin.log_blue "git clone #{self.git}"
  Git.init(temp_name)
  FileUtils.chdir(temp_name)
  system("git remote add origin #{self.git}")
  system("git fetch origin #{self.commit}")
  system("git reset --hard FETCH_HEAD")
  path.join(temp_name)
end

#git_clone_by_tag(path, temp_name) ⇒ Object



215
216
217
218
219
# File 'lib/lg_pod_plugin/git_util.rb', line 215

def git_clone_by_tag(path, temp_name)
  LgPodPlugin.log_blue "git clone --tag #{self.tag} #{self.git}"
  system("git clone --depth=1 -b #{self.tag} #{self.git} #{temp_name}")
  path.join(temp_name)
end

#git_clone_repository(path) ⇒ Object

clone 代码仓库



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/lg_pod_plugin/git_util.rb', line 232

def git_clone_repository(path)
  FileUtils.chdir(path)
  temp_name = "lg_temp_pod"
  if self.git && self.tag
    if self.git.include?("capp/iOS")
      return gitlab_download_tag_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return github_download_tag_zip path, temp_name
    else
      return self.git_clone_by_tag(path, temp_name)
    end
  elsif self.git && self.branch
    if self.git.include?("capp/iOS")
      return self.gitlab_download_branch_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return self.github_download_branch_zip path, temp_name
    else
      return self.git_clone_by_branch(path, temp_name)
    end
  elsif self.git && self.commit
    if self.git.include?("capp/iOS")
      return self.gitlab_download_commit_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return self.github_download_commit_zip path, temp_name
    else
      return self.git_clone_by_commit(path, temp_name)
    end
  elsif self.git
    if self.git.include?("capp/iOS")
      return self.gitlab_download_branch_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return self.github_download_branch_zip path, temp_name
    else
      return self.git_clone_by_branch(path, temp_name)
    end
  end

end

#github_download_branch_zip(path, temp_name) ⇒ Object

从 Github下载 zip 包 根据branch 下载 zip 包



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lg_pod_plugin/git_util.rb', line 116

def github_download_branch_zip(path, temp_name)
  file_name = "#{temp_name}.zip"
  branch = self.branch ||= "master"
  if self.git.include?(".git")
    base_url = self.git[0...self.git.length - 4]
  else
    base_url = self.git
  end
  project_name = base_url.split("/").last if base_url
  origin_url = base_url + "/archive/#{branch}.zip"
  download_url = "https://gh.api.99988866.xyz/#{origin_url}"
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl -o #{file_name} --connect-timeout 60 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("下载zip包失败, 尝试git clone #{self.git}")
    return self.git_clone_by_branch(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  new_file_name = "#{project_name}-#{branch}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_branch(path, temp_name)
  end
  path.join(new_file_name)
end

#github_download_commit_zip(path, temp_name) ⇒ Object

通过 commit 下载zip包



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lg_pod_plugin/git_util.rb', line 177

def github_download_commit_zip(path, temp_name)
  file_name = "#{temp_name}.zip"
  if self.git.include?(".git")
    base_url = self.git[0...self.git.length - 4]
  else
    base_url = self.git
  end
  project_name = base_url.split("/").last if base_url
  origin_url = base_url + "/archive/#{self.commit}.zip"
  download_url = "https://gh.api.99988866.xyz/#{origin_url}"
  # 下载文件
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl -s -o #{file_name} --connect-timeout 15 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_commit(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  new_file_name = "#{project_name}-#{self.commit}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_commit(path, temp_name)
  end
  path.join(new_file_name)
end

#github_download_tag_zip(path, temp_name) ⇒ Object

通过tag下载zip包



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lg_pod_plugin/git_util.rb', line 144

def github_download_tag_zip(path, temp_name)
  file_name = "#{temp_name}.zip"
  if self.git.include?(".git")
    base_url = self.git[0...self.git.length - 4]
  else
    base_url = self.git
  end
  project_name = base_url.split("/").last if base_url
  origin_url = base_url + "/archive/refs/tags/#{self.tag}.zip"
  download_url = "https://gh.api.99988866.xyz/#{origin_url}"
  # 下载文件
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl -s -o #{file_name} --connect-timeout 60 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_tag(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  if self.tag.include?("v") && self.tag[0...1] == "v"
    this_tag = self.tag[1...self.tag.length]
  else
    this_tag = self.tag
  end
  new_file_name = "#{project_name}-#{this_tag}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_tag(path, temp_name)
  end
  path.join(new_file_name)
end

#gitlab_download_branch_zip(path, temp_name) ⇒ Object

从 GitLab下载 zip包 根据branch 下载 zip 包



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lg_pod_plugin/git_util.rb', line 25

def gitlab_download_branch_zip(path, temp_name)
  branch = self.branch ||= "master"
  token = LRequest.shared.token
  unless token
    return self.git_clone_by_branch(path, temp_name)
  end
  file_name = "#{temp_name}.zip"
  base_url = self.git[0...self.git.length - 4]
  project_name = base_url.split("/").last
  unless project_name
    return self.git_clone_by_branch(path, temp_name)
  end
  download_url = base_url + "/-/archive/" + branch + "/#{project_name}-#{branch}.zip"
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl --header PRIVATE-TOKEN:#{token} -o #{file_name} --connect-timeout 60 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("下载zip包失败, 尝试git clone #{self.git}")
    return self.git_clone_by_branch(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  new_file_name = "#{project_name}-#{branch}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_branch(path, temp_name)
  end
  path.join(new_file_name)
end

#gitlab_download_commit_zip(path, temp_name) ⇒ Object

通过 commit 下载zip包



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lg_pod_plugin/git_util.rb', line 85

def gitlab_download_commit_zip(path, temp_name)
  token = LRequest.shared.token
  unless token
    return self.git_clone_by_commit(path, temp_name)
  end
  base_url = self.git[0...self.git.length - 4]
  project_name = base_url.split("/").last
  unless project_name
    return self.git_clone_by_commit(path, temp_name)
  end
  file_name = "#{temp_name}.zip"
  download_url = base_url + "/-/archive/" + self.commit + "/#{project_name}-#{self.commit}.zip"
  # 下载文件
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl -s --header PRIVATE-TOKEN:#{token} -o #{file_name} --connect-timeout 60 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_commit(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  new_file_name = "#{project_name}-#{self.commit}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_commit(path, temp_name)
  end
  path.join(new_file_name)
end

#gitlab_download_tag_zip(path, temp_name) ⇒ Object

通过tag下载zip包



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
80
81
82
# File 'lib/lg_pod_plugin/git_util.rb', line 55

def gitlab_download_tag_zip(path, temp_name)
  token = LRequest.shared.token
  unless token
    return self.git_clone_by_tag(path, temp_name)
  end
  base_url = self.git[0...self.git.length - 4]
  project_name = base_url.split("/").last
  unless project_name
    return self.git_clone_by_tag(path, temp_name)
  end
  file_name = "#{temp_name}.zip"
  download_url = base_url + "/-/archive/" + self.tag + "/#{project_name}-#{self.tag}.zip"
  # 下载文件
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  system("curl -s --header PRIVATE-TOKEN:#{token} -o #{file_name} --connect-timeout 60 --retry 3 #{download_url}")
  unless File.exist?(file_name)
    LgPodPlugin.log_red("下载zip包失败, 尝试git clone #{self.git}")
    return self.git_clone_by_tag(path, temp_name)
  end
  # 解压文件
  result = LUtils.unzip_file(path.join(file_name).to_path, "./")
  new_file_name = "#{project_name}-#{self.tag}"
  unless result && File.exist?(new_file_name)
    LgPodPlugin.log_red("正在尝试git clone #{self.git}")
    return self.git_clone_by_tag(path, temp_name)
  end
  path.join(new_file_name)
end

#pre_download_git_repositoryObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/lg_pod_plugin/git_util.rb', line 271

def pre_download_git_repository
  temp_path = LFileManager.download_director.join("temp")
  if temp_path.exist?
    FileUtils.rm_rf(temp_path)
  end
  lg_pod_path = LRequest.shared.cache.cache_root
  unless lg_pod_path.exist?
    lg_pod_path.mkdir(0700)
  end
  get_temp_folder = git_clone_repository(lg_pod_path)
  #下载 git 仓库失败
  unless get_temp_folder.exist?
    return nil
  end
  if LRequest.shared.single_git
    LgPodPlugin::LCache.cache_pod(self.name, get_temp_folder, { :git => self.git })
  end
  LgPodPlugin::LCache.cache_pod(self.name, get_temp_folder, LRequest.shared.get_cache_key_params)
  FileUtils.chdir(LFileManager.download_director)
  FileUtils.rm_rf(lg_pod_path)
end