Parcourir la source

refactor: use installer

Mark24 il y a 2 ans
Parent
commit
aa794aa671
8 fichiers modifiés avec 338 ajouts et 153 suppressions
  1. 1 1
      README.md
  2. 0 0
      backup/deploy.old.rb
  3. 49 0
      installer.rb
  4. 118 105
      lib/core.rb
  5. 72 0
      lib/task_manager.rb
  6. 0 47
      mac.rb
  7. 49 0
      os/linux.rb
  8. 49 0
      os/mac.rb

+ 1 - 1
README.md

@@ -19,7 +19,7 @@ step1: 克隆到本地
 
 step2: 执行部署脚本
 
-`./deploy.rb`
+`./installer.rb`
 
 # 自动部署内容:
 

+ 0 - 0
deploy.rb → backup/deploy.old.rb


+ 49 - 0
installer.rb

@@ -0,0 +1,49 @@
+#!/usr/bin/env ruby
+
+require "./lib/core"
+module RimeDeploy
+  class Installer
+    def initialize
+      @osname = nil
+      check_os
+      dispatch
+      run
+    end
+    def check_os
+      case RUBY_PLATFORM
+      when /darwin/
+        @osname = "mac"
+      when /linux/
+        @osname = "linux"
+      when /unix/
+        @osname = "unix"
+      when /mswin|win32|mingw|cygwin/
+        @osname = "win"
+        not_support_exit
+      else
+        not_support_exit
+      end
+    end
+
+    def not_support_exit
+      puts "Not support this system. Bye~"
+      exit 0
+    end
+
+    def dispatch
+      require "./os/#{@osname}"
+    end
+
+    def run
+      os_prefix = @osname.capitalize
+      code = <<-CODE
+  class #{os_prefix}JobGroup < JobGroup
+  end
+  #{os_prefix}JobGroup.new(#{os_prefix}::Jobs).call
+CODE
+      instance_eval(code)
+    end
+  end
+end
+
+RimeDeploy::Installer.new

+ 118 - 105
lib/core.rb

