lib/arli/actions/action.rb in arli-0.6.1 vs lib/arli/actions/action.rb in arli-0.6.2

- old
+ new

@@ -9,11 +9,11 @@ extend Forwardable def_delegators :@library, :exists?, :path, :dir, :libraries_home class << self def inherited(klazz) - action_name = klazz.name.gsub(/.*::/, '').underscore.to_sym + action_name = klazz.name.gsub(/.*::/, '').underscore.to_sym ::Arli::Actions.actions[action_name] = klazz end end attr_accessor :library, :config @@ -25,8 +25,48 @@ 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