Skip to content

Commit c27ed28

Browse files
Knit clean task (#565)
Co-authored-by: Andrey Bragin <[email protected]>
1 parent a3b3692 commit c27ed28

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ kover {
223223
val excludedProjects = setOf(
224224
":integration-tests",
225225
":examples",
226-
":buildSrc"
226+
":buildSrc",
227+
":docs",
227228
)
228229
merge {
229230
subprojects {

docs/README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ To ensure code snippets in documentation are compilable and up-to-date with the
3131
Knit provides a Gradle plugin that extracts specially annotated Kotlin code snippets from markdown files and generates Kotlin source files.
3232

3333
#### How to fix docs?
34-
1. Run knit to extract code snippets to /src/main/kotlin:
34+
1. Run `:docs:knitAssemble` task which will clean old knit-generated files, extract fresh code snippets to /src/main/kotlin and assemble the docs project:
3535
```
36-
./gradlew :docs:knit
36+
./gradlew :docs:knitAssemble
3737
```
38-
2. Run assemble to get compilation arrows:
39-
```
40-
./gradlew :docs:assemble
41-
```
42-
3. Navigate to the file with the compilation error `example-[md-file-name]-[index].kt`
43-
4. Fix the error in this file
44-
5. Navigate to the code snippet in Markdown `md-file-name.md` by searing `<!--- KNIT example-[md-file-name]-[index].kt` -->`
45-
6. Update the code snippet to reflect the changes in kt file
38+
2. Navigate to the file with the compilation error `example-[md-file-name]-[index].kt`
39+
3. Fix the error in this file
40+
4. Navigate to the code snippet in Markdown `md-file-name.md` by searing `<!--- KNIT example-[md-file-name]-[index].kt` -->`
41+
5. Update the code snippet to reflect the changes in kt file
4642
* Update dependencies (usually they are provided in `<!--- INCLUDE -->` section)
4743
* Edit code (don't forget about tabulation when you just copy paste from kt)
4844

docs/build.gradle.kts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.Properties
2+
13
group = rootProject.group
24
version = rootProject.version
35

@@ -14,6 +16,28 @@ dependencies {
1416
implementation(libs.opentelemetry.exporter.logging)
1517
}
1618

19+
val knitProperties: Provider<Properties> =
20+
providers.fileContents(layout.projectDirectory.file("knit.properties"))
21+
.asText
22+
.map { text ->
23+
Properties().apply {
24+
text.reader().use { load(it) }
25+
}
26+
}
27+
28+
val knitDir: Provider<String> =
29+
knitProperties.map { props ->
30+
requireNotNull(props.getProperty("knit.dir")) {
31+
"Missing 'knit.dir' in knit.properties"
32+
}
33+
}
34+
35+
ktlint {
36+
filter {
37+
exclude { it.file.path.contains("/docs/${knitDir.get()}/") }
38+
}
39+
}
40+
1741
knit {
1842
rootDir = project.rootDir
1943
files = fileTree("docs/") {
@@ -22,3 +46,22 @@ knit {
2246
moduleDocs = "docs/modules.md"
2347
siteRoot = "https://docs.koog.ai/"
2448
}
49+
50+
tasks.register<Delete>("knitClean") {
51+
delete(
52+
fileTree(project.rootDir) {
53+
include("**/docs/${knitDir.get()}/**")
54+
}
55+
)
56+
}
57+
58+
tasks.named("clean") {
59+
dependsOn("knitClean")
60+
}
61+
62+
tasks.register<Delete>("knitAssemble") {
63+
dependsOn("knitClean", "knit", "assemble")
64+
}
65+
66+
tasks.named("knit").configure { mustRunAfter("knitClean") }
67+
tasks.named("assemble").configure { mustRunAfter("knit") }

0 commit comments

Comments
 (0)