Skip to content

Commit

Permalink
feat: Add ability to globally configure extension projects
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Nov 26, 2024
1 parent c0ef614 commit 9f18768
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 32 deletions.
54 changes: 37 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ ReVanced Patches Gradle plugin configures a project to develop ReVanced Patches.
For that, the plugin provides:

- The [settings plugin](src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt):
Applied to the `settings.gradle.kts` file, configures the project repositories and subprojects
Applied to the `settings.gradle.kts` file, configures the project repositories and subprojects
- The [patches plugin](src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt):
Applied to the patches subproject by the settings plugin
Applied to the patches subproject by the settings plugin
- The [extension plugin](src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt):
Applied to extension subprojects by the settings plugin
Applied to extension subprojects by the settings plugin

> [!CAUTION]
> This plugin is not stable yet and likely to change due to lacking experience with Gradle plugins.
Expand All @@ -97,18 +97,36 @@ pluginManagement {
gradlePluginPortal()
google()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/revanced/registry")
credentials {
username = providers.gradleProperty("gpr.user")
password = providers.gradleProperty("gpr.key")
}
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/revanced/registry")
credentials {
username = providers.gradleProperty("gpr.user")
password = providers.gradleProperty("gpr.key")
}
}
}
}

plugins {
id("app.revanced.patches") version "<version>"
id("app.revanced.patches") version "<version>"
}

// This block is optional and can be used to configure the patches and extension projects.
settings {
// "patches" is the default.
patchesProjectPath = "patches"

extensions {
// The path containing the extension projects. "extensions" is the default.
projectsPath = "extensions"

// A default namespace for extension projects. null is the default.
defaultNamespace = "app.revanced.extension"

// Proguard files relative to the extension project.
// By default, isMinifyEnabled is false, unless a ProGuard file is added.
proguardFiles("../proguard-rules.pro")
}
}
```

Expand All @@ -132,15 +150,15 @@ patches {
> [!NOTE]
> By default, the plugin expects the patches project to be in the `patches` directory.
Create the extension project and configure the `build.gradle.kts` file:
Create the extension project and add an empty `build.gradle.kts` file.
Unless the `build.gradle.kts` file is empty, the plugin will not recognize the extension project.
By default, the extension name will be inferred from the relative path to the extension project.
For example, the extension name for the `extensions/extension` project will be `extensions/extension.rve`.
To set an extension name explicitly, add the following to the `build.gradle.kts` file:

```kotlin
extension {
name = "extensions/extension.rve"
}

android {
namespace = "app.revanced.extension"
name = "extensions/extension.rve"
}
```

Expand All @@ -162,7 +180,9 @@ To build ReVanced Patches Gradle plugin, follow these steps:
## 📜 Licence

ReVanced Patches Gradle plugin is licensed under the GPLv3 license.
Please see the [license file](LICENSE) for more information. [tl;dr](https://www.tldrlegal.com/license/gnu-general-public-license-v3-gpl-3) you may copy, distribute and modify
Please see the [license file](LICENSE) for more
information. [tl;dr](https://www.tldrlegal.com/license/gnu-general-public-license-v3-gpl-3) you may copy, distribute and
modify
ReVanced Patches Gradle plugin as long as you track changes/dates in source files.
Any modifications to ReVanced Patches Gradle plugin must also be made available under the GPL,
along with build & install instructions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.gradle.parallel = true
org.gradle.caching = true
kotlin.code.style = official
version = 1.0.0-dev.6
org.gradle.parallel=true
org.gradle.caching=true
kotlin.code.style=official
version=1.0.0-dev.6
16 changes: 9 additions & 7 deletions src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ abstract class ExtensionPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create("extension", ExtensionExtension::class.java)

project.configureAndroid()
val settingsExtensionProvider = project.gradle.sharedServices.registrations
.findByName("settingsExtensionProvider")!!.service.get() as SettingsExtensionProvider

project.configureAndroid(settingsExtensionProvider)
project.configureArtifactSharing(extension)
}

Expand Down Expand Up @@ -64,15 +67,16 @@ abstract class ExtensionPlugin : Plugin<Project> {
/**
* Set up the Android plugin for the extension project.
*/
private fun Project.configureAndroid() {
private fun Project.configureAndroid(settingsExtensionProvider: SettingsExtensionProvider) {
pluginManager.apply {
apply(AppPlugin::class.java)
apply(KotlinAndroidPluginWrapper::class.java)
}

extensions.configure(BaseAppModuleExtension::class.java) {
it.apply {
compileSdk = 33
compileSdk = 34
namespace = settingsExtensionProvider.parameters.defaultNamespace

defaultConfig {
minSdk = 23
Expand All @@ -81,13 +85,11 @@ abstract class ExtensionPlugin : Plugin<Project> {

buildTypes {
release {
// If this were true by default, and no proguard files would be present,
// no dex file would be generated.
isMinifyEnabled = false
isMinifyEnabled = settingsExtensionProvider.parameters.proguardFiles.isNotEmpty()

proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
*settingsExtensionProvider.parameters.proguardFiles.toTypedArray(),
)
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@ open class SettingsExtension {
val extensions = ExtensionsExtension()

fun extensions(block: ExtensionsExtension.() -> Unit) {
ExtensionsExtension().apply(block)
extensions.apply(block)
}

class ExtensionsExtension {
/**
* The path to the project containing the extension projects.
* The path containing the extension projects.
*/
var projectPath: String? = "extensions"
var projectsPath: String? = "extensions"

/**
* The default namespace for the extension projects.
*/
var defaultNamespace: String? = null

internal val proguardFiles = mutableSetOf<String>()

/**
* Add proguard files to the extension projects relative to the project root.
* Minification will be enabled if at least one file is provided.
*
* @param files The proguard files to add.
*/
fun proguardFiles(vararg files: String) {
proguardFiles += files
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.revanced.patches.gradle

import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters

abstract class SettingsExtensionProvider :
BuildService<SettingsExtensionProvider.Params>,
BuildServiceParameters {
interface Params : BuildServiceParameters {
var defaultNamespace: String?
var proguardFiles: Set<String>
}
}
14 changes: 13 additions & 1 deletion src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ abstract class SettingsPlugin @Inject constructor(
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)
}
Expand Down Expand Up @@ -43,7 +55,7 @@ abstract class SettingsPlugin @Inject constructor(
private fun Settings.configureProjects(extension: SettingsExtension) {
// region Include the projects

val extensionsProjectPath = extension.extensions.projectPath ?: return
val extensionsProjectPath = extension.extensions.projectsPath ?: return

objectFactory.fileTree().from(rootDir.resolve(extensionsProjectPath)).matching {
it.include("**/build.gradle.kts")
Expand Down

0 comments on commit 9f18768

Please sign in to comment.