|
| 1 | +/* |
| 2 | + * SonarSource Cloud Native Gradle Modules |
| 3 | + * Copyright (C) 2024-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonarsource.cloudnative.gradle |
| 18 | + |
| 19 | +import java.io.File |
| 20 | +import org.gradle.api.Project |
| 21 | +import org.gradle.api.internal.file.FileOperations |
| 22 | +import org.gradle.api.model.ObjectFactory |
| 23 | +import org.gradle.api.provider.MapProperty |
| 24 | +import org.gradle.api.services.BuildService |
| 25 | +import org.gradle.api.services.BuildServiceParameters |
| 26 | +import org.gradle.api.tasks.JavaExec |
| 27 | +import org.gradle.api.tasks.TaskProvider |
| 28 | +import org.gradle.kotlin.dsl.assign |
| 29 | +import org.gradle.kotlin.dsl.mapProperty |
| 30 | +import org.gradle.kotlin.dsl.register |
| 31 | +import org.gradle.kotlin.dsl.registerIfAbsent |
| 32 | + |
| 33 | +abstract class RuleApiExtension( |
| 34 | + objects: ObjectFactory, |
| 35 | + // This is a workaround for https://github.com/gradle/gradle/issues/13121 to access `FileOperations`. |
| 36 | + // After the corresponding issue is resolved, this can be simplified into an interface. |
| 37 | + val fileOperations: FileOperations, |
| 38 | +) { |
| 39 | + val languageToSonarpediaDirectory: MapProperty<String, String> = objects.mapProperty<String, String>() |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * An empty build service to serve as a synchronization point for rule-api tasks. |
| 44 | + * Because rule-api requires exclusive access to `$HOME/.sonar/rule-api/rspec`, we force tasks to never run in parallel |
| 45 | + * by configuring this service. |
| 46 | + */ |
| 47 | +abstract class RuleApiService : BuildService<BuildServiceParameters.None> |
| 48 | + |
| 49 | +fun Project.registerRuleApiUpdateTask( |
| 50 | + language: String, |
| 51 | + sonarpediaLocation: File, |
| 52 | +): TaskProvider<JavaExec> = |
| 53 | + registerRuleApiTask("ruleApiUpdate$language") { |
| 54 | + val branch = providers.gradleProperty("branch") |
| 55 | + description = "Update $language rules description" |
| 56 | + |
| 57 | + workingDir = sonarpediaLocation |
| 58 | + args( |
| 59 | + buildList { |
| 60 | + add("update") |
| 61 | + if (branch.isPresent) { |
| 62 | + add("-branch") |
| 63 | + add(branch.get()) |
| 64 | + } |
| 65 | + } |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | +fun Project.registerRuleApiGenerateTask( |
| 70 | + language: String, |
| 71 | + sonarpediaLocation: File, |
| 72 | +): TaskProvider<JavaExec> = |
| 73 | + registerRuleApiTask("ruleApiGenerateRule$language") { |
| 74 | + val rule = providers.gradleProperty("rule") |
| 75 | + val branch = providers.gradleProperty("branch") |
| 76 | + description = "Update rule description for $language" |
| 77 | + |
| 78 | + workingDir = sonarpediaLocation |
| 79 | + args( |
| 80 | + buildList { |
| 81 | + add("generate") |
| 82 | + add("-rule") |
| 83 | + add(rule.orNull ?: error("To generate rule rspec, please provide -Prule=SXXXX")) |
| 84 | + if (branch.isPresent) { |
| 85 | + add("-branch") |
| 86 | + add(branch.get()) |
| 87 | + } |
| 88 | + } |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | +private fun Project.registerRuleApiTask( |
| 93 | + name: String, |
| 94 | + configure: JavaExec.() -> Unit, |
| 95 | +): TaskProvider<JavaExec> = |
| 96 | + tasks.register<JavaExec>(name) { |
| 97 | + group = "Rule API" |
| 98 | + usesService( |
| 99 | + gradle.sharedServices.registerIfAbsent("ruleApiRepoProvider", RuleApiService::class) { |
| 100 | + // because rule-api requires exclusive access to `$HOME/.sonar/rule-api/rspec`, we force tasks to never run in parallel |
| 101 | + maxParallelUsages = 1 |
| 102 | + } |
| 103 | + ) |
| 104 | + classpath = configurations.getByName("ruleApi") |
| 105 | + mainClass = "com.sonarsource.ruleapi.Main" |
| 106 | + outputs.upToDateWhen { |
| 107 | + // As rule-api fetches data from rspec repo, we can't determine if the task is up-to-date |
| 108 | + false |
| 109 | + } |
| 110 | + |
| 111 | + configure(this) |
| 112 | + } |
0 commit comments