Module: MakeMenu::Builder

Defined in:
lib/make_menu/builder.rb

Class Method Summary collapse

Class Method Details

.build(makefile, menu) ⇒ Object

Parse ‘makefile` and add all annotated targets to `menu`



6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'lib/make_menu/builder.rb', line 6

def self.build(makefile, menu)
  File.open(makefile, 'r') do |file|
    option_number = 1
    current_group = nil

    file.each_line do |line|
      if line.start_with? '###'
        # Group header
        group_title = line.gsub(/###\s+/, '').strip
        current_group = menu.add_group MenuItemGroup.new(group_title.color(menu.group_title_color))

      elsif line.match(/^[a-zA-Z_-]+:.*?## .*$$/)
        # Menu item
        target = line.split(':').first.strip
        description = line.split('##').last.strip

        # Target 'menu' should not appear
        next if target == 'menu'

        current_group ||= menu.add_group MenuItemGroup.new('Commands'.color(menu.group_title_color))

        menu.add_item current_group.add_item(MenuItem.new(option_number, target, description))

        option_number += 1
      end
    end

    if option_number == 1
      puts
      puts 'No annotated targets found!'.red.bold
      puts
      puts 'Expecting something like this....'
      puts "    #{'my_target:'.cyan} #{'## Do some things'.yellow}"
      puts
      exit 1
    end
  end
rescue Errno::ENOENT => _e
  puts
  puts 'No Makefile!'.red.bold
  puts
  puts "File '#{makefile}' could not be found.".yellow
  puts
  exit 1
end