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.



745
746
747
748
# File 'lib/build-tool/commands.rb', line 745

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

Instance Method Details

#add_command(cmd) ⇒ Object



750
751
752
753
# File 'lib/build-tool/commands.rb', line 750

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

#complete_arguments(args, string) ⇒ Object



844
845
846
847
848
849
850
851
# File 'lib/build-tool/commands.rb', line 844

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



837
838
839
840
841
842
# File 'lib/build-tool/commands.rb', line 837

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



784
785
786
787
788
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
# File 'lib/build-tool/commands.rb', line 784

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



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/build-tool/commands.rb', line 820

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



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/build-tool/commands.rb', line 767

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



755
756
757
758
759
760
761
762
763
764
765
# File 'lib/build-tool/commands.rb', line 755

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



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/build-tool/commands.rb', line 872

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



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/build-tool/commands.rb', line 853

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