|
| 1 | +package com.getcode.buildlogic.analytics |
| 2 | + |
| 3 | +import org.gradle.api.DefaultTask |
| 4 | +import org.gradle.api.GradleException |
| 5 | +import org.gradle.api.file.RegularFileProperty |
| 6 | +import org.gradle.api.provider.Property |
| 7 | +import org.gradle.api.tasks.Input |
| 8 | +import org.gradle.api.tasks.InputFile |
| 9 | +import org.gradle.api.tasks.Internal |
| 10 | +import org.gradle.api.tasks.OutputFile |
| 11 | +import org.gradle.api.tasks.PathSensitive |
| 12 | +import org.gradle.api.tasks.PathSensitivity |
| 13 | +import org.gradle.api.tasks.TaskAction |
| 14 | + |
| 15 | +private fun page(catalogue: RegularFileProperty): String = try { |
| 16 | + AnalyticsCatalogue.document(AnalyticsCatalogue.parse(catalogue.get().asFile.readText())) |
| 17 | +} catch (malformed: CatalogueException) { |
| 18 | + throw GradleException(malformed.message.orEmpty(), malformed) |
| 19 | +} |
| 20 | + |
| 21 | +/** Writes `EVENTS.md` from `events.toml`. The page is checked in, so this writes into the module. */ |
| 22 | +abstract class WriteAnalyticsEventsPage : DefaultTask() { |
| 23 | + |
| 24 | + @get:InputFile |
| 25 | + @get:PathSensitive(PathSensitivity.NONE) |
| 26 | + abstract val catalogue: RegularFileProperty |
| 27 | + |
| 28 | + @get:OutputFile |
| 29 | + abstract val page: RegularFileProperty |
| 30 | + |
| 31 | + @TaskAction |
| 32 | + fun write() = page.get().asFile.writeText(page(catalogue)) |
| 33 | +} |
| 34 | + |
| 35 | +/** Fails when the checked-in `EVENTS.md` is not what `events.toml` generates. */ |
| 36 | +abstract class CheckAnalyticsEventsPage : DefaultTask() { |
| 37 | + |
| 38 | + @get:InputFile |
| 39 | + @get:PathSensitive(PathSensitivity.NONE) |
| 40 | + abstract val catalogue: RegularFileProperty |
| 41 | + |
| 42 | + /** Read only if it exists: a missing page is reported, not a missing-input failure. */ |
| 43 | + @get:Internal |
| 44 | + abstract val page: RegularFileProperty |
| 45 | + |
| 46 | + /** The command the failure tells the reader to run. */ |
| 47 | + @get:Input |
| 48 | + abstract val fixCommand: Property<String> |
| 49 | + |
| 50 | + @TaskAction |
| 51 | + fun check() { |
| 52 | + val file = page.get().asFile |
| 53 | + val expected = page(catalogue) |
| 54 | + if (!file.exists() || file.readText() != expected) { |
| 55 | + throw GradleException( |
| 56 | + "${file.name} is out of date with events.toml. Run `${fixCommand.get()}` and commit the result.", |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments