-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* (build-logic, core logging):Add Context function - Publish generated files - Restructure file generation logic * core: Fix indent of contract call * all: Unify test platform APIs * Add serialization module Co-Authored-By: Michael Rittmeister <michael@rittmeister.in> * Add EmptyJsonObject.kt * Final preparations for 1.1.0 * Install Proper Java version in CI * Fix kbson test dependency * Fix gradle buildscript * Export json dependency as api * Fix ios api Co-authored-by: NyCode <me@nycode.dev>
- Loading branch information
1 parent
c82e642
commit 8d995f3
Showing
56 changed files
with
1,228 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import com.squareup.kotlinpoet.FileSpec | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputDirectory | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
|
||
abstract class AbstractGenerateFilesTask : DefaultTask() { | ||
|
||
@get:Input | ||
abstract val `package`: Property<String> | ||
|
||
@get:OutputDirectory | ||
protected val outputDir = project.projectDir | ||
.toPath() | ||
.resolvePaths("src") | ||
|
||
protected fun generateFile( | ||
sourceSet: String, | ||
fileName: String, | ||
filter: (CharSequence) -> CharSequence = { it }, | ||
block: FileSpec.Builder.() -> Unit | ||
) { | ||
val file = FileSpec.builder(`package`.get(), fileName) | ||
.apply(block) | ||
.addFileComment("This file is generated by Gradle task $name please do not edit it manually") | ||
.build() | ||
|
||
val builder = StringBuilder() | ||
file.writeTo(builder) | ||
val newOutput = filter(builder) | ||
|
||
val out = outputDir | ||
.resolve("${sourceSet}Generated") | ||
.resolvePaths(*file.packageName.split('.').toTypedArray()) | ||
.resolve("$fileName.kt") | ||
Files.createDirectories(out.parent) | ||
Files.writeString(out, newOutput) | ||
} | ||
|
||
protected fun Path.resolvePaths(vararg relative: String) = | ||
relative.toList().fold(this) { parent, currentRelative -> parent.resolve(currentRelative) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.LambdaTypeName | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.TypeAliasSpec | ||
import com.squareup.kotlinpoet.TypeVariableName | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
const val returnTypeParameter = "ReturnType" | ||
|
||
abstract class GenerateContextFunctionsTask : AbstractGenerateFilesTask() { | ||
@TaskAction | ||
fun generate() { | ||
generateFile("common", "ContextFunctions", { | ||
it.replace("import `?lambda_A(, [A-Z])*`?\n".toRegex(), "") | ||
.replaceFirst("lambda_A", "A.() -> ReturnType") | ||
.replace("`lambda_A(?:, ((?:(?:, )?[A-Z])*)`)?".toRegex(), "context($1) A.() -> ReturnType") | ||
}) { | ||
addImport("kotlin.contracts", "InvocationKind", "contract") | ||
|
||
val alphabet = ('A'..'Z').toList() | ||
|
||
val functions = ArrayList<FunSpec>(alphabet.size) | ||
|
||
repeat(alphabet.size) { | ||
val count = it + 1 | ||
val current = alphabet.take(count) | ||
val typeVariables = (current.map(Char::toString) + returnTypeParameter).map { name -> | ||
TypeVariableName(name) | ||
} | ||
|
||
val lambdaRaw = LambdaTypeName.get( | ||
typeVariables.first(), | ||
returnType = typeVariables.last() | ||
).toString() | ||
|
||
val lambda = "lambda_${typeVariables.dropLast(1).joinToString(", ") { variable -> variable.name }}" | ||
|
||
val typeAlias = ClassName(packageName, "ContextReceiver$count") | ||
|
||
addTypeAlias( | ||
TypeAliasSpec.builder(typeAlias.simpleName, ClassName("", lambda)) | ||
.addModifiers(KModifier.PUBLIC) | ||
.addTypeVariables(typeVariables) | ||
.addKdoc( | ||
""" | ||
Lambda with $count context receivers. | ||
@see context""".trimIndent() | ||
) | ||
.build() | ||
) | ||
|
||
val argumentList = if (count > 1) { | ||
(typeVariables.subList(1, typeVariables.size - 1) | ||
.map { variable -> variable.name.toLowerCase() } + 'a').joinToString( | ||
", " | ||
) | ||
} else { | ||
'a' | ||
} | ||
|
||
val function = FunSpec.builder("context") | ||
.addModifiers(KModifier.PUBLIC, KModifier.INLINE) | ||
.addTypeVariables(typeVariables) | ||
.apply { | ||
typeVariables.dropLast(1).forEach { type -> | ||
addParameter(type.name.toLowerCase(), type) | ||
} | ||
} | ||
.addParameter("block", typeAlias.parameterizedBy(typeVariables)) | ||
.returns(typeVariables.last()) | ||
.addKdoc( | ||
""" | ||
Adds ${ | ||
typeVariables.dropLast(1) | ||
.joinToString("], [", "[", "]") { it.name.toLowerCase() } | ||
} to the context of [block] and returns its return value. | ||
""".trimIndent() | ||
) | ||
.addCode( | ||
""" | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
return block.invoke($argumentList) | ||
""".trimIndent() | ||
) | ||
.build() | ||
|
||
functions += function | ||
} | ||
|
||
functions.forEach { addFunction(it) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
} | ||
|
||
kotlin { | ||
fullJs() | ||
desktopAll() | ||
appleMobilePlatforms() | ||
} |
Oops, something went wrong.