Sha256: 77f6163dd2aea5f28fcbe42ee6a6f5879a85f00722b04f4c3a7341266d0da517

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'dotfile_linker/version'
require 'optparse'
require 'colorize'

class String
  def human_filename
    self.gsub(%r{^(/[^/]+){2}}, '~')
  end
end

module DotfileLinker
  BLACKLIST = %w{ .git }
  @@options = {}

  def self.parse_options
    optparse = OptionParser.new do |opts|
      opts.on('-d', '--delete', 'Delete symlinks') { @@options[:delete_mode] = true }
      opts.on_tail('-v', '--version', 'Show version') { puts VERSION; exit }
      opts.on_tail('-h', '--help', 'Show this message') { puts opts; exit }
    end
    optparse.parse!
  end

  def self.exclude_file?(filename)
    filename =~ /^\.\.?$/ or BLACKLIST.include?(filename)
  end

  def self.positive_user_response?
    case gets.strip
    when /^y/i
      true
    when /^n/i
      false
    else
      puts 'Exiting'
      exit
    end
  end

  def self.link_file(filename)
    unless exclude_file?(filename)
      symlink_file = File.expand_path("~/#{ filename }")
      actual_file = File.expand_path(filename)
      if @@options[:delete_mode]
        if File.symlink?(symlink_file)
          puts "delete symlink #{ symlink_file.human_filename.magenta }? (y/n)"
          File.delete(symlink_file) if positive_user_response?
        end
      else
        unless File.symlink?(symlink_file)
          puts "link %s -> %s? (y/n)" % [symlink_file.human_filename.magenta, actual_file.human_filename.cyan]
          File.symlink(actual_file, symlink_file) if positive_user_response?
        end
      end
    end
  end

  def self.link_files
    Dir.foreach(Dir.pwd) { |filename| link_file(filename) }
  end

  def self.start
    parse_options
    link_files
    puts 'Done'
  rescue Interrupt
    # do nothing
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dotfile_linker-0.0.2 lib/dotfile_linker.rb
dotfile_linker-0.0.1 lib/dotfile_linker.rb