@@ -1,6 +1,3 @@
-class RimeDeployError < StandardError
-end
-
 module FontStylePatch
   RESET_COLOR = "\e[0m" #重置所有颜色和样式
   COLORS = {
@@ -31,126 +28,91 @@ class String
   include StringPatch
 end
 
-class ChooseSession
-  def initialize(items)
-    @items = items #[[intro, method]] auto index
+module RimeDeploy
+  class RimeDeployError < StandardError
   end
 
-  def call
-    message = nil
-    choose = nil
-    while true
-      puts @title
-      puts "Choose mode:"
-      @items.each_with_index { |item, index| puts "[#{index + 1}] " + item[0] }
-      puts "Tips: input the index. e.g: 01"
-      puts "Message: #{message}".red if message
-      print ">>".green
-      choose_mode = gets
-      choose_index = choose_mode.strip
-      if choose_index =~ /\d+/ && choose_index.to_i <= @items.length
-        choose = @items[choose_index.to_i - 1]
-        break
-      else
-        message = "Wrong Index, try again."
-      end
+  class ChooseSession
+    def initialize(items)
+      @items = items #[[intro, method]] auto index
     end
 
-    choose[1].call
-  end
-end
-
-class Job
-  attr_accessor :status, :intro
-  def initialize
-    @status = :waiting # :waiting, :processing, :done, :fail
-    @intro = self.class.to_s.sub(/Job$/, "").nature_case
-  end
-
-  def call
-  end
+    def call
+      message = nil
+      choose = nil
+      while true
+        puts @title
+        puts "Choose mode:"
+        @items.each_with_index do |item, index|
+          puts "[#{index + 1}] " + item[0]
+        end
+        puts "Tips: input the index. e.g: 01"
+        puts "Message: #{message}".red if message
+        print ">>".green
+        choose_mode = gets
+        choose_index = choose_mode.strip
+        if choose_index =~ /\d+/ && choose_index.to_i <= @items.length
+          choose = @items[choose_index.to_i - 1]
+          break
+        else
+          message = "Wrong Index, try again."
+        end
+      end
 
-  def rollback
+      choose[1].call
+    end
   end
-end
-
-class JobGroup
-  def initialize(jobs)
-    @title = "=== Rime Deploy ===="
-    @queue = []
-    jobs.each { |job| @queue << job.new }
 
-    @current_index = 0
-  end
+  class Job
+    attr_accessor :status, :intro
+    def initialize
+      @status = :waiting # :waiting, :processing, :done, :fail
+      klass_name = self.class.to_s.split("::").last.sub(/Job$/, "")
+      @intro = klass_name.nature_case
+    end
 
-  def print_progress
-    system("clear")
-    puts @title
-    @queue.each_with_index do |job, index|
-      job_id = "[%02d]" % (index + 1)
-      job_intro = job.intro.to_s.ljust(20).green
-      job_status = job.status
-      case job_status
-      when :waiting
-        job_status = job_status.to_s.white
-      when :processing
-        job_status = job_status.to_s.yellow
-      when :done
-        job_status = job_status.to_s.green
-      end
-      job_status = job_status.rjust(15)
-      puts "#{job_id} #{job_intro}\t#{job_status}"
+    def call
     end
 
-    if @current_index < @queue.length
-      puts "total: #{@queue.length}".ljust(10)
-      puts "Detail " + "-" * 20
+    def rollback
     end
   end
 
-  def run_job_with_info_wrapper(current_job)
-    print_progress
-    begin
-      result = current_job.call
-      if result == :next
-        current_job.status = :done
-        @current_index += 1
-      else
-        # 失败处理
-        raise RimeDeployError
-      end
-      print_progress
-    rescue RimeDeployError
-      run_jobs_handle
+  class JobGroup
+    def initialize(jobs)
+      @title = "=== Rime Deploy ===="
+      @queue = []
+      jobs.each { |job| @queue << job.new }
+
+      @current_index = 0
     end
-  end
 
-  def guidance
-    ChooseSession.new(
-      [
-        ["Auto mode: Suitable for first-time operation.", -> { run_jobs_auto }],
-        ["Handle mode: Decide to execute on your own.", -> { run_jobs_handle }]
-      ]
-    ).call
-  end
-  def call
-    guidance
-  end
+    def print_progress
+      system("clear")
+      puts @title
+      @queue.each_with_index do |job, index|
+        job_id = "[%02d]" % (index + 1)
+        job_intro = job.intro.to_s.ljust(20).green
+        job_status = job.status
+        case job_status
+        when :waiting
+          job_status = job_status.to_s.white
+        when :processing
+          job_status = job_status.to_s.yellow
+        when :done
+          job_status = job_status.to_s.green
+        end
+        job_status = job_status.rjust(15)
+        puts "#{job_id} #{job_intro}\t#{job_status}"
+      end
 
-  def run_jobs_handle
-    handle_jobs = []
-    @queue.each_with_index do |job, index|
-      job_intro = job.intro.to_s.ljust(20).green
-      handle_jobs.push(["#{job_intro}", -> { run_job_with_info_wrapper(job) }])
+      if @current_index < @queue.length
+        puts "total: #{@queue.length}".ljust(10)
+        puts "Detail " + "-" * 20
+      end
     end
-    ChooseSession.new(handle_jobs).call
-  end
 
-  def run_jobs_auto
-    print_progress
-    while @current_index < @queue.length
-      current_job = @queue[@current_index]
-      current_job.status = :processing
+    def run_job_with_info_wrapper(current_job)
       print_progress
       begin
         result = current_job.call
@@ -166,5 +128,56 @@ class JobGroup
         run_jobs_handle
       end
     end
+
+    def guidance
+      ChooseSession.new(
+        [
+          [
+            "Auto mode: Suitable for first-time operation.",
+            -> { run_jobs_auto }
+          ],
+          [
+            "Handle mode: Decide to execute on your own.",
+            -> { run_jobs_handle }
+          ]
+        ]
+      ).call
+    end
+    def call
+      guidance
+    end
+
+    def run_jobs_handle
+      handle_jobs = []
+      @queue.each_with_index do |job, index|
+        job_intro = job.intro.to_s.ljust(20).green
+        handle_jobs.push(
+          ["#{job_intro}", -> { run_job_with_info_wrapper(job) }]
+        )
+      end
+      ChooseSession.new(handle_jobs).call
+    end
+
+    def run_jobs_auto
+      print_progress
+      while @current_index < @queue.length
+        current_job = @queue[@current_index]
+        current_job.status = :processing
+        print_progress
+        begin
+          result = current_job.call
+          if result == :next
+            current_job.status = :done
+            @current_index += 1
+          else
+            # 失败处理
+            raise RimeDeployError
+          end
+          print_progress
+        rescue RimeDeployError
+          run_jobs_handle
+        end
+      end
+    end
   end
 end

+ 72 - 0
lib/task_manager.rb

@@ -0,0 +1,72 @@
+module RimeDeploy
+  class TaskManager
+    def initialize
+      @osname = nil
+      @proxy = nil
+      @queue = nil
+      @current_job = nil
+      check_os
+    end
+
+    def call
+      require "./lib/os/#{@osname}.job.rb"
+      job_group_klass = Object.const_get("#{@osname.capitalize}Jobs")
+      @proxy = job_group_klass.new
+      @queue = @proxy.queue
+      while @queue.size > 0
+        @current_job = proxy.queue.pop
+        job.result = job.exe
+        dispatch_job(result)
+      end
+    end
+
+    def dispatch_job
+      if result == false
+        # 任务失败
+        puts "The job meets errors. What next do you want?"
+        puts "Enter first character : c(ontinue) /e(xit)/ r(etry) /s(kip)"
+        next_step = gets
+        next_step.strip!
+
+        case next_step.strip!
+        when "c"
+          puts "contine..."
+        when "e"
+          puts ""
+          puts "Bye~"
+          exit 0
+        when "r"
+          result = @current_job.rollback
+          if result
+            @queue.push(@current_job)
+            @current_job = nil
+          else
+            puts "#{@current_job.class.to_s} rollback failed."
+          end
+        end
+      end
+    end
+
+    private
+
+    def check_os
+      case RUBY_PLATFORM
+      when /darwin/
+        @osname = "mac"
+      when /linux/
+        @osname = "linux"
+      when /unix/
+        @osname = "unix"
+      when /mswin|win32|mingw|cygwin/
+        @osname = "win"
+        call_exit
+      else
+        call_exit
+      end
+    end
+
+    def call_exit
+      puts "We don't support this system."
+    end
+  end
+end

+ 0 - 47
mac.rb

@@ -1,47 +0,0 @@
-require "./lib/core"
-
-class MacOSJobGroup < JobGroup
-end
-
-class InstallRimeJob < Job
-  def call
-    puts "Job: InstallRimeJob".blue
-    sleep 1
-    # system("brew install --cask squirrel")
-    return :next
-  end
-end
-
-class BackupRimeConfigJob < Job
-  def call
-    puts "Job: BackupRimeConfigJob".blue
-    sleep 1
-    # system("mv ~/Library/Rime ~/Library/Rime.#{Time.now.to_i}.old")
-    return :next
-  end
-end
-
-class CloneConfigJob < Job
-  def call
-    puts "Job: CloneConfigJob".blue
-    sleep 1
-    # system(
-    #   "git clone --depth=1 https://github.com/Mark24Code/rime-ice.git ~/Library/Rime"
-    # )
-    return :next
-  end
-end
-
-class CopyCustomConfigJob < Job
-  def call
-    puts "Job: CopyCustomConfigJob".blue
-    sleep 1
-    # system("cp ./default.custom.yaml ~/Library/Rime/")
-    # system("cp ./squirrel.custom.yaml ~/Library/Rime/")
-    return :next
-  end
-end
-
-MacOSJobGroup.new(
-  [InstallRimeJob, BackupRimeConfigJob, CloneConfigJob, CopyCustomConfigJob]
-).call

+ 49 - 0
os/linux.rb

@@ -0,0 +1,49 @@
+module RimeDeploy
+  module Mac
+    class InstallRimeJob < Job
+      def call
+        puts "Job: InstallRimeJob".blue
+        sleep 1
+        # system("brew install --cask squirrel")
+        return :next
+      end
+    end
+
+    class BackupRimeConfigJob < Job
+      def call
+        puts "Job: BackupRimeConfigJob".blue
+        sleep 1
+        # system("mv ~/Library/Rime ~/Library/Rime.#{Time.now.to_i}.old")
+        return :next
+      end
+    end
+
+    class CloneConfigJob < Job
+      def call
+        puts "Job: CloneConfigJob".blue
+        sleep 1
+        # system(
+        #   "git clone --depth=1 https://github.com/Mark24Code/rime-ice.git ~/Library/Rime"
+        # )
+        return :next
+      end
+    end
+
+    class CopyCustomConfigJob < Job
+      def call
+        puts "Job: CopyCustomConfigJob".blue
+        sleep 1
+        # system("cp ./default.custom.yaml ~/Library/Rime/")
+        # system("cp ./squirrel.custom.yaml ~/Library/Rime/")
+        return :next
+      end
+    end
+
+    Jobs = [
+      InstallRimeJob,
+      BackupRimeConfigJob,
+      CloneConfigJob,
+      CopyCustomConfigJob
+    ]
+  end
+end

+ 49 - 0
os/mac.rb

@@ -0,0 +1,49 @@
+module RimeDeploy
+  module Mac
+    class InstallRimeJob < Job
+      def call
+        puts "Job: InstallRimeJob".blue
+        sleep 1
+        # system("brew install --cask squirrel")
+        return :next
+      end
+    end
+
+    class BackupRimeConfigJob < Job
+      def call
+        puts "Job: BackupRimeConfigJob".blue
+        sleep 1
+        # system("mv ~/Library/Rime ~/Library/Rime.#{Time.now.to_i}.old")
+        return :next
+      end
+    end
+
+    class CloneConfigJob < Job
+      def call
+        puts "Job: CloneConfigJob".blue
+        sleep 1
+        # system(
+        #   "git clone --depth=1 https://github.com/Mark24Code/rime-ice.git ~/Library/Rime"
+        # )
+        return :next
+      end
+    end
+
+    class CopyCustomConfigJob < Job
+      def call
+        puts "Job: CopyCustomConfigJob".blue
+        sleep 1
+        # system("cp ./default.custom.yaml ~/Library/Rime/")
+        # system("cp ./squirrel.custom.yaml ~/Library/Rime/")
+        return :next
+      end
+    end
+
+    Jobs = [
+      InstallRimeJob,
+      BackupRimeConfigJob,
+      CloneConfigJob,
+      CopyCustomConfigJob
+    ]
+  end
+end