generated from cortinico/kotlin-gradle-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsPlugin.kt
74 lines (65 loc) · 2.68 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
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.configureDependencies()
settings.configureIncludeProjects(extension)
settings.configurePlugins(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")
}
}
}
}
/**
* Add the patches and extension projects to the root project.
*/
private fun Settings.configureIncludeProjects(extension: SettingsExtension) {
include(extension.patchesProjectPath.get())
objectFactory.fileTree().from(settingsDir.resolve(extension.extensionsProjectPath.get())).matching {
it.include("**/build.gradle.kts")
}.forEach {
include(it.relativeTo(settingsDir).toPath().joinToString(":"))
}
}
/**
* Adds the required plugins to the patches and extension projects.
*/
private fun Settings.configurePlugins(extension: SettingsExtension) {
gradle.rootProject { rootProject ->
rootProject.project(extension.patchesProjectPath.get()).pluginManager.apply(PatchesPlugin::class.java)
try {
rootProject.project(extension.extensionsProjectPath.get())
} catch (e: UnknownProjectException) {
return@rootProject
}.let { extensionsProject ->
extensionsProject.subprojects { extensionProject ->
if (extensionProject.parent != extensionsProject) return@subprojects
extensionProject.pluginManager.apply(ExtensionPlugin::class.java)
}
}
}
}
}