generated from remal-gradle-plugins/template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle
547 lines (462 loc) · 21.9 KB
/
build.gradle
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import static java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.Files
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.ClassNode
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
buildscript {
String rootGroupId = project.ext.rootGroupId = "name.remal.gradle-plugins.${rootProject.name}"
String rootArtifactId = project.ext.rootArtifactId = rootProject.name
String rootSnapshotVersion = project.ext.rootSnapshotVersion = '4-SNAPSHOT'
dependencies {
//classpath("$rootGroupId:$rootArtifactId:$rootSnapshotVersion") { version { strictly(rootSnapshotVersion) } }
classpath 'name.remal.gradle-plugins.toolkit:build-logic:0.69.8'
classpath 'de.undercouch:gradle-download-task:5.6.0'
classpath 'com.tisonkun.os:os-detector-core:0.6.0'
classpath 'io.github.classgraph:classgraph:4.8.179'
classpath platform("org.ow2.asm:asm-bom:9.7.1")
classpath 'org.ow2.asm:asm-tree'
}
repositories {
mavenCentral()
gradlePluginPortal()
}
project.ext.rootBuildscriptClasspathVersionOf = { String notation ->
String version = project.buildscript.configurations.classpath
.resolvedConfiguration
.lenientConfiguration
.allModuleDependencies
.find { "${it.moduleGroup}:${it.moduleName}" == notation }
?.moduleVersion
if (version == null) {
throw new GradleException("Classpath dependency version can't be found for '${notation}'")
}
return version
}.memoize()
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
allprojects {
group = project.rootGroupId
version = project.rootSnapshotVersion
}
apply plugin: 'name.remal.toolkit.build-logic'
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
allprojects {
pluginManager.withPlugin('name.remal.sonarlint') {
sonarLint {
boolean hasCurrentSnapshotBuildscriptDependency = rootProject.buildscript.configurations.classpath.dependencies
.any { "${it.group}:${it.name}:${it.version}" == "$rootGroupId:$rootArtifactId:$rootSnapshotVersion" }
if (hasCurrentSnapshotBuildscriptDependency) {
fork.enabled = false
}
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
project.registerCrossCompileDependencyInfo(
[
name: 'sonarlint',
maxVersionNumbers: 3,
dependencyNotations: [
'org.sonarsource.sonarlint.core:sonarlint-core',
],
]
)
allprojects {
pluginManager.withPlugin('java') {
dependencies {
relocateClasses 'name.remal.gradle-plugins.toolkit:toolkit'
optional 'name.remal.gradle-api:gradle-api'
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
apply plugin: 'java-gradle-plugin'
apply plugin: 'name.remal.classes-relocation'
apply plugin: 'name.remal.generate-sources'
dependencies {
relocateClasses 'com.tisonkun.os:os-detector-core:0.6.0'
optional 'org.sonarsource.sonarlint.core:sonarlint-core:10.12.1.79852'
}
gradlePlugin {
plugins {
'name.remal.sonarlint' {
id = 'name.remal.sonarlint'
implementationClass = 'name.remal.gradle_plugins.sonarlint.SonarLintPlugin'
displayName = 'SonarLint'
description = property('repository-description')
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
tasks.named('generateJava') {
Provider<MavenPublication> publication = provider { publishing.publications.withType(MavenPublication).pluginMaven }
Provider<String> group = provider { publication.get().groupId }
Provider<String> artifactId = provider { publication.get().artifactId }
Provider<String> version = provider { publication.get().version }
inputs.property('groupId', group)
inputs.property('artifactId', artifactId)
inputs.property('version', version)
classFile(project.calculateBaseJavaPackage(), 'SonarLintPluginBuildInfo') {
it.writePackage()
it.println("")
it.writeBlock("interface ${it.simpleName}") {
it.println("String SONARLINT_PLUGIN_ID = \"${it.escapeJava(gradlePlugin.plugins.names.first())}\";")
it.println("String SONARLINT_PLUGIN_GROUP = \"${it.escapeJava(group.get())}\";")
it.println("String SONARLINT_PLUGIN_ARTIFACT_ID = \"${it.escapeJava(artifactId.get())}\";")
it.println("String SONARLINT_PLUGIN_VERSION = \"${it.escapeJava(version.get())}\";")
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Configuration allSonarArtifactsConf = project.configurations.create('allSonarArtifacts')
Map<String, String> nonReproducibleSonarDependencyVersions = new TreeMap<>().asSynchronized()
allSonarArtifactsConf.resolutionStrategy.eachDependency { DependencyResolveDetails details ->
ModuleVersionSelector target = details.target
String version = target.version
if (version == null) {
return
}
boolean isNotReproducible = version.endsWith('-SNAPSHOT') || ['(', '[', ']', ')'].any { version.contains(it) }
if (!isNotReproducible) {
return
}
String id = "${target.group}:${target.name}".toString()
nonReproducibleSonarDependencyVersions.put(id, version)
}
Closure<Configuration> createSonarArtifactsConfiguration = { String name, boolean addToIndirectApi = true ->
return project.configurations.create(name) { Configuration conf ->
conf.transitive = false
conf.extendsFrom = []
conf.dependencies.all { Dependency dep ->
allSonarArtifactsConf.dependencies.add(dep.copy())
if (dep instanceof ModuleDependency) {
dep.transitive = false
}
if (dep instanceof ExternalDependency) {
dep.version { strictly(dep.version) }
}
}
if (addToIndirectApi) {
project.configurations.indirectApi.extendsFrom(conf)
}
}
}
createSonarArtifactsConfiguration('sonarCoreArtifacts')
createSonarArtifactsConfiguration('sonarPluginArtifacts')
dependencies {
// Core:
sonarCoreArtifacts 'org.sonarsource.sonarlint.core:sonarlint-core:10.12.1.79852'
// Plugins:
// See full list of supported plugins here: https://github.com/SonarSource/sonarlint-intellij/blob/master/gradle/libs.versions.toml
sonarPluginArtifacts 'org.sonarsource.java:sonar-java-symbolic-execution-plugin:8.8.0.37665'
sonarPluginArtifacts 'org.sonarsource.java:sonar-java-plugin:8.8.0.37665'
sonarPluginArtifacts 'org.sonarsource.javascript:sonar-javascript-plugin:10.20.0.29356'
//sonarPluginArtifacts 'org.sonarsource.php:sonar-php-plugin:3.39.0.12526' // PHP projects are unlikely built with Gradle
//sonarPluginArtifacts 'org.sonarsource.python:sonar-python-plugin:4.23.0.17664' // Python projects are unlikely built with Gradle
sonarPluginArtifacts 'org.sonarsource.kotlin:sonar-kotlin-plugin:2.22.0.5972'
//sonarPluginArtifacts 'org.sonarsource.slang:sonar-ruby-plugin:1.17.1.134' // Ruby projects are unlikely built with Gradle
sonarPluginArtifacts 'org.sonarsource.html:sonar-html-plugin:3.18.0.5605'
sonarPluginArtifacts 'org.sonarsource.xml:sonar-xml-plugin:2.12.0.5749'
//sonarPluginArtifacts 'org.sonarsource.sonarlint.omnisharp:sonarlint-omnisharp-plugin:1.25.0.100205' // OmniSharp projects are unlikely built with Gradle
sonarPluginArtifacts 'org.sonarsource.text:sonar-text-plugin:2.20.0.5038'
//sonarPluginArtifacts 'org.sonarsource.slang:sonar-go-plugin:1.17.1.145' // Go projects are unlikely built with Gradle
sonarPluginArtifacts 'org.sonarsource.iac:sonar-iac-plugin:1.40.0.13983'
//sonarPluginArtifacts 'com.sonarsource.cpp:sonar-cfamily-plugin:6.60.0.76379' // C++ projects are unlikely built with Gradle
//sonarPluginArtifacts 'org.sonarsource.dotnet:sonar-csharp-plugin:10.2.0.103721' // C# projects are unlikely built with Gradle
//sonarPluginArtifacts 'com.sonarsource.dotnet:sonar-csharp-enterprise-plugin:10.2.0.103721' // C# projects are unlikely built with Gradle
sonarPluginArtifacts 'org.sonarsource.slang:sonar-scala-plugin:1.18.0.266'
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
project.ext.DownloadTask = Class.forName('de.undercouch.gradle.tasks.download.Download')
apply from: 'build-nodejs.gradle'
apply from: 'build-languages.gradle'
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
tasks.named('generateJava') { Task task ->
inputs.files(allSonarArtifactsConf).withPropertyName(allSonarArtifactsConf.name + 'Conf').optional()
classFile(project.calculateBaseJavaPackage(), 'SonarDependencies') {
it.writePackage()
it.println("")
it.writeStaticImport("lombok.AccessLevel", "PRIVATE")
it.println("")
it.writeImport("com.google.common.collect.ImmutableMap")
it.writeImport("java.util.Map")
it.writeImport("lombok.NoArgsConstructor")
it.writeImport("lombok.val")
it.println("")
it.println("@NoArgsConstructor(access = PRIVATE)")
it.writeBlock("abstract class ${it.simpleName}") {
it.println("")
it.println("private static final Map<String, SonarDependency> MAPPING = ImmutableMap.<String, SonarDependency>builder()")
configurations.sonarCoreArtifacts.dependencies.forEach { Dependency dep ->
it.println(" .put(\"${it.escapeJava(dep.name)}\", SonarDependency.builder()")
it.println(" .type(SonarDependencyType.CORE)")
it.println(" .group(\"${it.escapeJava(dep.group)}\")")
it.println(" .name(\"${it.escapeJava(dep.name)}\")")
it.println(" .version(\"${it.escapeJava(dep.version)}\")")
it.println(" .build()")
it.println(" )")
}
configurations.sonarPluginArtifacts.dependencies.forEach { Dependency dep ->
it.println(" .put(\"${it.escapeJava(dep.name)}\", SonarDependency.builder()")
it.println(" .type(SonarDependencyType.PLUGIN)")
it.println(" .group(\"${it.escapeJava(dep.group)}\")")
it.println(" .name(\"${it.escapeJava(dep.name)}\")")
it.println(" .version(\"${it.escapeJava(dep.version)}\")")
it.println(" .build()")
it.println(" )")
}
it.println(" .build();")
it.println("")
it.writeBlock("public static Map<String, SonarDependency> getSonarDependencies()") {
it.println("return MAPPING;")
}
it.println("")
it.writeBlock("public static SonarDependency getSonarDependency(String id)") {
it.println("val dependency = getSonarDependencies().get(id);")
it.writeBlock("if (dependency == null)") {
it.println("throw new IllegalStateException(\"Sonar dependency is not registered: \" + id);")
}
it.println("return dependency;")
}
}
}
classFile(project.calculateBaseJavaPackage(), 'ResolvedNonReproducibleSonarDependencies') {
it.writePackage()
it.println("")
it.writeStaticImport("lombok.AccessLevel", "PRIVATE")
it.println("")
it.writeImport("com.google.common.collect.ImmutableMap")
it.writeImport("java.util.Map")
it.writeImport("javax.annotation.Nullable")
it.writeImport("lombok.NoArgsConstructor")
it.println("")
it.println("@NoArgsConstructor(access = PRIVATE)")
it.writeBlock("abstract class ${it.simpleName}") {
it.println("")
it.println("private static final Map<String, SonarDependency> MAPPING = ImmutableMap.<String, SonarDependency>builder()")
allSonarArtifactsConf.resolvedConfiguration.lenientConfiguration.allModuleDependencies.forEach { ResolvedDependency dep ->
String depId = "${dep.moduleGroup}:${dep.moduleName}".toString()
String nonReproducibleVersion = nonReproducibleSonarDependencyVersions[depId]
if (nonReproducibleVersion != null) {
it.println(" .put(\"${it.escapeJava("${depId}:${nonReproducibleVersion}")}\", SonarDependency.builder()")
it.println(" .type(SonarDependencyType.OTHER)")
it.println(" .group(\"${it.escapeJava(dep.moduleGroup)}\")")
it.println(" .name(\"${it.escapeJava(dep.moduleName)}\")")
it.println(" .version(\"${it.escapeJava(dep.moduleVersion)}\")")
it.println(" .build()")
it.println(" )")
}
}
it.println(" .build();")
it.println("")
it.writeBlock("public static Map<String, SonarDependency> getResolvedNonReproducibleSonarDependencies()") {
it.println("return MAPPING;")
}
it.println("")
it.println("@Nullable")
it.writeBlock("public static SonarDependency getResolvedNonReproducibleSonarDependency(String notation)") {
it.println("return getResolvedNonReproducibleSonarDependencies().get(notation);")
}
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
tasks.named('processReadme') {
readmeFileProcessors.add(
{ File readmeFile ->
String content = readmeFile.getText('UTF-8')
content = content.replaceAll(
/<!--\s*(sonar-plugins-list)\s*-->[\s\S]*?<!--\s*\/\s*\1\s*-->/
) {
String tag = it[1]
logger.lifecycle(' Handling {}', tag)
StringBuilder listContent = new StringBuilder()
Map<String, String> languageDocPaths = [
'js': 'javascript',
'ts': 'typescript',
]
rootProject.pluginLanguageInfos.get()
.toSorted { it.key }
.forEach { info ->
String languageDocPath = languageDocPaths[info.key] ?: info.key
URL url = new URL("https://rules.sonarsource.com/${languageDocPath}/")
HttpURLConnection connection = url.openConnection()
connection.useCaches = false
connection.connectTimeout = 5_000
connection.readTimeout = 60_000
connection.connect()
try {
int responseCode = connection.responseCode
if (responseCode == 200) {
listContent.append("\n* [${info.name}]($url)")
} else if (responseCode == 404) {
listContent.append("\n* ${info.name}")
} else {
throw new GradleException("$url returned unexpected status: $responseCode")
}
} finally {
connection.disconnect()
}
}
return "<!--$tag-->\n$listContent\n\n<!--/$tag-->"
}
readmeFile.setText(content, 'UTF-8')
}
)
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
allprojects {
/*
if (gradle.startParameter.taskNames.isEmpty()
|| gradle.startParameter.taskNames == ['processIdeaSettings']
|| gradle.startParameter.taskNames == ['dependencies']
|| gradle.startParameter.taskNames == ['help']
) {
return
}
*/
Attribute<String> artifactType = Attribute.of('artifactType', String)
Attribute<Boolean> unprocessedSonar = Attribute.of('unprocessedSonar', Boolean)
dependencies {
attributesSchema {
attribute(unprocessedSonar)
}
artifactTypes.getByName('jar') {
attributes.attribute(unprocessedSonar, false)
}
}
configurations
.matching { !it.name.startsWith('sonar') }
.matching { !it.name.startsWith('allSonar') }
.all { Configuration conf ->
if (conf.state != Configuration.State.UNRESOLVED
|| !conf.canBeResolved
) {
return
}
Closure<Boolean> isSonarGroup = { String group ->
return group == 'org.sonarsource'
|| group.startsWith('org.sonarsource.')
}
conf.dependencies.withType(ExternalModuleDependency).configureEach { ExternalModuleDependency dep ->
if (isSonarGroup(dep.group)) {
dep.exclude(group: 'com.google.guava')
}
}
conf.resolutionStrategy.dependencySubstitution { DependencySubstitutions substitutions ->
substitutions.all { DependencySubstitution substitution ->
if (!(substitution.requested instanceof ModuleComponentSelector)) {
return
}
ModuleComponentSelector requested = (ModuleComponentSelector) substitution.requested
if (isSonarGroup(requested.group)) {
ComponentSelector target = substitutions.variant(requested) { VariantSelectionDetails details ->
details.attributes {
attribute(unprocessedSonar, true)
}
}
substitution.useTarget(target)
}
}
}
}
dependencies {
registerTransform(ProcessSonar) {
from.attribute(unprocessedSonar, false).attribute(artifactType, 'jar')
to.attribute(unprocessedSonar, true).attribute(artifactType, 'jar')
}
}
}
abstract class ProcessSonar implements TransformAction<ProcessSonarParameters> {
interface ProcessSonarParameters extends TransformParameters {}
@InputArtifact
abstract Provider<FileSystemLocation> getInputArtifact()
@Override
void transform(TransformOutputs outputs) {
File file = inputArtifact.get().asFile
//println file
File outputFile = outputs.file(file.name)
if (outputFile.exists() && outputFile.lastModified() >= rootProject.buildFile.lastModified()) {
return
}
Files.createDirectories(outputFile.parentFile.toPath())
new ZipInputStream(new FileInputStream(file), UTF_8).withCloseable { ZipInputStream inputStream ->
new ZipOutputStream(new FileOutputStream(outputFile), UTF_8).withCloseable { ZipOutputStream outputStream ->
while (true) {
ZipEntry zipEntry = inputStream.nextEntry
if (zipEntry == null) {
break
}
if (zipEntry.directory) {
continue
}
if (zipEntry.name.contains('/shaded/')) {
continue
}
if (!zipEntry.name.contains('sonar')
&& !zipEntry.name.startsWith('META-INF/')
) {
continue
}
outputStream.putNextEntry(zipEntry)
if (!zipEntry.name.endsWith('.class')) {
outputStream << inputStream
continue
}
if (!zipEntry.name.startsWith('org/sonarsource/')
&& !zipEntry.name.startsWith('org/sonar/')
) {
outputStream << inputStream
continue
}
ClassNode classNode = new ClassNode()
new ClassReader(inputStream).accept(classNode, 0)
Closure processDeprecatedAnnotations = { node ->
// remove parameters of @Deprecated
node.visibleAnnotations
?.findAll { it.desc == 'Ljava/lang/Deprecated;' && it.values?.size() }
?.forEach {
it.values = []
//println classNode.name
}
}
processDeprecatedAnnotations(classNode)
classNode.fields?.forEach { processDeprecatedAnnotations(it) }
classNode.methods?.forEach { processDeprecatedAnnotations(it) }
ClassWriter classWriter = new ClassWriter(0)
classNode.accept(classWriter)
outputStream << classWriter.toByteArray()
}
}
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
allprojects {
dependencies.components {
withModule('org.sonarsource.git.blame:git-files-blame', TargetJvmVersionRule) { params(8) }
}
}
@CacheableRule
abstract class TargetJvmVersionRule implements ComponentMetadataRule {
final Integer jvmVersion
@Inject
TargetJvmVersionRule(Integer jvmVersion) {
this.jvmVersion = jvmVersion
}
void execute(ComponentMetadataContext context) {
context.details.allVariants {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
}
}
}
@Override
String toString() {
return "${getClass().simpleName}[$jvmVersion]"
}
}