mac.rb 3.0 KB

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