File: binarycompatibility.gradle

package info (click to toggle)
groovy 2.4.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 30,708 kB
  • sloc: java: 157,114; xml: 728; sh: 357; makefile: 64; javascript: 15
file content (213 lines) | stat: -rw-r--r-- 7,947 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
import groovy.text.markup.MarkupTemplateEngine
import groovy.text.markup.TemplateConfiguration

import java.lang.reflect.Modifier

buildscript {
    // this block should not be necessary, but for some reason it fails without!
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'me.champeau.gradle:japicmp-gradle-plugin:0.1.1'
    }
}

task checkBinaryCompatibility {
    description = 'Generates binary compatibility reports'
}
check.dependsOn(checkBinaryCompatibility)

def excludeModules = ['performance', 'tests-vm8']
//def excludeModules = []

Set projectsToCheck = allprojects.findAll{ !(it.name in excludeModules) }

if (JavaVersion.current().isJava7Compatible()) {
    allprojects {
        if (project in projectsToCheck) {
            apply plugin: 'me.champeau.gradle.japicmp'
        }
    }

    def referenceMinorVersion = '2.4.15'

    def prettyPrint = { classOrMethod ->
        "${Modifier.toString(classOrMethod.get()?.modifiers)} ${classOrMethod.get()?.longName}"
    }

    def reportGenerator = { model ->
        outputProcessor {

            def skipClass = { c ->
                c.fullyQualifiedName =~ /\$[0-9]+$/ || // skip AIC
                c.fullyQualifiedName.startsWith('org.codehaus.groovy.runtime.dgm$') ||
                        c.fullyQualifiedName.contains('_closure')
            }
            def skipMethod = { c, m -> skipClass(c) || m.name =~ /access\$[0-9]+/ }
            def violations = [:].withDefault {
                // key = class name
                // value = map of violations
                [:].withDefault { [] }
            }
            removedConstructor { c, m ->
                if (!skipMethod(c, m)) {
                    def level = Modifier.isPrivate(m.oldConstructor.get()?.modifiers) ? 'info' : 'error'
                    violations[c.fullyQualifiedName][level] << "Constructor ${prettyPrint(m.oldConstructor)} has been removed"
                }
            }
            removedMethod { c, m ->
                if (!skipMethod(c, m)) {
                    def level = m.name.startsWith('super$') ? 'warning' : 'error'
                    violations[c.fullyQualifiedName][level] << "Method ${m.name} has been removed"
                }
            }
            removedClass { c ->
                if (!skipClass(c)) {
                    violations[c.fullyQualifiedName].error << "Class has been removed"
                }
            }

            modifiedMethod { c, m ->
                if (!skipMethod(c, m)) {
                    violations[c.fullyQualifiedName].warning << """<p>Method ${m.name} has been modified</p>
<p>From <pre>${prettyPrint(m.oldMethod)}</pre> to <pre>${prettyPrint(m.newMethod)}</pre></p>"""
                }
            }

            modifiedConstructor { c, m ->
                if (!skipMethod(c, m)) {
                    violations[c.fullyQualifiedName].warning << """<p>Constructor ${m.name} has been modified</p>
<p>From <pre>${prettyPrint(m.oldConstructor)}</pre> to <pre>${prettyPrint(m.newConstructor)}</pre></p>"""
                }
            }

            modifiedClass { c ->
                if (!skipClass(c)) {
                    def level = c.binaryCompatible?'info':'error'
                    def message = "Class ${c.fullyQualifiedName} has been modified"
                    violations[c.fullyQualifiedName][level] << message
                }
            }

            newClass { c ->
                if (!skipClass(c)) {
                    violations[c.fullyQualifiedName].info << "Class has been added"
                }
            }
            newMethod { c, m ->
                if (!skipMethod(c, m)) {
                    violations[c.fullyQualifiedName].info << """<p>Method ${m.name} has been added</p>
<p>Signature: <pre>${prettyPrint(m.newMethod)}</pre></p>"""
                }
            }

            after {
                model.violations = violations
            }
        }
    }

// using a global engine for all tasks in order to increase performance
    def configDir = file("$rootProject.projectDir/config/binarycompatibility")
    def templateFile = 'binarycompat-report.groovy'
    def templateConfiguration = new TemplateConfiguration()
    templateConfiguration.with {
        autoIndent = true
        autoNewLine = true
    }
    def engine = new MarkupTemplateEngine(this.class.classLoader, configDir, templateConfiguration)

    task japicmpAll(type: me.champeau.gradle.ArtifactJapicmpTask) {
        dependsOn jarAll
        baseline = "org.codehaus.groovy:groovy-all:${referenceMinorVersion}@jar"
        to = jarAll.archivePath
        accessModifier = 'protected'
        onlyModified = true
        failOnModification = false
        txtOutputFile = file("$buildDir/reports/japi.txt")

        doFirst {
            classpath = projectsToCheck.configurations.japicmp.files.flatten()
        }

        def htmlReportFile = file("${buildDir}/reports/binary-compat-${project.name}-all.html")
        inputs.file file("$configDir/$templateFile")
        inputs.file templateFile
        outputs.file htmlReportFile

        def model = [title   : "Binary compatibility report for ${project.name}",
                     project : project,
                     baseline: baseline,
                     archive : to.name]
        outputProcessor(reportGenerator.curry(model))

        doLast {
            htmlReportFile.withWriter('utf-8') { wrt ->
                engine.createTemplateByPath(templateFile).make(model).writeTo(wrt)
            }
        }
    }

    allprojects {
        if (project in projectsToCheck) {

            dependencies  {
                japicmp files(rootProject.jar.archivePath)
            }

            task japicmp(type: me.champeau.gradle.ArtifactJapicmpTask) {
                dependsOn replaceJarWithJarJar
                baseline = "org.codehaus.groovy:${project.name}:${referenceMinorVersion}@jar"
                to = jar.archivePath
                accessModifier = 'protected'
                onlyModified = true
                failOnModification = false
                txtOutputFile = file("$buildDir/reports/japi.txt")

                def htmlReportFile = file("${buildDir}/reports/binary-compat-${project.name}.html")
                inputs.file file("$configDir/$templateFile")
                inputs.file templateFile
                outputs.file htmlReportFile

                def model = [title   : "Binary compatibility report for ${project.name}",
                             project : project,
                             baseline: baseline,
                             archive : to.name]
                outputProcessor(reportGenerator.curry(model))

                doLast {
                    htmlReportFile.withWriter('utf-8') { wrt ->
                        engine.createTemplateByPath(templateFile).make(model).writeTo(wrt)
                    }
                }

            }
        }
    }

    allprojects {
        tasks.withType(me.champeau.gradle.ArtifactJapicmpTask) { task ->
            checkBinaryCompatibility.dependsOn(task)
        }
    }
}