Class: Sprout::Generator::Command

Inherits:
Object
  • Object
show all
Defined in:
sprout/lib/sprout/generator/command.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Command) initialize(generator)

A new instance of Command



7
8
9
10
11
12
# File 'sprout/lib/sprout/generator/command.rb', line 7

def initialize generator
  @generator             = generator
  @working_dir           = DirectoryManifest.new
  @working_dir.generator = generator
  @working_dir.path      = generator.path
end

Instance Attribute Details

- (Object) logger

Returns the value of attribute logger



4
5
6
# File 'sprout/lib/sprout/generator/command.rb', line 4

def logger
  @logger
end

- (Object) working_dir

Returns the value of attribute working_dir



5
6
7
# File 'sprout/lib/sprout/generator/command.rb', line 5

def working_dir
  @working_dir
end

Instance Method Details

- (Object) directory(path, &block)



14
15
16
17
18
19
20
21
22
23
24
# File 'sprout/lib/sprout/generator/command.rb', line 14

def directory path, &block
  raise Sprout::Errors::GeneratorError.new "Cannot create directory with nil path" if path.nil?
  manifest           = DirectoryManifest.new
  manifest.generator = @generator
  manifest.path      = File.join(@working_dir.path, path)
  @working_dir.children << manifest
  parent             = @working_dir
  @working_dir       = manifest
  yield if block_given?
  @working_dir       = parent
end

- (Object) execute



55
56
57
58
59
60
61
62
63
64
65
# File 'sprout/lib/sprout/generator/command.rb', line 55

def execute
  begin
    working_dir.create
  rescue StandardError => e
    @generator.say "[#{e.class}] #{e.message}"
    working_dir.children.each do |child|
      child.destroy
    end
    raise e
  end
end

- (Object) file(path, template = nil)



36
37
38
39
40
41
42
43
44
# File 'sprout/lib/sprout/generator/command.rb', line 36

def file path, template=nil
  raise Sprout::Errors::GeneratorError.new "Cannot create file with nil path" if path.nil?
  manifest           = FileManifest.new
  manifest.generator = @generator
  manifest.path      = File.join( working_dir.path, path )
  manifest.template  = template
  manifest.templates = @generator.template_paths
  working_dir.children << manifest
end

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



46
47
48
49
50
51
52
53
# File 'sprout/lib/sprout/generator/command.rb', line 46

def generator name, options={}
  raise Sprout::Errors::GeneratorError.new "Cannot call another generator with nil name" if name.nil?
  instance        = Sprout::Generator.create_instance name, options
  instance.logger = logger
  instance.path   = working_dir.path
  instance.from_hash options
  working_dir.generators << instance
end

- (Object) template(path, template = nil)



26
27
28
29
30
31
32
33
34
# File 'sprout/lib/sprout/generator/command.rb', line 26

def template path, template=nil
  raise Sprout::Errors::GeneratorError.new "Cannot create file with nil path" if path.nil?
  manifest           = TemplateManifest.new
  manifest.generator = @generator
  manifest.path      = File.join( working_dir.path, path )
  manifest.template  = template
  manifest.templates = @generator.template_paths
  working_dir.children << manifest
end

- (Object) unexecute



67
68
69
# File 'sprout/lib/sprout/generator/command.rb', line 67

def unexecute
  working_dir.destroy
end