Explorar el Código

refactor: slim code

Mark24 hace 2 años
padre
commit
8e8f1b5555
Se han modificado 5 ficheros con 67 adiciones y 64 borrados
  1. 2 14
      installer.rb
  2. 0 10
      lib/config.rb
  3. 39 11
      lib/core.rb
  4. 14 16
      os/LinuxDistro.rb
  5. 12 13
      os/MacOS.rb

+ 2 - 14
installer.rb

@@ -24,7 +24,6 @@ module RimeDeploy
     def initialize
       @osname = nil
       check_os
-      dispatch
       run
     end
 
@@ -33,20 +32,9 @@ module RimeDeploy
       exit 0
     end
 
-    def dispatch
-      require "./os/#{@osname}"
-    end
-
     def run
-      os_prefix = @osname
-      code = <<-CODE
-  class #{os_prefix}JobGroup < JobGroup
-  end
-  mod = #{os_prefix}JobGroup.new(#{os_prefix}::BeforeHook, #{os_prefix}::Jobs, #{os_prefix}::FinishedHook)
-  mod.call
-
-CODE
-      instance_eval(code)
+      require "./os/#{@osname}"
+      instance_eval("#{@osname}JobGroup.new.call")
     end
   end
 end

+ 0 - 10
lib/config.rb

@@ -1,15 +1,5 @@
 module RimeDeploy
   module Config
     RIME_CONFIG_REPO = "https://github.com/iDvel/rime-ice.git"
-    class MacOS
-      InstallCmd = "brew install --cask squirrel"
-      ConfigPath = "~/Library/Rime"
-    end
-
-    class LinuxDistro
-      ConfigPathIbus = "~/.config/ibus/rime"
-      ConfigPathFcitx = "~/.config/fcitx/rime"
-      ConfigPathFcitx5 = "~/.local/share/fcitx5/rime"
-    end
   end
 end

+ 39 - 11
lib/core.rb

@@ -79,16 +79,43 @@ module RimeDeploy
   end
 
   class JobGroup
-    def initialize(before_hooks = [], jobs = [], finished_hooks = [])
+    @jobs = []
+    @after_jobs = []
+    @before_jobs = []
+
+    def self.jobs
+      @jobs
+    end
+    def self.jobs=(value)
+      @jobs = value
+    end
+
+    def self.after_jobs
+      @after_jobs
+    end
+    def self.after_jobs=(value)
+      @after_jobs = value
+    end
+
+    def self.before_jobs
+      @before_jobs
+    end
+    def self.before_jobs=(value)
+      @before_jobs = value
+    end
+
+    def initialize()
       @title = "=== Rime Deploy ====".green
-      @before_queue = []
-      before_hooks.each { |job| @before_queue << job.new }
+      @before_jobs = []
+      if self.class.before_jobs
+        self.class.before_jobs.each { |job| @before_jobs << job.new }
+      end
 
       @queue = []
-      jobs.each { |job| @queue << job.new }
+      self.class.jobs.each { |job| @queue << job.new } if self.class.jobs.each
 
-      @finished_queue = []
-      finished_hooks.each { |job| @finished_queue << job.new }
+      @after_jobs = []
+      self.class.after_jobs.each { |job| @after_jobs << job.new }
 
       @current_index = 0
     end
@@ -152,11 +179,6 @@ module RimeDeploy
         ]
       ).call
     end
-    def call
-      @before_queue.each { |job| job.call }
-      guidance
-      @finished_queue.each { |job| job.call }
-    end
 
     def run_jobs_handle
       halt_flag =
@@ -223,5 +245,11 @@ module RimeDeploy
         ]
       ).call
     end
+
+    def call
+      @before_jobs.length > 0 && @before_jobs.each { |job| job.call }
+      guidance
+      @after_jobs.length > 0 && @after_jobs.each { |job| job.call }
+    end
   end
 end

+ 14 - 16
os/LinuxDistro.rb

@@ -1,12 +1,16 @@
 module RimeDeploy
-  class LinuxDistro
-    @@config_path = nil
+  class LinuxDistroJobGroup < JobGroup
+    ConfigPathIbus = "~/.config/ibus/rime"
+    ConfigPathFcitx = "~/.config/fcitx/rime"
+    ConfigPathFcitx5 = "~/.local/share/fcitx5/rime"
+
+    @config_path = nil
     def self.config_path=(value)
