Skip to content

Commit

Permalink
Compat with the recent Quarkus codegen changes
Browse files Browse the repository at this point in the history
This PR is to make code.quarkus ready for the codegen changes:
- quarkusio/quarkus#9787
- quarkusio/quarkus#9953
  • Loading branch information
ia3andy authored and gsmet committed Jul 8, 2020
1 parent c67e015 commit c52d5ab
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 261 deletions.
131 changes: 0 additions & 131 deletions src/main/kotlin/io/quarkus/code/misc/CommonsZipProjectWriter.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import javax.validation.constraints.Pattern
import javax.ws.rs.DefaultValue
import javax.ws.rs.QueryParam

class QuarkusProject {
class ProjectDefinition {

companion object {
const val DEFAULT_GROUPID = "org.acme"
Expand Down Expand Up @@ -104,7 +104,7 @@ class QuarkusProject {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as QuarkusProject
other as ProjectDefinition

if (groupId != other.groupId) return false
if (artifactId != other.artifactId) return false
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/io/quarkus/code/rest/CodeQuarkusResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.quarkus.code.config.GitHubConfig
import io.quarkus.code.config.GoogleAnalyticsConfig
import io.quarkus.code.model.CodeQuarkusExtension
import io.quarkus.code.model.PublicConfig
import io.quarkus.code.model.QuarkusProject
import io.quarkus.code.model.ProjectDefinition
import io.quarkus.code.service.QuarkusExtensionCatalogService
import io.quarkus.code.service.QuarkusProjectService
import io.quarkus.runtime.StartupEvent
Expand Down Expand Up @@ -97,12 +97,12 @@ class CodeQuarkusResource {
@Path("/download")
@Produces("application/zip")
@Operation(summary = "Download a custom Quarkus application with the provided settings")
fun download(@Valid @BeanParam project: QuarkusProject): Response {
fun download(@Valid @BeanParam projectDefinition: ProjectDefinition): Response {
try {
return Response
.ok(projectCreator.create(project))
.ok(projectCreator.create(projectDefinition))
.type("application/zip")
.header("Content-Disposition", "attachment; filename=\"${project.artifactId}.zip\"")
.header("Content-Disposition", "attachment; filename=\"${projectDefinition.artifactId}.zip\"")
.build()
} catch (e: IllegalStateException) {
LOG.warning("Bad request: ${e.message}")
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/io/quarkus/code/rest/GitHubResource.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.quarkus.code.rest

import io.quarkus.code.model.GitHubCreatedRepository
import io.quarkus.code.model.QuarkusProject
import io.quarkus.code.model.ProjectDefinition
import io.quarkus.code.service.GitHubService
import io.quarkus.code.service.QuarkusProjectService
import io.quarkus.runtime.StartupEvent
Expand Down Expand Up @@ -47,17 +47,17 @@ class GitHubResource {
@Path("/project")
@Produces(APPLICATION_JSON)
@Operation(summary = "Create project and push generated code to GitHub")
fun createProject(@Valid @BeanParam project: QuarkusProject,
fun createProject(@Valid @BeanParam projectDefinition: ProjectDefinition,
@NotEmpty @HeaderParam("GitHub-Code") code: String,
@NotEmpty @HeaderParam("GitHub-State") state: String): GitHubCreatedRepository {
check(gitHubService.isEnabled()) { "GitHub is not enabled" }
val location = projectCreator.createTmp(project)
val location = projectCreator.createTmp(projectDefinition)
val token = gitHubService.fetchAccessToken(code, state)
val login = gitHubService.login(token.accessToken)
if (gitHubService.repositoryExists(login, token.accessToken, project.artifactId)) {
throw WebApplicationException("This repository name ${project.artifactId} already exists", 409)
if (gitHubService.repositoryExists(login, token.accessToken, projectDefinition.artifactId)) {
throw WebApplicationException("This repository name ${projectDefinition.artifactId} already exists", 409)
}
val repo = gitHubService.createRepository(login, token.accessToken, project.artifactId)
val repo = gitHubService.createRepository(login, token.accessToken, projectDefinition.artifactId)
gitHubService.push(repo.ownerName, token.accessToken, repo.url, location)
return repo
}
Expand Down
105 changes: 48 additions & 57 deletions src/main/kotlin/io/quarkus/code/service/QuarkusProjectService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package io.quarkus.code.service

import io.quarkus.cli.commands.AddExtensions
import io.quarkus.cli.commands.CreateProject
import io.quarkus.cli.commands.writer.ProjectWriter
import io.quarkus.code.misc.CommonsZipProjectWriter
import io.quarkus.code.misc.FileProjectWriterWithPerms
import io.quarkus.code.misc.ProjectWriterWithPerms
import io.quarkus.code.model.QuarkusProject
import io.quarkus.generators.BuildTool
import java.io.ByteArrayOutputStream
import io.quarkus.code.model.ProjectDefinition
import io.quarkus.devtools.commands.CreateProject
import io.quarkus.devtools.project.BuildTool
import io.quarkus.devtools.project.QuarkusProject
import io.quarkus.devtools.project.compress.QuarkusProjectCompress
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermissions
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -38,83 +35,77 @@ class QuarkusProjectService {
@Inject
internal lateinit var extensionCatalog: QuarkusExtensionCatalogService

fun create(project: QuarkusProject): ByteArray {
fun create(projectDefinition: ProjectDefinition): ByteArray {
QuarkusExtensionCatalogService.checkPlatformInitialization()
val baos = ByteArrayOutputStream()
baos.use {
val zipWriter = CommonsZipProjectWriter.createWriter(baos, project.artifactId)
zipWriter.use {
createProject(project, zipWriter)
}
}
return baos.toByteArray()
val path = createTmp(projectDefinition)
val time = System.currentTimeMillis() - 24 * 3600000
val zipPath = Files.createTempDirectory("zipped-").resolve("project.zip")
QuarkusProjectCompress.zip(path, zipPath, true, time)
return Files.readAllBytes(zipPath)
}

fun createTmp(project: QuarkusProject): Path {
val location = Files.createTempDirectory("generated-")
val fileProjectWriter = FileProjectWriterWithPerms(location.toFile())
createProject(project, fileProjectWriter)
fun createTmp(projectDefinition: ProjectDefinition): Path {
val location = Files.createTempDirectory("generated-").resolve(projectDefinition.artifactId)
createProject(projectDefinition, location)
return location;
}

private fun createProject(project: QuarkusProject, projectWriter: ProjectWriter) {
val extensions = checkAndMergeExtensions(project)
private fun createProject(projectDefinition: ProjectDefinition, projectFolderPath: Path) {
val extensions = checkAndMergeExtensions(projectDefinition)
val sourceType = CreateProject.determineSourceType(extensions)
val context = mutableMapOf("path" to (project.path as Any))
val buildTool = io.quarkus.generators.BuildTool.valueOf(project.buildTool)
val success = CreateProject(projectWriter, QuarkusExtensionCatalogService.descriptor)
.groupId(project.groupId)
.artifactId(project.artifactId)
.version(project.version)
val context = mutableMapOf("path" to (projectDefinition.path as Any))
val buildTool = BuildTool.valueOf(projectDefinition.buildTool)
val success = CreateProject(projectFolderPath, QuarkusExtensionCatalogService.descriptor)
.groupId(projectDefinition.groupId)
.artifactId(projectDefinition.artifactId)
.version(projectDefinition.version)
.sourceType(sourceType)
.buildTool(buildTool)
.className(project.className)
.className(projectDefinition.className)
.javaTarget("11")
.extensions(extensions)
.doCreateProject(context)
if (!success) {
throw IOException("Error during Quarkus project creation")
}
AddExtensions(projectWriter, buildTool, QuarkusExtensionCatalogService.descriptor)
.extensions(extensions)
.execute()
if (buildTool == BuildTool.MAVEN) {
addMvnw(projectWriter)
addMvnw(projectFolderPath)
} else if (buildTool == BuildTool.GRADLE) {
addGradlew(projectWriter)
addGradlew(projectFolderPath)
}
}

private fun checkAndMergeExtensions(project: QuarkusProject): Set<String> {
return extensionCatalog.checkAndMergeExtensions(project.extensions, project.shortExtensions)
private fun checkAndMergeExtensions(projectDefinition: ProjectDefinition): Set<String> {
return extensionCatalog.checkAndMergeExtensions(projectDefinition.extensions, projectDefinition.shortExtensions)
}

private fun addMvnw(projectWriter: ProjectWriter) {
projectWriter.mkdirs(MVNW_WRAPPER_DIR)
writeResourceFile(projectWriter, MVNW_RESOURCES_DIR, MVNW_WRAPPER_JAR)
writeResourceFile(projectWriter, MVNW_RESOURCES_DIR, MVNW_WRAPPER_PROPS)
writeResourceFile(projectWriter, MVNW_RESOURCES_DIR, MVNW_WRAPPER_DOWNLOADER)
writeResourceFile(projectWriter, MVNW_RESOURCES_DIR, MVNW_CMD, true)
writeResourceFile(projectWriter, MVNW_RESOURCES_DIR, MVNW, true)
private fun addMvnw(projectFolderPath: Path) {
Files.createDirectories(projectFolderPath.resolve(MVNW_WRAPPER_DIR))
writeResourceFile(projectFolderPath, MVNW_RESOURCES_DIR, MVNW_WRAPPER_JAR)
writeResourceFile(projectFolderPath, MVNW_RESOURCES_DIR, MVNW_WRAPPER_PROPS)
writeResourceFile(projectFolderPath, MVNW_RESOURCES_DIR, MVNW_WRAPPER_DOWNLOADER)
writeResourceFile(projectFolderPath, MVNW_RESOURCES_DIR, MVNW_CMD, true)
writeResourceFile(projectFolderPath, MVNW_RESOURCES_DIR, MVNW, true)
}

private fun addGradlew(projectWriter: ProjectWriter) {
projectWriter.mkdirs(GRADLEW_WRAPPER_DIR)
writeResourceFile(projectWriter, GRADLEW_RESOURCES_DIR, GRADLEW_WRAPPER_JAR)
writeResourceFile(projectWriter, GRADLEW_RESOURCES_DIR, GRADLEW_WRAPPER_PROPS)
writeResourceFile(projectWriter, GRADLEW_RESOURCES_DIR, GRADLEW_BAT, true)
writeResourceFile(projectWriter, GRADLEW_RESOURCES_DIR, GRADLEW, true)
private fun addGradlew(projectFolderPath: Path) {
Files.createDirectories(projectFolderPath.resolve(GRADLEW_WRAPPER_DIR))
writeResourceFile(projectFolderPath, GRADLEW_RESOURCES_DIR, GRADLEW_WRAPPER_JAR)
writeResourceFile(projectFolderPath, GRADLEW_RESOURCES_DIR, GRADLEW_WRAPPER_PROPS)
writeResourceFile(projectFolderPath, GRADLEW_RESOURCES_DIR, GRADLEW_BAT, true)
writeResourceFile(projectFolderPath, GRADLEW_RESOURCES_DIR, GRADLEW, true)
}

private fun writeResourceFile(projectWriter: ProjectWriter, resourcesDir: String, filePath: String, allowExec: Boolean = false) {
if (!projectWriter.exists(filePath)) {
private fun writeResourceFile(projectFolderPath: Path, resourcesDir: String, filePath: String, allowExec: Boolean = false) {
val absoluteFilePath = projectFolderPath.resolve(filePath);
if (!absoluteFilePath.toFile().exists()) {
val resourcePath = "$resourcesDir/$filePath"
val resource = QuarkusProjectService::class.java.getResource(resourcePath)
?: throw IOException("missing resource $resourcePath")
val fileAsBytes = resource.readBytes()
if (projectWriter is ProjectWriterWithPerms) {
projectWriter.write(filePath, fileAsBytes, allowExec)
} else {
throw IllegalStateException("Unsupported projectWriter ${projectWriter.javaClass.name}")
Files.write(absoluteFilePath, fileAsBytes)
if(allowExec) {
Files.setPosixFilePermissions(absoluteFilePath, PosixFilePermissions.fromString("rwxr-xr-x"))
}
}
}
Expand Down
Loading

0 comments on commit c52d5ab

Please sign in to comment.