Class: Muding::Generator::Base

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/muding_generator/base.rb

Direct Known Subclasses

AppGenerator, Commands::Base, NamedBase

Instance Attribute Summary collapse

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Options

append_features

Constructor Details

#initialize(runtime_args, runtime_options = {}) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/muding_generator/base.rb', line 21

def initialize(runtime_args, runtime_options = {})
  @args = runtime_args
  parse!(@args, runtime_options)

  # Derive source and destination paths.
  @source_root = options[:source] || File.join(spec.path, 'templates')
  if options[:destination]
    @destination_root = options[:destination]
  elsif defined? ::MUDING_ROOT
    @destination_root = ::MUDING_ROOT
  end

  # Silence the logger if requested.
  logger.quiet = options[:quiet]

  # Raise usage error if help is requested.
  usage if options[:help]
end

Instance Attribute Details

#args ⇒ Object (readonly)

Returns the value of attribute args.



19
20
21
# File 'lib/muding_generator/base.rb', line 19

def args
  @args
end

#destination_root ⇒ Object (readonly)

Returns the value of attribute destination_root.



19
20
21
# File 'lib/muding_generator/base.rb', line 19

def destination_root
  @destination_root
end

#source_root ⇒ Object (readonly)

Returns the value of attribute source_root.



19
20
21
# File 'lib/muding_generator/base.rb', line 19

def source_root
  @source_root
end

Instance Method Details

#destination_path(relative_destination) ⇒ Object

Return the full path from the destination root for the given path. Example for destination_root = '/dest':

destination_path('some/path.rb') == '/dest/some/path.rb'


73
74
75
# File 'lib/muding_generator/base.rb', line 73

def destination_path(relative_destination)
  File.join(destination_root, relative_destination)
end

#manifest ⇒ Object

Generators must provide a manifest. Use the record method to create a new manifest and record your generator's actions.

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/muding_generator/base.rb', line 42

def manifest
  raise NotImplementedError, "No manifest for '#{spec.name}' generator."
end

#source_path(relative_source) ⇒ Object

Return the full path from the source root for the given path. Example for source_root = '/source':

source_path('some/path.rb') == '/source/some/path.rb'

The given path may include a colon ':' character to indicate that the file belongs to another generator. This notation allows any generator to borrow files from another. Example:

source_path('model:fixture.yml') = '/model/source/path/fixture.yml'


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/muding_generator/base.rb', line 54

def source_path(relative_source)
  # Check whether we're referring to another generator's file.
  name, path = relative_source.split(':', 2)

  # If not, return the full path to our source file.
  if path.nil?
    File.join(source_root, name)

  # Otherwise, ask our referral for the file.
  else
    # FIXME: this is broken, though almost always true.  Others'
    # source_root are not necessarily the templates dir.
    File.join(self.class.lookup(name).path, 'templates', path)
  end
end