core.rb 3.8 KB

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