codechecker/SmalllintReportGenerator.st
author Claus Gittinger <cg@exept.de>
Thu, 28 Mar 2019 13:54:38 +0100
changeset 542 aa25a71be62a
parent 37 0a356190f3f3
permissions -rw-r--r--
#DOCUMENTATION by cg
class: stx_goodies_builder_quickSelfTest
class definition

class: stx_goodies_builder_quickSelfTest class
added:18 methods
cg@36
     1
"{ Package: 'stx:goodies/builder/codechecker' }"
cg@36
     2
cg@36
     3
Object subclass:#SmalllintReportGenerator
cg@36
     4
	instanceVariableNames:'rules environment'
cg@36
     5
	classVariableNames:''
cg@36
     6
	poolDictionaries:''
cg@36
     7
	category:'Refactory-Lint'
cg@36
     8
!
cg@36
     9
cg@36
    10
!SmalllintReportGenerator class methodsFor:'documentation'!
cg@36
    11
cg@36
    12
documentation
cg@36
    13
"
cg@36
    14
    a standalone smallint runner.
cg@36
    15
    Generates an xml report compatible to pmd, to be processed by hudson.
cg@36
    16
cg@36
    17
    [author:]
cg@36
    18
        Claus Gittinger
cg@36
    19
"
cg@36
    20
!
cg@36
    21
cg@36
    22
examples
cg@36
    23
"
cg@37
    24
                                                                    [exBegin]
cg@36
    25
    |checker|
cg@36
    26
cg@36
    27
    checker := self new.
cg@36
    28
    checker addPackage:'exept:workflow'.
cg@36
    29
    checker performChecks.
cg@36
    30
    checker generateReportAs:'checkstyle.xml'
cg@37
    31
                                                                    [exEnd]
cg@37
    32
cg@37
    33
                                                                    [exBegin]
cg@37
    34
    |checker|
cg@37
    35
cg@37
    36
    checker := self new.
cg@37
    37
    checker addClasses:(Workflow::Datatype withAllSubclasses).
cg@37
    38
    checker performChecks.
cg@37
    39
    checker generateReportAs:'checkstyle.xml'
cg@37
    40
                                                                    [exEnd]
cg@36
    41
"
cg@36
    42
! !
cg@36
    43
cg@36
    44
!SmalllintReportGenerator class methodsFor:'instance creation'!
cg@36
    45
cg@36
    46
new
cg@36
    47
    "return an initialized instance"
cg@36
    48
cg@36
    49
    ^ self basicNew initialize.
cg@36
    50
! !
cg@36
    51
cg@36
    52
!SmalllintReportGenerator methodsFor:'checking'!
cg@36
    53
cg@36
    54
performChecks
cg@36
    55
    rules withIndexDo:[:rule :index|
cg@36
    56
       Stdout showCR:('Checking: ', rule name).
cg@36
    57
       (SmalllintChecker runRule: rule onEnvironment: environment)
cg@36
    58
    ].
cg@36
    59
cg@36
    60
    "Created: / 07-08-2011 / 01:10:00 / cg"
cg@36
    61
! !
cg@36
    62
cg@36
    63
!SmalllintReportGenerator methodsFor:'initialization'!
cg@36
    64
cg@36
    65
initialize
cg@36
    66
    self setupRules.
cg@36
    67
    self setupEnvironment
cg@36
    68
cg@36
    69
    "Modified: / 07-08-2011 / 01:14:51 / cg"
cg@36
    70
! !
cg@36
    71
cg@36
    72
!SmalllintReportGenerator methodsFor:'reporting'!
cg@36
    73
cg@36
    74
generateReportAs:aFilename
cg@36
    75
    aFilename asFilename writingFileDo:[:s | self generateReportOn:s].
cg@36
    76
cg@36
    77
    "Created: / 07-08-2011 / 01:17:46 / cg"
cg@36
    78
!
cg@36
    79
cg@36
    80
generateReportOn:aStream
cg@37
    81
    |perClass|
cg@37
    82
cg@37
    83
    perClass := IdentityDictionary new.
cg@37
    84
cg@36
    85
    rules do:[:eachRule | 
cg@36
    86
        eachRule problemCount > 0 ifTrue:[
cg@36
    87
            eachRule failedMethods do:[:method |
cg@37
    88
                |class perMethodAndClassMethod perMethod|
cg@36
    89
cg@36
    90
                class := method mclass.
cg@37
    91
                perMethodAndClassMethod := perClass at:class theNonMetaclass ifAbsentPut:[{ IdentityDictionary new. IdentityDictionary new }].
cg@37
    92
                perMethod := perMethodAndClassMethod at:(class isMeta ifTrue:[2] ifFalse:[1]).
cg@37
    93
                rules := perMethod at:method ifAbsentPut:[IdentitySet new].
cg@37
    94
                rules add:eachRule.
cg@37
    95
            ]
cg@37
    96
        ]
cg@37
    97
    ].
cg@37
    98
cg@37
    99
    aStream nextPutLine: '<?xml version="1.0"?>'.
cg@37
   100
    aStream nextPutLine: '<pmd>'.
cg@37
   101
