Class: SimpleTemplater::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-templater/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Generator

Returns a new instance of Generator.

Raises:

  • (GeneratorNotFound)

37
38
39
40
41
42
# File 'lib/simple-templater/generator.rb', line 37

def initialize(name, path)
  raise GeneratorNotFound unless File.directory?(path)
  @name, @path = name.to_sym, path
  @before_hooks, @after_hooks = Array.new, Array.new
  @context = Hash.new
end

Instance Attribute Details

#after_hooksObject (readonly)

Returns the value of attribute after_hooks


30
31
32
# File 'lib/simple-templater/generator.rb', line 30

def after_hooks
  @after_hooks
end

#before_hooksObject (readonly)

Returns the value of attribute before_hooks


30
31
32
# File 'lib/simple-templater/generator.rb', line 30

def before_hooks
  @before_hooks
end

#contextObject (readonly)

Returns the value of attribute context


30
31
32
# File 'lib/simple-templater/generator.rb', line 30

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name


30
31
32
# File 'lib/simple-templater/generator.rb', line 30

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


30
31
32
# File 'lib/simple-templater/generator.rb', line 30

def path
  @path
end

#targetObject

hook do |generator, context|

generator.target = "#{generator.target}.ru"

end


35
36
37
# File 'lib/simple-templater/generator.rb', line 35

def target
  @target
end

Instance Method Details

#after(*hooks) ⇒ Object


48
49
50
# File 'lib/simple-templater/generator.rb', line 48

def after(*hooks)
  self.after_hooks.push(*hooks)
end

#before(*hooks) ⇒ Object


44
45
46
# File 'lib/simple-templater/generator.rb', line 44

def before(*hooks)
  self.before_hooks.push(*hooks)
end

#configObject


124
125
126
127
# File 'lib/simple-templater/generator.rb', line 124

def config
  defaults = {processing: true, type: "full", directory: true}
  OpenStruct.new(defaults.merge(self.))
end

#file(path) ⇒ Object


102
103
104
# File 'lib/simple-templater/generator.rb', line 102

def file(path)
  File.join(self.path, path)
end

#flat?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'lib/simple-templater/generator.rb', line 110

def flat?
  self.config.flat
end

#full?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'lib/simple-templater/generator.rb', line 106

def full?
  self.config.full
end

#metadataObject

Metadata options :full: yes|no :flat: yes|no


117
118
119
120
121
122
# File 'lib/simple-templater/generator.rb', line 117

def 
   = File.join(self.path, "metadata.yml")
  YAML::load_file()
rescue Errno::ENOENT
  abort "SimpleTemplater expected '#{}'"
end

#parse_argv(args) ⇒ Object


52
53
54
55
56
57
58
# File 'lib/simple-templater/generator.rb', line 52

def parse_argv(args)
  args = args.dup
  args.extend(SimpleTemplater::ArgvParsingMixin)
  @target  = args.shift || File.basename(@path)
  @context = args.parse!
  self.context.merge!(name: File.basename(@target))
end

#run(args) ⇒ Object

Parameters:

  • target

    … ./blog

  • args

    Array –git-repository –no-github


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/simple-templater/generator.rb', line 62

def run(args)
  self.parse_argv(args)
  self.run_hook("setup.rb")
  if self.full? && Dir.exist?(self.target) # has to run after setup.rb hook, because setup.rb can manipulate with target
    raise TargetAlreadyExist, "#{self.target} already exist, aborting."
  end
  puts "[#{self.name} generator] Running before hooks #{self.before_hooks.inspect}"
  self.run_hooks(:before)
  puts "[#{self.name} generator] Creating #{@target} (#{self.config.type})"
  if self.flat?
    # flat/content/flat.ru.rbt
    # flat/content/%user%.rb
    SimpleTemplater::FlatBuilder.create(Dir["#{file("content")}/*"].first, self.target, context)
    self.run_hook("postprocess.rb")
  else
    FileUtils.mkdir_p(@target)
    Dir.chdir(@target) do
      SimpleTemplater::Builder.create(file("content"), context)
      self.run_hook("postprocess.rb")
      self.run_hooks(:after)
    end
  end
end

#run_hook(basename) ⇒ Object


86
87
88
89
90
91
92
93
94
# File 'lib/simple-templater/generator.rb', line 86

def run_hook(basename)
  if File.exist?(hook = file(basename))
    puts "Running #{basename} hook"
    DSL.new(self).instance_eval(File.read(hook))
    puts "Finished"
  end
rescue Exception => exception
  abort "Exception #{exception.inspect} occured during running #{basename}\n#{exception.backtrace.join("\n")}"
end

#run_hooks(type) ⇒ Object


96
97
98
99
100
# File 'lib/simple-templater/generator.rb', line 96

def run_hooks(type)
  self.send("#{type}_hooks").each do |hook|
    hook.invoke(self.context)
  end
end