public
Description: elliottcable's dotfiles
Homepage: http://elliottcable.name
Clone URL: git://github.com/elliottcable/dotfiles.git
Search Repo:
dotfiles / Rakefile.rb
100644 72 lines (59 sloc) 2.182 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
desc 'Installs dotfiles from this distribution for the first time'
task :setup do
  require 'fileutils'
  
  def sudo &block
    raise "error - try running as root" unless yield
  end
  
  # Map special locations
  locations = {
    :home => File.expand_path('~'),
    :etc => '/etc',
    :bin => File.join(File.expand_path('~'), 'bin')
  }
  
  Dir['*'].each do |location|
    next unless File.directory? location
    next if ENV['DIR'] && ENV['DIR'] != location
    next unless locations.include? location.to_sym
    
    sudo { FileUtils.mkdir_p(locations[location.to_sym]) }
    
    # Damn the ghey Dir['*']'s ignoring of dotfiles!
    files = Dir.new(location).entries # Get all the files
    files = files.reject { |file| file =~ /\.\.?$/ } # Remove superfluous '.' and '..' entries
    files = files.map { |file| File.join(location, file) } # Finally, create an absolute path from our working directory
    
    puts "Linking in #{location}:"
    files.each do |from|
      next unless (File.file?(from) || File.directory?(from))
      
      parent = File.dirname(from).split('/').first
      to = File.join(locations[parent.to_sym], File.basename(from))
      
      from = File.join(FileUtils.pwd, 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... '
              sudo { FileUtils.rm toto }
            when 's'
              puts ' ! Okay, skipped '
              next
            else
              puts " ! Invalid entry, so skipping"
              next
            end
          end
        
          sudo { FileUtils.mv to, toto }
          puts "Done!"
        end
      end
      
      sudo { FileUtils.symlink(from, to) }
    end
  end
end