Sha256: be0db59dab4e32acd12d025826f7900747e8517fd06443249a645066e0d0f891

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require_relative '../output'

module Arli
  module Actions
    # Represents an abstract action related to the library
    class Action
      include Arli::Output

      extend Forwardable
      def_delegators :@library, :exists?, :path, :dir, :libraries_home

      class << self
        def inherited(klazz)
          action_name                          = klazz.name.gsub(/.*::/, '').underscore.to_sym
          ::Arli::Actions.actions[action_name] = klazz
        end
      end

      attr_accessor :library, :config

      def initialize(library, config: Arli.config)
        self.library = library
        self.config  = config
      end

      def act(**_opts)
        raise 'Abstract method #act called on Action'
      end

      def overwrite?
        config.install.if_exists.overwrite
      end

      def backup?
        config.install.if_exists.backup
      end

      def abort?
        config.install.if_exists.abort
      end

      def mv(from, to)
        handle_preexisting_folder(to)
        FileUtils.mv(from, to)
      end

      def handle_preexisting_folder(to)
        if Dir.exist?(to)
          if abort?
            raise ::Arli::Errors::LibraryAlreadyExists, "Directory #{to} already exists"
          elsif backup?
            backup!(to)
          elsif overwrite?
            FileUtils.rm_rf(to)
          end
        end
      end

      def backup!(p)
        if Dir.exist?(p)
          backup_path = "#{p}.arli-backup-#{Time.now.strftime('%Y%m%d%H%M%S')}"
          FileUtils.mv(p, backup_path)
          if config.verbose
            ___ "\nNOTE: path #{p.blue} has been backed up to #{backup_path.bold.green}\n"
          elsif !config.quiet
            ___ ' backed up and'
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arli-0.7.0 lib/arli/actions/action.rb
arli-0.6.2 lib/arli/actions/action.rb