cg@37
   102
    perClass keysAndValuesDo:[:class :perMethodAndClassMethod |
cg@37
   103
        |fullSource sourceStream classFileName|
cg@37
   104
cg@37
   105
        fullSource := class source.
cg@37
   106
        sourceStream := class localSourceStreamFor:(class classFilename).
cg@37
   107
        sourceStream notNil ifTrue:[
cg@37
   108
            classFileName := sourceStream pathName.
cg@37
   109
            sourceStream close.
cg@37
   110
        ] ifFalse:[
cg@37
   111
            classFileName := class packageDirectory construct:(class classFilename).
cg@37
   112
        ].
cg@37
   113
cg@37
   114
        aStream nextPutLine:('  <file name="%1">' bindWith:classFileName asFilename pathName).
cg@37
   115
cg@37
   116
        { (perMethodAndClassMethod at:2).
cg@37
   117
          (perMethodAndClassMethod at:1) } do:[:perMethod |
cg@37
   118
            (perMethod keys copyAsOrderedCollection sort:[:a :b | a selector < b selector]) do:[:eachMethod |
cg@37
   119
                |rulesPerMethod charPosOfMethod lineNumberOfMethod|
cg@37
   120
cg@37
   121
                rulesPerMethod := perMethod at:eachMethod.
cg@37
   122
                
cg@37
   123
                charPosOfMethod := eachMethod sourcePosition ? 1.
cg@37
   124
                "/ q&d hack - editor knows how to compute line number - should go
cg@37
   125
                "/ somewhere else...
cg@37
   126
                lineNumberOfMethod := (ListView basicNew setList:fullSource) lineOfCharacterPosition:charPosOfMethod.
cg@37
   127
                rulesPerMethod do:[:eachRule |
cg@37
   128
                    |ruleName rationale|
cg@37
   129
cg@37
   130
                    ruleName := eachRule name.
cg@37
   131
                    rationale := eachRule rationale.
cg@37
   132
cg@37
   133
                    aStream nextPutLine:('    <violation line="%1" rule="%2">' 
cg@37
   134
                                                        bindWith:lineNumberOfMethod with:ruleName).
cg@37
   135
                    aStream nextPutLine:('%1' bindWith:rationale).
cg@37
   136
                    aStream nextPutLine: '    </violation>'.
cg@37
   137
cg@36
   138
                ].
cg@36
   139
            ].
cg@37
   140
        ].
cg@37
   141
        aStream nextPutLine: '  </file>'.
cg@36
   142
    ].
cg@36
   143
    aStream nextPutLine: '</pmd>'.
cg@36
   144
cg@36
   145
    "Created: / 07-08-2011 / 01:17:00 / cg"
cg@36
   146
! !
cg@36
   147
cg@36
   148
!SmalllintReportGenerator methodsFor:'setup'!
cg@36
   149
cg@36
   150
addClass:aClass
cg@36
   151
    environment addClass: aClass.
cg@36
   152
cg@36
   153
    "Created: / 07-08-2011 / 01:11:33 / cg"
cg@36
   154
!
cg@36
   155
cg@37
   156
addClasses:aCollectionOfClasses
cg@37
   157
    aCollectionOfClasses do:[:eachClass | self addClass: eachClass ].
cg@37
   158
cg@37
   159
    "Created: / 07-08-2011 / 11:51:52 / cg"
cg@37
   160
!
cg@37
   161
cg@36
   162
addPackage:aPackage
cg@36
   163
    Smalltalk loadPackage:aPackage.
cg@36
   164
    Smalltalk allClassesInPackage:aPackage do:[:cls | self addClass:cls]
cg@36
   165
cg@36
   166
    "Created: / 07-08-2011 / 01:12:31 / cg"
cg@36
   167
!
cg@36
   168
cg@36
   169
setupEnvironment
cg@36
   170
    environment := ClassEnvironment new.
cg@36
   171
cg@36
   172
    "Created: / 07-08-2011 / 01:10:56 / cg"
cg@36
   173
!
cg@36
   174
cg@36
   175
setupRules
cg@37
   176
    self setupRules:(RBCompositeLintRule allRules).
cg@36
   177
cg@37
   178
    "Created: / 07-08-2011 / 01:08:56 / cg"
cg@37
   179
!
cg@37
   180
cg@37
   181
setupRules:rulesArg
cg@37
   182
    | checks|
cg@37
   183
cg@37
   184
    checks := rulesArg rules detect:[ :each | each name = 'Lint checks' ].
cg@36
   185
    checks rules: (checks rules reject: [ :each | each name = 'Squeak bugs' ]).
cg@36
   186
cg@37
   187
    rules := rulesArg flattened.
cg@36
   188
cg@37
   189
    "Created: / 07-08-2011 / 11:48:30 / cg"
cg@36
   190
! !
cg@36
   191
cg@36
   192
!SmalllintReportGenerator class methodsFor:'documentation'!
cg@36
   193
cg@36
   194
version
cg@36
   195
    ^ '$Header$'
cg@36
   196
!
cg@36
   197
cg@36
   198
version_CVS
cg@36
   199
    ^ '$Header$'
cg@36
   200
! !