Class: Hiiro::Runners

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

Defined Under Namespace

Classes: Bin, Subcommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hiiro) ⇒ Runners

Returns a new instance of Runners.



566
567
568
569
570
571
572
# File 'lib/hiiro.rb', line 566

def initialize(hiiro)
  @hiiro = hiiro
  @bin_name = hiiro.bin_name
  @subcmd = hiiro.subcmd
  @subcommands = {}
  @default_subcommand = hiiro.default_subcommand
end

Instance Attribute Details

#bin_nameObject (readonly)

Returns the value of attribute bin_name.



564
565
566
# File 'lib/hiiro.rb', line 564

def bin_name
  @bin_name
end

#hiiroObject (readonly)

Returns the value of attribute hiiro.



564
565
566
# File 'lib/hiiro.rb', line 564

def hiiro
  @hiiro
end

#subcmdObject (readonly)

Returns the value of attribute subcmd.



564
565
566
# File 'lib/hiiro.rb', line 564

def subcmd
  @subcmd
end

#subcommandsObject (readonly)

Returns the value of attribute subcommands.



564
565
566
# File 'lib/hiiro.rb', line 564

def subcommands
  @subcommands
end

Instance Method Details

#add_default(handler, **values) ⇒ Object



574
575
576
577
578
579
580
581
# File 'lib/hiiro.rb', line 574

def add_default(handler, **values)
  @default_subcommand = Subcommand.new(
    bin_name,
    :DEFAULT,
    handler,
    values
  )
end

#add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values) ⇒ Object



612
613
614
615
616
617
618
619
# File 'lib/hiiro.rb', line 612

def add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values)
  @subcommands[name] = Subcommand.new(
    bin_name, name, handler, values,
    opts: opts,
    subcmd_args: subcmd_args,
    subcmd_opts: subcmd_opts
  )
end

#all_binsObject



606
607
608
609
610
# File 'lib/hiiro.rb', line 606

def all_bins
  pattern = format('{%s}/%s-*', paths.join(?,), bin_name)

  Dir.glob(pattern).map { |path| Bin.new(bin_name, path) }
end

#all_runnersObject



598
599
600
# File 'lib/hiiro.rb', line 598

def all_runners
  [*all_bins, *subcommands.values]
end

#ambiguous_matchesObject



642
643
644
645
646
# File 'lib/hiiro.rb', line 642

def ambiguous_matches
  return [] unless subcmd_result
  matches = matching_runners
  matches.count > 1 ? matches : []
end

#exact_runnerObject



632
633
634
# File 'lib/hiiro.rb', line 632

def exact_runner
  subcmd_result&.exact_match&.item
end

#matching_runnersObject



648
649
650
651
# File 'lib/hiiro.rb', line 648

def matching_runners
  return [] unless subcmd_result
  remove_child_runners(subcmd_result.matches.map(&:item))
end

#pathsObject



602
603
604
# File 'lib/hiiro.rb', line 602

def paths
  @paths ||= ENV['PATH'].split(?:).uniq
end

#remove_child_runners(list) ⇒ Object



653
654
655
656
657
# File 'lib/hiiro.rb', line 653

def remove_child_runners(list)
  list.reject do |r|
    list.any? { |other| r != other && r.full_name.start_with?(other.full_name + ?-) }
  end
end

#run_subcommand(name, *args) ⇒ Object



621
622
623
624
625
# File 'lib/hiiro.rb', line 621

def run_subcommand(name, *args)
  cmd = subcommands[name]

  cmd&.run(*args)
end

#runnerObject



583
584
585
586
587
588
# File 'lib/hiiro.rb', line 583

def runner
  return exact_runner if exact_runner
  return unambiguous_runner if unambiguous_runner

  @default_subcommand
end

#subcmd_resultObject



627
628
629
630
# File 'lib/hiiro.rb', line 627

def subcmd_result
  return nil unless subcmd
  @subcmd_result ||= Matcher.by_prefix(all_runners, subcmd.to_s, key: :subcommand_name)
end

#subcommand_namesObject



594
595
596
# File 'lib/hiiro.rb', line 594

def subcommand_names
  all_runners.map(&:subcommand_name)
end

#unambiguous_runnerObject



636
637
638
639
640
# File 'lib/hiiro.rb', line 636

def unambiguous_runner
  return nil unless subcmd_result
  matches = matching_runners
  matches.first if matches.count == 1
end

#using_default?Boolean

Returns:

  • (Boolean)


590
591
592
# File 'lib/hiiro.rb', line 590

def using_default?
  !exact_runner && !unambiguous_runner
end