Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce more kotlin #348

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tasks {
test {
useJUnitPlatform()
finalizedBy(jacocoTestReport)
testClassesDirs = sourceSets["main"].output.classesDirs
}
withType<AbstractArchiveTask> {
isPreserveFileTimestamps = false
Expand Down
8 changes: 3 additions & 5 deletions buildSrc/src/main/kotlin/integration-test.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// refs https://docs.gradle.org/current/userguide/java_testing.html

plugins {
`java`
`java-base`
`kotlin`
id("org.gradle.test-retry")
}

val integrationTest by sourceSets.creating {
java.srcDirs(
"src/integration-test/kotlin"
)
runtimeClasspath += sourceSets["main"].output.classesDirs
}

dependencies {
"integrationTestImplementation"(project)
"integrationTestImplementation"("org.junit.jupiter:junit-jupiter-api")
"integrationTestImplementation"("io.github.bonigarcia:selenium-jupiter:4.1.0")
"integrationTestImplementation"("com.codeborne:selenide:6.5.1")
Expand Down
73 changes: 0 additions & 73 deletions src/main/java/jp/skypencil/javadocky/JavadockyApplication.java

This file was deleted.

This file was deleted.

This file was deleted.

143 changes: 143 additions & 0 deletions src/main/kotlin/jp/skypencil/javadocky/JavadockyApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package jp.skypencil.javadocky

import jp.skypencil.javadocky.repository.ArtifactRepository
import jp.skypencil.javadocky.repository.LocalStorage
import jp.skypencil.javadocky.repository.LocalStorageArtifactRepository
import jp.skypencil.javadocky.repository.Storage
import jp.skypencil.javadocky.repository.VersionRepository
import jp.skypencil.javadocky.service.JavadocDownloader
import org.apache.maven.artifact.versioning.ArtifactVersion
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.router
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.net.URI
import java.nio.file.Paths
import java.util.*

@SpringBootApplication
open class JavadockyApplication {
private val log = LoggerFactory.getLogger(javaClass)

@Bean
open fun localStorage(): Storage {
val home = Paths.get(System.getProperty(USER_HOME), JAVADOCKY_ROOT, STORAGE_DIR)
home.toFile().mkdirs()
log.info("Making storage at {}", home.toFile().absolutePath)
return LocalStorage(home)
}

@Bean
open fun artifactRepository(): ArtifactRepository {
val home = Paths.get(System.getProperty(USER_HOME), JAVADOCKY_ROOT, JAVADOC_DIR)
home.toFile().mkdirs()
log.info("Making storage at {}", home.toFile().absolutePath)
return LocalStorageArtifactRepository(home)
}

@Bean
open fun javadocDownloader(
@Value("\${javadocky.maven.repository}") repoURL: String
): JavadocDownloader {
val home = Paths.get(System.getProperty(USER_HOME), JAVADOCKY_ROOT, JAVADOC_DIR)
home.toFile().mkdirs()
log.info("Making javadoc storage at {}", home.toFile().absolutePath)
return JavadocDownloader(home, repoURL)
}

@Bean
open fun routes(artifactRepo: ArtifactRepository, versionRepo: VersionRepository) = router {
GET("/doc/{groupId}/{artifactId}") { req: ServerRequest ->
val groupId = req.pathVariable("groupId")
val artifactId = req.pathVariable("artifactId")
val artifacts = artifactRepo.list(groupId)
val versions: Flux<out ArtifactVersion> = versionRepo.list(groupId, artifactId)
versionRepo
.findLatest(groupId, artifactId)
.flatMap { latestVersion: ArtifactVersion ->
val model = mapOf(
"groupId" to groupId,
"artifactId" to artifactId,
"artifactIds" to artifacts,
"version" to latestVersion,
"versions" to versions,
)
ServerResponse.ok().contentType(MediaType.TEXT_HTML)
.render("doc", model)
}
.switchIfEmpty(ServerResponse.notFound().build())
}
GET("/badge/{groupId}/{artifactId}.{ext}") { req: ServerRequest ->
val ext = req.pathVariable("ext")
if (ext != "png" && ext != "svg") {
ServerResponse.badRequest()
.body(
Mono.just("Unsupported extension"),
String::class.java
)
} else {
val groupId = req.pathVariable("groupId")
val artifactId = req.pathVariable("artifactId")
log.debug("Got access to badge for {}:{}", groupId, artifactId)
versionRepo
.findLatest(groupId, artifactId)
.flatMap { latestVersion: ArtifactVersion ->
val shieldsUri = URI.create(
String.format(
Locale.getDefault(),
"https://img.shields.io/badge/%s-%s-%s.%s",
escape(req.queryParam("label").orElse("javadoc")),
escape(latestVersion.toString()),
escape(req.queryParam("color").orElse("brightgreen")),
ext
)
)
ServerResponse.seeOther(shieldsUri).build()
}
.switchIfEmpty(ServerResponse.notFound().build())
}
}
}

companion object {
private const val USER_HOME = "user.home"
private const val JAVADOCKY_ROOT = ".javadocky"
private const val STORAGE_DIR = "storage"

/** Name of directory to store downloaded javadoc.jar file. */
private const val JAVADOC_DIR = "javadoc"
@JvmStatic
fun main(args: Array<String>) {
val isAppCds = args.contains("--appcds")
val port = System.getenv("PORT")
val app = SpringApplication(JavadockyApplication::class.java)
if (port != null) {
// for Heroku, respect the given PORT environment variable
app.setDefaultProperties(Collections.singletonMap<String, Any>("server.port", port))
}
@Suppress("SpreadOperator")
val ctx = app.run(*args)

// TODO consider the best timing to stop the process
if (isAppCds) {
System.err.println("Beans construction complete, so going to exit the process")
ctx.close()
}
}
}

/**
* @param s target string
* @return An escaped string based on the rule described by [shields.io](https://shields.io/)
*/
private fun escape(s: String): String {
return s.replace("-", "--").replace("_", "__").replace(" ", "_")
}
}