Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. def try_require(name, version=nil)
  4. gem name, version if version
  5. require name
  6. rescue LoadError
  7. if version
  8. STDERR.puts "Unable to load gem #{name} (#{version})"
  9. STDERR.puts "to install, type: gem install #{name} -v '#{version}'"
  10. else
  11. STDERR.puts "Unable to load gem #{name}"
  12. STDERR.puts "to install, type: gem install #{name}"
  13. end
  14. exit 1
  15. end
  16.  
  17. try_require 'slop', '~> 4.2.0'
  18. try_require 'awesome_print'
  19. try_require 'json'
  20.  
  21. require 'pathname'
  22. require 'erb'
  23.  
  24. VERSION = '0.1.0'
  25.  
  26. def test_erb_with_params(erbfile, params = {})
  27. local_binding = binding.clone
  28.  
  29. params.each do |param, value|
  30. local_binding.eval("@#{param} = #{value.inspect}")
  31. end
  32.  
  33. begin
  34. erb = ERB.new(IO.read(erbfile.to_s), nil, '-')
  35. erb.filename = erbfile.to_s
  36. erb.result(local_binding)
  37. rescue StandardError => e
  38. file_and_line = e.backtrace.first.split(':')[0..1].join(':')
  39. STDERR.puts "#{file_and_line}: #{e.message}"
  40. exit 1
  41. end
  42. end
  43.  
  44. module Slop
  45. class PathOption < Option
  46. def call(value)
  47. Pathname.new(value)
  48. end
  49. end
  50.  
  51. class DictOption < Option
  52. def call(value)
  53. raise ArgumentError, 'Hash option requires colon delimited key/value pair' unless value.include? ':'
  54. @value ||= {}
  55. @value.merge(Hash[ [value.split(':', 2)] ])
  56. end
  57. end
  58. end
  59.  
  60. slop_opts = Slop::Options.new
  61. slop_opts.banner = "usage: #{File.basename($0)} [options]"
  62. slop_opts.path '-F', '--file', 'file to erbify'
  63. slop_opts.dict '-p', '--params', 'colon delimited key:value pair to add as local variables for the erb (use json notation for complex values)'
  64. slop_opts.on('-V', '--version', 'show version') { STDERR.puts VERSION; exit 0 }
  65. slop_opts.on('-h', '--help', 'show help') { STDERR.puts slop_opts; exit 0 }
  66.  
  67. opts = Slop::Parser.new(slop_opts).parse(ARGV)
  68.  
  69. if opts[:file].nil? && !opts.arguments.empty?
  70. opts[:file] = Pathname.new File.expand_path(opts.arguments.first)
  71. end
  72.  
  73. if opts.to_hash.values.all?(&:nil?)
  74. STDERR.puts slop_opts
  75. exit 1
  76. end
  77.  
  78. if opts[:file].nil?
  79. STDERR.puts "Missing required option \`file'"
  80. STDERR.puts slop_opts
  81. exit 1
  82. elsif ! opts[:file].exist?
  83. STDERR.puts "Unable to find file #{opts[:file].to_s}"
  84. STDERR.puts slop_opts
  85. exit 1
  86. end
  87.  
  88. params = (opts[:params] || {}).each_with_object({}) do |(key, value), params|
  89. params[key] = value =~ /[\{\[]/ ? ( JSON.parse(value) rescue value) : value
  90. end
  91.  
  92. STDERR.puts params
  93.  
  94. STDOUT.puts test_erb_with_params(opts[:file], params)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement