core.rb 5.4 KB

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