Coding standards fixes based off of Rubocop and minor edits to make
logic flow easier to understand.
This commit is contained in:
@@ -7,7 +7,7 @@ require 'homesick/cli'
|
||||
|
||||
# Homesick's top-level module
|
||||
module Homesick
|
||||
GITHUB_NAME_REPO_PATTERN = /\A([A-Za-z0-9_-]+\/[A-Za-z0-9_-]+)\Z/
|
||||
GITHUB_NAME_REPO_PATTERN = %r{\A([A-Za-z0-9_-]+/[A-Za-z0-9_-]+)\Z}
|
||||
SUBDIR_FILENAME = '.homesick_subdir'
|
||||
|
||||
DEFAULT_CASTLE_NAME = 'dotfiles'
|
||||
|
||||
@@ -3,16 +3,14 @@ module Homesick
|
||||
module Actions
|
||||
# File-related helper methods for Homesick
|
||||
module FileActions
|
||||
def mv(source, destination, config = {})
|
||||
def mv(source, destination)
|
||||
source = Pathname.new(source)
|
||||
destination = Pathname.new(destination + source.basename)
|
||||
|
||||
if destination.exist?
|
||||
case
|
||||
when destination.exist? && (options[:force] || shell.file_collision(destination) { source })
|
||||
say_status :conflict, "#{destination} exists", :red
|
||||
|
||||
FileUtils.mv source, destination if (options[:force] || shell.file_collision(destination) { source }) && !options[:pretend]
|
||||
FileUtils.mv source, destination unless options[:pretend]
|
||||
else
|
||||
# this needs some sort of message here.
|
||||
FileUtils.mv source, destination unless options[:pretend]
|
||||
end
|
||||
end
|
||||
@@ -43,7 +41,7 @@ module Homesick
|
||||
FileUtils.rm_r dir
|
||||
end
|
||||
|
||||
def ln_s(source, destination, config = {})
|
||||
def ln_s(source, destination)
|
||||
source = Pathname.new(source)
|
||||
destination = Pathname.new(destination)
|
||||
FileUtils.mkdir_p destination.dirname
|
||||
|
||||
@@ -14,7 +14,7 @@ module Homesick
|
||||
def git_version_correct?
|
||||
info = `git --version`.scan(/(\d+)\.(\d+)\.(\d+)/).flatten.map(&:to_i)
|
||||
return false unless info.count == 3
|
||||
current_version = Hash[ [:major, :minor, :patch].zip(info) ]
|
||||
current_version = Hash[[:major, :minor, :patch].zip(info)]
|
||||
return true if current_version.eql?(MIN_VERSION)
|
||||
return true if current_version[:major] > MIN_VERSION[:major]
|
||||
return true if current_version[:major] == MIN_VERSION[:major] && current_version[:minor] > MIN_VERSION[:minor]
|
||||
@@ -27,7 +27,7 @@ module Homesick
|
||||
config ||= {}
|
||||
destination = config[:destination] || File.basename(repo, '.git')
|
||||
|
||||
destination = Pathname.new(destination) unless destination.kind_of?(Pathname)
|
||||
destination = Pathname.new(destination) unless destination.is_a?(Pathname)
|
||||
FileUtils.mkdir_p destination.dirname
|
||||
|
||||
if destination.directory?
|
||||
@@ -65,22 +65,22 @@ module Homesick
|
||||
end
|
||||
end
|
||||
|
||||
def git_submodule_init(config = {})
|
||||
def git_submodule_init
|
||||
say_status 'git submodule', 'init', :green
|
||||
system 'git submodule --quiet init'
|
||||
end
|
||||
|
||||
def git_submodule_update(config = {})
|
||||
def git_submodule_update
|
||||
say_status 'git submodule', 'update', :green
|
||||
system 'git submodule --quiet update --init --recursive >/dev/null 2>&1'
|
||||
end
|
||||
|
||||
def git_pull(config = {})
|
||||
def git_pull
|
||||
say_status 'git pull', '', :green
|
||||
system 'git pull --quiet'
|
||||
end
|
||||
|
||||
def git_push(config = {})
|
||||
def git_push
|
||||
say_status 'git push', '', :green
|
||||
system 'git push'
|
||||
end
|
||||
@@ -88,23 +88,23 @@ module Homesick
|
||||
def git_commit_all(config = {})
|
||||
say_status 'git commit all', '', :green
|
||||
if config[:message]
|
||||
system %Q(git commit -a -m "#{config[:message]}")
|
||||
system %(git commit -a -m "#{config[:message]}")
|
||||
else
|
||||
system 'git commit -v -a'
|
||||
end
|
||||
end
|
||||
|
||||
def git_add(file, config = {})
|
||||
def git_add(file)
|
||||
say_status 'git add file', '', :green
|
||||
system "git add '#{file}'"
|
||||
end
|
||||
|
||||
def git_status(config = {})
|
||||
def git_status
|
||||
say_status 'git status', '', :green
|
||||
system 'git status'
|
||||
end
|
||||
|
||||
def git_diff(config = {})
|
||||
def git_diff
|
||||
say_status 'git diff', '', :green
|
||||
system 'git diff'
|
||||
end
|
||||
|
||||
@@ -25,17 +25,12 @@ module Homesick
|
||||
exit(1)
|
||||
end
|
||||
# Hack in support for diffing symlinks
|
||||
self.shell = Thor::Shell::Color.new
|
||||
class << shell
|
||||
def show_diff(destination, content)
|
||||
destination = Pathname.new(destination)
|
||||
if destination.symlink?
|
||||
say "- #{destination.readlink}", :red, true
|
||||
say "+ #{content.expand_path}", :green, true
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
shell_metaclass = class << shell; self; end
|
||||
shell_metaclass.send(:define_method, :show_diff) do |destination, content|
|
||||
destination = Pathname.new(destination)
|
||||
return super unless destination.symlink?
|
||||
say "- #{destination.readlink}", :red, true
|
||||
say "+ #{content.expand_path}", :green, true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -55,7 +50,7 @@ module Homesick
|
||||
git_clone "https://github.com/#{Regexp.last_match[1]}.git",
|
||||
destination: destination
|
||||
elsif uri =~ /%r([^%r]*?)(\.git)?\Z/ || uri =~ /[^:]+:([^:]+)(\.git)?\Z/
|
||||
destination = Pathname.new(Regexp.last_match[1].gsub(/\.git$/,'')).basename
|
||||
destination = Pathname.new(Regexp.last_match[1].gsub(/\.git$/, '')).basename
|
||||
git_clone uri, destination: destination
|
||||
else
|
||||
fail "Unknown URI format: #{uri}"
|
||||
@@ -73,18 +68,12 @@ module Homesick
|
||||
inside repos_dir do
|
||||
destination = Pathname.new(name)
|
||||
homesickrc = destination.join('.homesickrc').expand_path
|
||||
if homesickrc.exist?
|
||||
proceed = options[:force] || shell.yes?("#{name} has a .homesickrc. Proceed with evaling it? (This could be destructive)")
|
||||
if proceed
|
||||
say_status 'eval', homesickrc
|
||||
inside destination do
|
||||
eval homesickrc.read, binding, homesickrc.expand_path.to_s
|
||||
end
|
||||
else
|
||||
say_status 'eval skip',
|
||||
"not evaling #{homesickrc}, #{destination} may need manual configuration",
|
||||
:blue
|
||||
end
|
||||
return unless homesickrc.exist?
|
||||
proceed = options[:force] || shell.yes?("#{name} has a .homesickrc. Proceed with evaling it? (This could be destructive)")
|
||||
return say_status 'eval skip', "not evaling #{homesickrc}, #{destination} may need manual configuration", :blue unless proceed
|
||||
say_status 'eval', homesickrc
|
||||
inside destination do
|
||||
eval homesickrc.read, binding, homesickrc.expand_path.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -252,11 +241,10 @@ module Homesick
|
||||
desc 'destroy CASTLE', 'Delete all symlinks and remove the cloned repository'
|
||||
def destroy(name)
|
||||
check_castle_existance name, 'destroy'
|
||||
return unless shell.yes?('This will destroy your castle irreversible! Are you sure?')
|
||||
|
||||
if shell.yes?('This will destroy your castle irreversible! Are you sure?')
|
||||
unlink(name)
|
||||
rm_rf repos_dir.join(name)
|
||||
end
|
||||
unlink(name)
|
||||
rm_rf repos_dir.join(name)
|
||||
end
|
||||
|
||||
desc 'cd CASTLE', 'Open a new shell in the root of the given castle'
|
||||
|
||||
@@ -35,12 +35,11 @@ module Homesick
|
||||
end
|
||||
|
||||
def check_castle_existance(name, action)
|
||||
unless castle_dir(name).exist?
|
||||
say_status :error,
|
||||
"Could not #{action} #{name}, expected #{castle_dir(name)} exist and contain dotfiles",
|
||||
:red
|
||||
exit(1)
|
||||
end
|
||||
return if castle_dir(name).exist?
|
||||
say_status :error,
|
||||
"Could not #{action} #{name}, expected #{castle_dir(name)} exist and contain dotfiles",
|
||||
:red
|
||||
exit(1)
|
||||
end
|
||||
|
||||
def all_castles
|
||||
@@ -53,7 +52,7 @@ module Homesick
|
||||
end
|
||||
end
|
||||
|
||||
def inside_each_castle(&block)
|
||||
def inside_each_castle
|
||||
all_castles.each do |git_dir|
|
||||
castle = git_dir.dirname
|
||||
Dir.chdir castle do # so we can call git config from the right contxt
|
||||
@@ -130,7 +129,6 @@ module Homesick
|
||||
def move_dir_contents(target, dir_path)
|
||||
child_files = dir_path.children
|
||||
child_files.each do |child|
|
||||
|
||||
target_path = target.join(child.basename)
|
||||
if target_path.exist?
|
||||
if more_recent?(child, target_path) && target.file?
|
||||
@@ -193,7 +191,7 @@ module Homesick
|
||||
end
|
||||
|
||||
def unsymlink_each(castle, basedir, subdirs)
|
||||
each_file(castle, basedir, subdirs) do |absolute_path, home_path|
|
||||
each_file(castle, basedir, subdirs) do |_absolute_path, home_path|
|
||||
rm_link home_path
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user