Separate Actions into two new modules: FileActions and GitActions
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# -*- encoding : utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
require 'homesick/shell'
|
require 'homesick/shell'
|
||||||
require 'homesick/actions'
|
require 'homesick/file_actions'
|
||||||
|
require 'homesick/git_actions'
|
||||||
require 'homesick/version'
|
require 'homesick/version'
|
||||||
require 'homesick/utils'
|
require 'homesick/utils'
|
||||||
require 'homesick/cli'
|
require 'homesick/cli'
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ module Homesick
|
|||||||
# Homesick's command line interface
|
# Homesick's command line interface
|
||||||
class CLI < Thor
|
class CLI < Thor
|
||||||
include Thor::Actions
|
include Thor::Actions
|
||||||
include Homesick::Actions
|
include Homesick::FileActions
|
||||||
|
include Homesick::GitActions
|
||||||
include Homesick::Version
|
include Homesick::Version
|
||||||
include Homesick::Utils
|
include Homesick::Utils
|
||||||
|
|
||||||
|
|||||||
89
lib/homesick/file_actions.rb
Normal file
89
lib/homesick/file_actions.rb
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
# -*- encoding : utf-8 -*-
|
||||||
|
module Homesick
|
||||||
|
# File-related helper methods for Homesick
|
||||||
|
module FileActions
|
||||||
|
def mv(source, destination, config = {})
|
||||||
|
source = Pathname.new(source)
|
||||||
|
destination = Pathname.new(destination + source.basename)
|
||||||
|
|
||||||
|
if destination.exist?
|
||||||
|
say_status :conflict, "#{destination} exists", :red unless options[:quiet]
|
||||||
|
|
||||||
|
FileUtils.mv source, destination if (options[:force] || shell.file_collision(destination) { source }) && !options[:pretend]
|
||||||
|
else
|
||||||
|
# this needs some sort of message here.
|
||||||
|
FileUtils.mv source, destination unless options[:pretend]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def rm_rf(dir)
|
||||||
|
say_status "rm -rf #{dir}", '', :green unless options[:quiet]
|
||||||
|
FileUtils.rm_r dir, force: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def rm_link(target)
|
||||||
|
target = Pathname.new(target)
|
||||||
|
|
||||||
|
if target.symlink?
|
||||||
|
say_status :unlink, "#{target.expand_path}", :green unless options[:quiet]
|
||||||
|
FileUtils.rm_rf target
|
||||||
|
else
|
||||||
|
say_status :conflict, "#{target} is not a symlink", :red unless options[:quiet]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def rm(file)
|
||||||
|
say_status "rm #{file}", '', :green unless options[:quiet]
|
||||||
|
FileUtils.rm file, force: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def rm_r(dir)
|
||||||
|
say_status "rm -r #{dir}", '', :green unless options[:quiet]
|
||||||
|
FileUtils.rm_r dir
|
||||||
|
end
|
||||||
|
|
||||||
|
def ln_s(source, destination, config = {})
|
||||||
|
source = Pathname.new(source)
|
||||||
|
destination = Pathname.new(destination)
|
||||||
|
FileUtils.mkdir_p destination.dirname
|
||||||
|
|
||||||
|
action = if destination.symlink? && destination.readlink == source
|
||||||
|
:identical
|
||||||
|
elsif destination.symlink?
|
||||||
|
:symlink_conflict
|
||||||
|
elsif destination.exist?
|
||||||
|
:conflict
|
||||||
|
else
|
||||||
|
:success
|
||||||
|
end
|
||||||
|
|
||||||
|
handle_symlink_action action, source, destination
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_symlink_action(action, source, destination)
|
||||||
|
case action
|
||||||
|
when :identical
|
||||||
|
say_status :identical, destination.expand_path, :blue unless options[:quiet]
|
||||||
|
when :symlink_conflict
|
||||||
|
say_status :conflict,
|
||||||
|
"#{destination} exists and points to #{destination.readlink}",
|
||||||
|
:red unless options[:quiet]
|
||||||
|
|
||||||
|
FileUtils.rm destination
|
||||||
|
FileUtils.ln_s source, destination, force: true unless options[:pretend]
|
||||||
|
when :conflict
|
||||||
|
say_status :conflict, "#{destination} exists", :red unless options[:quiet]
|
||||||
|
|
||||||
|
if collision_accepted?
|
||||||
|
FileUtils.rm_r destination, force: true unless options[:pretend]
|
||||||
|
FileUtils.ln_s source, destination, force: true unless options[:pretend]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
say_status :symlink,
|
||||||
|
"#{source.expand_path} to #{destination.expand_path}",
|
||||||
|
:green unless options[:quiet]
|
||||||
|
FileUtils.ln_s source, destination unless options[:pretend]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# -*- encoding : utf-8 -*-
|
# -*- encoding : utf-8 -*-
|
||||||
module Homesick
|
module Homesick
|
||||||
# Git-related and file-related helper methods for Homesick
|
# Git-related helper methods for Homesick
|
||||||
module Actions
|
module GitActions
|
||||||
# TODO: move this to be more like thor's template, empty_directory, etc
|
# TODO: move this to be more like thor's template, empty_directory, etc
|
||||||
def git_clone(repo, config = {})
|
def git_clone(repo, config = {})
|
||||||
config ||= {}
|
config ||= {}
|
||||||
@@ -88,89 +88,5 @@ module Homesick
|
|||||||
say_status 'git diff', '', :green unless options[:quiet]
|
say_status 'git diff', '', :green unless options[:quiet]
|
||||||
system 'git diff' unless options[:pretend]
|
system 'git diff' unless options[:pretend]
|
||||||
end
|
end
|
||||||
|
|
||||||
def mv(source, destination, config = {})
|
|
||||||
source = Pathname.new(source)
|
|
||||||
destination = Pathname.new(destination + source.basename)
|
|
||||||
|
|
||||||
if destination.exist?
|
|
||||||
say_status :conflict, "#{destination} exists", :red unless options[:quiet]
|
|
||||||
|
|
||||||
FileUtils.mv source, destination if (options[:force] || shell.file_collision(destination) { source }) && !options[:pretend]
|
|
||||||
else
|
|
||||||
# this needs some sort of message here.
|
|
||||||
FileUtils.mv source, destination unless options[:pretend]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def rm_rf(dir)
|
|
||||||
say_status "rm -rf #{dir}", '', :green unless options[:quiet]
|
|
||||||
FileUtils.rm_r dir, force: true
|
|
||||||
end
|
|
||||||
|
|
||||||
def rm_link(target)
|
|
||||||
target = Pathname.new(target)
|
|
||||||
|
|
||||||
if target.symlink?
|
|
||||||
say_status :unlink, "#{target.expand_path}", :green unless options[:quiet]
|
|
||||||
FileUtils.rm_rf target
|
|
||||||
else
|
|
||||||
say_status :conflict, "#{target} is not a symlink", :red unless options[:quiet]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def rm(file)
|
|
||||||
say_status "rm #{file}", '', :green unless options[:quiet]
|
|
||||||
FileUtils.rm file, force: true
|
|
||||||
end
|
|
||||||
|
|
||||||
def rm_r(dir)
|
|
||||||
say_status "rm -r #{dir}", '', :green unless options[:quiet]
|
|
||||||
FileUtils.rm_r dir
|
|
||||||
end
|
|
||||||
|
|
||||||
def ln_s(source, destination, config = {})
|
|
||||||
source = Pathname.new(source)
|
|
||||||
destination = Pathname.new(destination)
|
|
||||||
FileUtils.mkdir_p destination.dirname
|
|
||||||
|
|
||||||
action = if destination.symlink? && destination.readlink == source
|
|
||||||
:identical
|
|
||||||
elsif destination.symlink?
|
|
||||||
:symlink_conflict
|
|
||||||
elsif destination.exist?
|
|
||||||
:conflict
|
|
||||||
else
|
|
||||||
:success
|
|
||||||
end
|
|
||||||
|
|
||||||
handle_symlink_action action, source, destination
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_symlink_action(action, source, destination)
|
|
||||||
case action
|
|
||||||
when :identical
|
|
||||||
say_status :identical, destination.expand_path, :blue unless options[:quiet]
|
|
||||||
when :symlink_conflict
|
|
||||||
say_status :conflict,
|
|
||||||
"#{destination} exists and points to #{destination.readlink}",
|
|
||||||
:red unless options[:quiet]
|
|
||||||
|
|
||||||
FileUtils.rm destination
|
|
||||||
FileUtils.ln_s source, destination, force: true unless options[:pretend]
|
|
||||||
when :conflict
|
|
||||||
say_status :conflict, "#{destination} exists", :red unless options[:quiet]
|
|
||||||
|
|
||||||
if collision_accepted?
|
|
||||||
FileUtils.rm_r destination, force: true unless options[:pretend]
|
|
||||||
FileUtils.ln_s source, destination, force: true unless options[:pretend]
|
|
||||||
end
|
|
||||||
else
|
|
||||||
say_status :symlink,
|
|
||||||
"#{source.expand_path} to #{destination.expand_path}",
|
|
||||||
:green unless options[:quiet]
|
|
||||||
FileUtils.ln_s source, destination unless options[:pretend]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user