Compiler/metadata module
Overview
The compiler module is a Kotlin/JVM KSP processor module that turns extension metadata declared outside this module into generated source classes. Its processor entry point is registered in compiler/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider, which points KSP at keiyoushi.processor.SourceProcessor$Provider in compiler/…/SourceProcessor.kt.
SourceProcessor runs once per KSP invocation. It discovers exactly one class annotated with keiyoushi.annotation.Source, reads serialized source definitions from the KSP option kei_sources, optionally reads localized UI strings from kei_translations, validates that the annotated class participates in the supported source hierarchy, and generates a fixed class keiyoushi.source.Generated. The generated class is either a concrete source subclass or a eu.kanade.tachiyomi.source.SourceFactory implementation, depending on the number of source {} definitions and whether the annotated source class is abstract.
The module does not itself scan Gradle projects or write package repositories. It participates in packaging by enforcing metadata ownership and by generating the runtime entry point consumed by the extension manifest or app integration. compiler/build.gradle.kts wires the module as a JVM Kotlin artifact with KSP API, KotlinPoet KSP, and kotlinx serialization dependencies.
Entry points and tasks
compiler/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvideris the service-loader registration file. If the provider class name changes in SourceProcessor.kt, this resource must be updated or KSP will not instantiate the processor.- compiler/…/SourceProcessor.kt contains the KSP provider (
SourceProcessor.Provider) and all compile-time validation/generation logic.Provider.create()passes the KSPCodeGenerator, options, and logger intoSourceProcessor. SourceProcessor.process()is the main task. It gathers@Sourceclasses, validates that the Gradle-providedkei_sourcesoption and annotation are both present, decodesSourceDefmetadata, checks inheritance, determines which source properties/functions are already overridden, and writeskeiyoushi.source.Generatedthrough KotlinPoet.- compiler/build.gradle.kts declares the compiler module dependencies required by the processor: KSP API for symbol processing, KotlinPoet KSP for code generation, and kotlinx serialization JSON for decoding
kei_sourcesand translations.
Data model
SourceProcessor.kt defines the serialized metadata it expects from build configuration:
SourceDefis the per-source definition decoded fromkei_sources. It containsname,lang, numericid, andbaseUrl.BaseUrlSpecDatacontains atype, adefaultUrl, and optionalmirrors. The processor recognizes three base URL modes in generation:static,mirrors, andcustom.MirrorDatacontains a mirrorurland optional displaylabel.PartialLocaleStringsis decoded fromkei_translations, keyed by language code.stringsForLang()resolves locale-specific strings by exact language, then language prefix before-, then required English defaults.LocaleStringsis the fully resolved in-memory set of generated UI strings: mirror title, custom URL title, custom URL dialog message, and fetch filter hint.
The generated class name is intentionally fixed by constants in SourceProcessor.kt: package keiyoushi.source, class Generated. The comment in the file notes this is independent of the module directory, the @Source class package, and any package-name override because the generated manifest expects that FQN.
Important algorithms and flows
Source discovery and validation
SourceProcessor.process() guards itself with an invoked flag so the processor only performs generation once. It requests symbols annotated with keiyoushi.annotation.Source and filters them to KSClassDeclaration.
The validation flow is strict:
- If there are no annotated classes and no
kei_sourcesoption, the processor exits without generation. - If
kei_sourcesexists but no annotated class is found, it logs an error telling the extension author to annotate the source class. - If more than one
@Sourceclass is found, it logs an error because only one annotated source class is allowed per module. - If an annotated class exists but
kei_sourcesis absent or empty, it logs an error. - The annotated class must derive from
keiyoushi.source.KeiSource. Classes deriving only fromeu.kanade.tachiyomi.source.online.HttpSourceare rejected.
After this, the processor collects all non-abstract overrides declared below HttpSource, plus an override of fetchBaseUrls, so generation can decide which DSL-owned members may be generated and which user-owned members should warn or fail.
Generated class shape
Generation has two modes in SourceProcessor.kt:
buildSingleSourceClass()is used when there is exactly oneSourceDefand the annotated source class is abstract. It generatesinternal class Generated : AnnotatedSource()and injects metadata-backed members directly.buildSourceFactoryClass()is used for multiple sources or for concrete annotated source classes. It generatesinternal class Generated : SourceFactorywithcreateSources()returning the configured source instances.
For abstract sources in factory mode, each configured source is emitted as an anonymous subclass of the annotated source class. For concrete source classes, the processor calls the class constructor directly; the inline comment explains this preserves the concrete source FQN, whereas an anonymous subclass would have a null qualifiedName and break app-side delegated sources or enhanced trackers.
Concrete source classes get extra validation in validateConcreteSource(): they may only use a static base URL, must declare override val lang and override val id as primary constructor parameters, and must not own versionId. If they override name or baseUrl instead of taking those as constructor parameters, the processor warns that DSL values are still used for metadata/hosts.
DSL-owned member generation
applySourceMembers() is the central member-generation routine for abstract generated sources. It enforces that several values are owned by the DSL metadata:
filterFetchHintis generated from translations and may not be overridden by the source class.lang,id, andversionIdare DSL-owned; overriding them logs errors, withversionIdexpected to be configured in the source block rather than source code.namemay be source-owned, but then the processor warns that the DSL name is used only for metadata.baseUrlis generated for DSL-owned dynamic URL modes and is only allowed to be source-owned for static base URLs, where it warns that metadata/hosts still use the DSL value.
Static base URLs generate an override returning BaseUrlSpecData.defaultUrl. Mirror and custom base URLs generate private file-level preference helper properties and a generated baseUrl getter backed by those helpers.
Mirror and custom URL generation
For baseUrl.type == "mirrors", applySourceMembers() creates a private MirrorPreferences property initialized with getPreferences(source.id), the configured mirrors as label/URL pairs, and a localized title from stringsForLang(source.lang). The generated source's baseUrl delegates to mirrorPrefs*.baseUrl; if fetchBaseUrls is overridden, fetchableBaseUrl delegates to the same preference object. The generated class also gets setupPreferenceScreen(screen) to add mirror preferences and is made ConfigurableSource if the annotated class did not already implement it.
For baseUrl.type == "custom", the same pattern uses CustomUrlPreferences, hardcodedUrl = defaultUrl, localized title, and localized dialog message. Custom URL sources also get generated preference screen setup and optional fetchableBaseUrl integration.
addPreferenceScreen() adds the generated preference setup function and calls super.setupPreferenceScreen(screen) only when the original source class already implements ConfigurableSource; otherwise, generation adds the ConfigurableSource interface itself.
Coupling
- If the build logic changes the KSP option names or JSON shape for source metadata, update SourceProcessor.kt at the same time.
process()readskei_sourcesasList<SourceDef>, andtranslationsreadskei_translationsasMap<String, PartialLocaleStrings>; mismatched names or missing required English translation fields fail generation or localization. - If the runtime manifest or packaging logic expects a different generated entry-point class, update the
GENERATED_CLASS_PACKAGEandGENERATED_CLASS_NAMEconstants in SourceProcessor.kt together with that runtime expectation. The current generated FQN is fixed askeiyoushi.source.Generated. - If preference helper APIs change, verify mirror/custom URL generation in SourceProcessor.kt. Generated code directly constructs
keiyoushi.source.MirrorPreferences,keiyoushi.source.CustomUrlPreferences, callskeiyoushi.utils.getPreferences(id), and optionally exposeskeiyoushi.source.FetchableBaseUrl. - If the source hierarchy changes, revisit both validation constants in SourceProcessor.kt: the processor requires
keiyoushi.source.KeiSourceand useseu.kanade.tachiyomi.source.online.HttpSourceto decide which overridden members are user-provided below the base HTTP source.
Risks
- compiler/…/SourceProcessor.kt:
stringsForLang()usestranslations.getValue("en")and non-null assertions on English fields. Ifkei_translationsis omitted while a generated member needs localized strings, or if the English entry is incomplete, compilation can fail during processing rather than producing a clear validation error. compiler/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider: service registration is a single literal line. IfSourceProcessor.Provideris renamed or moved without updating this file, the compiler module can build but KSP will not discover the processor.
Path-routable coverage notes
- compiler/build.gradle.kts: module-level build setup for the KSP processor artifact and its code-generation/serialization dependencies.
- compiler/…/SourceProcessor.kt: all processor behavior, metadata data classes, validation, generated class selection, source member generation, translation handling, and KSP provider implementation.
compiler/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider: service-loader bridge from KSP toSourceProcessor.Provider.