generated from cortinico/kotlin-gradle-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsPlugin.kt
95 lines (80 loc) · 3.29 KB
/
SettingsPlugin.kt
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
package app.revanced.patches.gradle
import org.gradle.api.Plugin
import org.gradle.api.UnknownProjectException
import org.gradle.api.initialization.Settings
import org.gradle.api.model.ObjectFactory
import java.net.URI
import javax.inject.Inject
@Suppress("unused")
abstract class SettingsPlugin @Inject constructor(
private val objectFactory: ObjectFactory,
) : Plugin<Settings> {
override fun apply(settings: Settings) {
val extension = settings.extensions.create("settings", SettingsExtension::class.java)
settings.gradle.settingsEvaluated {
settings.gradle.sharedServices.registerIfAbsent(
"settingsExtensionProvider",
SettingsExtensionProvider::class.java,
) {
it.parameters.apply {
defaultNamespace = extension.extensions.defaultNamespace
proguardFiles = extension.extensions.proguardFiles
}
}
}
settings.configureDependencies()
settings.configureProjects(extension)
}
/**
* Add required repositories.
*/
private fun Settings.configureDependencies() {
@Suppress("UnstableApiUsage")
dependencyResolutionManagement.repositories.apply {
mavenCentral()
google()
maven { repository ->
// A repository must be specified. "registry" is a dummy.
repository.url = URI("https://maven.pkg.github.com/revanced/registry")
repository.credentials {
it.username = providers.gradleProperty("gpr.user").orNull ?: System.getenv("GITHUB_ACTOR")
it.password = providers.gradleProperty("gpr.key").orNull ?: System.getenv("GITHUB_TOKEN")
}
}
}
}
/**
* Adds the required plugins to the patches and extension projects.
*/
private fun Settings.configureProjects(extension: SettingsExtension) {
// region Include the projects
val extensionsProjectPath = extension.extensions.projectsPath ?: return
objectFactory.fileTree().from(rootDir.resolve(extensionsProjectPath)).matching {
it.include("**/build.gradle.kts")
}.forEach {
include(it.relativeTo(rootDir).toPath().joinToString(":"))
}
include(extension.patchesProjectPath)
// endregion
// region Apply the plugins
gradle.rootProject { rootProject ->
val extensionsProject = try {
rootProject.project(extensionsProjectPath)
} catch (e: UnknownProjectException) {
return@rootProject
}
extensionsProject.subprojects { extensionProject ->
if (
extensionProject.buildFile.exists() &&
!extensionProject.parent!!.plugins.hasPlugin(ExtensionPlugin::class.java)
) {
extensionProject.pluginManager.apply(ExtensionPlugin::class.java)
}
}
// Needs to be applied after the extension plugin
// so that their extensionConfiguration is available for consumption.
rootProject.project(extension.patchesProjectPath).pluginManager.apply(PatchesPlugin::class.java)
}
// endregion
}
}