Sha256: dce17aedf0e4963cd8f67920de526e186d2928ac561df8512930d1c36044c02a

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module MakeMenu
  module Builder
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
make_menu-2.0.0 lib/make_menu/builder.rb
make_menu-1.1.0 lib/make_menu/builder.rb
make_menu-1.0.0 lib/make_menu/builder.rb