core.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. module FontStylePatch
  2. RESET_COLOR = "\e[0m" #重置所有颜色和样式
  3. COLORS = {
  4. black: "\e[30m", #黑色文本
  5. red: "\e[31m", #红色文本
  6. green: "\e[32m", #绿色文本
  7. yellow: "\e[33m", #黄色文本
  8. blue: "\e[34m", #蓝色文本
  9. carmine: "\e[35m", #洋红色文本
  10. cyan: "\e[36m", #青色文本
  11. white: "\e[37m" #白色文本
  12. }
  13. COLORS.keys.each do |color_name|
  14. define_method(color_name) do
  15. return "#{COLORS[color_name]}#{self}#{RESET_COLOR}"
  16. end
  17. end
  18. end
  19. module StringPatch
  20. def nature_case
  21. self.gsub(/(.)([A-Z])/, '\1 \2').downcase.capitalize
  22. end
  23. end
  24. class String
  25. include FontStylePatch
  26. include StringPatch
  27. end
  28. module RimeDeploy
  29. class RimeDeployError < StandardError
  30. end
  31. class ChooseSession
  32. def initialize(items)
  33. @items = items #[[intro, method]] auto index
  34. end
  35. def call
  36. message = nil
  37. choose = nil
  38. while true
  39. puts @title
  40. puts "Choose mode:"
  41. @items.each_with_index do |item, index|
  42. puts "[#{index + 1}] " + item[0]
  43. end
  44. puts "Tips: input the index. e.g: 01"
  45. puts "Message: #{message}".red if message
  46. print ">>".green
  47. choose_mode = gets
  48. choose_index = choose_mode.strip
  49. if choose_index =~ /\d+/ && choose_index.to_i <= @items.length
  50. choose = @items[choose_index.to_i - 1]
  51. break
  52. else
  53. message = "Wrong Index, try again."
  54. end
  55. end
  56. choose[1].call
  57. end
  58. end
  59. class Job
  60. attr_accessor :status, :intro
  61. def initialize
  62. @status = :waiting # :waiting, :processing, :done, :fail
  63. klass_name = self.class.to_s.split("::").last.sub(/Job$/, "")
  64. @intro = klass_name.nature_case
  65. end
  66. def call
  67. end
  68. def rollback
  69. end
  70. end
  71. class JobGroup
  72. def initialize(jobs)
  73. @title = "=== Rime Deploy ===="
  74. @queue = []
  75. jobs.each { |job| @queue << job.new }
  76. @current_index = 0
  77. end
  78. def print_progress
  79. system("clear")
  80. puts @title
  81. @queue.each_with_index do |job, index|
  82. job_id = "[%02d]" % (index + 1)
  83. job_intro = job.intro.to_s.ljust(20).green
  84. job_status = job.status
  85. case job_status
  86. when :waiting
  87. job_status = job_status.to_s.white
  88. when :processing
  89. job_status = job_status.to_s.yellow
  90. when :done
  91. job_status = job_status.to_s.green
  92. end
  93. job_status = job_status.rjust(15)
  94. puts "#{job_id} #{job_intro}\t#{job_status}"
  95. end
  96. if @current_index < @queue.length
  97. puts "total: #{@queue.length}".ljust(10)
  98. puts "Detail " + "-" * 20
  99. end
  100. end
  101. def run_job_with_info_wrapper(current_job)
  102. print_progress
  103. begin
  104. result = current_job.call
  105. if result == :next
  106. current_job.status = :done
  107. @current_index += 1
  108. else
  109. # 失败处理
  110. raise RimeDeployError
  111. end
  112. print_progress
  113. rescue RimeDeployError
  114. run_jobs_handle
  115. end
  116. end
  117. def guidance
  118. ChooseSession.new(
  119. [
  120. [
  121. "Auto mode: Suitable for first-time operation.",
  122. -> { run_jobs_auto }
  123. ],
  124. [
  125. "Handle mode: Decide to execute on your own.",
  126. -> { run_jobs_handle }
  127. ]
  128. ]
  129. ).call
  130. end
  131. def call
  132. guidance
  133. end
  134. def run_jobs_handle
  135. handle_jobs = []
  136. @queue.each_with_index do |job, index|
  137. job_intro = job.intro.to_s.ljust(20).green
  138. handle_jobs.push(
  139. ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
  140. )
  141. end
  142. ChooseSession.new(handle_jobs).call
  143. end
  144. def run_jobs_auto
  145. print_progress
  146. while @current_index < @queue.length
  147. current_job = @queue[@current_index]
  148. current_job.status = :processing
  149. print_progress
  150. begin
  151. result = current_job.call
  152. if result == :next
  153. current_job.status = :done
  154. @current_index += 1
  155. else
  156. # 失败处理
  157. raise RimeDeployError
  158. end
  159. print_progress
  160. rescue RimeDeployError
  161. run_jobs_handle
  162. end
  163. end
  164. end
  165. end
  166. end