-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate GradleConnectorService into interface and infrastructure imp…
…lementation
- Loading branch information
Showing
4 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ter/src/main/kotlin/ch/addere/dga/importer/application/service/GradleProjectLoaderImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 5 additions & 48 deletions
53
importer/src/main/kotlin/ch/addere/dga/importer/domain/service/GradleConnectorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,12 @@ | ||
package ch.addere.dga.importer.domain.service | ||
|
||
import ch.addere.dga.dependencymodel.DependencyModel | ||
import org.gradle.tooling.GradleConnector | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.absolutePathString | ||
import kotlin.io.path.createTempDirectory | ||
|
||
class GradleConnectorService { | ||
interface GradleConnectorService { | ||
|
||
private val scriptFileName = "init.gradle.kts" | ||
private val scriptParentDir = "dga" | ||
|
||
|
||
fun connectToGradle(gradleProjectFolder: File): DependencyModel { | ||
|
||
val pathToInitScript = createInitScript() | ||
|
||
val connector = GradleConnector.newConnector() | ||
connector.forProjectDirectory(gradleProjectFolder) | ||
|
||
var model: DependencyModel? | ||
|
||
connector.connect().use { con -> | ||
|
||
val customModelBuilder = con.model(DependencyModel::class.java) | ||
customModelBuilder.withArguments("--init-script", pathToInitScript.absolutePathString()) | ||
|
||
model = customModelBuilder.get() | ||
} | ||
|
||
deleteInitScript(pathToInitScript) | ||
return model!! | ||
} | ||
|
||
private fun createInitScript(): Path { | ||
val osTempDir = createTempDirectory().parent | ||
val scriptDir = Files.createDirectories(Path("$osTempDir/$scriptParentDir/")) | ||
val initScript = File("$scriptDir/$scriptFileName") | ||
initScript.writeText(javaClass.getResource("/$scriptFileName")!!.readText()) | ||
return initScript.toPath() | ||
} | ||
|
||
private fun deleteInitScript(pathToInitScript: Path) { | ||
if (pathToInitScript.endsWith(scriptFileName)) { | ||
Files.delete(pathToInitScript) | ||
} | ||
if (pathToInitScript.parent.endsWith(scriptParentDir)) { | ||
Files.delete(pathToInitScript.parent) | ||
} | ||
} | ||
/** | ||
* Connect to a Gradle project and provide dependency data model. | ||
*/ | ||
fun connectToGradle(gradleProjectFolder: File): DependencyModel | ||
} |
51 changes: 51 additions & 0 deletions
51
importer/src/main/kotlin/ch/addere/dga/importer/infrastructure/GradleConnectorServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ch.addere.dga.importer.infrastructure | ||
|
||
import ch.addere.dga.dependencymodel.DependencyModel | ||
import ch.addere.dga.importer.domain.service.GradleConnectorService | ||
import org.gradle.tooling.GradleConnector | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.absolutePathString | ||
import kotlin.io.path.createTempDirectory | ||
|
||
class GradleConnectorServiceImpl : GradleConnectorService { | ||
|
||
private val scriptFileName = "init.gradle.kts" | ||
private val scriptParentDir = "dga" | ||
|
||
override fun connectToGradle(gradleProjectFolder: File): DependencyModel { | ||
|
||
val pathToInitScript = createInitScript() | ||
|
||
val connector = GradleConnector.newConnector() | ||
connector.forProjectDirectory(gradleProjectFolder) | ||
|
||
val dependencyModel: DependencyModel = connector.connect().use { con -> | ||
val customModelBuilder = con.model(DependencyModel::class.java) | ||
customModelBuilder.withArguments("--init-script", pathToInitScript.absolutePathString()) | ||
customModelBuilder.get() | ||
} | ||
|
||
deleteInitScript(pathToInitScript) | ||
return dependencyModel | ||
} | ||
|
||
private fun createInitScript(): Path { | ||
val osTempDir = createTempDirectory().parent | ||
val scriptDir = Files.createDirectories(Path("$osTempDir/$scriptParentDir/")) | ||
val initScript = File("$scriptDir/$scriptFileName") | ||
initScript.writeText(javaClass.getResource("/$scriptFileName")!!.readText()) | ||
return initScript.toPath() | ||
} | ||
|
||
private fun deleteInitScript(pathToInitScript: Path) { | ||
if (pathToInitScript.endsWith(scriptFileName)) { | ||
Files.delete(pathToInitScript) | ||
} | ||
if (pathToInitScript.parent.endsWith(scriptParentDir)) { | ||
Files.delete(pathToInitScript.parent) | ||
} | ||
} | ||
} |