Class: Dotfiles

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

Instance Method Summary collapse

Instance Method Details

#configObject



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

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

#dotfiledirObject



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

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

#execObject



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

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


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

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



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

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