Class: SimpleTemplater::Generator
- Inherits:
-
Object
- Object
- SimpleTemplater::Generator
- Defined in:
- lib/simple-templater/generator.rb
Instance Attribute Summary collapse
-
#after_hooks ⇒ Object
readonly
Returns the value of attribute after_hooks.
-
#before_hooks ⇒ Object
readonly
Returns the value of attribute before_hooks.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#target ⇒ Object
hook do |generator, context| generator.target = “#generatorgenerator.target.ru” end.
Instance Method Summary collapse
- #after(*hooks) ⇒ Object
- #before(*hooks) ⇒ Object
- #config ⇒ Object
- #file(path) ⇒ Object
- #flat? ⇒ Boolean
- #full? ⇒ Boolean
-
#initialize(name, path) ⇒ Generator
constructor
A new instance of Generator.
-
#metadata ⇒ Object
Metadata options :full: yes|no :flat: yes|no.
- #parse_argv(args) ⇒ Object
- #run(args) ⇒ Object
- #run_hook(basename) ⇒ Object
- #run_hooks(type) ⇒ Object
Constructor Details
#initialize(name, path) ⇒ Generator
Returns a new instance of Generator.
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_hooks ⇒ Object (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_hooks ⇒ Object (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 |
#context ⇒ Object (readonly)
Returns the value of attribute context.
30 31 32 |
# File 'lib/simple-templater/generator.rb', line 30 def context @context end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/simple-templater/generator.rb', line 30 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
30 31 32 |
# File 'lib/simple-templater/generator.rb', line 30 def path @path end |
#target ⇒ Object
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 |
#config ⇒ Object
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
110 111 112 |
# File 'lib/simple-templater/generator.rb', line 110 def flat? self.config.flat end |
#full? ⇒ Boolean
106 107 108 |
# File 'lib/simple-templater/generator.rb', line 106 def full? self.config.full end |
#metadata ⇒ Object
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
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 |