Skip to content

Commit a8dceff

Browse files
committed
new way for creating custom task
1 parent 3c74312 commit a8dceff

File tree

4 files changed

+85
-66
lines changed

4 files changed

+85
-66
lines changed

README.md

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ affectedModuleDetector {
9494
]
9595
includeUncommitted = true
9696
top = "HEAD"
97+
customTasks = [
98+
new AffectedModuleConfiguration.CustomTask(
99+
"runDetektByImpact",
100+
"detekt",
101+
"Run static analysis tool without auto-correction by Impact analysis"
102+
)
103+
]
97104
}
98105
```
99106

@@ -109,7 +116,7 @@ affectedModuleDetector {
109116
- `excludedModules`: A list of modules that will be excluded from the build process
110117
- `includeUncommitted`: If uncommitted files should be considered affected
111118
- `top`: The top of the git log to use. Must be used in combination with configuration `includeUncommitted = false`
112-
- `customTasks`: set of enum fields which implemented [AffectedModuleTaskType]()
119+
- `customTasks`: set of [CustomTask](https://github.com/dropbox/AffectedModuleDetector/blob/main/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt)
113120

114121
By default, the Detector will look for `assembleAndroidDebugTest`, `connectedAndroidDebugTest`, and `testDebug`. Modules can specify a configuration block to specify which variant tests to run:
115122
```groovy
@@ -175,44 +182,22 @@ You should see zero tests run. Make a change within one of the modules and commi
175182
## Custom tasks
176183

