view lib/pmuxinator/cli.rb @ 2:67a6071afec7

Rename tmuxinator -> pmuxinator $ zmv **/*tmuxinator* **/*pmuxinator* $ gsed -e 's/tmuxinator/pmuxinator/g' -i **/*.* $ gsed -e 's/Tmuxinator/Pmuxinator/g' -i **/*.*
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Mon, 21 Jul 2014 08:32:00 +0900
parents
children 73ee80dc9415
line wrap: on
line source

module Pmuxinator
  class Cli < Thor
    include Pmuxinator::Util

    attr_reader :command_list

    def initialize(*args)
      super
      @command_list = %w(commands copy debug delete doctor help implode list start version)
    end

    package_name "pmuxinator" unless Gem::Version.create(Thor::VERSION) < Gem::Version.create("0.18")

    desc "commands", "Lists commands available in pmuxinator"

    def commands
      puts command_list.join("\n")
    end

    desc "completions [arg1 arg2]", "Used for shell completion"

    def completions(arg)
      if %w(start open copy delete).include?(arg)
        configs = Pmuxinator::Config.configs
        puts configs
      end
    end

    desc "new [PROJECT]", "Create a new project file and open it in your editor"
    map "open" => :new
    map "o" => :new
    map "n" => :new

    def new(name)
      config = Pmuxinator::Config.project(name)

      unless Pmuxinator::Config.exists?(name)
        template = Pmuxinator::Config.default? ? Pmuxinator::Config.default : Pmuxinator::Config.sample
        erb  = Erubis::Eruby.new(File.read(template)).result(binding)
        File.open(config, "w") { |f| f.write(erb) }
      end

      Kernel.system("$EDITOR #{config}") || doctor
    end

    desc "start [PROJECT]", "Start a tmux session using a project's pmuxinator config"
    map "s" => :start

    def start(name)
      project = Pmuxinator::Config.validate(name)

      if project.deprecations.any?
        project.deprecations.each { |deprecation| say deprecation, :red }
        puts
        print "Press ENTER to continue."
        STDIN.getc
      end

      Kernel.exec(project.render)
    end

    desc "debug [PROJECT]", "Output the shell commands that are generated by pmuxinator"

    def debug(name)
      project = Pmuxinator::Config.validate(name)
      puts project.render
    end

    desc "copy [EXISTING] [NEW]", "Copy an existing project to a new project and open it in your editor"
    map "c" => :copy
    map "cp" => :copy

    def copy(existing, new)
      existing_config_path = Pmuxinator::Config.project(existing)
      new_config_path = Pmuxinator::Config.project(new)

      exit!("Project #{existing} doesn't exist!") unless Pmuxinator::Config.exists?(existing)

      if Pmuxinator::Config.exists?(new)
        if yes?("#{new} already exists, would you like to overwrite it?", :red)
          FileUtils.rm(new_config_path)
          say "Overwriting #{new}"
        end
      end

      FileUtils.copy_file(existing_config_path, new_config_path)
      Kernel.system("$EDITOR #{new_config_path}")
    end

    desc "delete [PROJECT]", "Deletes given project"
    map "d" => :delete
    map "rm" => :delete

    def delete(project)
      if Pmuxinator::Config.exists?(project)
        config =  "#{Pmuxinator::Config.root}/#{project}.yml"

        if yes?("Are you sure you want to delete #{project}?(y/n)", :red)
          FileUtils.rm(config)
          say "Deleted #{project}"
        end
      else
        exit! "That file doesn't exist."
      end
    end

    desc "implode", "Deletes all pmuxinator projects"
    map "i" => :implode

    def implode
      if yes?("Are you sure you want to delete all pmuxinator configs?", :red)
        FileUtils.remove_dir(Pmuxinator::Config.root)
        say "Deleted all pmuxinator projects."
      end
    end

    desc "list", "Lists all pmuxinator projects"
    map "l" => :list
    map "ls" => :list

    def list
      say "pmuxinator projects:"

      print_in_columns Pmuxinator::Config.configs
    end

    desc "version", "Display installed pmuxinator version"
    map "-v" => :version

    def version
      say "pmuxinator #{Pmuxinator::VERSION}"
    end

    desc "doctor", "Look for problems in your configuration"

    def doctor
      say "Checking if tmux is installed ==> "
      yes_no Pmuxinator::Config.installed?

      say "Checking if $EDITOR is set ==> "
      yes_no Pmuxinator::Config.editor?

      say "Checking if $SHELL is set ==> "
      yes_no  Pmuxinator::Config.shell?
    end
  end
end