Skip to content

Commit

Permalink
Kotlinfmt used as well (w/ defaults) that seems closest to existing f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
cstamas committed May 24, 2024
1 parent 2736d6a commit 6ce92fa
Show file tree
Hide file tree
Showing 76 changed files with 4,150 additions and 3,972 deletions.
9 changes: 9 additions & 0 deletions polyglot-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<configuration>
<kotlin>
<ktfmt />
</kotlin>
</configuration>
</plugin>
</plugins>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.sonatype.maven.polyglot.kotlin

import org.sonatype.maven.polyglot.mapping.MappingSupport
import javax.inject.Named
import javax.inject.Singleton
import org.sonatype.maven.polyglot.mapping.MappingSupport

@Singleton
@Named( "kotlin" )
@Named("kotlin")
class KotlinMapping : MappingSupport("kotlin") {

init {
setPomNames("pom.kts")
setAcceptLocationExtensions(".kts")
setAcceptOptionKeys("kotlin:4.0.0", "kts:4.0.0")
priority = 1f
}
init {
setPomNames("pom.kts")
setAcceptLocationExtensions(".kts")
setAcceptOptionKeys("kotlin:4.0.0", "kts:4.0.0")
priority = 1f
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
package org.sonatype.maven.polyglot.kotlin

import org.apache.maven.model.Model
import org.apache.maven.model.building.ModelProcessor
import org.apache.maven.model.io.ModelReader
import org.sonatype.maven.polyglot.execute.ExecuteManager
import org.sonatype.maven.polyglot.kotlin.dsl.Project
import org.sonatype.maven.polyglot.kotlin.engine.ScriptHost
import java.io.File
import java.io.InputStream
import java.io.Reader
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
import org.apache.maven.model.Model
import org.apache.maven.model.building.ModelProcessor
import org.apache.maven.model.io.ModelReader
import org.sonatype.maven.polyglot.execute.ExecuteManager
import org.sonatype.maven.polyglot.kotlin.dsl.Project
import org.sonatype.maven.polyglot.kotlin.engine.ScriptHost

@Singleton
@Named( "kotlin" )
@Named("kotlin")
class KotlinModelReader : ModelReader {

@Inject
private lateinit var executeManager: ExecuteManager
@Inject private lateinit var executeManager: ExecuteManager

override fun read(input: File, options: Map<String, *>): Model {
val source = options[ModelProcessor.SOURCE].toString()
val sourceFile = File(source)
val basedir = sourceFile.parentFile.canonicalFile
val model = Project(input)
ScriptHost.eval(input, basedir, model)
val tasks = ArrayList(model.tasks)
executeManager.register(model, tasks)
executeManager.install(model, options)
model.tasks.clear() // Must be cleared or Maven goes into an infinitely repeatable introspection
return model
}
override fun read(input: File, options: Map<String, *>): Model {
val source = options[ModelProcessor.SOURCE].toString()
val sourceFile = File(source)
val basedir = sourceFile.parentFile.canonicalFile
val model = Project(input)
ScriptHost.eval(input, basedir, model)
val tasks = ArrayList(model.tasks)
executeManager.register(model, tasks)
executeManager.install(model, options)
model.tasks.clear() // Must be cleared or Maven goes into an infinitely repeatable introspection
return model
}

override fun read(input: Reader, options: MutableMap<String, *>): Model {
val temp = File.createTempFile("pom", ".kts")
temp.deleteOnExit()
temp.writer().use { input.copyTo(it) }
val model = read(temp, options)
temp.delete()
return model
}
override fun read(input: Reader, options: MutableMap<String, *>): Model {
val temp = File.createTempFile("pom", ".kts")
temp.deleteOnExit()
temp.writer().use { input.copyTo(it) }
val model = read(temp, options)
temp.delete()
return model
}

override fun read(input: InputStream, options: MutableMap<String, *>): Model {
val temp = File.createTempFile("pom", ".kts")
temp.deleteOnExit()
temp.outputStream().use { input.copyTo(it) }
val model = read(temp, options)
temp.delete()
return model
}
override fun read(input: InputStream, options: MutableMap<String, *>): Model {
val temp = File.createTempFile("pom", ".kts")
temp.deleteOnExit()
temp.outputStream().use { input.copyTo(it) }
val model = read(temp, options)
temp.delete()
return model
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
package org.sonatype.maven.polyglot.kotlin

import org.apache.maven.model.Model
import org.apache.maven.project.MavenProject
import org.slf4j.LoggerFactory
import org.sonatype.maven.polyglot.io.ModelWriterSupport
import org.sonatype.maven.polyglot.kotlin.serialization.ModelScriptWriter
import java.io.StringWriter
import java.io.Writer
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Provider
import javax.inject.Singleton
import org.apache.maven.model.Model
import org.apache.maven.project.MavenProject
import org.slf4j.LoggerFactory
import org.sonatype.maven.polyglot.io.ModelWriterSupport
import org.sonatype.maven.polyglot.kotlin.serialization.ModelScriptWriter

@Singleton
@Named( "kotlin" )
@Named("kotlin")
class KotlinModelWriter : ModelWriterSupport() {

private var log = LoggerFactory.getLogger( KotlinModelWriter::class.java )
private var log = LoggerFactory.getLogger(KotlinModelWriter::class.java)

@Inject
private lateinit var projectProvider: Provider<MavenProject>
@Inject private lateinit var projectProvider: Provider<MavenProject>

override fun write(output: Writer, options: Map<String, Any>, model: Model) {
output.write(with(StringWriter(1024)) {
val config = HashMap<String, Any>(options)
config.putIfAbsent("xml.dsl.enabled", projectProvider.get()?.properties?.getProperty("polyglot-kotlin.xml-dsl-enabled", "true") ?: "true")
config.putIfAbsent("flavor", projectProvider.get()?.properties?.getProperty("polyglot-kotlin.flavor", "mixed") ?: "mixed")
ModelScriptWriter(this, config).write(model)
val kotlinScript = toString()
if (log.isDebugEnabled) {
log.debug(({ "POM model converted from XML: \n$kotlinScript\n" })())
}
kotlinScript
override fun write(output: Writer, options: Map<String, Any>, model: Model) {
output.write(
with(StringWriter(1024)) {
val config = HashMap<String, Any>(options)
config.putIfAbsent(
"xml.dsl.enabled",
projectProvider
.get()
?.properties
?.getProperty("polyglot-kotlin.xml-dsl-enabled", "true") ?: "true")
config.putIfAbsent(
"flavor",
projectProvider.get()?.properties?.getProperty("polyglot-kotlin.flavor", "mixed")
?: "mixed")
ModelScriptWriter(this, config).write(model)
val kotlinScript = toString()
if (log.isDebugEnabled) {
log.debug(({ "POM model converted from XML: \n$kotlinScript\n" })())
}
kotlinScript
})
output.flush()
}

output.flush()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,40 @@ package org.sonatype.maven.polyglot.kotlin.dsl
@PomDsl
class Activation : org.apache.maven.model.Activation(), Cloneable {

@PomDsl
fun os(name: String? = null, block: ActivationOS.(ActivationOS) -> Unit) {
val os = ActivationOS().apply {
this.name = name
}
block(os, os)
this.os = os
}
@PomDsl
fun os(name: String? = null, block: ActivationOS.(ActivationOS) -> Unit) {
val os = ActivationOS().apply { this.name = name }
block(os, os)
this.os = os
}

@PomDsl
fun property(name: String? = null, block: ActivationProperty.(ActivationProperty) -> Unit) {
val property = ActivationProperty().apply {
this.name = name
}
block(property, property)
this.property = property
}
@PomDsl
fun property(name: String? = null, block: ActivationProperty.(ActivationProperty) -> Unit) {
val property = ActivationProperty().apply { this.name = name }
block(property, property)
this.property = property
}

@PomDsl
fun file(block: ActivationFile.(ActivationFile) -> Unit) {
val file = ActivationFile()
block(file, file)
this.file = file
}
@PomDsl
fun file(block: ActivationFile.(ActivationFile) -> Unit) {
val file = ActivationFile()
block(file, file)
this.file = file
}

@PomDsl
fun jdk(jdk: String): Activation {
this.jdk = jdk
return this
}
@PomDsl
fun jdk(jdk: String): Activation {
this.jdk = jdk
return this
}

@PomDsl
fun activeByDefault(activeByDefault: Boolean = true): Activation {
this.isActiveByDefault = activeByDefault
return this
}
@PomDsl
fun activeByDefault(activeByDefault: Boolean = true): Activation {
this.isActiveByDefault = activeByDefault
return this
}

override fun clone(): org.apache.maven.model.Activation {
return super<org.apache.maven.model.Activation>.clone()
}
override fun clone(): org.apache.maven.model.Activation {
return super<org.apache.maven.model.Activation>.clone()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package org.sonatype.maven.polyglot.kotlin.dsl
@PomDsl
class ActivationFile : org.apache.maven.model.ActivationFile(), Cloneable {

@PomDsl
fun missing(missing: String): ActivationFile {
this.missing = missing
return this
}
@PomDsl
fun missing(missing: String): ActivationFile {
this.missing = missing
return this
}

@PomDsl
fun exists(exists: String): ActivationFile {
this.exists = exists
return this
}
@PomDsl
fun exists(exists: String): ActivationFile {
this.exists = exists
return this
}

override fun clone(): org.apache.maven.model.ActivationFile {
return super<org.apache.maven.model.ActivationFile>.clone()
}
override fun clone(): org.apache.maven.model.ActivationFile {
return super<org.apache.maven.model.ActivationFile>.clone()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ package org.sonatype.maven.polyglot.kotlin.dsl
@PomDsl
class ActivationOS : org.apache.maven.model.ActivationOS(), Cloneable {

@PomDsl
fun name(name: String): ActivationOS {
this.name = name
return this
}
@PomDsl
fun name(name: String): ActivationOS {
this.name = name
return this
}

@PomDsl
fun family(family: String): ActivationOS {
this.family = family
return this
}
@PomDsl
fun family(family: String): ActivationOS {
this.family = family
return this
}

@PomDsl
fun arch(arch: String): ActivationOS {
this.arch = arch
return this
}
@PomDsl
fun arch(arch: String): ActivationOS {
this.arch = arch
return this
}

@PomDsl
fun version(version: String): ActivationOS {
this.version = version
return this
}
@PomDsl
fun version(version: String): ActivationOS {
this.version = version
return this
}

override fun clone(): org.apache.maven.model.ActivationOS {
return super<org.apache.maven.model.ActivationOS>.clone()
}
override fun clone(): org.apache.maven.model.ActivationOS {
return super<org.apache.maven.model.ActivationOS>.clone()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package org.sonatype.maven.polyglot.kotlin.dsl
@PomDsl
class ActivationProperty : org.apache.maven.model.ActivationProperty(), Cloneable {

@PomDsl
fun name(name: String): ActivationProperty {
this.name = name
return this
}
@PomDsl
fun name(name: String): ActivationProperty {
this.name = name
return this
}

@PomDsl
fun value(value: String): ActivationProperty {
this.value = value
return this
}
@PomDsl
fun value(value: String): ActivationProperty {
this.value = value
return this
}

override fun clone(): org.apache.maven.model.ActivationProperty {
return super<org.apache.maven.model.ActivationProperty>.clone()
}
override fun clone(): org.apache.maven.model.ActivationProperty {
return super<org.apache.maven.model.ActivationProperty>.clone()
}
}
Loading

0 comments on commit 6ce92fa

Please sign in to comment.