Class: LgPodPlugin::LUtils

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

Class Method Summary collapse

Class Method Details

.base64_decode(text) ⇒ Object



30
31
32
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 30

def self.base64_decode(text)
  Base64.decode64(text)
end

.base64_encode(text) ⇒ Object



26
27
28
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 26

def self.base64_encode(text)
  Base64.encode64(text)
end

.decrypt(message, password) ⇒ Object



17
18
19
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 17

def self.decrypt(message, password)
  AESCrypt.decrypt message, password
end

.encrypt(message, password) ⇒ Object



12
13
14
15
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 12

def self.encrypt(message, password)
  encrypted_data = AESCrypt.encrypt(message, password)
  encrypted_data.tr("\n", "")
end

.get_git_project_name(git) ⇒ Object



95
96
97
98
99
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 95

def self.get_git_project_name(git)
  base_url = self.get_gitlab_base_url(git)
  match = %r{[^/]+$}.match(base_url)
  return match[0] unless match.nil?
end

.get_gitlab_base_url(git) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 103

def self.get_gitlab_base_url(git)
  if git.include?(".git")
    math = /(.*(?=.git))/.match(git)
    return math[0] unless math.nil?
  else
    return git
  end
end

.is_a_string?(obj) ⇒ Boolean

判断对象是不是 String

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 35

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

.is_gitlab_uri(git, hostname) ⇒ Object



125
126
127
128
129
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 125

def self.is_gitlab_uri(git, hostname)
  match1 = %r{(github.com|gitee.com|coding.net|code.aliyun.com)}.match(git)
  match2 = %r{(github.com|gitee.com|coding.net|code.aliyun.com)}.match(hostname)
  return match1.nil? && match2.nil?
end

.md5(text) ⇒ Object



21
22
23
24
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 21

def self.md5(text)
  return "" unless text
  return Digest::MD5.hexdigest(text)
end

.pod_real_name(name) ⇒ Object



118
119
120
121
122
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 118

def self.pod_real_name(name)
  math = %r{(.*(?=/))}.match(name)
  return name unless math
  return math[0]
end

.refs_from_ls_remote(git, branch) ⇒ Object



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

def self.refs_from_ls_remote(git, branch)
  cmds = ['git']
  cmds << "ls-remote"
  cmds << git
  cmds << branch if branch
  cmds_to_s = cmds.join(" ")
  LgPodPlugin.log_blue cmds_to_s
  begin
    return %x(timeout 5 #{cmds_to_s})
  rescue
    return %x(#{cmds_to_s})
  end
end

.sha_from_result(output, branch_name) ⇒ Object



61
62
63
64
65
66
67
68
69
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/utils/l_util.rb', line 61

def self.sha_from_result(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)
    if match1.nil?
      sha = ""
    else
      sha = match1[1]
    end
    return [nil, nil] unless sha && !sha.empty?
    match2 = %r{(#{sha})\trefs\/heads\/([a-z0-9]*)}.match(output)
    return [nil, nil] if match2.nil?
    if match2[1] == sha
      new_branch = match2[2]
      return [sha, new_branch]
    else
      return [nil, nil]
    end
  else
    match = %r{([a-z0-9]*)\trefs\/(heads|tags)\/#{Regexp.quote(encoded_branch_name)}}.match(output)
    if !match.nil?
      sha = match[1]
      branch = match[0].split("refs/heads/").last
    else
      sha = nil
      branch = nil
    end
    return [sha, branch]
  end
end

.url_encode(url) ⇒ Object



113
114
115
# File 'lib/lg_pod_plugin/utils/l_util.rb', line 113

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