Class: BuildTool::Commands::SubCommands

Inherits:
Base
  • Object
show all
Defined in:
lib/build-tool/commands.rb

Overview

Commands that has subcommands

Direct Known Subclasses

CLI, EnvironmentsCLI, RecipesCLI, Shell

Instance Attribute Summary

Attributes inherited from Base

#cmd, #options, #parent

Instance Method Summary collapse

Methods inherited from Base

#<=>, #applicable?, #cleanup_after_vcs_access, #complete, #complete_readline_1_8, #complete_readline_1_9, #configuration, #do_execute, #each_option, #fullname, #initialize_options, #log?, #say, #setup_command, #skip_command, #summarize, #teardown_command, #usage

Methods included from HelpText

#cmdalias, #description, included, #long_description, #name

Constructor Details

#initialize(*args) ⇒ SubCommands

Returns a new instance of SubCommands.



662
663
664
665
# File 'lib/build-tool/commands.rb', line 662

def initialize( *args )
    super( *args )
    @commands = []
end

Instance Method Details

#add_command(cmd) ⇒ Object



667
668
669
670
# File 'lib/build-tool/commands.rb', line 667

def add_command( cmd )
    logger.trace "#{name}: Adding command #{cmd.name}"
    @commands << cmd
end

#complete_arguments(args, string) ⇒ Object



761
762
763
764
765
766
767
768
# File 'lib/build-tool/commands.rb', line 761

def complete_arguments( args, string )
    # args[0] is supposed to contain the command name
    if cmd = get_command( args[0] )
        return cmd.complete_arguments( args, string )
    else
        return []
    end
end

#complete_command(cmdname) ⇒ Object



754
755
756
757
758
759
# File 'lib/build-tool/commands.rb', line 754

def complete_command( cmdname )
    # puts "#{self.name}.complete_command( #{cmdname.inspect} )"
    cmds = @commands.collect { |com|
        com.name if com.name.start_with? cmdname
    }.compact
end

#do_complete_1_8(args) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/build-tool/commands.rb', line 701

def do_complete_1_8( args )
    # puts "#{self.name}.do_complete_1_8( #{args.inspect}"
    if args.length <= 1
        # There is only one or no argument. We try to complete to a
        # command.
        if subcmd = get_command( args[0] )
            # We have a exact match. If we had to complete to get to
            # the commands return here. If not forward to the command
            if args[0] != subcmd.name
                return [ subcmd.name ]
            else
                return subcmd.do_complete_1_8( [""] ).collect do |res|
                    [ subcmd.name ].push(res).join(" ")
                end
            end
        else
            # We have a no exact match. Complete the available
            # commands
            return complete_command( args[0] )
        end
    else
        # At least two arguments are present
        if subcmd = get_command( args[0] )
            # We have a exact match. Forward to that command.
            return subcmd.do_complete_1_8( args[1..-1] ).collect do |res|
                [ subcmd.name ].push(res).join(" ")
            end
        else
            # No exact match. Since there is at least on argument
            # behind the current position just keep the user input
            # intact.
            return []
        end
    end
end

#do_complete_1_9(args) ⇒ Object



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/build-tool/commands.rb', line 737

def do_complete_1_9( args )
    # puts "#{self.name}.do_complete_1_9( #{args.inspect} )"
    if args.length <= 1
        if subcmd = get_command( args[0] )
            return subcmd.do_complete_1_9( [""] )
        else
            return complete_command( args[0] )
        end
    else
        if subcmd = get_command( args[0] )
            return subcmd.do_complete_1_9( args[1..-1] )
        else
            return args
        end
    end
end

#execute(args) ⇒ Object



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/build-tool/commands.rb', line 684

def execute( args )
    # There has to be a subcommand
    if args.length == 0
        return show_help
    end

    cmd = get_command( args[0] )
    if cmd.nil?
        if self.name
            raise UsageError, "Unknown command #{args[0]} for #{self.name}"
        else
            raise UsageError, "Unknown command #{args[0]}"
        end
    end
    cmd.execute( args[1..-1] )
end

#get_command(name) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
# File 'lib/build-tool/commands.rb', line 672

def get_command( name )
    partial = Array.new
    @commands.each do |cmd|
        return cmd if cmd.name == name
        partial << cmd if cmd.name.start_with? name
    end
    if partial.length == 1
        return partial[0]
    end
    nil
end

#load_commands(path, parentmod, submod = nil) ⇒ Object



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/build-tool/commands.rb', line 789

def load_commands( path, parentmod, submod = nil )
    logger.debug "load_commands( #{path} #{parentmod.inspect} #{submod.inspect}"
    # Load all available ruby files in path
    Dir[  "#{path}/*.rb" ].each do |mod|
        load "#{mod}"
    end

    if !submod.nil?
        parentmod = parentmod.const_get( submod )
    end
    # Now look at all constants for candidates
    parentmod.constants.each do |objname|
        # Get the obj
        obj = parentmod.const_get( objname )
        # :TODO: Better solution
        next if [ "Standard", "ModuleBasedCommand", "Base", "SubCommands", "Shell" ].include? objname.to_s
        next if ! obj.kind_of? Class

        tmp = obj
        while tmp.superclass
            if tmp == BuildTool::Commands::SubCommands
                cmd = obj.new( self )
                break if !cmd.applicable?
                if cmd.cmdalias
                    parent.add_command( Alias.new( cmd.cmdalias, cmd ) )
                end
                add_command( cmd )
                cmd.load_commands( "#{path}/#{cmd.name}", parentmod, cmd.name.capitalize )
                break
            elsif tmp == BuildTool::Commands::Base
                cmd = obj.new( self )
                break if !cmd.applicable?
                if cmd.cmdalias
                    parent.add_command( Alias.new( cmd.cmdalias, cmd ) )
                end
                add_command( cmd )
                break
            end
            tmp = tmp.superclass
        end
    end

end

#show_help(args = []) ⇒ Object



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/build-tool/commands.rb', line 770

def show_help( args = [] )
    if args.length == 0
        @commands.sort.each do |cmd|
            say "%-20s: %s" % [ cmd.name, cmd.description ]
        end
    else
        if cmd = get_command( args[0] )
            cmd.show_help( args[1..-1] )
        else
            if self.name
                raise UsageError, "Unknown command #{args[0]} for #{self.name}"
            else
                raise UsageError, "Unknown command #{args[0]}"
            end
        end
    end
    return 0
end