-
-
Notifications
You must be signed in to change notification settings - Fork 424
Simplify code snippets executor #1310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 20 additions & 19 deletions
39
src/integrationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/DocCodeSnippetTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,36 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.executable.CodeSnippetExtractor | ||
| import com.github.jengelman.gradle.plugins.shadow.executor.GroovyBuildExecutor | ||
| import com.github.jengelman.gradle.plugins.shadow.executor.NoopExecutor | ||
| import com.github.jengelman.gradle.plugins.shadow.fixture.GroovyDslFixture | ||
| import com.github.jengelman.gradle.plugins.shadow.snippet.CodeSnippetExtractor | ||
| import com.github.jengelman.gradle.plugins.shadow.snippet.DslLang | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.Path | ||
| import kotlin.io.path.createTempDirectory | ||
| import org.junit.jupiter.api.DynamicTest | ||
| import org.junit.jupiter.api.TestFactory | ||
| import org.junit.jupiter.api.io.TempDir | ||
|
|
||
| class DocCodeSnippetTest { | ||
|
|
||
| @OptIn(ExperimentalStdlibApi::class) | ||
| @TestFactory | ||
| fun provideDynamicTests(@TempDir root: Path): List<DynamicTest> { | ||
| return fixtures.flatMap { (selector, executor) -> | ||
| CodeSnippetExtractor.extract(root, docsDir, selector, executor) | ||
| }.map { | ||
| DynamicTest.dynamicTest(it.testName, it) | ||
| val langExecutables = DslLang.entries.map { executor -> | ||
| CodeSnippetExtractor.extract(executor) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private val fixtures = mapOf( | ||
| "groovy" to GroovyBuildExecutor( | ||
| GroovyDslFixture, | ||
| GroovyDslFixture.importsExtractor, | ||
| ), | ||
| "groovy no-run" to NoopExecutor, | ||
| ) | ||
| check(langExecutables.sumOf { it.size } > 0) { | ||
| "No code snippets found." | ||
| } | ||
| check(langExecutables.size == DslLang.entries.size) { | ||
| "We must provide build script snippets for all languages." | ||
| } | ||
| check(langExecutables.map { it.size }.distinct().size == 1) { | ||
| "All languages must have the same number of code snippets." | ||
| } | ||
|
|
||
| val docsDir: Path = Path(System.getProperty("DOCS_DIR")) | ||
| return langExecutables.flatten().map { | ||
| // Create a temporary directory for each test, root will be deleted after all tests are run. | ||
| it.tempDir = createTempDirectory(root) | ||
| DynamicTest.dynamicTest(it.displayName, it) | ||
| } | ||
| } | ||
| } | ||
22 changes: 0 additions & 22 deletions
22
...est/kotlin/com/github/jengelman/gradle/plugins/shadow/executable/CodeSnippetExecutable.kt
This file was deleted.
Oops, something went wrong.
92 changes: 0 additions & 92 deletions
92
...Test/kotlin/com/github/jengelman/gradle/plugins/shadow/executable/CodeSnippetExtractor.kt
This file was deleted.
Oops, something went wrong.
71 changes: 0 additions & 71 deletions
71
...ionTest/kotlin/com/github/jengelman/gradle/plugins/shadow/executor/GroovyBuildExecutor.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
...ntegrationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/executor/NoopExecutor.kt
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
...grationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/executor/SnippetExecutor.kt
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
...grationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/fixture/GroovyDslFixture.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...tegrationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/fixture/SnippetFixture.kt
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
...ionTest/kotlin/com/github/jengelman/gradle/plugins/shadow/snippet/CodeSnippetExtractor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.snippet | ||
|
|
||
| import java.nio.file.Path | ||
| import java.util.regex.Pattern | ||
| import kotlin.io.path.ExperimentalPathApi | ||
| import kotlin.io.path.Path | ||
| import kotlin.io.path.name | ||
| import kotlin.io.path.pathString | ||
| import kotlin.io.path.readText | ||
| import kotlin.io.path.relativeTo | ||
| import kotlin.io.path.walk | ||
|
|
||
| object CodeSnippetExtractor { | ||
| private val docRoot = Path(System.getProperty("DOCS_DIR")) | ||
|
|
||
| @OptIn(ExperimentalPathApi::class) | ||
| private val markdownPaths = docRoot.walk() | ||
| .filter { it.name.endsWith(".md", ignoreCase = true) } | ||
| .toList() | ||
|
|
||
| fun extract(lang: DslLang): List<SnippetExecutable> { | ||
| return markdownPaths.flatMap { path -> | ||
| createExecutables(lang, path) | ||
| } | ||
| } | ||
|
|
||
| private fun createExecutables( | ||
| lang: DslLang, | ||
| markdownPath: Path, | ||
| ): List<SnippetExecutable> { | ||
| val relativeDocPath = markdownPath.relativeTo(docRoot).pathString | ||
| return createSnippets(markdownPath.readText(), lang).map { (lineNumber, snippet) -> | ||
| SnippetExecutable.create( | ||
| lang, | ||
| snippet, | ||
| "$relativeDocPath:$lineNumber", | ||
| ) { | ||
| RuntimeException("The error line in the doc is near ${markdownPath.toUri()}:$lineNumber", it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun createSnippets(source: String, lang: DslLang) = buildMap { | ||
| val pattern = Pattern.compile("(?ims)```${lang}\n(.*?)\n```") | ||
| val matcher = pattern.matcher(source) | ||
|
|
||
| while (matcher.find()) { | ||
| val line = source.lineNumberAt(matcher.start()) | ||
| val code = matcher.group(1) | ||
| put(line, code) | ||
| } | ||
| } | ||
|
|
||
| private fun String.lineNumberAt(index: Int): Int { | ||
| var line = 1 | ||
| for (i in 0 until index.coerceAtMost(length)) { | ||
| if (this[i] == '\n') line++ | ||
| } | ||
| return line | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/integrationTest/kotlin/com/github/jengelman/gradle/plugins/shadow/snippet/DslLang.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.snippet | ||
|
|
||
| enum class DslLang { | ||
| // Kotlin, | ||
| Groovy, | ||
| ; | ||
|
|
||
| override fun toString(): String = name.lowercase() | ||
| } |
19 changes: 19 additions & 0 deletions
19
...onTest/kotlin/com/github/jengelman/gradle/plugins/shadow/snippet/GroovyBuildExecutable.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.snippet | ||
|
|
||
| class GroovyBuildExecutable( | ||
| override val snippet: String, | ||
| override val displayName: String, | ||
| override val exceptionTransformer: (Throwable) -> Throwable, | ||
| ) : SnippetExecutable() { | ||
|
|
||
| override val lang: DslLang = DslLang.Groovy | ||
|
|
||
| override val buildScriptName: String = "build.gradle" | ||
|
|
||
| override val pluginsBlock: String = """ | ||
| plugins { | ||
| id 'java' | ||
| id 'com.gradleup.shadow' | ||
| } | ||
| """.trimIndent() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.