Module: YKCitool::Tools

Included in:
Helper
Defined in:
lib/ykfastlane/tools.rb

Class Method Summary collapse

Class Method Details

.clone_git_repository(remote, destinationPath) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ykfastlane/tools.rb', line 118

def self.clone_git_repository(remote, destinationPath)
  begin
    puts "start clone:#{remote}"
    cloneResult = Git::clone(remote, destinationPath, :log => Logger.new(Logger::Severity::INFO))
    puts "clone_result:#{cloneResult}"
  rescue Git::GitExecuteError => e
    puts "clone failed:#{e}"
    return 1 #任务失败
  end
  return 0
end

.display_yml(path) ⇒ Object



82
83
84
# File 'lib/ykfastlane/tools.rb', line 82

def self.display_yml(path)
  puts Tools::green(JSON.pretty_generate(load_yml(path)))
end

.git_commit(path, msg) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ykfastlane/tools.rb', line 145

def self.git_commit(path, msg)
  git = Git::open(path)
  git.add()
  cur_branch = git.current_branch
  begin
    git.commit("update:#{msg}")
  rescue Git::GitExecuteError => e
    puts "commit update exception:#{e}"
  end

  status = git.status()
  if status.untracked.count != 0 || status.changed.count != 0
    puts "git not clean, work failed"
    return 1
  else
    puts "git clean, work success"
    git.push('origin', cur_branch)
    return 0
  end
end

.git_pull(path) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ykfastlane/tools.rb', line 130

def self.git_pull(path)
  begin
    git = Git::open(path)
    git.add()
    git.reset_hard()
    curbranch = git.current_branch
    git.pull('origin', curbranch)
  rescue Git::GitExecuteError => e
    puts "pull remote failed:#{e}"
    return 1 #任务失败
  end

  return 0
end

.green(string) ⇒ Object



7
8
9
# File 'lib/ykfastlane/tools.rb', line 7

def self.green(string)
  "\033[0;32m#{string}\e[0m"
end

.load_yml(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ykfastlane/tools.rb', line 69

def self.load_yml(path)
  if File.exist?(path) == false
    FileUtils.makedirs(File.dirname(path))
    File.new(path, 'w+')
  end

  f = File.open(path)
  yml = YAML.load(f, symbolize_names: false)
  f.close
  yml = {} if yml == false #空yml的时候, yml = false
  yml
end

.load_yml_value(path, key) ⇒ Object



86
87
88
89
90
91
# File 'lib/ykfastlane/tools.rb', line 86

def self.load_yml_value(path, key)
  yml = load_yml(path)
  yml = {} if yml == false #空yml的时候, yml = false
  re = yml[key].blank? ? nil : yml[key]
  re
end

.notify_message_to_enterprise_wechat(msg, status) ⇒ Object



166
167
168
169
# File 'lib/ykfastlane/tools.rb', line 166

def self.notify_message_to_enterprise_wechat(msg, status)
  puts "should send failed message to enterprise wechat:\"#{self.green(msg)}\""
  exit!(status) unless status == 0
end

.over_write_yml_dict(path, dict) ⇒ Object



112
113
114
115
116
# File 'lib/ykfastlane/tools.rb', line 112

def self.over_write_yml_dict(path, dict)
  f = File.open(path, "w+")
  YAML.dump(dict, f, symbolize_names: false)
  f.close
end

.show_promptObject



15
16
17
# File 'lib/ykfastlane/tools.rb', line 15

def self.show_prompt
  print green(">")
end

.UI(string) ⇒ Object



11
12
13
# File 'lib/ykfastlane/tools.rb', line 11

def self.UI(string)
  puts(self.green(string))
end

.update_yml(question, path, key, value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ykfastlane/tools.rb', line 93

def self.update_yml(question, path, key, value)
  yml = load_yml(path)

  updateFlag = :yes
  if value.blank? #外部未传该参数需要通过问答形式,获取该参数
    value = Tools.yk_ask("please input #{Tools::green(question)}")

    if yml[key].blank? == false #需要确认修改
      puts "#{key} existed:#{yml[key]}"
      updateFlag = Tools.yk_ask_with_answers("Are you sure to update #{self.green(question)}", ["Yes", "No"]).to_sym
    end
  end
  yml[key] = value unless updateFlag != :yes
  c_path = Helper::YKCONFIG_PATH
  f = File.open(path, "w+")
  YAML.dump(yml, f, symbolize_names: false)
  f.close
end

.yk_ask(question) ⇒ Object



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

def self.yk_ask(question)
  answer = ""
  loop do
    puts "\n#{question}?"
    show_prompt()
    answer = STDIN.gets.chomp

    break if answer.length > 0

    print "\nYou need to provide an answer."
  end
  puts "answier:#{answer}"
  answer
end

.yk_ask_with_answers(question, possible_answers) ⇒ Object



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
# File 'lib/ykfastlane/tools.rb', line 34

def self.yk_ask_with_answers(question, possible_answers)
  print "\n#{question}? ["
  print_info = Proc.new {
    possible_answers_string = possible_answers.each_with_index do |answer, i|
      _answer = (i == 0) ? answer.underlined : answer
      print " " + _answer
      print(" /") if i != possible_answers.length - 1
    end
    print " ]\n"
  }
  print_info.call

  answer = ""
  loop do
    show_prompt()
    answer = STDIN.gets.chomp

    answer = "yes" if answer == "y"
    answer = "no" if answer == "n"

    # default to first answer
    if answer == ""
      answer = possible_answers[0].downcase
      print answer.yellow
    end

    break if possible_answers.map { |a| a.downcase }.include? answer

    print "\nPossible answers are ["
    print_info.call
  end

  answer
end