diff --git a/src/main/kotlin/app/revanced/patches/gradle/ExtensionExtension.kt b/src/main/kotlin/app/revanced/patches/gradle/ExtensionExtension.kt index 5ce16c3..e6811e6 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/ExtensionExtension.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/ExtensionExtension.kt @@ -1,13 +1,11 @@ package app.revanced.patches.gradle -import org.gradle.api.provider.Property - -abstract class ExtensionExtension { +open class ExtensionExtension { /** * The name of the extension. * * The name is the full resource path of the extension in the final patches file. * Example: `extensions/extension.rve`. */ - abstract val name: Property + var name: String? = null } diff --git a/src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt b/src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt index c28c24e..d5db552 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt @@ -39,10 +39,16 @@ abstract class ExtensionPlugin : Plugin { dependsOn(dexTask) + val extensionName = if (extension.name != null) { + Path(extension.name!!) + } else { + projectDir.resolveSibling(project.name + ".rve").relativeTo(rootDir).toPath() + } + from(dexTask.outputs.files.asFileTree.matching { include("**/*.dex") }) - into(layout.buildDirectory.dir("revanced/${Path(extension.name.get()).parent.pathString}")) + into(layout.buildDirectory.dir("revanced/${extensionName.parent.pathString}")) - rename { Path(extension.name.get()).fileName.toString() } + rename { extensionName.fileName.toString() } } configurations.create("extensionConfiguration").apply { diff --git a/src/main/kotlin/app/revanced/patches/gradle/PatchesExtension.kt b/src/main/kotlin/app/revanced/patches/gradle/PatchesExtension.kt index 4470d62..74a8037 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/PatchesExtension.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/PatchesExtension.kt @@ -1,12 +1,6 @@ package app.revanced.patches.gradle -import org.gradle.api.Project -import org.gradle.api.model.ObjectFactory -import org.gradle.api.provider.Property -import javax.inject.Inject - -@Suppress("unused") -abstract class PatchesExtension @Inject constructor(objectFactory: ObjectFactory) { +open class PatchesExtension { /** * The path to the extensions project relative to the root project. * @@ -14,37 +8,29 @@ abstract class PatchesExtension @Inject constructor(objectFactory: ObjectFactory * * Defaults to `:extensions`. */ - abstract val extensionsProjectPath: Property + var extensionsProjectPath: String? = ":extensions" - internal val about = objectFactory.newInstance(About::class.java) + /** + * About information for the project. + */ + val about = About() fun about(block: About.() -> Unit) { about.block() } - init { - extensionsProjectPath.convention(":extensions") - } - /** * About information for the project. * * Used by the patches plugin to create the manifest file and set up the publication of the patches project. */ - abstract class About @Inject constructor(project: Project) { - abstract val name: Property - abstract val description: Property - abstract val source: Property - abstract val author: Property - abstract val contact: Property - abstract val website: Property - abstract val license: Property - internal abstract val version: Property - internal abstract val timestamp: Property - - init { - version.convention(project.version.toString()) - timestamp.convention(System.currentTimeMillis()) - } + class About { + var name: String? = null + var description: String? = null + var source: String? = null + var author: String? = null + var contact: String? = null + var website: String? = null + var license: String? = null } } diff --git a/src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt b/src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt index 9a3184a..4fe9a4d 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt @@ -186,13 +186,13 @@ abstract class PatchesPlugin : Plugin { */ private fun Project.configureConsumeExtensions(patchesExtension: PatchesExtension) { val extensionsProject = try { - project(patchesExtension.extensionsProjectPath.get()) + project(patchesExtension.extensionsProjectPath ?: return) } catch (e: UnknownProjectException) { return } - val extensionProjects = extensionsProject.subprojects.filter { - it.parent == extensionsProject + val extensionProjects = extensionsProject.subprojects.filter { extensionProject -> + extensionProject.plugins.hasPlugin(ExtensionPlugin::class.java) } val extensionsDependencyScopeConfiguration = @@ -232,8 +232,8 @@ private fun Project.configureJarTask(patchesExtension: PatchesExtension) { it.manifest.apply { attributes["Name"] = patchesExtension.about.name attributes["Description"] = patchesExtension.about.description - attributes["Version"] = patchesExtension.about.version - attributes["Timestamp"] = patchesExtension.about.timestamp + attributes["Version"] = project.version.toString() + attributes["Timestamp"] = System.currentTimeMillis().toString() attributes["Source"] = patchesExtension.about.source attributes["Author"] = patchesExtension.about.author attributes["Contact"] = patchesExtension.about.contact diff --git a/src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt b/src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt index 6692fda..1940596 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt @@ -1,30 +1,23 @@ package app.revanced.patches.gradle -import org.gradle.api.provider.Property - -abstract class SettingsExtension { +open class SettingsExtension { /** - * The path to the patches project relative to the root project. - * - * Used by the settings plugin to include the patches project - * and apply the patches plugin to the patches project. - * - * Defaults to `patches`. + * The path to the patches project. */ - abstract val patchesProjectPath: Property + var patchesProjectPath = "patches" - /** - * The path to the extensions project relative to the root project. - * - * Used by the settings plugin to include the extensions project - * and apply the extensions plugin to the extensions project. - * - * Defaults to `extensions`. - */ - abstract val extensionsProjectPath: Property + // Need to rename, otherwise it will conflict with the `getExtensions` property from ExtensionAware. + @get:JvmName("getExtensionsExtension") + val extensions = ExtensionsExtension() + + fun extensions(block: ExtensionsExtension.() -> Unit) { + ExtensionsExtension().apply(block) + } - init { - patchesProjectPath.convention("patches") - extensionsProjectPath.convention("extensions") + class ExtensionsExtension { + /** + * The path to the project containing the extension projects. + */ + var projectPath: String? = "extensions" } } diff --git a/src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt b/src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt index 94ff520..1833117 100644 --- a/src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt +++ b/src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt @@ -15,8 +15,7 @@ abstract class SettingsPlugin @Inject constructor( val extension = settings.extensions.create("settings", SettingsExtension::class.java) settings.configureDependencies() - settings.configureIncludeProjects(extension) - settings.configurePlugins(extension) + settings.configureProjects(extension) } /** @@ -39,36 +38,46 @@ abstract class SettingsPlugin @Inject constructor( } /** - * Add the patches and extension projects to the root project. + * Adds the required plugins to the patches and extension projects. */ - private fun Settings.configureIncludeProjects(extension: SettingsExtension) { - include(extension.patchesProjectPath.get()) + private fun Settings.configureProjects(extension: SettingsExtension) { + // region Include the projects + + val extensionsProjectPath = extension.extensions.projectPath ?: return - objectFactory.fileTree().from(settingsDir.resolve(extension.extensionsProjectPath.get())).matching { + objectFactory.fileTree().from(rootDir.resolve(extensionsProjectPath)).matching { it.include("**/build.gradle.kts") }.forEach { - include(it.relativeTo(settingsDir).toPath().joinToString(":")) + include(it.relativeTo(rootDir).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) + include(extension.patchesProjectPath) - try { - rootProject.project(extension.extensionsProjectPath.get()) + // endregion + + // region Apply the plugins + + gradle.rootProject { rootProject -> + val extensionsProject = try { + rootProject.project(extensionsProjectPath) } catch (e: UnknownProjectException) { return@rootProject - }.let { extensionsProject -> - extensionsProject.subprojects { extensionProject -> - if (extensionProject.parent != extensionsProject) return@subprojects + } + 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 } }