lib/myosx/dotfiles.rb in myosx-0.2.2 vs lib/myosx/dotfiles.rb in myosx-0.2.4

- old
+ new

@@ -6,47 +6,51 @@ require 'git' require_relative 'config' class Dotfiles < Config def config - $global_config['dotfiles'] + Config.new.global['dotfiles'] end def dotfiledir - File.join($workspace, 'dotfiles') + File.join(Config.new.workspace_directory, 'dotfiles') end - def repo(repo) + def repo(repo, target, local_repo = 'dotfiles') if Git.ls_remote(repo) - unless File.exist?(dotfiledir) - puts "Cloning #{dotfile_repo}" - Git.clone(repo, 'dotfiles', :path => $workspace) + unless File.exist?(target) + puts "Cloning #{repo}" + Git.clone(repo, local_repo, :path => File.dirname(target)) else - g = Git.init(dotfiledir) + g = Git.init(target) puts "Pulling latest #{repo}" g.pull end end end def link(file, dest) - source_file = "#{dotfiledir}/#{file}" - dest = File.expand_path(dest) - raise "#{source_file} doesn't exist! Check your config or repo" unless File.exist?(source_file) + raise "#{file} doesn't exist! Check your config or repo" unless File.exist?(file) if File.exist?(dest) + backup_file = "#{dest}.#{Date.today.to_s}" + puts "Creating backup of #{dest} called: #{backup_file}" + File.rename(dest, backup_file) + end + + if File.symlink?(dest) File.delete(dest) end - puts "Linking #{source_file} to #{dest}" - File.symlink(source_file, dest) + puts "Linking #{file} to #{dest}" + File.symlink(file, dest) end def exec - if repo(config['repo']) - config['files'].each do |k, v| - link(k, v) + if repo(config['repo'], dotfiledir) + config['files'].each do |file, dest| + link("#{dotfiledir}/#{file}", dest) end end end end