core.rb 7.4 KB

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