Class: LgPodPlugin::LUtils

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

Class Method Summary collapse

Class Method Details

.commit_from_ls_remote(output, branch_name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lg_pod_plugin/l_util.rb', line 70

def self.commit_from_ls_remote(output, branch_name)
  return nil if branch_name.nil?
  encoded_branch_name = branch_name.dup.force_encoding(Encoding::ASCII_8BIT)
  if branch_name == "HEAD"
    match1 = %r{([a-z0-9]*)\t#{Regexp.quote(encoded_branch_name)}}.match(output)
    sha = match1[1] unless match1.nil?
    refs = output.split("\n")
    return [sha, nil] unless refs.is_a?(Array)
    refs.each do |element|
      next if element.include?("HEAD") || element.include?("refs/tags")
      next unless element.include?(sha)
      find_branch = element.split("refs/heads/").last
      return [sha, find_branch]
    end
  else
    match = %r{([a-z0-9]*)\trefs\/(heads|tags)\/#{Regexp.quote(encoded_branch_name)}}.match(output)
    sha = match[1] unless match.nil?
    ref = match[0].split("refs/heads/").last unless match.nil?
    return [sha, ref]
  end

end

.download_github_zip_file(download_url, file_name) ⇒ Object

gitlab 下载压缩文件



48
49
50
51
52
53
54
55
56
# File 'lib/lg_pod_plugin/l_util.rb', line 48

def self.download_github_zip_file(download_url, file_name)
  cmds = ['curl']
  cmds << "-o #{file_name}"
  cmds << "--connect-timeout 15"
  cmds << "--retry 3"
  cmds << download_url
  cmds_to_s = cmds.join(" ")
  system(cmds_to_s)
end

.download_gitlab_zip_file(download_url, token, file_name) ⇒ Object

下载 zip 格式文件



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lg_pod_plugin/l_util.rb', line 35

def self.download_gitlab_zip_file(download_url, token, file_name)
  cmds = ['curl']
  cmds << "--header \"Authorization: Bearer #{token}\"" if token
  # cmds << "--progress-bar"
  cmds << "-o #{file_name}"
  cmds << "--connect-timeout 15"
  cmds << "--retry 3"
  cmds << download_url
  cmds_to_s = cmds.join(" ")
  system(cmds_to_s)
end

.get_git_project_name(git) ⇒ Object

截取git-url 拿到项目绝对名称 比如 l-base-ios



94
95
96
# File 'lib/lg_pod_plugin/l_util.rb', line 94

def self.get_git_project_name(git)
  self.get_gitlab_base_url(git).split("/").last
end

.get_gitlab_base_url(git) ⇒ Object

截取 url



110
111
112
113
114
115
116
# File 'lib/lg_pod_plugin/l_util.rb', line 110

def self.get_gitlab_base_url(git)
  if git.include?(".git")
    base_url = git.split(".git").first
  else
    base_url = git
  end
end

.get_gitlab_download_url(base_url, branch, tag, commit, project_name) ⇒ Object

根据参数生成下载 url



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lg_pod_plugin/l_util.rb', line 119

def self.get_gitlab_download_url(base_url, branch, tag, commit, project_name)
  if base_url.include?("http:") || base_url.include?("https:")
    if branch
      return base_url + "/-/archive/" + branch + "/#{project_name}-#{branch}.zip"
    elsif tag
      return base_url + "/-/archive/" + tag + "/#{project_name}-#{tag}.zip"
    elsif commit
      return base_url + "/-/archive/" + commit + "/#{project_name}-#{commit}.zip"
    else
      return nil
    end
  end
  return nil unless base_url.include?("ssh://git@gitlab") || base_url.include?("git@")
  project = LRequest.shared.config.project
  if project && project.web_url && project.web_url.include?("http")
    self.get_gitlab_download_url(project.web_url, branch, tag, commit, project_name)
  else
    return nil
  end
end

.git_to_uri(git) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lg_pod_plugin/l_util.rb', line 58

def self.git_to_uri(git)
  begin
    uri = URI(git)
  rescue
    if git.include?("git@") && git.include?(":")
      uri = URI("http://" + git[4...git.length].split(":").first)
    else
      return nil
    end
  end
end

.is_string(obj) ⇒ Object

判断对象是不是 String



11
12
13
14
15
16
17
# File 'lib/lg_pod_plugin/l_util.rb', line 11

def self.is_string(obj)
  if "#{obj.class}" == "String"
    return true 
  else 
    return false 
  end
end

.is_use_gitlab_archive_file(git) ⇒ Object

是否能够使用 gitlab 下载 zip 文件



99
100
101
102
103
104
105
106
107
# File 'lib/lg_pod_plugin/l_util.rb', line 99

def self.is_use_gitlab_archive_file(git)
  return false if git.include?("https://github.com") || git.include?("https://gitee.com")
  config = LRequest.shared.config
  return false if (!config || !config.access_token)
  return true if project = config.project
  project_name = self.get_git_project_name(git)
  LRequest.shared.config.project = GitLabAPI.request_project_info(config.host, project_name, config.access_token, git)
  return true if LRequest.shared.config.project
end

.unzip_file(zip_file, dest_dir) ⇒ Object

解压文件



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lg_pod_plugin/l_util.rb', line 20

def self.unzip_file (zip_file, dest_dir)
  begin
    Archive::Zip.extract(
      zip_file,
      dest_dir,
      :symlinks => true
    )
    return true
  rescue => err
    return false
  end

end

.url_encode(url) ⇒ Object



140
141
142
# File 'lib/lg_pod_plugin/l_util.rb', line 140

def self.url_encode(url)
  url.to_s.b.gsub(/[^a-zA-Z0-9_\-.~]/n) { |m| sprintf('%%%02X', m.unpack1('C')) }
end