Skip to content

Commit

Permalink
[link] refactor linking some more
Browse files Browse the repository at this point in the history
  • Loading branch information
indirect committed Aug 9, 2020
1 parent b91f045 commit bf6e1c4
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions script/link
Expand Up @@ -10,43 +10,53 @@ EXCLUDES = %w[
support
]

def link(from:, to:, prefix: "", dupes_to: "~/old_dotfiles")
srcdir = File.expand_path(*from)
destdir = File.expand_path(*to)
DOTFILES = File.expand_path("../..", __FILE__)

def link_file(from:, to:, dupes_to: "~/old_dotfiles")
from = File.expand_path(from, DOTFILES)
to = File.expand_path(to)
dupes_to = File.expand_path(dupes_to)
file = File.basename(from)

if File.symlink?(to)
print "removing link #{file}... "
FileUtils.rm(to)
elsif File.exist?(to)
print "moving #{file} to #{dupes_to}... "
FileUtils.mkdir_p(dupes_to) unless File.exist?(dupes_to)
FileUtils.mv to, File.join(dupes_to, file)
end

puts "linking #{file}"
# puts "ln -s #{from} #{to}"
`ln -s #{from} #{to}`
end

def link(from:, to:, prefix: "")
srcdir = File.expand_path(from, DOTFILES)
destdir = File.expand_path(to)

glob = File.join(srcdir, "*")
Dir[glob].each do |path|
file = File.basename(path)
next if EXCLUDES.include?(file)

target = File.join(destdir, "#{prefix}#{file}")
if File.symlink?(target)
print "removing link #{file}... "
FileUtils.rm(target)
elsif File.exist?(target)
print "moving #{file} to #{dupes_to}... "
FileUtils.mkdir_p(dupes_to) unless File.exist?(dupes_to)
FileUtils.mv target, File.join(dupes_to, file)
end

puts "linking #{file}"
`ln -s #{path} #{target}`
link_file(from: path, to: target)
end
end

link(from: ["../..", __FILE__], to: "~", prefix: ".")
link(from: ".", to: "~", prefix: ".")

puts "linking ssh/config"
`[[ -d ~/.ssh ]] || ( mkdir ~/.ssh && chmod 0700 ~/.ssh )`
`[[ -L ~/.ssh/config ]] || ln -s #{File.expand_path "ssh/config"} #{File.expand_path("~/.ssh/config")}`
link_file(from: "ssh/config", to: "~/.ssh/config")

puts "linking KeyBindings"
`[[ -d ~/Library/KeyBindings ]] && mv ~/Library/KeyBindings ~/OldKeyBindings`
`[[ -L ~/Library/KeyBindings ]] && ln -s #{File.expand_path "support/KeyBindings"} ~/Library/KeyBindings`
link_file(from: "support/KeyBindings", to: "~/Library/KeyBindings")

puts "linking Preferences"
link(from: ["../../support/Preferences", __FILE__], to: "~/Library/Preferences")
link(from: "support/Preferences", to: "~/Library/Preferences")

puts "linking oh-my-tmux config"
`[[ -L ~/.tmux.conf ]] || ln -s #{File.expand_path "support/tmux/.tmux.conf"} #{File.expand_path("~/.tmux.conf")}`
Expand Down

0 comments on commit bf6e1c4

Please sign in to comment.