Introducing a new way to review your PRs.

Read the announcement

keiyoushi / extensions-source

Source code of extensions in https://github.com/keiyoushi/extensions

View on GitHub

13 documents · Updated Nov 19, 2026

docs / compiler-module

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.SymbolProcessorProvider is 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 KSP CodeGenerator, options, and logger into SourceProcessor.
  • SourceProcessor.process() is the main task. It gathers @Source classes, validates that the Gradle-provided kei_sources option and annotation are both present, decodes SourceDef metadata, checks inheritance, determines which source properties/functions are already overridden, and writes keiyoushi.source.Generated through 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_sources and translations.

Data model

SourceProcessor.kt defines the serialized metadata it expects from build configuration:

  • SourceDef is the per-source definition decoded from kei_sources. It contains name, lang, numeric id, and baseUrl.
  • BaseUrlSpecData contains a type, a defaultUrl, and optional mirrors. The processor recognizes three base URL modes in generation: static, mirrors, and custom.
  • MirrorData contains a mirror url and optional display label.
  • PartialLocaleStrings is decoded from kei_translations, keyed by language code. stringsForLang() resolves locale-specific strings by exact language, then language prefix before -, then required English defaults.
  • LocaleStrings is 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:

  1. If there are no annotated classes and no kei_sources option, the processor exits without generation.
  2. If kei_sources exists but no annotated class is found, it logs an error telling the extension author to annotate the source class.
  3. If more than one @Source class is found, it logs an error because only one annotated source class is allowed per module.
  4. If an annotated class exists but kei_sources is absent or empty, it logs an error.
  5. The annotated class must derive from keiyoushi.source.KeiSource. Classes deriving only from eu.kanade.tachiyomi.source.online.HttpSource are 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 one SourceDef and the annotated source class is abstract. It generates internal class Generated : AnnotatedSource() and injects metadata-backed members directly.
  • buildSourceFactoryClass() is used for multiple sources or for concrete annotated source classes. It generates internal class Generated : SourceFactory with createSources() 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:

  • filterFetchHint is generated from translations and may not be overridden by the source class.
  • lang, id, and versionId are DSL-owned; overriding them logs errors, with versionId expected to be configured in the source block rather than source code.
  • name may be source-owned, but then the processor warns that the DSL name is used only for metadata.
  • baseUrl is 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() reads kei_sources as List<SourceDef>, and translations reads kei_translations as Map<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_PACKAGE and GENERATED_CLASS_NAME constants in SourceProcessor.kt together with that runtime expectation. The current generated FQN is fixed as keiyoushi.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, calls keiyoushi.utils.getPreferences(id), and optionally exposes keiyoushi.source.FetchableBaseUrl.
  • If the source hierarchy changes, revisit both validation constants in SourceProcessor.kt: the processor requires keiyoushi.source.KeiSource and uses eu.kanade.tachiyomi.source.online.HttpSource to decide which overridden members are user-provided below the base HTTP source.

Risks

  • compiler/…/SourceProcessor.kt: stringsForLang() uses translations.getValue("en") and non-null assertions on English fields. If kei_translations is 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. If SourceProcessor.Provider is 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 to SourceProcessor.Provider.