Class: LgPodPlugin::GitLabArchive

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

Instance Method Summary collapse

Constructor Details

#initialize(name, git, branch, tag, commit) ⇒ GitLabArchive

Returns a new instance of GitLabArchive.



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

def initialize(name, git, branch, tag, commit)
  self.git = git ||= LRequest.shared.request_params[:git]
  self.tag = tag ||= LRequest.shared.request_params[:tag]
  self.name = name ||= LRequest.shared.request_params[:name]
  self.commit = commit ||= LRequest.shared.request_params[:commit]
  self.branch = branch ||= LRequest.shared.request_params[:branch]
end

Instance Method Details

#github_download_branch_zip(path, temp_name) ⇒ Object

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 197

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
  origin_url = base_url + "/archive/#{branch}.zip"
  project_name = base_url.split("/").last if base_url
  download_url = "https://gh.api.99988866.xyz/#{origin_url}"
  LgPodPlugin.log_blue "开始下载 => #{download_url}"
  LUtils.download_github_zip_file(download_url, file_name)
  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包



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 258

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}"
  LUtils.download_github_zip_file(download_url, file_name)
  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包



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 225

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}"
  LUtils.download_github_zip_file(download_url, file_name)
  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(root_path, temp_name) ⇒ Object

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



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 74

def gitlab_download_branch_zip(root_path, temp_name)
  project_path = root_path.join(self.name)
  unless project_path.exist?
    project_path.mkdir
    FileUtils.chdir(project_path)
  end
  branch = self.branch ||= "master"
  token = LRequest.shared.config.access_token
  base_url = LRequest.shared.config.project.web_url
  project_name = LRequest.shared.config.project.path
  podspec_name = self.name + ".podspec"
  LgPodPlugin.log_blue "开始下载 => #{base_url}"
  self.gitlab_download_file_by_name(project_path, podspec_name,"#{podspec_name}.zip", project_name)
  podspec_path = project_path.join(podspec_name)
  return nil unless File.exist?(podspec_path)
  begin
    need_download_files = PodSpec.form_file(podspec_path).source_files
  rescue
    need_download_files = Set[]
  end
  unless !need_download_files.empty?
    FileUtils.chdir(root_path)
    file_name = "#{temp_name}.zip"
    download_url = LUtils.get_gitlab_download_url(base_url, branch, nil, nil, project_name)
    raise "download_url不存在" unless download_url
    # LgPodPlugin.log_blue "开始下载 => #{download_url}"
    LUtils.download_gitlab_zip_file(download_url, token, file_name)
    raise "下载zip包失败, 尝试git clone #{self.git}" unless File.exist?(file_name)
    # 解压文件
    result = LUtils.unzip_file(root_path.join(file_name).to_path, "./")
    new_file_name = "#{project_name}-#{branch}"
    raise "解压文件失败, #{new_file_name}不存在" unless result && File.exist?(new_file_name)
    return root_path.join(new_file_name)
  end
  need_download_files.each do |file|
    next if project_path.join(file).exist?
    self.gitlab_download_file_by_name(project_path, file,"#{file}.zip", project_name)
  end
  return project_path
end

#gitlab_download_commit_zip(root_path, temp_name) ⇒ Object

通过 commit 下载zip包



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 156

def gitlab_download_commit_zip(root_path, temp_name)
  project_path = root_path.join(self.name)
  unless project_path.exist?
    project_path.mkdir
    FileUtils.chdir(project_path)
  end
  token = LRequest.shared.config.access_token
  base_url = LRequest.shared.config.project.web_url
  project_name = LRequest.shared.config.project.path
  podspec_name = self.name + ".podspec"
  LgPodPlugin.log_blue "开始下载 => #{base_url}"
  self.gitlab_download_file_by_name(project_path, podspec_name,"#{podspec_name}.zip", project_name)
  podspec_path = project_path.join(podspec_name)
  return nil unless File.exist?(podspec_path)
  begin
    need_download_files = PodSpec.form_file(podspec_path).source_files
  rescue
    need_download_files = Set[]
  end
  unless !need_download_files.empty?
    FileUtils.chdir(root_path)
    file_name = "#{temp_name}.zip"
    download_url = LUtils.get_gitlab_download_url(base_url, nil, nil, self.commit, project_name)
    raise "download_url不存在" unless download_url
    # LgPodPlugin.log_blue "开始下载 => #{download_url}"
    LUtils.download_gitlab_zip_file(download_url, token, file_name)
    raise "下载zip包失败, 尝试git clone #{self.git}" unless File.exist?(file_name)
    # 解压文件
    result = LUtils.unzip_file(root_path.join(file_name).to_path, "./")
    new_file_name = "#{project_name}-#{self.commit}"
    raise "解压文件失败, #{new_file_name}不存在" unless result && File.exist?(new_file_name)
    return root_path.join(new_file_name)
  end
  need_download_files.each do |file|
    self.gitlab_download_file_by_name(project_path, file,"#{file}.zip", project_name)
  end
  return project_path
