Skip to content

Add extractSnippets Gradle task and plugin to export transformed snippets - #1037

Merged
bsagmoe merged 4 commits into
mainfrom
bsagmoe/extract-snippets-task
Aug 24, 2026
Merged

Add extractSnippets Gradle task and plugin to export transformed snippets#1037
bsagmoe merged 4 commits into
mainfrom
bsagmoe/extract-snippets-task

Conversation

@bsagmoe

@bsagmoe bsagmoe commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an extractSnippets Gradle plugin in buildSrc to extract snippets and transform them to match rendering on developer.android.com. Output files are saved to each module's build/extracted-snippets/ directory, making it easy to use them as the source for inline code blocks while staging new or updated documentation.

Details:

  • Extracts [START <tag>] regions, handles [START_EXCLUDE] comments and silent blocks, strips inner delimiters, and normalizes indentation the same way the DevSite processor does.
  • Supports repository-wide extraction, per-module execution, and filtering via -Ptag=<name> or -Ppackage=<path>.
  • Includes 17 unit tests verifying region parsing, comment exclusion styles, delimiter stripping, and warning diagnostics.

Usage:

# Extract all snippets across the entire repository
./gradlew extractSnippets

# Extract snippets for a specific submodule
./gradlew :compose:snippets:extractSnippets

# Filter by snippet tag or package substring
./gradlew :compose:snippets:extractSnippets -Ptag=android_compose_navigation3_basic_2
./gradlew :room:extractSnippets -Ppackage=migration

@bsagmoe
bsagmoe force-pushed the bsagmoe/extract-snippets-task branch 5 times, most recently from 1c8382f to 4d837ca Compare August 18, 2026 19:13
…pets

Adds a custom extractSnippets task and plugin in buildSrc to parse region tags, handle [START_EXCLUDE] blocks (with language-specific ellipses comments including block comments), strip region delimiters, normalize CRLF/LF line endings, and normalize indentation via trimIndent(). Supports per-submodule execution, multiplatform source sets (including .kt, .java, .xml, .json, .proto, .swift, .kts, .gradle, .pro), duplicate tag detection, explicit UTF-8 I/O, output directory purging, and tag (-Ptag=...) / package (-Ppackage=...) filtering. Includes a golden unit test suite in buildSrc and diagnostic warning reporting.
@bsagmoe
bsagmoe force-pushed the bsagmoe/extract-snippets-task branch from 4d837ca to e523e4b Compare August 18, 2026 19:31
@bsagmoe bsagmoe changed the title Add extractSnippets Gradle task to extract DevSite-transformed snippets Add extractSnippets Gradle task and plugin to export transformed snippets Aug 18, 2026
@bsagmoe
bsagmoe requested a review from kkuan2011 August 19, 2026 18:22
@bsagmoe
bsagmoe marked this pull request as ready for review August 19, 2026 18:26
@bsagmoe
bsagmoe requested a review from a team as a code owner August 19, 2026 18:26
@snippet-bot

snippet-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Here is the summary of possible violations 😱

Details

There are 33 possible violations for not having product prefix.
There are format violations for 4 region tags.

The end of the violation section. All the stuff below is FYI purposes only.


Here is the summary of changes.

You are about to add 34 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@kkuan2011

Copy link
Copy Markdown
Contributor

Thanks Ben! I haven't reviewed the code yet but could you clarify the use case you're envisioning here? "making it easy to use them as the source for inline code blocks while staging new or updated documentation."

Also could you share an example of what the output file will look like? Is it a list of region tags for that module?

@bsagmoe

bsagmoe commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

An example would be I'm authoring some new documentation for returning results in Nav 3 and am adding snippets in #1035. But because those snippets aren't yet available in the GitHub mirror that DAC pulls from, it's easier to inline them in the overall DAC CL while I'm working on it to see how the final page will look.

This build script makes it easier to author the snippets just in the snippets repo and copy them over to inline code in the DAC CL.

For example, with this as the snippet file:

// compose/snippets/src/main/java/com/example/compose/snippets/navigation3/decorators/DecoratorSnippets.kt 
@Composable
fun DecoratorsResultEventBus() {
    // [START android_compose_navigation3_decorator_result_hoist]
    val resultEventBus = rememberResultEventBus()

    NavDisplay(
        // [START_EXCLUDE]
        backStack = rememberNavBackStack(Home),
        entryProvider = entryProvider<NavKey> {
            entry<Home> { Text("Welcome to Nav3") }
        },
        // [END_EXCLUDE]
        entryDecorators = listOf(
            rememberSaveableStateHolderNavEntryDecorator<NavKey>(),
            rememberResultEventBusNavEntryDecorator<NavKey>(resultEventBus = resultEventBus)
        )
    )
    // [END android_compose_navigation3_decorator_result_hoist]
}

Running ./gradlew :compose:snippets:extractSnippets -Ptag=android_compose_navigation3_decorator_result_hoist would output the following in compose/snippets/build/extracted-snippets/android_compose_navigation3_decorator_result_hoist.kt:

val resultEventBus = rememberResultEventBus()

NavDisplay(
    // ...
    entryDecorators = listOf(
        rememberSaveableStateHolderNavEntryDecorator<NavKey>(),
        rememberResultEventBusNavEntryDecorator<NavKey>(resultEventBus = resultEventBus)
    )
)

Which I (or an agent) can easily copy-paste into the DAC files.

It also sounds like this would be useful for including snippets in skills during the eval phase before they're fully published on DAC.

@kkuan2011 kkuan2011 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additional context, Ben! Cool that it can show a rendered version of what the snippet would look like on the webpage.

If this addition doesn't affect build time for people using snippets repo, then sounds good to submit. Otherwise, let's discuss more.

Comment thread buildSrc/src/main/kotlin/ExtractSnippets.kt Outdated
Comment thread buildSrc/src/main/kotlin/ExtractSnippetsPlugin.kt Outdated
Comment thread buildSrc/src/main/kotlin/ExtractSnippets.kt Outdated
Comment thread build.gradle.kts
Comment thread buildSrc/src/main/kotlin/ExtractSnippets.kt Outdated
@kkuan2011

Copy link
Copy Markdown
Contributor

Also is there any way to get the snippet-bot check to pass? I saw that you added the exclusion and I tried to get it to run again by clicking the checkbox "Refresh this comment" but that didn't seem to work.

@bsagmoe

bsagmoe commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Also is there any way to get the snippet-bot check to pass? I saw that you added the exclusion and I tried to get it to run again by clicking the checkbox "Refresh this comment" but that didn't seem to work.

Yeah, tried that as well. Dug into it and it looks like it always runs against the current main branch snippet-bot.yml, so the only way to do it would be to merge that as a change first. I think we should be OK to merge as-is, and any future changes in buildSrc will be excluded.

- Move buildSrc extraction sources into com.android.snippets.build.extractor package.
- Add @option annotations to enable double-dash CLI arguments (--tag, --package).
- Configure property conventions in plugin task registration for Configuration Cache compatibility.
- Remove test directory exclusions to scan documentation test snippets.
- Require tag names in ANY_DELIMITER_REGEX and remove early-return in END_EXCLUDE handling.
- Replace external DevSite references with generic documentation terminology.
- Add unit tests for multi-exclude, warning reporting branches, and tag filtering isolation.
@bsagmoe
bsagmoe merged commit 71b157d into main Aug 24, 2026
9 of 10 checks passed
@bsagmoe
bsagmoe deleted the bsagmoe/extract-snippets-task branch August 24, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants