Sha256: 0dec0833058207ab429a4edf72ed615cd52a60a69c3d1642756cffa0baf41154

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require "cogy/engine"
require "cogy/handler"
require "cogy/command"

module Cogy
  COG_BUNDLE_VERSION = 4

  # Holds all the registered Commands. Not to be messed with.
  mattr_accessor :commands
  @@commands = {}

  # Bundle config-related stuff
	mattr_accessor :bundle_name
	@@bundle_name = "cogy"

  mattr_accessor :bundle_description
  @@bundle_description = "Cogy-generated commands"

  # Can be either a string or an object that responds to `#call` and returns
  # a string.
  #
  # Must be set explicitly
  mattr_accessor :bundle_version
  @@bundle_version = nil

  # The path in the Cog Relay where the command executable is located.
  #
  # Must be set explicitly.
  mattr_accessor :executable_path
  @@executable_path = nil

  # Paths where the files that define the commands will be searched in
  mattr_accessor :command_load_paths
  @@command_load_paths = ["cogy"]

  def self.on(cmd_name, opts = {}, &blk)
    cmd = Command.new(cmd_name, opts)
    handler = Handler.new(blk)
    cmd.register!(handler)
  end

  # @return [Hash]
  def self.bundle_config
    version = if bundle_version.respond_to?(:call)
      bundle_version.call
    else
      bundle_version
    end

    config = {
      "cog_bundle_version" => COG_BUNDLE_VERSION,
      "name" => bundle_name,
      "description" => bundle_description,
      "version" => version,
      "commands" => {}
    }

    commands.each do |name, cmd|
      config["commands"][name] = {
        "executable" => executable_path,
        "description" => cmd.desc,
        "arguments" => cmd.formatted_args,
        "options" => cmd.formatted_opts,
        "rules" => cmd.rules
      }

      if cmd.long_desc
        config["commands"][name]["long_description"] = cmd.long_desc
      end

      if cmd.example
        config["commands"][name]["example"] = cmd.example
      end
    end

    config
  end

  def self.configure
    yield self
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogy-0.0.1 lib/cogy.rb