Class: Cog::DSL::Cogfile

Inherits:
Object
  • Object
show all
Includes:
Generator
Defined in:
lib/cog/dsl/cogfile.rb

Overview

In your project’s Cogfile, self has been set to an instance of this class. Typing cog init will create a Cogfile in the present working directory.

Instance Method Summary collapse

Methods included from Generator

#embed, #gcontext, #stamp

Methods included from Generator::LanguageMethods

#end_all_scopes, #include_guard_begin, #named_scope_begin, #scope_begin, #scope_end, #use_named_scope, #warning

Methods included from Generator::Filters

#call_filter, #comment

Methods included from Generator::FileMethods

#copy_file_if_missing, #files_are_same?, #get_template, #touch_directory, #touch_file

Constructor Details

#initialize(config, path, opt = {}) ⇒ Cogfile

Initialize with an instance of Config

Parameters:

  • config (Config)

    the object which will be configured by this Cogfile

  • path (String)

    path to the cogfile

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :project (Boolean) — default: false

    is this the project cogfile?

  • :plugin_path_only (Boolean) — default: false

    only process plugin_path calls in the given cogfile

  • :plugin (Plugin) — default: nil

    indicate that the cogfile is for the given plugin



17
18
19
20
21
22
23
24
25
26
# File 'lib/cog/dsl/cogfile.rb', line 17

def initialize(config, path, opt={})
  @cogfile_context = {
    :config => config,
    :cogfile_path => path,
    :cogfile_dir => File.dirname(path),
    :project => opt[:project],
    :plugin_path_only => opt[:plugin_path_only],
    :plugin => opt[:plugin],
  }
end

Instance Method Details

#autoload_plugin(identifier_name, path) ⇒ nil

Register an autoload variable in the class GeneratorSandbox. That way when generators are run as instances of a sandbox, they will be able to load plugins just by referencing them using the provided identifier_name. A call to this method will be ignored unless the containing cogfile is being treated as a plugin.

Parameters:

  • identifier_name (String)

    identifier by which the plugin can be referenced in generators

  • path (String)

    path to the ruby file to load when the identifier is referenced (relative to the cogfile)

Returns:

  • (nil)


108
109
110
111
112
# File 'lib/cog/dsl/cogfile.rb', line 108

def autoload_plugin(identifier_name, path)
  if plugin?
    GeneratorSandbox.autoload_plugin(identifier_name, File.join(plugin.path, path))
  end
end

#generator_path(path, absolute = false) ⇒ nil

Define a directory in which to find generators

Parameters:

Returns:

  • (nil)


50
51
52
53
# File 'lib/cog/dsl/cogfile.rb', line 50

def generator_path(path, absolute=false)
  return if plugin_path_only?
  add_config_path :generator, path, absolute
end

#interpretnil

Interpret the Cogfile at Config::ProjectConfig#cogfile_path

Returns:

  • (nil)


31
32
33
34
# File 'lib/cog/dsl/cogfile.rb', line 31

def interpret
  eval File.read(cogfile_path), binding
  nil
end

#language(key) {|lang| ... } ⇒ Object

Define and register a language with cog

Parameters:

  • key (String)

    unique case-insensitive identifier

Yield Parameters:

  • lang (LanguageDSL)

    an interface for defining the language

Returns:

  • (Object)

    the return value of the block



93
94
95
96
97
98
99
100
101
102
# File 'lib/cog/dsl/cogfile.rb', line 93

def language(key, &block)
  return if plugin_path_only?
  dsl = LanguageDSL.new key
  r = block.call dsl
  lang = dsl.finalize
  config_eval do
    @language[lang.key] = lang
  end
  r
end

#language_extensions(map) ⇒ nil

Explicitly specify a mapping from file extensions to languages

Parameters:

  • map (Hash)

    key-value pairs from this mapping will override the default language map supplied by cog

Returns:

  • (nil)


80
81
82
83
84
85
86
87
# File 'lib/cog/dsl/cogfile.rb', line 80

def language_extensions(map)
  return if plugin_path_only?
  config_eval do
    map.each_pair do |key, value|
      @language_extension_map[key.to_s.downcase] = value.to_s.downcase
    end
  end
end

#plugin_path(path, absolute = false) ⇒ nil

Define a directory in which to find plugins. A call to this method will be ignored if the containing cogfile is itself being treated as a plugin.

Parameters:

Returns:

  • (nil)


68
69
70
71
72
73
74
75
# File 'lib/cog/dsl/cogfile.rb', line 68

def plugin_path(path, absolute=false)
  return if plugin?
  path = add_config_path :plugin, path, absolute
  if path && File.exists?(path)
    raise Errors::PluginPathIsNotADirectory.new path unless File.directory?(path)
    @cogfile_context[:config].register_plugins path
  end
end

#project_path(path, absolute = false) ⇒ nil

Define the directory in which to generate code. A call to this method will be ignored if the containing cogfile is being treated as a plugin.

Parameters:

Returns:

  • (nil)


40
41
42
43
44
# File 'lib/cog/dsl/cogfile.rb', line 40

def project_path(path, absolute=false)
  return if plugin_path_only? || plugin?
  path = File.join @cogfile_context[:cogfile_dir], path unless absolute
  config_eval { @project_path = path }
end

#stamp_generator {|name, dest| ... } ⇒ nil

Define a block to call when stamping a generator for a plugin. A call to this method will be ignored unless the containing cogfile is being treated as a plugin.

Yield Parameters:

  • name (String)

    name of the generator to stamp

  • dest (String)

    file system path where the file will be created

Yield Returns:

  • (nil)

Returns:

  • (nil)


119
120
121
# File 'lib/cog/dsl/cogfile.rb', line 119

def stamp_generator(&block)
  plugin.stamp_generator_block = block if plugin?
end

#template_path(path, absolute = false) ⇒ nil

Define a directory in which to find templates

Parameters:

Returns:

  • (nil)


59
60
61
62
# File 'lib/cog/dsl/cogfile.rb', line 59

def template_path(path, absolute=false)
  return if plugin_path_only?
  add_config_path :template, path, absolute
end