Skip to content

Commit

Permalink
move .zshrc / .bash_profile when installing and fail on failed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 1, 2014
1 parent cd57490 commit 2185b5c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions dotbash_profile
@@ -0,0 +1 @@
source ~/.bashrc
42 changes: 31 additions & 11 deletions install.rb
Expand Up @@ -17,9 +17,35 @@ def backup_and_replace(dotfile, home, backup)

puts "linking #{dotfile} -> #{original}"

prefix = move_file_to_backup(original, backup)
sh "#{prefix} ln -s #{dotfile} #{original}"
end

def move_file_to_backup(original, backup)
prefix = 'sudo' unless system("touch #{original}")
`#{prefix} mv #{original} #{backup}/#{original.gsub('/','-')} 2>&1`
`#{prefix} ln -s #{dotfile} #{original}`
if File.exist?(original)
sh "#{prefix} mv #{original} #{backup}/#{original.gsub('/', '-')} 2>&1"
end
prefix
end

def add_include_to_gitconfig(home, dotfiles)
gitconfig = "#{home}/.gitconfig"
if File.exist?(gitconfig) && !File.read(gitconfig).include?("[include]")
File.open(gitconfig, "a+") { |f| f.write "\n\n[include]\n path=#{dotfiles}/gitconfig\n" }
end
end

def create_bckup_folder(home)
backup = "#{home}/backup_dotfiles_#{Time.now.strftime('%Y-%m-%dT%H:%M:%S')}"
sh "mkdir #{backup}"
backup
end

def sh(command)
result = `#{command}`
raise "FAILED #{command}: #{result}" unless $?.success?
result
end

dotfiles = File.expand_path(File.dirname(__FILE__))
Expand All @@ -28,20 +54,14 @@ def backup_and_replace(dotfile, home, backup)
expected = "#{home}/dotfiles"
raise "dotfiles must be checked out as #{expected}" if expected != dotfiles

# create backup folder
backup = "#{home}/dotfiles_backup_#{Time.now.strftime('%Y-%m-%dT%H:%M:%S')}"
`mkdir #{backup}`
backup = create_bckup_folder(home)

# backup and replace files through links
Dir["#{dotfiles}/*", "#{dotfiles}/secret/*"].each do |dotfile|
backup_and_replace(dotfile, home, backup)
end

# gitconfig
# add include
gitconfig = "#{home}/.gitconfig"
if File.exist?(gitconfig) && !File.read(gitconfig).include?("[include]")
File.open(gitconfig, "a+") { |f| f.write "\n\n[include]\n path=#{dotfiles}/gitconfig\n" }
end
move_file_to_backup("#{home}/.zshrc", backup)
add_include_to_gitconfig(home, dotfiles)

puts "Everything done!!"

0 comments on commit 2185b5c

Please sign in to comment.