Sha256: cfad673104cf9091a192c279efed986cd1d262c6b4ecc860178af2b74c3c4aa9

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module MakeMenu
  module Builder
    # Parse `makefile` and add all annotated targets to `menu`
    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.1.2 lib/make_menu/builder.rb
make_menu-2.1.1 lib/make_menu/builder.rb
make_menu-2.1.0 lib/make_menu/builder.rb