SShrike/confmacs

View on GitHub
bin/confmacs

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# Confmacs - Switch between Emacs configurations with ease.
# Copyright (c) 2015 Severen Redwood <severen.redwood@gmail.com>
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'confmacs'

require 'thor'
require 'colorize'
require 'fileutils'
require 'highline/import'

class CLI < Thor
  map %w(version -V --version) => :__print_version

  desc 'init', 'Create the  ~/.confmacs directory'
  long_desc <<-LONGDESC
    `confmacs init` will create a new ~/.confmacs directory if it does not
    exist.
  LONGDESC
  def init
    Confmacs.config_dir_check
  end

  desc 'list', 'List the Emacs configs stored under ~/.confmacs/configs/'
  long_desc <<-LONGDESC
    `confmacs list` will print a list of all the available Emacs configurations
    that are stored under the ~/.confmacs/configs/ directory.

    These different configurations can be selected with the `confmacs select`
    command.
  LONGDESC
  def list
    puts '==> Available Emacs configurations:'.colorize(:green)
    puts Confmacs.config_scan
  end

  desc 'select <config>', 'Select the active Emacs config'
  long_desc <<-LONGDESC
    `confmacs select` will change the ~/.emacs.d symlink to point towards the
    selected configuration.

    You can view a list of the available configurations (stored in
    ~/.confmacs/configs/) with the `confmacs list` command.
  LONGDESC
  def select(config)
    Confmacs.config_dir_check
    config_path = "#{Confmacs::EMACS_CONFIG_DIR}/#{config}"
    unless File.exist?("#{config_path}")
      Kernel.abort("The configuration '#{config}' does not exist!"
        .colorize(:red))
    end

    puts '~/.emacs.d will be deleted and replaced with a symlink.'
      .colorize(:red).blink
    exit unless HighLine.agree('Do you want to proceed? (Y/N)')
    if File.exist?("#{Dir.home}/.emacs.d")
      FileUtils.remove_dir("#{Dir.home}/.emacs.d")
    end
    FileUtils.symlink("#{config_path}", "#{Dir.home}/.emacs.d")
  end

  desc 'version', 'Print version info and exit'
  def __print_version
    puts "Confmacs #{Confmacs::VERSION}\n".colorize(:blue)

    puts 'Copyright (c) 2015 Severen Redwood <severen.redwood@gmail.com>'
      .colorize(:red)
    puts 'This is free software; see the source for copying conditions.'
      .colorize(:red)
  end
end

CLI.start(ARGV)