Skip to content

Commit

Permalink
Expose addSourcePaths function for directory dependencies (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Wong <tbwong3@gmail.com>
  • Loading branch information
DSteve595 and tylerbwong authored Feb 25, 2022
1 parent e67dbc2 commit c3ab42a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
3 changes: 2 additions & 1 deletion plugin/api/0.2.2-SNAPSHOT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ package me.tylerbwong.gradle.metalava {
package me.tylerbwong.gradle.metalava.extension {

public class MetalavaExtension {
ctor public MetalavaExtension();
ctor @javax.inject.Inject public MetalavaExtension(org.gradle.api.model.ObjectFactory objects);
method public final void addSourcePaths(Object sourcePaths);
method public final me.tylerbwong.gradle.metalava.Documentation getDocumentation();
method public final boolean getEnforceCheck();
method public final String getFilename();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import me.tylerbwong.gradle.metalava.Documentation
import me.tylerbwong.gradle.metalava.Format
import me.tylerbwong.gradle.metalava.Signature
import org.gradle.api.JavaVersion
import org.gradle.api.model.ObjectFactory
import javax.inject.Inject

open class MetalavaExtension {
open class MetalavaExtension @Inject constructor(
objects: ObjectFactory,
) {
/**
* The version of Metalava to use.
*/
Expand Down Expand Up @@ -96,9 +100,22 @@ open class MetalavaExtension {
/**
* The directories to search for source files. An exception will be thrown is the named
* directories are not direct children of the project root. The default is "src".
*
* @see addSourcePaths
*/
var sourcePaths = mutableSetOf("src")

/** Internal. Do not use. */
internal val sourcePathsFileCollection = objects.fileCollection()

/**
* Add a directory (or multiple) in which to search for source files.
* The given paths are evaluated as per [org.gradle.api.Project.files].
*/
fun addSourcePaths(sourcePaths: Any) {
sourcePathsFileCollection.from(sourcePaths)
}

/**
* If the tasks should run as part of Gradle's `check` task. The default is yes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal object MetalavaSignature : MetalavaTaskContainer() {

inputs.files(compileClasspath)
inputs.files(sourceFiles)
inputs.files(extension.sourcePathsFileCollection)
inputs.property("documentation", extension.documentation)
inputs.property("format", extension.format)
inputs.property("signature", extension.signature)
Expand All @@ -48,7 +49,21 @@ internal object MetalavaSignature : MetalavaTaskContainer() {
doFirst {
val fullClasspath = (module.bootClasspath + compileClasspath).joinToString(File.pathSeparator)

val sourcePaths = listOf("--source-path") + sourceFiles.joinToString(File.pathSeparator)
val sourcePaths = (
sourceFiles +
extension.sourcePathsFileCollection.elements.get().map { it.asFile }
.also { files ->
val nonExistentDirs = files.filter { !it.exists() }
require(nonExistentDirs.isEmpty()) {
"Specified source path doesn't exist: $nonExistentDirs"
}
val nonDirectories = files.filter { !it.isDirectory }
require(nonDirectories.isEmpty()) {
"Specified source path isn't a directory: $nonDirectories"
}
}
)
.joinToString(File.pathSeparator)

val hidePackages =
extension.hiddenPackages.flatMap { listOf("--hide-package", it) }
Expand All @@ -64,8 +79,9 @@ internal object MetalavaSignature : MetalavaTaskContainer() {
"--classpath", fullClasspath,
"--output-kotlin-nulls=${extension.outputKotlinNulls.flagValue}",
"--output-default-values=${extension.outputDefaultValues.flagValue}",
"--include-signature-version=${extension.includeSignatureVersion.flagValue}"
) + sourcePaths + hidePackages + hideAnnotations
"--include-signature-version=${extension.includeSignatureVersion.flagValue}",
"--source-path", sourcePaths,
) + hidePackages + hideAnnotations

isIgnoreExitValue = true
setArgs(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ class MetalavaGradlePluginTest {
assertTrue(result.output.contains("not supported by the Metalava Gradle plugin"))
}

@Test
fun `check addSourcePaths propagates task dependency`() {
buildscriptFile = testProjectDir.newFile("build.gradle.kts").apply {
appendText(
"""
allprojects {
repositories {
google()
mavenCentral()
}
}
plugins {
`java-library`
id("me.tylerbwong.gradle.metalava")
}
val customSourceGeneratingTaskProvider = tasks.register("customSourceGeneratingTask") {
val outputDir = file("customSrc/")
outputs.dir(outputDir)
doFirst {
mkdir(outputDir)
}
}
metalava {
addSourcePaths(customSourceGeneratingTaskProvider.map { it.outputs.files })
}
"""
)
}
val result = gradleRunner
.withArguments("metalavaGenerateSignature")
.build()
assertTrue(result.tasks.any { it.path == ":customSourceGeneratingTask" })
}

@After
fun tearDown() {
buildscriptFile.delete()
Expand Down

0 comments on commit c3ab42a

Please sign in to comment.