core.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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: 1; Ctrl-C exit."
  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(before_hooks = [], jobs = [], finished_hooks = [])
  73. @title = "=== Rime Deploy ====".green
  74. @before_queue = []
  75. before_hooks.each { |job| @before_queue << job.new }
  76. @queue = []
  77. jobs.each { |job| @queue << job.new }
  78. @finished_queue = []
  79. finished_hooks.each { |job| @finished_queue << job.new }
  80. @current_index = 0
  81. end
  82. def print_progress
  83. system("clear")
  84. puts @title
  85. @queue.each_with_index do |job, index|
  86. job_id = "[%02d]" % (index + 1)
  87. job_intro = job.intro.to_s.ljust(20).green
  88. job_status = job.status
  89. case job_status
  90. when :waiting
  91. job_status = job_status.to_s.white
  92. when :processing
  93. job_status = job_status.to_s.yellow
  94. when :done
  95. job_status = job_status.to_s.green
  96. end
  97. job_status = job_status.rjust(15)
  98. puts "#{job_id} #{job_intro}\t#{job_status}"
  99. end
  100. if @current_index < @queue.length
  101. puts "Total: #{@queue.length}".ljust(10)
  102. puts "Detail " + "-" * 20
  103. end
  104. end
  105. def run_job_with_info_wrapper(current_job)
  106. print_progress
  107. begin
  108. result = current_job.call
  109. if result == :next
  110. current_job.status = :done
  111. @current_index += 1
  112. else
  113. # 失败处理
  114. current_job.rollback if current_job.respond_to? :rollback
  115. raise RimeDeployError
  116. end
  117. print_progress
  118. rescue RimeDeployError
  119. what_next
  120. end
  121. end
  122. def guidance
  123. puts @title
  124. puts "welcome to use Rime installer."
  125. ChooseSession.new(
  126. [
  127. [
  128. "Auto mode: Suitable for first-time operation.",
  129. -> { run_jobs_auto }
  130. ],
  131. [
  132. "Handle mode: Decide to execute on your own.",
  133. -> { run_jobs_handle }
  134. ]
  135. ]
  136. ).call
  137. end
  138. def call
  139. @before_queue.each { |job| job.call }
  140. guidance
  141. @finished_queue.each { |job| job.call }
  142. end
  143. def run_jobs_handle
  144. halt_flag =
  145. catch :halt do
  146. system("clear")
  147. puts "[Handle Mode]".yellow
  148. handle_jobs = []
  149. @queue.each_with_index do |job, index|
  150. job_intro = job.intro.to_s.ljust(20).green
  151. handle_jobs.push(
  152. ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
  153. )
  154. end
  155. begin
  156. ChooseSession.new(handle_jobs).call
  157. rescue RimeDeployError
  158. what_next
  159. end
  160. end
  161. run_jobs_handle if halt_flag == :handle_mode
  162. end
  163. def run_jobs_auto
  164. system("clear")
  165. puts "[Auto Mode]".green
  166. print_progress
  167. halt_flag =
  168. catch :halt do
  169. while @current_index < @queue.length
  170. current_job = @queue[@current_index]
  171. current_job.status = :processing
  172. print_progress
  173. begin
  174. result = current_job.call
  175. if result == :next
  176. current_job.status = :done
  177. @current_index += 1
  178. else
  179. # 失败处理
  180. raise RimeDeployError
  181. end
  182. print_progress
  183. rescue RimeDeployError
  184. what_next
  185. end
  186. end
  187. end
  188. run_jobs_handle if halt_flag == :handle_mode
  189. end
  190. def reset_status
  191. @queue.each { |q| q.status = :waiting }
  192. end
  193. def what_next
  194. puts ""
  195. puts "Raise an error. Next, you want to...".red
  196. ChooseSession.new(
  197. [
  198. ["Retry", -> {}],
  199. ["Change to: Handle mode", -> { throw :halt, :handle_mode }],
  200. ["Exit", -> { exit 0 }]
  201. ]
  202. ).call
  203. end
  204. end
  205. end