core.rb 6.0 KB

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