Class: VhdlHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vhdl_helper.rb

Constant Summary collapse

VERSION =
"0.4.2"

Instance Method Summary collapse

Constructor Details

#initializeVhdlHelper

Returns a new instance of VhdlHelper.



10
11
12
13
14
15
16
# File 'lib/vhdl_helper.rb', line 10

def initialize
  puts "-- "+"="*60
  puts "-- VHDL Helper. #{VERSION}. JC Le Lann 2017-2018"
  puts "-- "+"="*60
  @date = Time.now.strftime('%c')
  @options={}
end

Instance Method Details

#analyze_options(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vhdl_helper.rb', line 18

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

#generate(what) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vhdl_helper.rb', line 92

def generate what
  filename=__dir__+"/templates/#{what}.vhd"
  unless File.exist? filename
    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) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/vhdl_helper.rb', line 71

def header filename
  code=[]
  code << "-- generated : #{@date}"
  code << "-- design    : #{filename}"
  code << "-- author    : "
  code << "-- "+"="*60
  code.join("\n")
end

#helpObject

main method



56
57
58
# File 'lib/vhdl_helper.rb', line 56

def help
  @args.each{|arg| generate(arg)}
end

#show_keywordsObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/vhdl_helper.rb', line 60

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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vhdl_helper.rb', line 80

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