end

#gitlab_download_file_by_name(path, filename, temp_name, project_name) ⇒ Object

下载某个文件zip格式



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 23

def gitlab_download_file_by_name(path, filename, temp_name, project_name)
  host = LRequest.shared.config.host
  project = LRequest.shared.config.project
  unless host
    http = Ping.new(project.web_url)
    host = http.uri.scheme + "://" + http.uri.hostname
  end
  if self.git && self.tag
    sha = self.tag
  elsif self.git && self.branch
    sha = self.branch
  elsif self.git && self.commit
    sha = self.commit
  end
  token = LRequest.shared.config.access_token
  begin
    encode_fiename = LUtils.url_encode(filename)
    download_url = host + "/api/v4/projects/" + "#{project.id}" + "/repository/archive.zip#{"\\?"}" + "path#{"\\="}#{encode_fiename}#{"\\&"}sha#{"\\="}#{sha}"
  rescue => exception
    return nil
  end
  LUtils.download_gitlab_zip_file(download_url, token, temp_name)
  return nil unless File.exist?(temp_name)
  return nil unless (result = LUtils.unzip_file(temp_name, "./"))
  temp_zip_folder = nil
  path.each_child do |f|
    ftype = File::ftype(f)
    next unless ftype == "directory"
    next unless f.to_path.include?("#{filename}") || f.to_path.include?("#{project_name}")
    temp_zip_folder = f
    break
  end
  return nil unless temp_zip_folder && temp_zip_folder.exist?
  begin
    FileUtils.chdir(temp_zip_folder)
    temp_zip_folder.each_child do |f|
      ftype = File::ftype(f)
      FileUtils.mv(f, path)
    end
    FileUtils.chdir(path)
    FileUtils.rm_rf(temp_zip_folder)
    FileUtils.rm_rf("./#{temp_name}")
    return path
  rescue => exception
    pp exception
    return path
  end
end

#gitlab_download_tag_zip(root_path, temp_name) ⇒ Object

通过tag下载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
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/lg_pod_plugin/gitlab_archive.rb', line 116

def gitlab_download_tag_zip(root_path, temp_name)
  project_path = root_path.join(self.name)
  unless project_path.exist?
    project_path.mkdir
    FileUtils.chdir(project_path)
  end
  token = LRequest.shared.config.access_token
  base_url = LRequest.shared.config.project.web_url
  project_name = LRequest.shared.config.project.path
  podspec_name = self.name + ".podspec"
  LgPodPlugin.log_blue "开始下载 => #{base_url}"
  self.gitlab_download_file_by_name(project_path, podspec_name,"#{podspec_name}.zip", project_name)
  podspec_path = project_path.join(podspec_name)
  return nil unless File.exist?(podspec_path)
  begin
    need_download_files = PodSpec.form_file(podspec_path).source_files
  rescue
    need_download_files = Set[]
  end
  unless !need_download_files.empty?
    tag = self.tag
    FileUtils.chdir(root_path)
    file_name = "#{temp_name}.zip"
    download_url = LUtils.get_gitlab_download_url(base_url, nil, tag, nil, project_name)
    raise "download_url不存在" unless download_url
    LUtils.download_gitlab_zip_file(download_url, token, file_name)
    raise "下载zip包失败, 尝试git clone #{self.git}" unless File.exist?(file_name)
    # 解压文件
    result = LUtils.unzip_file(root_path.join(file_name).to_path, "./")
    new_file_name = "#{project_name}-#{tag}"
    raise "解压文件失败, #{new_file_name}不存在" unless result && File.exist?(new_file_name)
    return root_path.join(new_file_name)
  end
  need_download_files.each do |file|
    self.gitlab_download_file_by_name(project_path, file,"#{file}.zip", project_name)
  end
  return project_path
end