Skip to content

Commit

Permalink
Prefer bundled jextract binary over PATH for better compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
krakowski committed Jul 3, 2022
1 parent f31173e commit 5d145ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Since the plugin is available on [Gradle's Plugin Portal](https://plugins.gradle

```gradle
plugins {
id "io.github.krakowski.jextract" version "0.2.6"
id "io.github.krakowski.jextract" version "0.2.7"
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "io.github.krakowski"
version = "0.2.6"
version = "0.2.7"

repositories {
mavenCentral()
Expand Down
24 changes: 16 additions & 8 deletions src/main/kotlin/io/github/krakowski/jextract/JextractTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import org.gradle.api.GradleException
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Optional
import org.gradle.internal.os.OperatingSystem
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
import kotlin.NoSuchElementException
import kotlin.collections.ArrayList

abstract class JextractTask : DefaultTask() {

Expand Down Expand Up @@ -47,19 +51,23 @@ abstract class JextractTask : DefaultTask() {
val operatingSystem = OperatingSystem.current()
val executable = if (operatingSystem.isWindows) WINDOWS_EXECUTABLE else UNIX_EXECUTABLE

// Search for jextract in PATH
System.getenv(ENV_PATH).split(File.pathSeparator).forEach { pathEntry ->
val exPath = Paths.get(pathEntry, executable)
if (Files.exists(exPath)) return@findExecutable exPath
}

// Use bundled jextract binary as a fallback
// Try bundled jextract binary first to ensure compatibility with the currently used JDK
val bundledExecutable = Paths.get(toolchain.get(), "bin", executable)
if (Files.exists(bundledExecutable)) {
return bundledExecutable
}

throw GradleException("jextract binary could not be found in PATH or at ${bundledExecutable}")
// Search for jextract in PATH if JDK has no bundled binary
val pathExecutable = System.getenv(ENV_PATH)
.split(File.pathSeparator)
.map { path -> Paths.get(path, executable) }
.filter { path -> Files.exists(path) }

try {
return pathExecutable.first()
} catch (exception: NoSuchElementException) {
throw GradleException("jextract binary could not be found in PATH or at ${bundledExecutable}")
}
}

@TaskAction
Expand Down

0 comments on commit 5d145ec

Please sign in to comment.