class Warg::Runner

Public Class Methods

new(argv) click to toggle source
# File lib/warg.rb, line 1447
def initialize(argv)
  @argv = argv.dup
  @path = nil

  find_warg_directory!
  load_config!

  @context = Context.new(@argv)
  @context.copy(Warg.config)

  load_scripts!
  load_commands!

  @command = Command.find(@argv)
end

Public Instance Methods

run() click to toggle source
# File lib/warg.rb, line 1463
def run
  if @command.nil?
    $stderr.puts "Could not find command from #{@argv.inspect}"
    exit 1
  end

  @command.(@context)
  @context.parse_options!

  Warg.console.redirecting_stdout_and_stderr do
    @context.run!
  end
end

Private Instance Methods

find_warg_directory!() click to toggle source
# File lib/warg.rb, line 1479
def find_warg_directory!
  previous_directory = nil
  current_directory = Pathname.new(Dir.pwd)

  while @path.nil? && current_directory.directory? && current_directory != previous_directory
    target = current_directory.join("warg")

    if target.directory?
      @path = target

      Warg.search_paths.unshift(@path)
      Warg.search_paths.uniq!
    else
      previous_directory = current_directory
      current_directory = current_directory.parent
    end
  end

  if @path.nil?
    $stderr.puts "`warg' directory not found in current directory or ancestors"
    exit 1
  end
end
load_commands!() click to toggle source
# File lib/warg.rb, line 1515
def load_commands!
  Warg.search_paths.each do |warg_path|
    Dir.glob(warg_path.join("commands", "**", "*.rb")).each do |command_path|
      require command_path
    end
  end
end
load_config!() click to toggle source
# File lib/warg.rb, line 1503
def load_config!
  config_path = @path.join("config.rb")

  if config_path.exist?
    load config_path
  end

  Dir.glob(@path.join("config", "**", "*.rb")).each do |config_file|
    load config_file
  end
end
load_scripts!() click to toggle source
# File lib/warg.rb, line 1523
def load_scripts!
  Warg.search_paths.each do |warg_path|
    warg_scripts_path = warg_path.join("scripts")

    Dir.glob(warg_scripts_path.join("**", "*")).each do |path|
      script_path = Pathname.new(path)

      if script_path.directory? || script_path.basename.to_s.index("_defaults") == 0
        next
      end

      relative_script_path = script_path.relative_path_from(warg_scripts_path)

      command_name = Command::Name.from_relative_script_path(relative_script_path)

      object_names = command_name.object.split("::")
      object_names.inject(Object) do |namespace, object_name|
        if namespace.const_defined?(object_name)
          object = namespace.const_get(object_name)
        else
          if object_name == object_names[-1]
            object = Class.new do
              include Command::BehaviorWithoutRegistration

              def setup
                run_script
              end
            end
          else
            object = Module.new
          end

          namespace.const_set(object_name, object)

          if object < Command::BehaviorWithoutRegistration
            Warg::Command.register(object)
          end
        end

        object
      end
    end
  end
end
setup() click to toggle source
# File lib/warg.rb, line 1547
def setup
  run_script
end