Class TypeTrigger
In: checktype.rb
Parent: P4Trigger

The trigger class itself. The main method in here is validate() which is invoked from the super-class’ parse_change() method.

Methods
filetype    new    report    validate   
Public Class methods
new( max_errors )
# File checktype.rb, line 78
    def initialize( max_errors )
        @max_errors = max_errors
        super()
    end
Public Instance methods
filetype( depot_file )

Method to return the expected filetype given a filename. Returns nil if no specific filetype has been mandated.

# File checktype.rb, line 97
    def filetype( depot_file )
        ext = depot_file.sub( /.*\.(.*)/, '\1' )
        ext = ext.downcase
        TYPEMAP[ ext ]
    end
validate()

Enforce type checking. Basic algorithm is that we iterate over the typemap and match each extension against the depot file. If they match, we check the type.

# File checktype.rb, line 106
    def validate()

        # First gather the list of bad files
        badlist = Array.new
        change.each_file do
            |file|
            # Ignore files not open for add
            next unless ( file.revisions[ 0 ].action == "add" )
            
            type = filetype( file.depot_file )
            next unless type

            basetype = file.revisions[ 0 ].type.sub( /\+.*/, "" )
            if ( ! type.match( basetype ) )
                badlist.push( file )
            end
        end

        # Now report any problems to the user
        report( badlist ) if ( ! badlist.empty? )
        return badlist.empty?
    end
report( badfiles )

Method to report the error to the user. Just formats the error message and sends it. We only report the first @max_errors bad files. On a large changelist they’ll be grateful for that.

# File checktype.rb, line 132
    def report( badfiles )
        errors = 0
        msg = @@USER_MESSAGE
        badfiles.each do
            |file|
            type = filetype( file.depot_file )
            msg += sprintf( @@BADFILE_FORMAT, 
                           file.depot_file, 
                            file.revisions[ 0 ].type,
                            type.msg
                          )
            errors += 1
            break if ( errors >= @max_errors )

        end
        message( msg )
    end