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

[compiler] Refactor CodegenLayout #5554

Merged
merged 16 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions libraries/apollo-compiler/api/apollo-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public final class com/apollographql/apollo3/compiler/ApolloCompiler {
public final fun build (Ljava/util/Set;Ljava/util/Set;Ljava/io/File;Ljava/io/File;Ljava/io/File;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;Ljava/io/File;Ljava/io/File;)V
public static synthetic fun build$default (Lcom/apollographql/apollo3/compiler/ApolloCompiler;Ljava/util/Set;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo3/compiler/IrOptions;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;Ljava/io/File;Ljava/io/File;ILjava/lang/Object;)V
public static synthetic fun build$default (Lcom/apollographql/apollo3/compiler/ApolloCompiler;Ljava/util/Set;Ljava/util/Set;Ljava/io/File;Ljava/io/File;Ljava/io/File;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;Ljava/io/File;Ljava/io/File;ILjava/lang/Object;)V
public final fun buildCodegenSchema (Ljava/util/Set;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;Ljava/util/Set;Ljava/io/File;Ljava/io/File;)V
public static synthetic fun buildCodegenSchema$default (Lcom/apollographql/apollo3/compiler/ApolloCompiler;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;Ljava/util/Set;Ljava/io/File;Ljava/io/File;ILjava/lang/Object;)V
public final fun buildCodegenSchema (Ljava/util/Set;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;Ljava/io/File;)V
public static synthetic fun buildCodegenSchema$default (Lcom/apollographql/apollo3/compiler/ApolloCompiler;Ljava/util/Set;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;Ljava/io/File;ILjava/lang/Object;)V
public final fun buildIrOperations (Ljava/io/File;Ljava/util/Set;Ljava/util/Set;Ljava/io/File;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;)V
public static synthetic fun buildIrOperations$default (Lcom/apollographql/apollo3/compiler/ApolloCompiler;Ljava/io/File;Ljava/util/Set;Ljava/util/Set;Ljava/io/File;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;ILjava/lang/Object;)V
public final fun buildIrSchema (Ljava/io/File;Ljava/util/Set;Ljava/io/File;)V
Expand Down Expand Up @@ -74,8 +74,8 @@ public final class com/apollographql/apollo3/compiler/CodegenSchema {
public static final field Companion Lcom/apollographql/apollo3/compiler/CodegenSchema$Companion;
public fun <init> (Lcom/apollographql/apollo3/ast/Schema;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Lcom/apollographql/apollo3/compiler/TargetLanguage;Z)V
public final fun getCodegenModels ()Ljava/lang/String;
public final fun getFilePath ()Ljava/lang/String;
public final fun getGenerateDataBuilders ()Z
public final fun getPackageName ()Ljava/lang/String;
public final fun getScalarMapping ()Ljava/util/Map;
public final fun getSchema ()Lcom/apollographql/apollo3/ast/Schema;
public final fun getTargetLanguage ()Lcom/apollographql/apollo3/compiler/TargetLanguage;
Expand All @@ -98,12 +98,10 @@ public final class com/apollographql/apollo3/compiler/CodegenSchema$Companion {

public final class com/apollographql/apollo3/compiler/CodegenSchemaOptions {
public static final field Companion Lcom/apollographql/apollo3/compiler/CodegenSchemaOptions$Companion;
public fun <init> (Lcom/apollographql/apollo3/compiler/TargetLanguage;Ljava/util/Map;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/String;)V
public synthetic fun <init> (Lcom/apollographql/apollo3/compiler/TargetLanguage;Ljava/util/Map;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lcom/apollographql/apollo3/compiler/TargetLanguage;Ljava/util/Map;Ljava/lang/String;Ljava/lang/Boolean;)V
public synthetic fun <init> (Lcom/apollographql/apollo3/compiler/TargetLanguage;Ljava/util/Map;Ljava/lang/String;Ljava/lang/Boolean;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCodegenModels ()Ljava/lang/String;
public final fun getGenerateDataBuilders ()Ljava/lang/Boolean;
public final fun getPackageName ()Ljava/lang/String;
public final fun getPackageNamesFromFilePaths ()Ljava/lang/Boolean;
public final fun getScalarMapping ()Ljava/util/Map;
public final fun getTargetLanguage ()Lcom/apollographql/apollo3/compiler/TargetLanguage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,19 @@ object ApolloCompiler {
fun buildCodegenSchema(
schemaFiles: Set<File>,
logger: Logger = defaultLogger,
packageNameGenerator: PackageNameGenerator? = null,
packageNameRoots: Set<String>? = null,
codegenSchemaOptionsFile: File,
codegenSchemaFile: File,
) {
buildCodegenSchema(
schemaFiles,
logger,
packageNameGenerator,
packageNameRoots,
codegenSchemaOptionsFile.toCodegenSchemaOptions(),
).writeTo(codegenSchemaFile)
}

private fun buildCodegenSchema(
schemaFiles: Set<File>,
logger: Logger = defaultLogger,
packageNameGenerator: PackageNameGenerator? = null,
packageNameRoots: Set<String>? = null,
codegenSchemaOptions: CodegenSchemaOptions,
): CodegenSchema {
val schemaDocuments = schemaFiles.map {
Expand Down Expand Up @@ -136,17 +130,11 @@ object ApolloCompiler {
checkScalars(schema, scalarMapping)

val codegenModels = codegenModels(codegenModels = codegenSchemaOptions.codegenModels, codegenSchemaOptions.targetLanguage)
@Suppress("NAME_SHADOWING")
val packageNameGenerator = packageNameGenerator ?: packageNameGenerator(
codegenSchemaOptions.packageName,
codegenSchemaOptions.packageNamesFromFilePaths,
packageNameRoots
)

val generateDataBuilders = codegenSchemaOptions.generateDataBuilders ?: defaultGenerateDataBuilders
return CodegenSchema(
schema = schema,
packageName = packageNameGenerator.packageName(mainSchemaDocument.sourceLocation!!.filePath!!),
filePath = mainSchemaDocument.sourceLocation?.filePath,
codegenModels = codegenModels,
scalarMapping = scalarMapping,
targetLanguage = codegenSchemaOptions.targetLanguage,
Expand Down Expand Up @@ -564,8 +552,6 @@ object ApolloCompiler {
val codegenSchema = buildCodegenSchema(
schemaFiles = schemaFiles,
logger = logger,
packageNameGenerator = packageNameGenerator,
packageNameRoots = packageNameRoots,
codegenSchemaOptions = codegenSchemaOptions
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.serialization.Serializable
class CodegenSchema(
@Serializable(with = SchemaSerializer::class)
val schema: Schema,
val packageName: String,
val filePath: String?,
val codegenModels: String,
val scalarMapping: Map<String, ScalarInfo>,
val targetLanguage: TargetLanguage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ class CodegenSchemaOptions(
val scalarMapping: Map<String, ScalarInfo> = emptyMap(),
val codegenModels: String? = null,
val generateDataBuilders: Boolean? = null,
val packageNamesFromFilePaths: Boolean? = null,
val packageName: String? = null
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ interface PackageNameGenerator {
/**
* This will be called with
* - the executable filePath for operations and fragments
* - the main schema filePath for everything else
* - the main schema filePath for schema types
* - the empty string if the schema and/or operations are not read from a [File]
*/
fun packageName(filePath: String): String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,38 @@ fun String.decapitalizeFirstLetter(): String {
}
return builder.toString()
}

internal fun upperCamelCaseIgnoringNonLetters(strings: Collection<String>): String {
return strings.map {
it.capitalizeFirstLetter()
}.joinToString("")
}

internal fun lowerCamelCaseIgnoringNonLetters(strings: Collection<String>): String {
return strings.map {
it.decapitalizeFirstLetter()
}.joinToString("")
}

/**
* On case-insensitive filesystems, we need to make sure two schema types with
* different cases like 'Url' and 'URL' are not generated or their files will
* overwrite each other.
*
* For Kotlin, we _could_ just change the file name (and not the class name) but
* that only postpones the issue to later on when .class files are generated.
*
* In order to get predictable results independently of the system, we make the
* case-insensitive checks no matter the actual filesystem.
*/
internal fun uniqueName(name: String, usedNames: Set<String>): String {
var i = 1
var uniqueName = name
while (uniqueName.lowercase() in usedNames) {
uniqueName = "${name}$i"
i++
}
return uniqueName
}

internal fun String.withUnderscorePrefix(): String = if (this == "__typename") this else "_$this"