-
Notifications
You must be signed in to change notification settings - Fork 319
Adding Config Registry Linter to Ensure Alias Keys Are Documented #10068
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,12 +23,14 @@ class ConfigInversionLinter : Plugin<Project> { | |||||||||||||
| registerLogEnvVarUsages(target, extension) | ||||||||||||||
| registerCheckEnvironmentVariablesUsage(target) | ||||||||||||||
| registerCheckConfigStringsTask(target, extension) | ||||||||||||||
| verifyAliasKeysAreSupported(target, extension) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Data class for fields from generated class | ||||||||||||||
| private data class LoadedConfigFields( | ||||||||||||||
| val supported: Set<String>, | ||||||||||||||
| val aliases: Map<String, List<String>> = emptyMap(), | ||||||||||||||
| val aliasMapping: Map<String, String> = emptyMap() | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -53,9 +55,13 @@ private fun loadConfigFields( | |||||||||||||
| else -> throw IllegalStateException("SUPPORTED field must be either Set<String> or Map<String, Any>, but was ${supportedField?.javaClass}") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Suppress("UNCHECKED_CAST") | ||||||||||||||
| val aliases = clazz.getField("ALIASES").get(null) as Map<String, List<String>> | ||||||||||||||
|
|
||||||||||||||
| @Suppress("UNCHECKED_CAST") | ||||||||||||||
| val aliasMappingMap = clazz.getField("ALIAS_MAPPING").get(null) as Map<String, String> | ||||||||||||||
| LoadedConfigFields(supportedSet, aliasMappingMap) | ||||||||||||||
|
|
||||||||||||||
| LoadedConfigFields(supportedSet, aliases, aliasMappingMap) | ||||||||||||||
| }.also { cachedConfigFields = it } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -246,3 +252,44 @@ private fun registerCheckConfigStringsTask(project: Project, extension: Supporte | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| /** Registers `verifyAliasKeysAreSupported` to ensure all alias keys are documented as supported configurations. */ | ||||||||||||||
| private fun verifyAliasKeysAreSupported(project: Project, extension: SupportedTracerConfigurations) { | ||||||||||||||
| val ownerPath = extension.configOwnerPath | ||||||||||||||
| val generatedFile = extension.className | ||||||||||||||
|
|
||||||||||||||
| project.tasks.register("verifyAliasKeysAreSupported") { | ||||||||||||||
| group = "verification" | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: By the way you may use this constant |
||||||||||||||
| description = | ||||||||||||||
| "Verifies that all alias keys in `metadata/supported-configurations.json` are also documented as supported configurations." | ||||||||||||||
|
|
||||||||||||||
| val mainSourceSetOutput = ownerPath.map { | ||||||||||||||
| project.project(it) | ||||||||||||||
| .extensions.getByType<SourceSetContainer>() | ||||||||||||||
| .named(SourceSet.MAIN_SOURCE_SET_NAME) | ||||||||||||||
| .map { main -> main.output } | ||||||||||||||
| } | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: It's possible to unwrap the nested value with a
Suggested change
|
||||||||||||||
| inputs.files(mainSourceSetOutput) | ||||||||||||||
|
|
||||||||||||||
| doLast { | ||||||||||||||
| val configFields = loadConfigFields(mainSourceSetOutput.get().get(), generatedFile.get()) | ||||||||||||||
| val supported = configFields.supported | ||||||||||||||
| val aliases = configFields.aliases.keys | ||||||||||||||
|
|
||||||||||||||
| val unsupportedAliasKeys = aliases - supported | ||||||||||||||
| val violations = buildList { | ||||||||||||||
| unsupportedAliasKeys.forEach { key -> | ||||||||||||||
| add("$key is listed as an alias key but is not documented as a supported configuration in the `supportedConfigurations` key") | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| if (violations.isNotEmpty()) { | ||||||||||||||
| logger.error("\nFound alias keys not documented as supported configurations:") | ||||||||||||||
| violations.forEach { logger.lifecycle(it) } | ||||||||||||||
| throw GradleException("Undocumented alias keys found. Please add the above keys to the `supportedConfigurations` in '${extension.jsonFile.get()}'.") | ||||||||||||||
| } else { | ||||||||||||||
| logger.info("All alias keys are documented as supported configurations.") | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.