class VhdlHelper
Constants
- VERSION
Public Class Methods
new()
click to toggle source
# File lib/vhdl_helper.rb, line 11 def initialize puts "-- "+"="*60 puts "-- VHDL Helper. #{VERSION}. JC Le Lann 2017-2018" puts "-- "+"="*60 @date = Time.now.strftime('%c') @options={} end
Public Instance Methods
analyze_options(args)
click to toggle source
# File lib/vhdl_helper.rb, line 19 def analyze_options args args << "-h" if args.empty? opt_parser = OptionParser.new do |opts| opts.banner = "Usage: vhdl_help <keyword>" opts.on("-k", "--keywords" ,"list concepts handled for far") do |n| show_keywords abort end opts.on("-gen", "generates a VHDL file") do |n| @options[:gen]=true end opts.on("-v","--version", "Prints version") do |n| puts VERSION abort end opts.on("-h", "--help", "Prints this help") do puts "Provides basic code examples in VHDL" puts opts exit end end begin opt_parser.parse!(args) @args=args rescue Exception => e #puts e #puts e.backtrace exit end end
find_closer(word)
click to toggle source
# File lib/vhdl_helper.rb, line 109 def find_closer word files=Dir[__dir__+"/templates/*.vhd"].collect{|f| File.basename(f,'.vhd')} concept_h=files.inject({}){|h,name| h[name]=name.similar(word);h} end
generate(what)
click to toggle source
# File lib/vhdl_helper.rb, line 93 def generate what concept_h=find_closer(what) concept=concept_h.sort_by{|k,v| v}.reverse.first concept,value=concept # puts "closer concept : #{concept}" filename=__dir__+"/templates/#{concept}.vhd" unless value > 50.0 puts "Sorry...I cannot help you concerning '#{what}'" else template=IO.read(filename) renderer = ERB.new(template,nil,'>') code = renderer.result(binding) write_file code,"#{what}.vhd" end end
header(filename)
click to toggle source
# File lib/vhdl_helper.rb, line 72 def header filename code=[] code << "-- generated : #{@date}" code << "-- design : #{filename}" code << "-- author : " code << "-- "+"="*60 code.join("\n") end
help()
click to toggle source
main method
# File lib/vhdl_helper.rb, line 57 def help @args.each{|arg| generate(arg)} end
show_keywords()
click to toggle source
# File lib/vhdl_helper.rb, line 61 def show_keywords puts "Here are the keywords I know about :" path=__dir__+"/templates/*.vhd" files=Dir[path] concepts=files.collect{|filename| filename.split("/").last.match(/(.*).vhd/)[1]} concepts.sort! concepts.each do |concept| puts "- #{concept}" end end
write_file(code,filename)
click to toggle source
# File lib/vhdl_helper.rb, line 81 def write_file code,filename vhdl=[] vhdl << header(filename) vhdl << code vhdl=vhdl.join("\n") puts vhdl if @options[:gen] File.open(filename,'w'){|f| f.puts vhdl} puts "VHDL code written in : #{filename}" end end