mac.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. class RimeDeployError < StandardError
  2. end
  3. module FontStylePatch
  4. RESET_COLOR = "\e[0m" #重置所有颜色和样式
  5. COLORS = {
  6. black: "\e[30m", #黑色文本
  7. red: "\e[31m", #红色文本
  8. green: "\e[32m", #绿色文本
  9. yellow: "\e[33m", #黄色文本
  10. blue: "\e[34m", #蓝色文本
  11. carmine: "\e[35m", #洋红色文本
  12. cyan: "\e[36m", #青色文本
  13. white: "\e[37m" #白色文本
  14. }
  15. COLORS.keys.each do |color_name|
  16. define_method(color_name) do
  17. return "#{COLORS[color_name]}#{self}#{RESET_COLOR}"
  18. end
  19. end
  20. end
  21. module StringPatch
  22. def nature_case
  23. self.gsub(/(.)([A-Z])/, '\1 \2').downcase.capitalize
  24. end
  25. end
  26. class String
  27. include FontStylePatch
  28. include StringPatch
  29. end
  30. class ChooseSession
  31. def initialize(items)
  32. @items = items #[[intro, method]] auto index
  33. end
  34. def call
  35. message = nil
  36. choose = nil
  37. while true
  38. puts @title
  39. puts "Choose mode:"
  40. @items.each_with_index { |item, index| puts "[#{index + 1}] " + item[0] }
  41. puts "Tips: input the index. e.g: 01"
  42. puts "Message: #{message}".red if message
  43. print ">>".green
  44. choose_mode = gets
  45. choose_index = choose_mode.strip
  46. if choose_index =~ /\d+/ && choose_index.to_i <= @items.length
  47. choose = @items[choose_index.to_i - 1]
  48. break
  49. else
  50. message = "Wrong Index, try again."
  51. end
  52. end
  53. choose[1].call
  54. end
  55. end
  56. class Job
  57. attr_accessor :status, :intro
  58. def initialize
  59. @status = :waiting # :waiting, :processing, :done, :fail
  60. @intro = self.class.to_s.sub(/Job$/, "").nature_case
  61. end
  62. def call
  63. end
  64. def rollback
  65. end
  66. end
  67. class InstallRimeJob < Job
  68. def call
  69. puts "Job: InstallRimeJob".blue
  70. sleep 1
  71. # system("brew install --cask squirrel")
  72. return :next
  73. end
  74. end
  75. class BackupRimeConfigJob < Job
  76. def call
  77. puts "Job: BackupRimeConfigJob".blue
  78. sleep 1
  79. # system("mv ~/Library/Rime ~/Library/Rime.#{Time.now.to_i}.old")
  80. return :next
  81. end
  82. end
  83. class CloneConfigJob < Job
  84. def call
  85. puts "Job: CloneConfigJob".blue
  86. sleep 1
  87. # system(
  88. # "git clone --depth=1 https://github.com/Mark24Code/rime-ice.git ~/Library/Rime"
  89. # )
  90. return :next
  91. end
  92. end
  93. class CopyCustomConfigJob < Job
  94. def call
  95. puts "Job: CopyCustomConfigJob".blue
  96. sleep 1
  97. # system("cp ./default.custom.yaml ~/Library/Rime/")
  98. # system("cp ./squirrel.custom.yaml ~/Library/Rime/")
  99. return :next
  100. end
  101. end
  102. class MacOSJobGroup
  103. def initialize(jobs)
  104. @title = "=== Rime Deploy ===="
  105. @queue = []
  106. jobs.each { |job| @queue << job.new }
  107. @current_index = 0
  108. @mode = :auto #:handle
  109. end
  110. def print_progress
  111. system("clear")
  112. puts @title
  113. @queue.each_with_index do |job, index|
  114. job_id = "[%02d]" % (index + 1)
  115. job_intro = job.intro.to_s.ljust(20).green
  116. job_status = job.status
  117. case job_status
  118. when :waiting
  119. job_status = job_status.to_s.white
  120. when :processing
  121. job_status = job_status.to_s.yellow
  122. when :done
  123. job_status = job_status.to_s.green
  124. end
  125. job_status = job_status.rjust(15)
  126. puts "#{job_id} #{job_intro}\t#{job_status}"
  127. end
  128. if @current_index < @queue.length
  129. puts "total: #{@queue.length}".ljust(10)
  130. puts "Detail " + "-" * 20
  131. end
  132. end
  133. def run_job_with_info_wrapper(current_job)
  134. print_progress
  135. begin
  136. result = current_job.call
  137. if result == :next
  138. current_job.status = :done
  139. @current_index += 1
  140. else
  141. # 失败处理
  142. raise RimeDeployError
  143. end
  144. print_progress
  145. rescue RimeDeployError
  146. run_jobs_handle
  147. end
  148. end
  149. def guidance
  150. ChooseSession.new(
  151. [
  152. ["Auto mode: Suitable for first-time operation.", -> { run_jobs_auto }],
  153. ["Handle mode: Decide to execute on your own.", -> { run_jobs_handle }]
  154. ]
  155. ).call
  156. end
  157. def call
  158. guidance
  159. end
  160. def run_jobs_handle
  161. handle_jobs = []
  162. @queue.each_with_index do |job, index|
  163. job_intro = job.intro.to_s.ljust(20).green
  164. handle_jobs.push(["#{job_intro}", -> { run_job_with_info_wrapper(job) }])
  165. end
  166. ChooseSession.new(handle_jobs).call
  167. end
  168. def run_jobs_auto
  169. print_progress
  170. while @current_index < @queue.length && @mode == :auto
  171. current_job = @queue[@current_index]
  172. current_job.status = :processing
  173. print_progress
  174. begin
  175. result = current_job.call
  176. if result == :next
  177. current_job.status = :done
  178. @current_index += 1
  179. else
  180. # 失败处理
  181. raise RimeDeployError
  182. end
  183. rescue RimeDeployError
  184. @mode = :handle
  185. end
  186. print_progress
  187. end
  188. end
  189. end
  190. MacOSJobGroup.new(
  191. [InstallRimeJob, BackupRimeConfigJob, CloneConfigJob, CopyCustomConfigJob]
  192. ).call