Update linker string

This commit is contained in:
Anton Zadvorny 2018-06-27 16:52:45 +03:00
parent 33880507d3
commit c9e8e938b6

View File

@ -1,12 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
#/ Usage: dotlinker <command> require 'optparse'
#/
#/ list List files to link
#/ update Update dotfiles repo
#/ symlink Symlink all files
#/ unsymlink Unsymlink all files
require 'pathname' require 'pathname'
require 'fileutils' require 'fileutils'
@ -29,10 +23,6 @@ module ShellHelpers
"\033[0;#{COLOR_CODES[color] + 30}m#{str}\033[0m" "\033[0;#{COLOR_CODES[color] + 30}m#{str}\033[0m"
end end
def say_help
exec "grep ^#/<'#{__FILE__}'|cut -c4-"
end
def say(message, color = :default) def say(message, color = :default)
$stdout.print(colorify_string(message, color)) $stdout.print(colorify_string(message, color))
$stdout.flush $stdout.flush
@ -135,7 +125,7 @@ module FileHelpers
elsif destination.exist? elsif destination.exist?
say_status(:conflict, "#{destination} exists", :red) say_status(:conflict, "#{destination} exists", :red)
if collision_accepted?(destination) if options[:force] || collision_accepted?(destination)
FileUtils.rm_r(destination, force: true) FileUtils.rm_r(destination, force: true)
FileUtils.ln_s(source, destination, force: true) FileUtils.ln_s(source, destination, force: true)
end end
@ -147,25 +137,17 @@ module FileHelpers
end end
end end
module GitHelpers
extend self
def git_pull
say_status(:git, "Pulling #{`git config --get remote.origin.url`}", :green)
system 'git pull'
end
end
class Linker class Linker
include GitHelpers
include FileHelpers include FileHelpers
include ShellHelpers include ShellHelpers
VERSION = '0.1.0'.freeze VERSION = '0.2.1'.freeze
LINKDIR_FILENAME = '.linkdir'.freeze LINKDIR_FILENAME = '.linkdir'.freeze
def git_dir attr_reader :options
@git_dir ||= Pathname.new(File.dirname(__FILE__)).realpath
def initialize(options = {})
@options = options
end end
def home_dir def home_dir
@ -176,10 +158,6 @@ class Linker
@repo_dir ||= Pathname.new(File.dirname(__FILE__)).join('home').realpath @repo_dir ||= Pathname.new(File.dirname(__FILE__)).join('home').realpath
end end
def update
inside(git_dir) { git_pull }
end
def each_file def each_file
skip_dirs = [] skip_dirs = []
@ -206,7 +184,7 @@ class Linker
def symlink_all def symlink_all
each_file do |absolute_path, home_path| each_file do |absolute_path, home_path|
ln_s(absolute_path, home_path) ln_s(absolute_path, home_path, options)
end end
end end
@ -217,24 +195,37 @@ class Linker
end end
end end
COMMANDS = ['list', 'update', 'symlink', 'unsymlink'].freeze COMMANDS = ['list', 'update', 'link', 'unlink'].freeze
options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} <command> [options]"
opts.separator ''
opts.separator 'Commands:'
opts.separator 'list List files to link'
opts.separator 'link Symlink all files'
opts.separator 'unlink Unsymlink all files'
opts.separator ''
opts.separator 'Options:'
opts.on('-f', '--force', 'Force overwrite all files') { |force| options[:force] = force }
opts.on('-v', '--version', 'Show version information') { |_| ShellHelpers.say(Linker::VERSION); exit(0) }
end
opt_parser.parse!
command = ARGV.pop command = ARGV.pop
if command.nil? || !COMMANDS.include?(command) if command.nil? || !COMMANDS.include?(command)
ShellHelpers.say_help ShellHelpers.say(opt_parser.help)
else else
linker = Linker.new linker = Linker.new(options)
case command case command
when 'list' when 'list'
linker.list_all linker.list_all
when 'update' when 'link'
linker.update
when 'symlink'
linker.symlink_all linker.symlink_all
when 'unsymlink' when 'unlink'
linker.unsymlink_all linker.unsymlink_all
else
ShellHelpers.say_help
end end
end end