177184
If you want to add a custom gradle command to execute with impact analysis
178-
you must create data structure which implementing [AffectedModuleTaskType]().
179-
180-
**NOTE1:** Your declared data structure might be in any place of your project.<br/>
181-
**NOTE2:** In example bottom we using ENUM, but it not necessary. You can use any way for overriding fields of interface.
182-
183-
Example:
184-
```kotlin
185-
enum class CustomImpactAnalysisTaskType(
186-
override val commandByImpact: String,
187-
override val originalGradleCommand: String,
188-
override val taskDescription: String
189-
): AffectedModuleTaskType {
190-
191-
DETEKT_TASK(
192-
commandByImpact = "runDetektByImpact",
193-
originalGradleCommand = "detekt",
194-
taskDescription = "Run static analysis tool without auto-correction by Impact analysis"
195-
)
196-
197-
// other tasks
198-
}
199-
```
200-
201-
And then you must add your custom enum fields to configuration in `build.gradle` of your project:
185+
you must declare [AffectedModuleConfiguration.CustomTask](https://github.com/dropbox/AffectedModuleDetector/blob/main/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt)
186+
which is implementing [AffectedModuleTaskType]() in the configuration in `build.gradle` of your project:
202187

203188
```groovy
204-
// some code
189+
// ...
205190
206191
affectedModuleDetector {
207-
baseDir = "${project.rootDir}"
208-
pathsAffectingAllModules = ["buildSrc/"]
209-
specifiedBranch = "dev"
210-
customTasks = [ // <- list of enum fields
211-
CustomImpactAnalysisTaskType.DETEKT_TASK,
212-
CustomImpactAnalysisTaskType.OTHER_TASK
192+
// ...
193+
customTasks = [
194+
new AffectedModuleConfiguration.CustomTask(
195+
"runDetektByImpact",
196+
"detekt",
197+
"Run static analysis tool without auto-correction by Impact analysis"
198+
)
213199
]
214-
compareFrom = "SpecifiedBranchCommit"
215-
includeUncommitted = false
200+
// ...
216201
}
217202
```
218203

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,45 @@ import java.io.File
44

55
class AffectedModuleConfiguration {
66

7+
/**
8+
* Implementation of [AffectedModuleTaskType] for easy adding of custom gradle task to
9+
* AffectedModuleDetector. You can declare a new instance of it in build.gradle.
10+
*
11+
* @see AffectedModuleTaskType - interface
12+
* @see customTasks - configuration field
13+
*/
14+
data class CustomTask(
15+
override val commandByImpact: String,
16+
override val originalGradleCommand: String,
17+
override val taskDescription: String
18+
) : AffectedModuleTaskType
19+
20+
/**
21+
* If you want to add a custom task for impact analysis you must set the list
22+
* of [AffectedModuleTaskType] implementations.
23+
*
24+
* Example:
25+
* `build.gradle
26+
*
27+
* affectedModuleDetector {
28+
* ...
29+
* customTasks = [ // <- list of custom gradle invokes
30+
* new AffectedModuleConfiguration.CustomTask(
31+
* "runSomeCustomTaskByImpact",
32+
* "someTaskForExample",
33+
* "Task description."
34+
* )
35+
* ]
36+
* ...
37+
* }
38+
* `
39+
*
40+
* @see AffectedModuleTaskType - interface
41+
* @see CustomTask - Implementation class
42+
* @see AffectedModuleDetectorPlugin - gradle plugin
43+
*/
44+
var customTasks = emptySet<AffectedModuleConfiguration.CustomTask>()
45+
746
/**
847
* Folder to place the log in
948
*/
@@ -64,28 +103,6 @@ class AffectedModuleConfiguration {
64103
*/
65104
var includeUncommitted: Boolean = true
66105

67-
/**
68-
* If you want to add a custom task for impact analysis you must set the list
69-
* of [AffectedModuleTaskType] implementations.
70-
*
71-
* Example:
72-
* `build.gradle
73-
*
74-
* affectedModuleDetector {
75-
* baseDir = "${project.rootDir}"
76-
* pathsAffectingAllModules = ["buildSrc/"]
77-
* specifiedBranch = "dev"
78-
* customTasks = [MyCustomTask.DETEKT_TASK] // <- list of enum fields
79-
* compareFrom = "SpecifiedBranchCommit"
80-
* includeUncommitted = false
81-
* }
82-
* `
83-
*
84-
* @see AffectedModuleTaskType
85-
* @see AffectedModuleDetectorPlugin
86-
*/
87-
var customTasks = emptySet<AffectedModuleTaskType>()
88-
89106
/**
90107
* The top of the git log to use, only used when [includeUncommitted] is false
91108
*/

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
8282
lazyMessage = { "Unable to find ${AffectedTestConfiguration.name} in $rootProject" }
8383
) as AffectedModuleConfiguration
8484

85-
mainConfiguration
86-
.customTasks
87-
.forEach { taskType ->
88-
registerImpactAnalysisTask(
89-
rootProject = rootProject,
90-
taskType = taskType,
91-
groupName = CUSTOM_TASK_GROUP_NAME
92-
)
93-
}
85+
rootProject.afterEvaluate {
86+
mainConfiguration
87+
.customTasks
88+
.forEach { taskType ->
89+
registerImpactAnalysisTask(
90+
rootProject = rootProject,
91+
taskType = taskType,
92+
groupName = CUSTOM_TASK_GROUP_NAME
93+
)
94+
}
95+
}
9496
}
9597

9698
@VisibleForTesting

sample/build.gradle

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.dropbox.affectedmoduledetector.AffectedModuleConfiguration
12
import com.dropbox.sample.Dependencies
23

34
// Top-level build file where you can add configuration options common to all sub-projects/modules.
@@ -27,13 +28,27 @@ affectedModuleDetector {
2728
]
2829
specifiedBranch = "main"
2930
compareFrom = "SpecifiedBranchCommit"
30-
31+
customTasks = [
32+
new AffectedModuleConfiguration.CustomTask(
33+
"runSomeCustomTaskByImpact",
34+
"someTaskForExample",
35+
"Command someTaskForExample is just an example task for demonstration of" +
36+
"how to add custom commands to impact analysis"
37+
)
38+
]
3139
logFolder = "${project.rootDir}"
3240
excludedModules = [
3341
"sample-util"
3442
]
3543
}
3644

45+
// just for example; Do not add to you project.
46+
allprojects {
47+
tasks.register("someTaskForExample") {task ->
48+
println("someTaskForExample on path: ${task.path} was called")
49+
}
50+
}
51+
3752
allprojects {
3853
repositories {
3954
google()

0 commit comments

Comments
 (0)