view lib/pmuxinator/config.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 Config
    class << self
      def root
        Dir.mkdir("#{ENV["HOME"]}/.pmuxinator") unless File.directory?(File.expand_path("~/.pmuxinator"))
        "#{ENV["HOME"]}/.pmuxinator"
      end

      def sample
        "#{File.dirname(__FILE__)}/assets/sample.yml"
      end

      def default
        "#{ENV["HOME"]}/.pmuxinator/default.yml"
      end

      def default?
        exists?("default")
      end

      def installed?
        Kernel.system("type tmux > /dev/null")
      end

      def version
        `tmux -V`.split(" ")[1].to_f if installed?
      end

      def default_path_option
        version && version < 1.7 ? "default-path" : "-c"
      end

      def editor?
        !ENV["EDITOR"].nil? && !ENV["EDITOR"].empty?
      end

      def shell?
        !ENV["SHELL"].nil? && !ENV["SHELL"].empty?
      end

      def exists?(name)
        File.exists?(project(name))
      end

      def project(name)
        projects = Dir.glob("#{root}/**/*.yml")
        project_file = projects.detect { |project| project =~ /^#{name}.yml$/ }
        project_file || "#{root}/#{name}.yml"
      end

      def template
        "#{File.dirname(__FILE__)}/assets/template.erb"
      end

      def wemux_template
        "#{File.dirname(__FILE__)}/assets/wemux_template.erb"
      end

      def configs
        Dir["#{Pmuxinator::Config.root}/*.yml"].sort.map do |path|
          File.basename(path, ".yml")
        end
      end

      def validate(name)
        unless Pmuxinator::Config.exists?(name)
          puts "Project #{name} doesn't exist."
          exit!
        end

        config_path = Pmuxinator::Config.project(name)

        yaml = begin
          YAML.load(File.read(config_path))
        rescue SyntaxError, StandardError
          puts "Failed to parse config file. Please check your formatting."
          exit!
        end

        project = Pmuxinator::Project.new(yaml)

        unless project.windows?
          puts "Your project file should include some windows."
          exit!
        end

        unless project.name?
          puts "Your project file didn't specify a 'project_name'"
          exit!
        end

        project
      end
    end
  end
end