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

Fix execution order of the github actions #97

Closed
wants to merge 7 commits into from
Closed
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 .github/workflows/maven-central.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

name: Publish package to the Maven Central Repository
on:
workflow_run:
workflows:
- "Test PR"
types:
- completed
push:
branches:
- main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.InheritKeyword
import net.theevilreaper.dartpoet.annotation.AnnotationSpec
import net.theevilreaper.dartpoet.enum.EnumPropertySpec
import net.theevilreaper.dartpoet.function.ConstructorBase
import net.theevilreaper.dartpoet.function.FunctionSpec
import net.theevilreaper.dartpoet.meta.SpecData
import net.theevilreaper.dartpoet.meta.SpecMethods
Expand Down Expand Up @@ -33,7 +34,7 @@ class ClassBuilder internal constructor(
internal val isMixinClass get() = classType == ClassType.MIXIN
internal val isAbstract get() = classType == ClassType.ABSTRACT
internal val isLibrary get() = classType == ClassType.CLASS
internal val constructorStack: MutableList<ConstructorSpec> = mutableListOf()
internal val constructorStack: MutableList<ConstructorBase> = mutableListOf()
internal val propertyStack: MutableList<PropertySpec> = mutableListOf()
internal val functionStack: MutableList<FunctionSpec> = mutableListOf()
internal val enumPropertyStack: MutableList<EnumPropertySpec> = mutableListOf()
Expand Down Expand Up @@ -184,7 +185,7 @@ class ClassBuilder internal constructor(
* @param constructor the constructor to add
* @return the given instance of an [ClassBuilder]
*/
fun constructor(constructor: ConstructorSpec) = apply {
fun constructor(constructor: ConstructorBase) = apply {
this.constructorStack += constructor
}

Expand All @@ -193,7 +194,7 @@ class ClassBuilder internal constructor(
* @param constructor the constructor to add
* @return the given instance of an [ClassBuilder]
*/
fun constructor(constructor: () -> ConstructorSpec) = apply {
fun constructor(constructor: () -> ConstructorBase) = apply {
this.constructorStack += constructor()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.function.FunctionSpec
import net.theevilreaper.dartpoet.function.constructor.ConstructorSpec
import net.theevilreaper.dartpoet.directive.Directive
import net.theevilreaper.dartpoet.function.ConstructorBase
import net.theevilreaper.dartpoet.function.factory.FactorySpec
import net.theevilreaper.dartpoet.function.typedef.TypeDefSpec
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.parameter.ParameterSpec
Expand Down Expand Up @@ -39,6 +41,7 @@ val Char.isMultiCharNoArgPlaceholder get() = this == '%'
internal val String.isPlaceholder
get() = (length == 1 && first().isSingleCharNoArgPlaceholder) ||
(length == 2 && first().isMultiCharNoArgPlaceholder)

fun String.nextPotentialPlaceholderPosition(startIndex: Int) =
indexOfAny(NO_ARG_PLACEHOLDERS, startIndex)

Expand Down Expand Up @@ -82,28 +85,24 @@ internal fun Set<AnnotationSpec>.emitAnnotations(
}
}

internal fun Set<ConstructorSpec>.emitConstructors(
internal fun Set<ConstructorBase>.emitConstructors(
codeWriter: CodeWriter,
forceNewLines: Boolean = false,
leadingNewLine: Boolean = false,
emitBlock: (ConstructorSpec) -> Unit = { it.write(codeWriter) }
emitBlock: (ConstructorBase) -> Unit = {
if (it is ConstructorSpec) {
(it.write(codeWriter))
} else if (it is FactorySpec) {
(it.write(codeWriter))
}
}
) = with(codeWriter) {
if (isNotEmpty()) {
if (leadingNewLine) {
codeWriter.emit(NEW_LINE)
}
forEachIndexed { index, constructorSpec ->
val emitNewLines = size >= 1 || forceNewLines

if (index > 0 && emitNewLines) {
codeWriter.emit(NEW_LINE)
}

emitBlock(constructorSpec)

if (emitNewLines) {
codeWriter.emit(NEW_LINE)
}
forEach { constructorBase ->
emitBlock(constructorBase)
codeWriter.emit(NEW_LINE)
}
}
}
Expand All @@ -117,7 +116,7 @@ internal fun List<ParameterSpec>.emitParameters(
if (isNotEmpty()) {
val emitComma = size > 1
forEachIndexed { index, parameter ->
if (index > 0 && forceNewLines) {
if (index > 0 && forceNewLines) {
emit(NEW_LINE)
}

Expand All @@ -143,7 +142,7 @@ internal fun List<ExtensionSpec>.emitExtensions(
val emitNewLines = size > 1 || forceNewLines

forEachIndexed { index, parameter ->
if (index > 0 && emitNewLines) {
if (index > 0 && emitNewLines) {
emit(NEW_LINE)
}
emitBlock(parameter)
Expand All @@ -155,7 +154,7 @@ internal fun List<ExtensionSpec>.emitExtensions(
}
}

internal fun <T: Directive> List<T>.writeImports(
internal fun <T : Directive> List<T>.writeImports(
writer: CodeWriter,
newLineAtBegin: Boolean = true,
emitBlock: (T) -> String = { it.asString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ internal interface DocumentationAppender {
if (docs.isEmpty()) return
docs.forEach { writer.emitDoc(it) }
}

fun emitDocumentation(doc: CodeBlock, writer: CodeWriter) {
if (doc.isEmpty()) return
writer.emitDoc(doc)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ internal class ConstructorWriter : Writeable<ConstructorSpec>, DocumentationAppe
writer.emit("${DartModifier.CONST.identifier}·")
}

if (spec.isFactory) {
writer.emit("${DartModifier.FACTORY.identifier}·")
}

writer.emit(spec.name)

if (spec.isNamed) {
Expand Down Expand Up @@ -69,10 +65,6 @@ internal class ConstructorWriter : Writeable<ConstructorSpec>, DocumentationAppe
return
}

if (spec.isFactory) {
writer.emit(" = _${spec.name}")
}

writer.emit(SEMICOLON)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package net.theevilreaper.dartpoet.code.writer

import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.DocumentationAppender
import net.theevilreaper.dartpoet.code.InitializerAppender
import net.theevilreaper.dartpoet.code.emitParameters
import net.theevilreaper.dartpoet.function.factory.FactorySpec
import net.theevilreaper.dartpoet.parameter.ParameterSpec
import net.theevilreaper.dartpoet.util.CURLY_CLOSE
import net.theevilreaper.dartpoet.util.CURLY_OPEN
import net.theevilreaper.dartpoet.util.ROUND_CLOSE
import net.theevilreaper.dartpoet.util.ROUND_OPEN
import net.theevilreaper.dartpoet.util.toImmutableList

internal class FactoryWriter : InitializerAppender<FactorySpec>, DocumentationAppender {

fun write(spec: FactorySpec, codeWriter: CodeWriter) {
emitDocumentation(spec.documentation, codeWriter)
if (spec.isConst) {
codeWriter.emitCode("%L·", DartModifier.CONST.identifier)
}
codeWriter.emitCode("%L·%T", DartModifier.FACTORY.identifier, spec.typeName)

if (spec.hasNamedData) {
codeWriter.emitCode(".%L", spec.named!!)
}

if (!spec.hasParameters) {
writeSimpleConstructor(spec, codeWriter)
return
}

codeWriter.emitCode(ROUND_OPEN)
writeParameters(spec, codeWriter)
codeWriter.emitCode(ROUND_CLOSE)

if (spec.withLambda) {
codeWriter.emitCode("·=>·")
codeWriter.emitCode(spec.initializerBlock)
return
}

codeWriter.emitCode("·$CURLY_OPEN\n")
codeWriter.indent()
codeWriter.emitCode(spec.initializerBlock)
codeWriter.unindent()
codeWriter.emit("\n$CURLY_CLOSE")
}

private fun writeSimpleConstructor(spec: FactorySpec, codeWriter: CodeWriter) {
if (spec.withLambda) {
codeWriter.emitCode("·=>·")
codeWriter.emitCode(spec.initializerBlock)
return
}
codeWriter.emitCode("·$CURLY_OPEN\n")

codeWriter.indent()
codeWriter.emitCode(spec.initializerBlock)
codeWriter.unindent()
codeWriter.emit("\n$CURLY_CLOSE")
}

private fun writeParameters(spec: FactorySpec, codeWriter: CodeWriter) {
spec.normalParameter.emitParameters(codeWriter, forceNewLines = false)

if (spec.normalParameter.isNotEmpty() && (spec.hasAdditionalParameters || spec.parametersWithDefaults.isNotEmpty())) {
codeWriter.emit(", ")
}

if (spec.hasAdditionalParameters) {
emitRequiredAndNamedParameter(spec, codeWriter)
}

if (spec.parametersWithDefaults.isNotEmpty()) {
codeWriter.emit("[")
spec.parametersWithDefaults.emitParameters(codeWriter, forceNewLines = false)
codeWriter.emit("]")
}
}

private fun emitRequiredAndNamedParameter(spec: FactorySpec, codeWriter: CodeWriter) {
codeWriter.emit("$CURLY_OPEN")

val namedRequired = spec.namedParameter.filter { it.isRequired && !it.hasInitializer }.toImmutableList()

writeParameters(namedRequired, spec.normalParameter.isNotEmpty(), codeWriter)
writeParameters(spec.requiredParameter, namedRequired.isNotEmpty(), codeWriter)

val test =
spec.namedParameter.minus(namedRequired).filter { it.isNullable || it.hasInitializer }.toImmutableList()

writeParameters(test, spec.requiredParameter.isNotEmpty() || namedRequired.isNotEmpty(), codeWriter)

codeWriter.emit("$CURLY_CLOSE")
}

private fun writeParameters(
parameters: List<ParameterSpec>,
emitSpaceComma: Boolean = false,
codeWriter: CodeWriter,
) {
if (parameters.isEmpty()) return
if (emitSpaceComma) {
codeWriter.emit(", ")
}
parameters.emitParameters(codeWriter, forceNewLines = false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.theevilreaper.dartpoet.function


interface ConstructorBase {


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ConstructorBuilder(
internal val parameters: MutableList<ParameterSpec> = mutableListOf()
internal var lambda: Boolean = false
internal val initializer: CodeBlock.Builder = CodeBlock.builder()
internal var factory: Boolean = false
internal val modifiers: MutableList<DartModifier> = mutableListOf(*modifiers)
internal val docs: MutableList<CodeBlock> = mutableListOf()

Expand Down Expand Up @@ -42,14 +41,6 @@ class ConstructorBuilder(
this.modifiers += modifiers
}

/**
* Indicates if the constructor should be generated as factory.
* @param factory True for a factory generation otherwise false
*/
fun asFactory(factory: Boolean) = apply {
this.factory = factory
}

/**
* Add a format string with arguments as initializer.
* @param format the format for the block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package net.theevilreaper.dartpoet.function.constructor
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ConstructorWriter
import net.theevilreaper.dartpoet.function.ConstructorBase
import net.theevilreaper.dartpoet.util.toImmutableList
import net.theevilreaper.dartpoet.util.toImmutableSet

class ConstructorSpec(
builder: ConstructorBuilder
) {
): ConstructorBase {

internal val name = builder.name
internal val named = builder.named
internal val isNamed = named.orEmpty().trim().isNotEmpty()
internal val isLambda = builder.lambda
internal val isFactory = builder.factory
internal val initializer = builder.initializer
internal val modifiers = builder.modifiers.toImmutableSet()
private val modelParameters = builder.parameters.toImmutableSet()
Expand All @@ -40,7 +40,6 @@ class ConstructorSpec(
fun toBuilder(): ConstructorBuilder {
val builder = ConstructorBuilder(this.name, this.named)
builder.lambda = this.isLambda
builder.factory = this.isFactory
builder.modifiers.addAll(this.modifiers)
builder.parameters.addAll(this.modelParameters)

Expand Down
Loading
Loading