-
Notifications
You must be signed in to change notification settings - Fork 20
/
CreateFocusSettingsTask.kt
77 lines (65 loc) · 2.53 KB
/
CreateFocusSettingsTask.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.dropbox.focus
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
@DisableCachingByDefault(because = "Not worth caching")
public abstract class CreateFocusSettingsTask : DefaultTask() {
@get:OutputFile
public abstract val settingsFile: RegularFileProperty
init {
outputs.upToDateWhen { false }
}
@TaskAction
public fun createFocusSettings() {
val dependencies = project.collectDependencies().sortedBy { it.path }
settingsFile.get().asFile.writer().use { writer ->
writer.write("// ${project.path} specific settings\n")
writer.appendLine("//")
writer.appendLine("// This file is autogenerated by the focus task. Changes will be overwritten.")
writer.appendLine()
// Add the includes statements
dependencies.forEach { dep ->
writer.appendLine("include(\"${dep.path}\")")
}
writer.appendLine()
// Add overrides for projects with a root that's different from the gradle path
dependencies
.forEach { dep ->
val gradleProjectPath = dep.path.substring(1).replace(":", "/")
if (project.rootDir.resolve(gradleProjectPath) != dep.projectDir) {
val gradleProjectDir = dep.projectDir.path.replace("\\", "\\\\")
// language=groovy
writer.appendLine("""project("${dep.path}").projectDir = new File('$gradleProjectDir')""")
}
}
}
}
private fun Project.collectDependencies(): Set<Project> {
val result = mutableSetOf<Project>()
fun addDependent(project: Project) {
val configuredProject = this.evaluationDependsOn(project.path)
if (result.add(configuredProject)) {
configuredProject.configurations.forEach { config ->
config.dependencies
.filterIsInstance<ProjectDependency>()
.map { it.dependencyProject }
.forEach(::addDependent)
}
}
}
addDependent(this)
return result
}
public companion object {
public operator fun invoke(subExtension: FocusSubExtension): CreateFocusSettingsTask.() -> Unit = {
group = FOCUS_TASK_GROUP
settingsFile.set(subExtension.focusSettingsFile)
notCompatibleWithConfigurationCache("This reads configurations from the project at action-time.")
}
}
}