core.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 self.upgrade_jobs
  94. @upgrade_jobs
  95. end
  96. def self.upgrade_jobs=(value)
  97. @upgrade_jobs = value
  98. end
  99. def add_jobs_to_queue(jobs = nil)
  100. jobs.each { |job| @queue << job.new } if jobs
  101. end
  102. def add_before_jobs_to_queue(jobs = nil)
  103. jobs.each { |job| @before_jobs << job.new } if jobs
  104. end
  105. def add_after_jobs_to_queue(jobs = nil)
  106. jobs.each { |job| @after_jobs << job.new }
  107. end
  108. def initialize()
  109. @title = "=== Rime Deploy ====".green
  110. @before_jobs = []
  111. @queue = []
  112. @after_jobs = []
  113. @current_index = 0
  114. add_before_jobs_to_queue(self.class.before_jobs)
  115. add_after_jobs_to_queue(self.class.after_jobs)
  116. end
  117. def clear_screen
  118. system("clear")
  119. end
  120. def print_progress
  121. clear_screen
  122. puts @title
  123. @queue.each_with_index do |job, index|
  124. job_id = "[%02d]" % (index + 1)
  125. job_intro = job.intro.to_s.ljust(20).green
  126. job_status = job.status
  127. case job_status
  128. when :waiting
  129. job_status = job_status.to_s.white
  130. when :processing
  131. job_status = job_status.to_s.yellow
  132. when :done
  133. job_status = job_status.to_s.green
  134. end
  135. job_status = job_status.rjust(15)
  136. puts "#{job_id} #{job_intro}\t#{job_status}"
  137. end
  138. if @current_index < @queue.length
  139. puts "Total: #{@queue.length}".ljust(10)
  140. puts "Detail " + "-" * 20
  141. end
  142. end
  143. def run_job_with_info_wrapper(current_job)
  144. print_progress
  145. begin
  146. result = current_job.call
  147. if result == :next
  148. current_job.status = :done
  149. @current_index += 1
  150. else
  151. # 失败处理
  152. current_job.rollback if current_job.respond_to? :rollback
  153. raise RimeDeployError
  154. end
  155. print_progress
  156. rescue RimeDeployError
  157. what_next
  158. end
  159. end
  160. def guidance
  161. puts @title
  162. puts "welcome to use Rime installer."
  163. ChooseSession.new(
  164. [
  165. [
  166. "Auto mode: Suitable for first-time operation.".green,
  167. -> { run_jobs_auto }
  168. ],
  169. [
  170. "Handle mode: Decide to execute on your own.".green,
  171. -> { run_jobs_handle }
  172. ],
  173. [
  174. "Ugrade mode: Suitable for upgrade exist Rime".green,
  175. -> { run_jobs_upgrade }
  176. ]
  177. ]
  178. ).call
  179. end
  180. def run_jobs_handle
  181. add_jobs_to_queue(self.class.jobs)
  182. halt_flag =
  183. catch :halt do
  184. clear_screen
  185. puts "[Handle Mode]".yellow
  186. handle_jobs = []
  187. @queue.each_with_index do |job, index|
  188. job_intro = job.intro.to_s.ljust(20).green
  189. handle_jobs.push(
  190. ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
  191. )
  192. end
  193. begin
  194. ChooseSession.new(handle_jobs).call
  195. rescue RimeDeployError
  196. what_next
  197. end
  198. end
  199. reset_queue
  200. run_jobs_handle if halt_flag == :handle_mode
  201. end
  202. def run_jobs_auto
  203. add_jobs_to_queue(self.class.jobs)
  204. clear_screen
  205. puts "[Auto Mode]".green
  206. print_progress
  207. halt_flag =
  208. catch :halt do
  209. while @current_index < @queue.length
  210. current_job = @queue[@current_index]
  211. current_job.status = :processing
  212. print_progress
  213. begin
  214. result = current_job.call
  215. if result == :next
  216. current_job.status = :done
  217. @current_index += 1
  218. else
  219. # 失败处理
  220. raise RimeDeployError
  221. end
  222. print_progress
  223. rescue RimeDeployError
  224. what_next
  225. end
  226. end
  227. end
  228. reset_queue
  229. run_jobs_handle if halt_flag == :handle_mode
  230. end
  231. def run_jobs_upgrade
  232. add_jobs_to_queue(self.class.upgrade_jobs)
  233. halt_flag =
  234. catch :halt do
  235. clear_screen
  236. puts "[Upgrade Mode]".yellow
  237. upgrade_jobs = []
  238. @queue.each_with_index do |job, index|
  239. job_intro = job.intro.to_s.ljust(20).green
  240. upgrade_jobs.push(
  241. ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
  242. )
  243. end
  244. begin
  245. ChooseSession.new(upgrade_jobs).call
  246. rescue RimeDeployError
  247. what_next
  248. end
  249. end
  250. reset_queue
  251. run_jobs_upgrade if halt_flag == :run_jobs_upgrade
  252. end
  253. def reset_queue
  254. @queue = []
  255. end
  256. def reset_status
  257. @queue.each { |q| q.status = :waiting }
  258. end
  259. def what_next
  260. puts ""
  261. puts "Raise an error. Next, you want to...".red
  262. ChooseSession.new(
  263. [
  264. ["Retry", -> {}],
  265. ["Change to: Handle mode", -> { throw :halt, :handle_mode }],
  266. ["Exit", -> { exit 0 }]
  267. ]
  268. ).call
  269. end
  270. def call
  271. @before_jobs.length > 0 && @before_jobs.each { |job| job.call }
  272. guidance
  273. @after_jobs.length > 0 && @after_jobs.each { |job| job.call }
  274. end
  275. end
  276. end