Skip to content

Commit

Permalink
pruebas scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubentxu committed Oct 26, 2023
1 parent c31f0e4 commit 706d4c8
Show file tree
Hide file tree
Showing 57 changed files with 1,373 additions and 35 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Comandos


```bash
# Construir e instalar dependencias
$ gradle clean :pipeline-cli:shadowJar
```



```bash
# Ejecutar
$ java -jar pipeline-cli/build/libs/pipeline-cli-1.0-SNAPSHOT.jar -c pipeline-cli/src/test/resources/config.yaml -s pipeline-cli/src/test/resources/HelloWorld.pipeline.kts

```

```bash
# Ejecutar con print classpath
gradle clean :pipeline-cli:printClasspath
``
36 changes: 7 additions & 29 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
plugins {
kotlin("jvm") version "1.9.10"
application
id("io.kotest") version "0.4.10"

}

group = "dev.rubentxu.pipeline"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
val kotlinVersion: String by extra("1.9.10")
val kotlinCoroutinesVersion: String by extra("1.7.3")

testImplementation(kotlin("test"))

testImplementation("io.kotest:kotest-runner-junit5-jvm:5.7.2")
testImplementation("io.kotest:kotest-assertions-core-jvm:5.7.2")
implementation("io.kotest:kotest-property-jvm:5.7.2")
allprojects {

repositories {
mavenCentral()
maven { url = uri("https://repo.gradle.org/gradle/libs-releases") }
}
}

tasks.test {
useJUnitPlatform()
}

kotlin {
jvmToolchain(17)
}

application {
mainClass.set("MainKt")
}
42 changes: 42 additions & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
45 changes: 45 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
kotlin("jvm")
id("io.kotest") version "0.4.10"
id("com.github.johnrengelman.shadow") version "7.1.2"

}

group = "dev.rubentxu.pipeline.core"
version = "1.0-SNAPSHOT"

val kotlinVersion: String by rootProject.extra
val kotlinCoroutinesVersion: String by rootProject.extra

dependencies {

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
implementation("org.gradle:gradle-tooling-api:8.4")
implementation("org.eclipse.jgit:org.eclipse.jgit:6.7.0.202309050840-r")


testImplementation(kotlin("test"))

testImplementation("io.kotest:kotest-runner-junit5-jvm:5.7.2")
testImplementation("io.kotest:kotest-assertions-core-jvm:5.7.2")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.1.0")
testImplementation("io.kotest:kotest-property-jvm:5.7.2")

}

tasks.test {
useJUnitPlatform()
}

kotlin {
jvmToolchain(17)
}

tasks {
shadowJar {
archiveClassifier.set("")
archiveBaseName.set("core")

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.rubentxu.pipeline.compiler

import dev.rubentxu.pipeline.library.JarFileNotFoundException
import dev.rubentxu.pipeline.library.LibraryConfiguration
import dev.rubentxu.pipeline.library.LibraryNotFoundException
import dev.rubentxu.pipeline.library.SourceNotFoundException
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.GradleProject
import java.io.File
import java.nio.file.Path

class GradleCompiler {
fun compileAndJar(sourcePath: String, libraryConfiguration: LibraryConfiguration): File {
val file = File(resolveAndNormalizeAbsolutePath(sourcePath))
if(!file.exists()) {
throw SourceNotFoundException("File ${file.path} not found")
}

val connection: ProjectConnection = GradleConnector.newConnector()
.forProjectDirectory(file)
.connect()

val project: GradleProject = connection.getModel(GradleProject::class.java)
project.name

try {
connection.newBuild()
.withArguments("clean", "build", "-ParchiveBaseName${libraryConfiguration.name}")
.run()

val jarFile = findJarFile(File(resolveAndNormalizeAbsolutePath(sourcePath), "build/libs/"))

if (jarFile?.exists() == false) {
throw JarFileNotFoundException(jarFile.path)
}

return jarFile!!
} finally {
connection.close()
}
}

fun getProjectName(projectDir: String): String {
val connection: ProjectConnection = GradleConnector.newConnector()
.forProjectDirectory(File(projectDir))
.connect()

try {
val project: GradleProject = connection.getModel(GradleProject::class.java)
return project.name
} finally {
connection.close()
}
}

fun findJarFile(directory: File): File? {
return directory.walk()
.filter { it.isFile && it.extension == "jar" }
.firstOrNull() // Devuelve el primer archivo JAR encontrado o null si no se encontró ninguno
}

fun resolveAndNormalizeAbsolutePath(relativePath: String): String {
val path = Path.of(relativePath)
return path.toAbsolutePath().normalize().toString()

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.rubentxu.pipeline.dsl

import dev.rubentxu.pipeline.steps.EnvVars
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking

/**
* Enum representing the status of a pipeline or stage.
Expand Down Expand Up @@ -48,13 +50,16 @@ data class PipelineResult(
* * @param block A block of code to run in the pipeline.
* * @return A PipelineResult instance containing the results of the pipeline execution.
* */
suspend fun pipeline(block: suspend PipelineDsl.() -> Unit): PipelineResult {
fun pipeline(block: suspend PipelineDsl.() -> Unit): PipelineResult {
val pipeline = PipelineDsl()

var status: Status

try {
pipeline.block()
runBlocking(Dispatchers.Default) {
pipeline.block()
}


status = if (pipeline.stageResults.any { it.status == Status.Failure }) Status.Failure else Status.Success
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PipelineDsl: Configurable{
* @param any Placeholder for the any available agent.
*/
suspend fun agent(any: Placeholder) {
logger.debug("Running pipeline using any available agent...")
logger.debug("Running pipeline using any available agent... ${any}")
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.rubentxu.pipeline.extensions

import dev.rubentxu.pipeline.dsl.PipelineDsl
import dev.rubentxu.pipeline.dsl.StepBlock
import dev.rubentxu.pipeline.steps.Shell

/**
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/kotlin/dev/rubentxu/pipeline/library/GitSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.rubentxu.pipeline.library

import dev.rubentxu.pipeline.compiler.GradleCompiler
import org.eclipse.jgit.api.Git
import java.io.File

class GitSource(private val gradleCompiler: GradleCompiler) : SourceRetriever {
override fun retrieve(libraryConfiguration: LibraryConfiguration): File {
val gitRepoPath = "build/to/local/git/repo"
Git.cloneRepository()
.setURI(libraryConfiguration.sourcePath)
.setDirectory(File(gitRepoPath))
.call()

return gradleCompiler.compileAndJar(gitRepoPath, libraryConfiguration)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.rubentxu.pipeline.library

data class LibraryConfiguration(
val name: String,
val sourcePath: String,
val version: String,
val retriever: SourceRetriever,
val credentialsId: String?


)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.rubentxu.pipeline.library

class LibraryNotFoundException(id: LibraryId) : Exception("Librería ${id.name} con ${id.version} no encontrada")

class SourceNotFoundException(message:String) : Exception(message)

class JarFileNotFoundException(path: String) : Exception("No se encontró el archivo JAR en la ruta especificada: $path")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.rubentxu.pipeline.library

data class LibraryId (
val name: String,
val version: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.rubentxu.pipeline.library



import java.io.File
import java.net.URLClassLoader
import java.nio.file.Path

class LibraryLoader {
val libraries = mutableMapOf<LibraryId, LibraryConfiguration>()

fun loadLibrary(id: LibraryId): File {
val libraryConfiguration = libraries[id] ?: throw LibraryNotFoundException(id)
return doLoadLibrary(libraryConfiguration)
}

private fun doLoadLibrary(libraryConfiguration: LibraryConfiguration): File {
val retriever = libraryConfiguration.retriever
val jarFile = retriever.retrieve(libraryConfiguration)
val url = jarFile.toURI().toURL()

// Crear un nuevo URLClassLoader con la nueva URL
URLClassLoader.newInstance(arrayOf(url))
return jarFile
}

fun resolveAndNormalizeAbsolutePath(relativePath: String): String {
val path = Path.of(relativePath)
return path.toAbsolutePath().normalize().toString()

}

}
16 changes: 16 additions & 0 deletions core/src/main/kotlin/dev/rubentxu/pipeline/library/LocalJar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.rubentxu.pipeline.library

import java.io.File

class LocalJar : SourceRetriever {
override fun retrieve(libraryConfiguration: LibraryConfiguration): File {
val jarPath = libraryConfiguration.sourcePath
val jarFile = File(jarPath)

if (!jarFile.exists()) {
throw JarFileNotFoundException(jarPath)
}

return jarFile
}
}
11 changes: 11 additions & 0 deletions core/src/main/kotlin/dev/rubentxu/pipeline/library/LocalSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.rubentxu.pipeline.library

import dev.rubentxu.pipeline.compiler.GradleCompiler
import java.io.File

class LocalSource(private val gradleCompiler: GradleCompiler) : SourceRetriever {
override fun retrieve(libraryConfiguration: LibraryConfiguration): File {
val sourcePath = libraryConfiguration.sourcePath
return gradleCompiler.compileAndJar(sourcePath, libraryConfiguration)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.rubentxu.pipeline.library

import java.io.File

interface SourceRetriever {
fun retrieve(libraryConfiguration: LibraryConfiguration): File
}
Loading

0 comments on commit 706d4c8

Please sign in to comment.