Skip to content

Commit

Permalink
Separate GradleConnectorService into interface and infrastructure imp…
Browse files Browse the repository at this point in the history
…lementation
  • Loading branch information
ryru committed Sep 8, 2024
1 parent 3442522 commit 3c73fdb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import ch.addere.dga.importer.application.service.GradleProjectLoader
import ch.addere.dga.importer.application.service.GradleProjectLoaderImpl
import ch.addere.dga.importer.domain.service.GradleConnectorService
import ch.addere.dga.importer.domain.service.ModelImporterService
import ch.addere.dga.importer.infrastructure.GradleConnectorServiceImpl
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module

val importerModule = module {
singleOf(::GradleConnectorService)
singleOf(::ModelImporterService)
singleOf(::GradleConnectorServiceImpl) { bind<GradleConnectorService>() }
singleOf(::GradleProjectLoaderImpl) { bind<GradleProjectLoader>() }
singleOf(::ModelImporterService)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.addere.dga.importer.application.service

import ch.addere.dga.dependencymodel.DependencyModel
import ch.addere.dga.importer.domain.service.GradleConnectorService
import ch.addere.dga.importer.domain.service.ModelImporterService
import ch.addere.dga.importer.infrastructure.GradleConnectorServiceImpl
import java.io.File

class GradleProjectLoaderImpl(
private val connectorService: GradleConnectorService,
private val connectorService: GradleConnectorServiceImpl,
private val modelImporterService: ModelImporterService
) : GradleProjectLoader {

Expand Down
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
}
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)
}
}
}

0 comments on commit 3c73fdb

Please sign in to comment.