Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'rubygems'
- require 'commander/import'
- require 'yaml'
- class ErbExecutor
- include Commander::Methods
- def run
- program :name, 'Erb Executor'
- program :version, '0.0.1'
- program :description, 'Render template from model'
- default_command :render
- command :render do |c|
- c.syntax = 'erb_executor [render] --model model_file_path --template template_file_path --output '
- c.description = 'Render template from model'
- c.option '--model STRING', String, 'Model file path'
- c.option '--template STRING', String, 'Template file path'
- c.option '--output STRING', String, 'Output file path'
- c.action do |args, options|
- unless File.exist? options.output
- File.open(options.output, File::RDWR|File::CREAT, 0644) do |f|
- unless options.model.nil?
- if File.exist? options.model and !File.directory? options.model
- model = YAML::load_file(options.model)
- else
- abort "Non-existed model file path"
- end
- else
- abort "Missing model parameter"
- end
- if File.exist? options.template and !File.directory? options.template
- template = File.read(options.template)
- else
- abort "Non-existed template file path"
- end
- b = binding
- f.flock(File::LOCK_EX)
- f.rewind
- f.write ERB.new(template).result(b)
- f.flush
- f.truncate(f.pos)
- end
- else
- abort "Output file path #{options.output} existed on the file system, please give a check"
- end
- end
- end
- end
- end
- ErbExecutor.new.run if $0 == __FILE__
Add Comment
Please, Sign In to add comment