class DropboxDotfiles
Public Class Methods
configuration(&block)
click to toggle source
# File bin/dropbox-dotfiles, line 163 def DropboxDotfiles.configuration(&block) dropbox = File.expand_path File.join $my_dropbox_directory, 'Dotfiles' home = File.expand_path '~' dp = DropboxDotfiles.new(dropbox, home, Stats.new) dp.instance_eval &block dp.report end
new(dropbox, home, stats)
click to toggle source
# File bin/dropbox-dotfiles, line 153 def initialize(dropbox, home, stats) @stats = stats create_dir_if_doesnt_exists dropbox @dropbox = dropbox create_dir_if_doesnt_exists home @home = home end
Public Instance Methods
cd(dir, &block)
click to toggle source
# File bin/dropbox-dotfiles, line 186 def cd(dir, &block) dropbox = File.join @dropbox, dir home = File.join @home, dir DropboxDotfiles.new(dropbox, home, @stats).instance_eval &block end
dir(name)
click to toggle source
# File bin/dropbox-dotfiles, line 192 def dir(name) create_link name, :directory? end
file(name)
click to toggle source
# File bin/dropbox-dotfiles, line 182 def file(name) create_link name, :file? end
report()
click to toggle source
# File bin/dropbox-dotfiles, line 172 def report if @stats.correct_links == 0 and @stats.links_created == 0 and @stats.errors == 0 puts 'Your configuration file is empty!' else puts "#{@stats.correct_links} link were already present and correct" puts "#{@stats.links_created} new links were created" if @stats.links_created > 0 puts "#{@stats.errors} errors" if @stats.errors > 0 end end
Private Instance Methods
correct_link(dropbox, home, is)
click to toggle source
# File bin/dropbox-dotfiles, line 280 def correct_link(dropbox, home, is) if File.exists? dropbox and File.send is, dropbox puts 'Do you want to create a backup of the link and then correct it? (y/N): ' response = STDIN.gets.chomp.downcase if response == 'y' backup = File.join home, '.old' File.move home, backup File.symlink dropbox, home else puts 'link not corrected' @stats.error! end else @stats.error! end end
create_dir_if_doesnt_exists(dir)
click to toggle source
# File bin/dropbox-dotfiles, line 197 def create_dir_if_doesnt_exists(dir) unless File.directory? dir if File.exists? dir error "'#{dir}' is not a directory!" exit! else begin Dir.mkdir dir rescue Exception => e error "#{e.message}" exit! end end end end
create_link(name, is)
click to toggle source
# File bin/dropbox-dotfiles, line 213 def create_link(name, is) dropbox = File.join @dropbox, name home = File.join @home, name type = is.to_s.delete '?' if File.symlink? home link = File.readlink home if File.send is, home if dropbox == link @stats.correct! else error "'#{home}' points to '#{link}' and not '#{dropbox}'" correct_link dropbox, home, is end else error "'#{home}' points to '#{dropbox}' which is not a #{type}" correct_link dropbox, home, is end else if File.exists? home if File.send is, home if File.exists? dropbox print "Dropbox already contains '#{home}'" print "Do you want to create a backup of '#{home}' and then create the link? (y/N): " response = STDIN.gets.chomp.downcase if response == 'y' backup = home + '.old' FileUtils.move home, backup File.symlink dropbox, home else puts "'#{home}' not touched. remember to correct the conflict manually!" @stats.error! end else print "Should I move '#{home}' inside Dropbox and create a link? (y/N): " response = STDIN.gets.chomp.downcase if response == 'y' FileUtils.move home, dropbox File.symlink dropbox, home puts "link '#{home}' created" @stats.created! else puts "'#{home}' ignored" end end else error "'#{home}' is not a #{type}" @stats.error! end else if File.exists? dropbox if File.send is, dropbox File.symlink dropbox, home puts "link '#{home}' created" @stats.created! else error "'#{dropbox}' is not a #{type}" @stats.error! end else error "'#{dropbox}' doesnt exists!" @stats.error! end end end end