elliottcable / dotfiles

elliottcable's dotfiles

commit  c8669ad2204fb7774579daa3a25082045a3e11a1
tree    a825012fcf682cf4a0035cf5441fd138bb9f9ad6
parent  a8031d7609c665724183f9714e57da9d960081eb
dotfiles / Rakefile.rb
100644 53 lines (42 sloc) 1.426 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'fileutils'
 
task :default => :setup
 
desc 'Installs dotfiles from this distribution for the first time'
task :setup do
    
  files = Dir['*'] # Get all the files
  files = files.reject {|f| f =~ /^(Rakefile|README)/i}
  files = files.map { |file| File.join( File.dirname(File.expand_path(__FILE__)), file ) } # Finally, create an absolute path from our working directory
  
  puts "Linking in $HOME/"
  files.each do |from|
    next unless (File.file?(from) || File.directory?(from))
    
    to = File.join(File.expand_path('~/'), '.' + File.basename(from))
    
    puts " - " + [from, to].join(' -> ')
    if File.exists?(to)
      print " ! Target exists... "
      if File.symlink? to
        FileUtils.rm to
        puts "as a symlink, removed"
      else
        print "as a normal file/directory, moving to #{File.basename(to)}~... "
        toto = to + '~'
      
        if File.exist? toto
          print "already exists! r)emove, or s)kip? "
          order = STDIN.gets.chomp
        
          case order
          when 'r'
            print ' ! Removing... '
            FileUtils.rm toto
          when 's'
            puts ' ! Okay, skipped '
            next
          else
            puts " ! Invalid entry, so skipping"
            next
          end
        end
      
        FileUtils.mv to, toto
        puts "Done!"
      end
    end
    
    FileUtils.symlink(from, to)
  end
end