mac.rb 3.0 KB

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