# -*- coding: UTF-8 -*- require 'build-tool/commands' require 'ansi' module BuildTool; module Commands; # # BuildCommand # class History < Standard name "history" description "show command history" long_description [ "Query the command history", " NO PARAM: Show the history list", " NUMBER: Show detailed information about entry NUMBER", " +NUMBER: Show detailed information about NUMBER next to last entry", " MODULE: Show the last three commands on MODULE" ] def initialize_options options.banner = "Usage: #{self.fullname} [OPTIONS]... [NUMBER|+NUMBER|MODULE]" options.separator( "" ) options.separator( "Options" ) # Show detailed history. Add start time, duration and success/failure @long = false options.on( "-l", "--long", "Use long list format." ) { |t| @long = true } super @lines = nil options.on( "-n", "--number COUNT", "Number of lines to show." ) { |n| if not /^[0-9]+$/.match( n ) raise OptionParser::ParseError.new( 'value "%s" is not a number' % n ) end @lines = n.to_i() } super end def applicable? BuildTool::Application.instance.has_recipe? end def do_execute( args ) case args.length when 0 return show_command_history when 1 return show_detailed_command_history( args[0] ) if args[0] =~ /^[0-9]+/ case args[0][0,1] when '+' return show_detailed_command_history( args[0] ) else return show_module_history( args[0] ) end else return usage( "To many arguments" ) end end def show_command( cmd ) if @long info( " %04d %s # %s [%s]" % [ cmd.id, cmd.started_at.strftime("%x %X"), cmd.command, cmd.state_str ] ) else info( " %04d %s [%s]" % [ cmd.id, cmd.command, cmd.state_str ] ) end end def show_command_history # *TODO* Make the number of printed command configurable BuildTool::History::CommandLog.last(@lines || 50 ).reverse().each do |cmd| show_command cmd end return 0 end def show_module_history( modname ) mod = complete_module( modname ) if mod.nil? return usage( "Unknown module #{modname}" ) end BuildTool::History::CommandLog.last_by_module( mod.name, @lines || 3 ).reverse.each do |cmd| show_command( cmd ) last_module="" cmd.module_logs.where( :module => mod.name ).each do |e| if e.module != last_module info( blue { "\t#{e.module}" } ) last_module = e.module end info( "\t\t %s %s (%s) [%s]" % [ e.duration, e.event, e.logfile, e.state_str ] ) end end return 0 end def show_detailed_command_history( id ) if id[0,1] == '+' cmd = BuildTool::History::CommandLog.most_recent( id ) return 0 if cmd.nil? else cmd = BuildTool::History::CommandLog.find( id ) if cmd.nil? error( "No entry with id #{id.to_i} found in history!") return -1 end end show_command( cmd ) last_module="" cmd.module_logs.each do |e| if e.module != last_module info( blue { "\t#{e.module}" } ) last_module = e.module end info( "\t\t %s %s (%s) [%s]" % [ e.duration, e.event, e.logfile, e.state_str ] ) end return 0 end end # class Info end; end # module BuildTool::Commands