mac.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. class String
  22. include FontStylePatch
  23. end
  24. class Job
  25. attr_accessor :status
  26. def initialize
  27. @status = :waiting # :processing, :done, :fail
  28. end
  29. def intro
  30. self.class.to_s
  31. end
  32. def call
  33. end
  34. def rollback
  35. end
  36. # def highlight(text)
  37. # puts text.green
  38. # end
  39. # def info(text)
  40. # puts text.blue
  41. # end
  42. # def status(text)
  43. # puts text.yellow
  44. # end
  45. end
  46. class InstallRimeJob < Job
  47. def call
  48. puts "Job: InstallRimeJob".blue
  49. sleep 1
  50. # system("brew install --cask squirrel")
  51. return :next
  52. end
  53. end
  54. class BackupRimeConfigJob < Job
  55. def call
  56. puts "Job: BackupRimeConfigJob".blue
  57. sleep 1
  58. # system("mv ~/Library/Rime ~/Library/Rime.#{Time.now.to_i}.old")
  59. return :next
  60. end
  61. end
  62. class CloneConfigJob < Job
  63. def call
  64. puts "Job: CloneConfigJob".blue
  65. sleep 1
  66. # system(
  67. # "git clone --depth=1 https://github.com/Mark24Code/rime-ice.git ~/Library/Rime"
  68. # )
  69. return :next
  70. end
  71. end
  72. class CopyCustomConfigJob < Job
  73. def call
  74. puts "Job: CopyCustomConfigJob".blue
  75. sleep 1
  76. # system("cp ./default.custom.yaml ~/Library/Rime/")
  77. # system("cp ./squirrel.custom.yaml ~/Library/Rime/")
  78. return :next
  79. end
  80. end
  81. class MacOSJobGroup
  82. def initialize(jobs)
  83. @queue = []
  84. jobs.each { |job| @queue << job.new }
  85. @current_index = 0
  86. @mode = :auto #:handle
  87. end
  88. def print_progress
  89. system("clear")
  90. puts "=== Rime Deploy ===="
  91. @queue.each_with_index do |job, index|
  92. job_id = "[%02d]" % (index + 1)
  93. job_intro = job.intro.to_s.ljust(20).green
  94. job_status = job.status
  95. case job_status
  96. when :waiting
  97. job_status = job_status.to_s.white
  98. when :processing
  99. job_status = job_status.to_s.yellow
  100. when :done
  101. job_status = job_status.to_s.green
  102. end
  103. job_status = job_status.rjust(15)
  104. puts "#{job_id} #{job_intro}\t#{job_status}"
  105. end
  106. if @current_index < @queue.length
  107. puts "total: #{@queue.length}".ljust(10)
  108. puts "Detail " + "-" * 20
  109. end
  110. end
  111. def call
  112. print_progress
  113. while @current_index < @queue.length
  114. current_job = @queue[@current_index]
  115. current_job.status = :processing
  116. print_progress
  117. result = current_job.call
  118. if result == :next
  119. current_job.status = :done
  120. @current_index += 1
  121. else
  122. # 失败处理
  123. end
  124. print_progress
  125. end
  126. end
  127. end
  128. MacOSJobGroup.new(
  129. [InstallRimeJob, BackupRimeConfigJob, CloneConfigJob, CopyCustomConfigJob]
  130. ).call