|
1 | 1 | package buildsrc.utils
|
2 | 2 |
|
| 3 | +import java.io.File |
| 4 | +import javax.inject.Inject |
3 | 5 | import org.gradle.api.Project
|
4 |
| -import org.gradle.api.file.ProjectLayout |
5 |
| -import org.gradle.plugins.ide.idea.model.IdeaModule |
| 6 | +import org.gradle.api.Task |
| 7 | +import org.gradle.api.file.DirectoryProperty |
| 8 | +import org.gradle.api.file.FileSystemOperations |
| 9 | +import org.gradle.api.file.RegularFileProperty |
| 10 | +import org.gradle.api.provider.ValueSource |
| 11 | +import org.gradle.api.provider.ValueSourceParameters |
| 12 | +import org.gradle.configurationcache.extensions.serviceOf |
| 13 | +import org.gradle.kotlin.dsl.* |
| 14 | +import org.gradle.plugins.ide.idea.model.IdeaModel |
6 | 15 |
|
7 | 16 |
|
8 |
| -/** exclude generated Gradle code, so it doesn't clog up search results */ |
9 |
| -fun IdeaModule.excludeGeneratedGradleDsl(layout: ProjectLayout) { |
| 17 | +/** |
| 18 | + * Exclude directories containing |
| 19 | + * |
| 20 | + * - generated Gradle code, |
| 21 | + * - IDE files, |
| 22 | + * - Gradle config, |
| 23 | + * |
| 24 | + * so they don't clog up search results. |
| 25 | + */ |
| 26 | +fun Project.excludeProjectConfigurationDirs( |
| 27 | + idea: IdeaModel |
| 28 | +) { |
| 29 | + val excludedDirs = providers.of(IdeaExcludedDirectoriesSource::class) { |
| 30 | + parameters.projectDir.set(layout.projectDirectory) |
| 31 | + }.get() |
| 32 | + |
| 33 | + idea.module.excludeDirs.addAll(excludedDirs) |
| 34 | +} |
10 | 35 |
|
11 |
| - val generatedSrcDirs = listOf( |
12 |
| - "kotlin-dsl-accessors", |
13 |
| - "kotlin-dsl-external-plugin-spec-builders", |
14 |
| - "kotlin-dsl-plugins", |
15 |
| - ) |
| 36 | +// Have to use a ValueSource to find the files, otherwise Gradle |
| 37 | +// considers _all files_ an input for configuration cache 🙄 |
| 38 | +internal abstract class IdeaExcludedDirectoriesSource : |
| 39 | + ValueSource<Set<File>, IdeaExcludedDirectoriesSource.Parameters> { |
| 40 | + |
| 41 | + interface Parameters : ValueSourceParameters { |
| 42 | + val projectDir: DirectoryProperty |
| 43 | + } |
16 | 44 |
|
17 |
| - excludeDirs.addAll( |
18 |
| - layout.projectDirectory.asFile.walk() |
19 |
| - .filter { it.isDirectory && it.parentFile.name in generatedSrcDirs } |
| 45 | + override fun obtain(): Set<File> { |
| 46 | + val projectDir = parameters.projectDir.get().asFile |
| 47 | + |
| 48 | + val doNotWalkDirs = setOf( |
| 49 | + ".git", |
| 50 | + ".kotlin", |
| 51 | + ) |
| 52 | + |
| 53 | + val generatedSrcDirs = listOf( |
| 54 | + "kotlin-dsl-accessors", |
| 55 | + "kotlin-dsl-external-plugin-spec-builders", |
| 56 | + "kotlin-dsl-plugins", |
| 57 | + ) |
| 58 | + |
| 59 | + val generatedDirs = projectDir |
| 60 | + .walk() |
| 61 | + .onEnter { it.name !in doNotWalkDirs && it.parentFile.name !in generatedSrcDirs } |
| 62 | + .filter { it.isDirectory } |
| 63 | + .filter { it.parentFile.name in generatedSrcDirs } |
20 | 64 | .flatMap { file ->
|
21 | 65 | file.walk().maxDepth(1).filter { it.isDirectory }.toList()
|
22 | 66 | }
|
23 |
| - ) |
| 67 | + .toSet() |
| 68 | + |
| 69 | + // exclude .gradle, IDE dirs from nested projects (e.g. example & template projects) |
| 70 | + // so IntelliJ project-wide search isn't cluttered with irrelevant files |
| 71 | + val projectDirsToExclude = setOf( |
| 72 | + ".idea", |
| 73 | + ".gradle", |
| 74 | + "build", |
| 75 | + "gradle/wrapper", |
| 76 | + "ANDROID_SDK", |
| 77 | + "examples/versioning-multimodule-example/dokkatoo/previousDocVersions", |
| 78 | + "examples/versioning-multimodule-example/dokka/previousDocVersions", |
| 79 | + "modules/dokkatoo-plugin-integration-tests/example-project-data", |
| 80 | + ) |
| 81 | + |
| 82 | + val excludedProjectDirs = projectDir |
| 83 | + .walk() |
| 84 | + .onEnter { it.name !in doNotWalkDirs } |
| 85 | +// .filter { it.isDirectory } |
| 86 | + .filter { dir -> |
| 87 | + projectDirsToExclude.any { |
| 88 | + dir.invariantSeparatorsPath.endsWith("/$it") |
| 89 | + } |
| 90 | + } |
| 91 | + .toSet() |
| 92 | + |
| 93 | + // can't use buildSet {} https://github.com/gradle/gradle/issues/28325 |
| 94 | + return mutableSetOf<File>().apply { |
| 95 | + addAll(generatedDirs) |
| 96 | + addAll(excludedProjectDirs) |
| 97 | + } |
| 98 | + } |
24 | 99 | }
|
25 | 100 |
|
26 | 101 |
|
27 |
| -/** Sets a logo for project IDEs */ |
28 |
| -fun Project.initIdeProjectLogo( |
| 102 | +/** |
| 103 | + * Sets a logo for project IDEs. |
| 104 | + * |
| 105 | + * (Avoid updating the logo during project configuration, |
| 106 | + * instead piggyback off a random task that runs on IJ import.) |
| 107 | + */ |
| 108 | +fun Task.initIdeProjectLogo( |
29 | 109 | svgLogoPath: String
|
30 | 110 | ) {
|
31 |
| - val logoSvg = rootProject.layout.projectDirectory.file(svgLogoPath) |
32 |
| - val ideaDir = rootProject.layout.projectDirectory.dir(".idea") |
33 |
| - |
34 |
| - if ( |
35 |
| - logoSvg.asFile.exists() |
36 |
| - && ideaDir.asFile.exists() |
37 |
| - && !ideaDir.file("icon.png").asFile.exists() |
38 |
| - && !ideaDir.file("icon.svg").asFile.exists() |
39 |
| - ) { |
40 |
| - copy { |
41 |
| - from(logoSvg) { rename { "icon.svg" } } |
42 |
| - into(ideaDir) |
| 111 | + val fs = project.serviceOf<FileSystemOperations>() |
| 112 | + |
| 113 | + val logoSvg = project.layout.projectDirectory.file(svgLogoPath) |
| 114 | + val ideaDir = project.layout.projectDirectory.dir(".idea") |
| 115 | + // don't register task inputs, we don't really care about up-to-date checks |
| 116 | + |
| 117 | + doLast("initIdeProjectLogo") { |
| 118 | + if ( |
| 119 | + logoSvg.asFile.exists() |
| 120 | + && ideaDir.asFile.exists() |
| 121 | + && !ideaDir.file("icon.png").asFile.exists() |
| 122 | + && !ideaDir.file("icon.svg").asFile.exists() |
| 123 | + ) { |
| 124 | + fs.copy { |
| 125 | + from(logoSvg) { rename { "icon.svg" } } |
| 126 | + into(ideaDir) |
| 127 | + } |
43 | 128 | }
|
44 | 129 | }
|
45 | 130 | }
|
0 commit comments