Class: Sprout::Generator::Base

Inherits:
Executable::Base show all
Defined in:
sprout/lib/sprout/generator/base.rb

Direct Known Subclasses

FlashSDK::ClassGenerator, FlashSDK::TestClassGenerator, Sprout::GeneratorGenerator, LibraryGenerator, RubyGenerator, ToolGenerator

Constant Summary

Constants included from Executable

DEFAULT_FILE_EXPRESSION, DEFAULT_PREFIX, DEFAULT_SHORT_PREFIX

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Executable::Base

add_param, add_param_alias, #binary_path, #binary_path=, #create_outer_task, #default_file_expression, #execute_delegate, #from_hash, #initialize, #library_added, #parse!, #parse_extra_options!, #parse_rake_task_arg, #prepare, set, static_default_value_collection, static_parameter_collection, #stderr, #stderr=, #stdout, #stdout=, #system_execute, #to_hash, #to_help, #to_rake, #to_shell, #update_rake_task_name_from_args

Methods included from Concern

#append_features, extended, #included

Constructor Details

This class inherits a constructor from Sprout::Executable::Base

Instance Attribute Details

- (Boolean) destroy

Insteast of creating, destroy the files.

Returns:

  • (Boolean)

    An instance of Boolean



# File ''

add_param :destroy, Boolean

- (Boolean) force

Force the creation of files without prompting.

Returns:

  • (Boolean)

    An instance of Boolean



# File ''

add_param :force, Boolean

- (String) input

The primary input for the application or component.

Returns:

  • (String)

    An instance of String



# File ''

add_param :input, String, { :hidden_name => true, :required => true }

- (Object) logger

Returns the value of attribute logger



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

def logger
  @logger
end

- (Object) name

The symbol name for which this generator is most appropriate.

This value defaults to :application so, if you're working on an application generator, you can leave it as the default.

For all other generator types, you'll want to select the most general project type that this generator may be useful in.

Following are some example values:

:as3, :flex3, :flex4, :air2

or core libraries:

:asunit4, :flexunit4

or even other libraries:

:puremvc, :robotlegs, :swizz


72
73
74
# File 'sprout/lib/sprout/generator/base.rb', line 72

def name
  @name
end

- (Path) path

The directory where files will be created.

Returns:

  • (Path)

    An instance of Path



# File ''

add_param :path, Path, { :default => Dir.pwd }

- (Object) pkg_name

Returns the value of attribute pkg_name



74
75
76
# File 'sprout/lib/sprout/generator/base.rb', line 74

def pkg_name
  @pkg_name
end

- (Object) pkg_version

Returns the value of attribute pkg_version



75
76
77
# File 'sprout/lib/sprout/generator/base.rb', line 75

def pkg_version
  @pkg_version
end

- (Boolean) quiet

Run the generator in Quiet mode - do not write status to standard output.

Returns:

  • (Boolean)

    An instance of Boolean



# File ''

add_param :quiet, Boolean

- (Boolean) show_template_paths

Display the paths this generator will use to look for templates on this system and exit.

Returns:

  • (Boolean)

    An instance of Boolean



# File ''

add_param :show_template_paths, Boolean

- (Array<String> [Paths]) templates

A collection of paths to look in for named templates.

Returns:

  • (Array<String> [Paths])

    An instance of Array<String> [Paths]



# File ''

add_param :templates, Paths

Class Method Details

+ (Object) inherited(base)



6
7
8
9
10
11
12
13
14
# File 'sprout/lib/sprout/generator/base.rb', line 6

def self.inherited base
  # NOTE: We can NOT instantiate the class here,
  # because only it's first line has been interpreted, if we 
  # instantiate here, none of the code declared in the class body will
  # be associated with this instance.
  #
  # Go ahead and register the class and update instances later...
  Sprout::Generator.register base, template_from_caller(caller.first)
end

Instance Method Details

- (Object) create_template_paths



122
123
124
125
126
# File 'sprout/lib/sprout/generator/base.rb', line 122

def create_template_paths
  paths = templates.dup
  paths = paths.concat Sprout::Generator.search_paths
  paths << Sprout::Generator::template_folder_for(self)
end

- (Object) default_search_paths (protected)



145
146
147
# File 'sprout/lib/sprout/generator/base.rb', line 145

def default_search_paths
  Sprout::Generator.search_paths.collect { |path| File.join(path, 'templates') }
end

- (Object) directory(name, &block) (protected)



157
158
159
# File 'sprout/lib/sprout/generator/base.rb', line 157

def directory name, &block
  @command.directory name, &block
end

- (Object) do_show_template_paths (protected)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'sprout/lib/sprout/generator/base.rb', line 130

def do_show_template_paths
  @logger ||= $stdout
  message = "The following paths will be checked for templates:\n"
  
  paths = ["--templates+=[value]"]
  paths = paths.concat Sprout::Generator.create_search_paths
  paths << "ENV['SPROUT_GENERATORS']"
  paths << Sprout::Generator::template_folder_for(self)

  message << "  * "
  message << paths.join("\n  * ")
  say message
  message
end

- (Object) execute

Record the actions and trigger them



79
80
81
82
83
# File 'sprout/lib/sprout/generator/base.rb', line 79

def execute
  return do_show_template_paths if show_template_paths
  return prepare_command.unexecute if destroy
  prepare_command.execute
end

- (Object) file(name, template = nil) (protected)



161
162
163
# File 'sprout/lib/sprout/generator/base.rb', line 161

def file name, template=nil
  @command.file name, template
end

- (Object) generator(name, options = {}) (protected)



169
170
171
# File 'sprout/lib/sprout/generator/base.rb', line 169

def generator name, options={}
  @command.generator name, to_hash.merge(options)
end

- (Object) prepare_command (protected)



149
150
151
152
153
154
155
# File 'sprout/lib/sprout/generator/base.rb', line 149

def prepare_command
  @logger ||= $stdout
  @command = Command.new self
  @command.logger = logger
  manifest
  @command
end

- (Object) resolve_template(content)

TODO: Add support for arbitrary templating languages. For now, just support ERB...

TODO: This is also a possible spot where those of you that don't want to snuggle might put pretty-print code or some such modifiers...



107
108
109
110
# File 'sprout/lib/sprout/generator/base.rb', line 107

def resolve_template content
  require 'erb'
  ERB.new(content, nil, '>').result(binding)
end

- (Object) say(message)



96
97
98
# File 'sprout/lib/sprout/generator/base.rb', line 96

def say message
  logger.puts message.gsub("#{path}", '.') unless quiet
end

- (Object) template(name, template = nil) (protected)



165
166
167
# File 'sprout/lib/sprout/generator/base.rb', line 165

def template name, template=nil
  @command.template name, template
end

- (Object) template_paths

Returns a collection of templates that were provided on the command line, followed by templates that are created by a concrete generator, followed by the Sprout::Generator.search_paths + 'templates' folders.



118
119
120
# File 'sprout/lib/sprout/generator/base.rb', line 118

def template_paths
  create_template_paths
end

- (Object) unexecute

Rollback the generator



92
93
94
# File 'sprout/lib/sprout/generator/base.rb', line 92

def unexecute
  prepare_command.unexecute
end

- (Object) validate



85
86
87
88
# File 'sprout/lib/sprout/generator/base.rb', line 85

def validate
  return true if show_template_paths
  super
end