-      @@config_path = value
+      @config_path = value
     end
 
     def self.config_path
-      @@config_path
+      @config_path
     end
 
     class CheckInstallRimeJob < Job
@@ -31,22 +35,19 @@ https://wiki.archlinux.org/title/Rime
             [
               "ibus-rime",
               -> do
-                LinuxDistro.config_path =
-                  ::RimeDeploy::Config::LinuxDistro::ConfigPathIbus
+                LinuxDistro.config_path = LinuxDistroJobGroup::ConfigPathIbus
               end
             ],
             [
               "fcitx-rime",
               -> do
-                LinuxDistro.config_path =
-                  ::RimeDeploy::Config::LinuxDistro::ConfigPathFcitx
+                LinuxDistro.config_path = LinuxDistroJobGroup::ConfigPathFcitx
               end
             ],
             [
               "fcitx5-rime",
               -> do
-                LinuxDistro.config_path =
-                  ::RimeDeploy::Config::LinuxDistro::ConfigPathFcitx5
+                LinuxDistro.config_path = LinuxDistroJobGroup::ConfigPathFcitx5
               end
             ],
             [
@@ -105,11 +106,8 @@ https://wiki.archlinux.org/title/Rime
         return :next
       end
     end
-
-    BeforeHook = [CheckInstallRimeJob]
-
-    Jobs = [BackupRimeConfigJob, CloneConfigJob, CopyCustomConfigJob]
-
-    FinishedHook = [FinishedJob]
+    self.before_jobs = [CheckInstallRimeJob]
+    self.jobs = [BackupRimeConfigJob, CloneConfigJob, CopyCustomConfigJob]
+    self.after_jobs = [FinishedJob]
   end
 end

+ 12 - 13
os/MacOS.rb

@@ -1,9 +1,12 @@
 module RimeDeploy
-  module MacOS
+  class MacOSJobGroup < JobGroup
+    InstallCmd = "brew install --cask squirrel"
+    ConfigPath = "~/Library/Rime"
+
     class InstallRimeJob < Job
       def call
         puts intro
-        system(Config::MacOS::InstallCmd)
+        system(InstallCmd)
         sleep 1
         return :next
       end
@@ -13,7 +16,7 @@ module RimeDeploy
       def call
         puts intro
         system(
-          "mv #{Config::MacOS::ConfigPath} #{Config::MacOS::ConfigPath}.#{Time.now.to_i}.old"
+          "mv #{MacOSJobGroup::ConfigPath} #{MacOSJobGroup::ConfigPath}.#{Time.now.to_i}.old"
         )
         sleep 1
         return :next
@@ -24,7 +27,7 @@ module RimeDeploy
       def call
         puts intro
         system(
-          "git clone --depth=1 #{Config::RIME_CONFIG_REPO} #{Config::MacOS::ConfigPath}"
+          "git clone --depth=1 #{Config::RIME_CONFIG_REPO} #{MacOSJobGroup::ConfigPath}"
         )
         sleep 1
         return :next
@@ -34,8 +37,8 @@ module RimeDeploy
     class CopyCustomConfigJob < Job
       def call
         puts intro
-        system("cp ./custom/default.custom.yaml #{Config::MacOS::ConfigPath}/")
-        system("cp ./custom/squirrel.custom.yaml #{Config::MacOS::ConfigPath}/")
+        system("cp ./custom/default.custom.yaml #{MacOSJobGroup::ConfigPath}/")
+        system("cp ./custom/squirrel.custom.yaml #{MacOSJobGroup::ConfigPath}/")
         sleep 1
         return :next
       end
@@ -50,20 +53,16 @@ module RimeDeploy
                "DEPLOY".yellow + " button."
         puts "Enjoy~ 🍻"
         puts "more info:".yellow
-        puts "Config path: #{Config::MacOS::ConfigPath}/"
+        puts "Config path: #{MacOSJobGroup::ConfigPath}/"
         return :next
       end
     end
-
-    BeforeHook = []
-
-    Jobs = [
+    self.jobs = [
       InstallRimeJob,
       BackupRimeConfigJob,
       CloneConfigJob,
       CopyCustomConfigJob
     ]
-
-    FinishedHook = [FinishedJob]
+    self.after_jobs = [FinishedJob]
   end
 end