core.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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)
  73. @title = "=== Rime Deploy ====".green
  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. current_job.rollback if current_job.respond_to? :rollback
  111. raise RimeDeployError
  112. end
  113. print_progress
  114. rescue RimeDeployError
  115. what_next
  116. end
  117. end
  118. def guidance
  119. puts @title
  120. puts "welcome to use Rime installer."
  121. ChooseSession.new(
  122. [
  123. [
  124. "Auto mode: Suitable for first-time operation.",
  125. -> { run_jobs_auto }
  126. ],
  127. [
  128. "Handle mode: Decide to execute on your own.",
  129. -> { run_jobs_handle }
  130. ]
  131. ]
  132. ).call
  133. end
  134. def call
  135. guidance
  136. end
  137. def run_jobs_handle
  138. halt_flag =
  139. catch :halt do
  140. system("clear")
  141. puts "[Handle Mode]".yellow
  142. handle_jobs = []
  143. @queue.each_with_index do |job, index|
  144. job_intro = job.intro.to_s.ljust(20).green
  145. handle_jobs.push(
  146. ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
  147. )
  148. end
  149. begin
  150. ChooseSession.new(handle_jobs).call
  151. rescue RimeDeployError
  152. what_next
  153. end
  154. end
  155. run_jobs_handle if halt_flag == :handle_mode
  156. end
  157. def run_jobs_auto
  158. system("clear")
  159. puts "[Auto Mode]".green
  160. print_progress
  161. halt_flag =
  162. catch :halt do
  163. while @current_index < @queue.length
  164. current_job = @queue[@current_index]
  165. current_job.status = :processing
  166. print_progress
  167. begin
  168. result = current_job.call
  169. if result == :next
  170. current_job.status = :done
  171. @current_index += 1
  172. else
  173. # 失败处理
  174. raise RimeDeployError
  175. end
  176. print_progress
  177. rescue RimeDeployError
  178. what_next
  179. end
  180. end
  181. end
  182. run_jobs_handle if halt_flag == :handle_mode
  183. end
  184. def reset_status
  185. @queue.each { |q| q.status = :waiting }
  186. end
  187. def what_next
  188. puts ""
  189. puts "Raise an error. Next, you want to...".red
  190. ChooseSession.new(
  191. [
  192. ["Retry", -> {}],
  193. ["Change to: Handle mode", -> { throw :halt, :handle_mode }],
  194. ["Exit", -> { exit 0 }]
  195. ]
  196. ).call
  197. end
  198. end
  199. end