Sha256: 31ab9b610b92e042d844eb313a1db9f70f7e08bd8d569a42db536390b3916f2c

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Scide

  # Utility class to run scide in a script.
  class Overmind

    # Awakens the overmind.
    def initialize
      @cli = Scide::Opts.new
      @config = Scide::Config.new
    end

    # Parses command-line arguments and loads the configuration file.
    # Any error will be run through Scide.fail.
    def brood
      @cli.parse! ARGV
      @config.file = @cli.funnel[:config] if @cli.funnel.key? :config
      @config.load!
      @initialized = true
      self
    end

    # Runs GNU \Screen with the project given as argument.
    # The <tt>--dry-run</tt> option will cause scide to print the
    # resulting configuration instead of running it.
    #
    # ==== Errors
    # * <tt>not_initialized</tt> - If #brood was not called.
    # * <tt>unknown_project</tt> - If the given project is not found
    #   in the configuration file.
    # * <tt>screen_not_found</tt> - If the GNU \Screen binary is not
    #   found with <tt>which</tt>.
    def dominate

      Scide.fail :not_initialized, 'ERROR: call #brood to initialize.' unless @initialized
    
      project_key = ARGV.shift
      
      if project_key.blank?
        available_projects = @config.projects.keys.join(', ')
        Scide.fail :invalid_argument, "You must choose a project. Available projects: #{available_projects}."
      end

      unless @config.projects.key? project_key
        Scide.fail :unknown_project, "ERROR: there is no project '#{project_key}' in configuration #{@config.file}."
      end

      screen = Scide::Screen.new @config.projects[project_key], @config.screen
      screen.check_binary

      if @cli.funnel[:'dry-run']
        puts
        puts Paint['COMMAND', :bold]
        puts "   #{screen.to_command}"
        puts
        puts Paint['SCREEN CONFIGURATION', :bold]
        puts screen.to_s.gsub(/^/, '   ')
        puts
      else
        screen.run
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scide-0.0.6 lib/scide/overmind.rb