Guest User

Untitled

a guest
Mar 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3.  
  4. require 'rubygems'
  5. require 'commander/import'
  6. require 'yaml'
  7.  
  8. class ErbExecutor
  9. include Commander::Methods
  10.  
  11. def run
  12. program :name, 'Erb Executor'
  13. program :version, '0.0.1'
  14. program :description, 'Render template from model'
  15. default_command :render
  16.  
  17. command :render do |c|
  18. c.syntax = 'erb_executor [render] --model model_file_path --template template_file_path --output '
  19. c.description = 'Render template from model'
  20. c.option '--model STRING', String, 'Model file path'
  21. c.option '--template STRING', String, 'Template file path'
  22. c.option '--output STRING', String, 'Output file path'
  23. c.action do |args, options|
  24. unless File.exist? options.output
  25. File.open(options.output, File::RDWR|File::CREAT, 0644) do |f|
  26. unless options.model.nil?
  27. if File.exist? options.model and !File.directory? options.model
  28. model = YAML::load_file(options.model)
  29. else
  30. abort "Non-existed model file path"
  31. end
  32. else
  33. abort "Missing model parameter"
  34. end
  35. if File.exist? options.template and !File.directory? options.template
  36. template = File.read(options.template)
  37. else
  38. abort "Non-existed template file path"
  39. end
  40. b = binding
  41. f.flock(File::LOCK_EX)
  42. f.rewind
  43. f.write ERB.new(template).result(b)
  44. f.flush
  45. f.truncate(f.pos)
  46. end
  47. else
  48. abort "Output file path #{options.output} existed on the file system, please give a check"
  49. end
  50. end
  51. end
  52. end
  53. end
  54.  
  55. ErbExecutor.new.run if $0 == __FILE__
Add Comment
Please, Sign In to add comment