Class: Dotfiles

Inherits:
Config show all
Defined in:
lib/myosx/dotfiles.rb

Instance Method Summary collapse

Methods inherited from Config

#config_file, #create_workspace, #global, #workspace_directory

Instance Method Details

#configObject



10
11
12
# File 'lib/myosx/dotfiles.rb', line 10

def config
  Config.new.global['dotfiles']
end

#dotfiledirObject



14
15
16
# File 'lib/myosx/dotfiles.rb', line 14

def dotfiledir
  File.join(Config.new.workspace_directory, 'dotfiles')
end

#execObject



49
50
51
52
53
54
55
# File 'lib/myosx/dotfiles.rb', line 49

def exec
  if repo(config['repo'], dotfiledir)
    config['files'].each do |file, dest|
      link("#{dotfiledir}/#{file}", dest)
    end
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/myosx/dotfiles.rb', line 31

def link(file, dest)
  dest = File.expand_path(dest)
  raise "#{file} doesn't exist! Check your config or repo" unless File.exist?(file)

  if File.symlink?(dest)
    File.delete(dest)
  end

  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

  puts "Linking #{file} to #{dest}"
  File.symlink(file, dest)
end

#repo(repo, target, local_repo = 'dotfiles') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/myosx/dotfiles.rb', line 18

def repo(repo, target, local_repo = 'dotfiles')
  if Git.ls_remote(repo)
    unless File.exist?(target)
      puts "Cloning #{repo}"
      Git.clone(repo, local_repo, :path => File.dirname(target))
    else
      g = Git.init(target)
      puts "Pulling latest #{repo}"
      g.pull
    end
  end
end