core.rb 6.0 KB

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