-
Notifications
You must be signed in to change notification settings - Fork 19
/
MigrationCriteria.kt
121 lines (110 loc) · 4.06 KB
/
MigrationCriteria.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2021 Grabtaxi Holdings PTE LTE (GRAB)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.grab.grazel.gradle
import com.google.common.graph.ImmutableValueGraph
import com.grab.grazel.GrazelExtension
import com.grab.grazel.gradle.dependencies.DependenciesDataSource
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.multibindings.ElementsIntoSet
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import java.util.concurrent.ConcurrentSkipListMap
import javax.inject.Inject
import javax.inject.Singleton
internal interface MigrationCriteria {
fun canMigrate(project: Project): Boolean
}
@Module
internal object MigrationCriteriaModule {
@ElementsIntoSet
@Provides
fun migrationCriteria(
pluginsMigrationCriteria: PluginsMigrationCriteria,
androidMigrationCriteria: AndroidMigrationCriteria,
dependenciesMigrationCriteria: DependenciesMigrationCriteria
): Set<MigrationCriteria> = setOf(
pluginsMigrationCriteria,
androidMigrationCriteria,
dependenciesMigrationCriteria
)
}
@Singleton
internal class MigrationChecker @Inject constructor(
private val projectDependencyGraph: Lazy<ImmutableValueGraph<Project, Configuration>>,
private val migrationCriteria: Set<@JvmSuppressWildcards MigrationCriteria>
) : MigrationCriteria {
/**
* Maintain a cache to not do expensive work when a project is queried again.
*/
private val migratableProjectCache = ConcurrentSkipListMap<String, Boolean>()
override fun canMigrate(project: Project): Boolean {
fun canMigrateInternal(project: Project): Boolean {
return migratableProjectCache.getOrPut(project.path) {
migrationCriteria.all { criterion -> criterion.canMigrate(project) }
}
}
return when {
canMigrateInternal(project) -> true
else -> project
.dependenciesSubGraph(projectDependencyGraph.get())
.all(::canMigrateInternal)
}
}
}
/**
* Validates common plugin usage for migration. Currently checks for following plugins
* * Java
* * Kotlin
* * Android Gradle Plugin
*/
@Singleton
internal class PluginsMigrationCriteria @Inject constructor() : MigrationCriteria {
override fun canMigrate(project: Project): Boolean {
return project.isAndroid || project.isJava || project.isKotlin
}
}
/**
* Default migrate criteria for Android Project instances
* * If project uses databinding, allow migration only if user explicitly enabled it.
*/
@Singleton
internal class AndroidMigrationCriteria @Inject constructor(
private val grazelExtension: GrazelExtension
) : MigrationCriteria {
override fun canMigrate(project: Project): Boolean {
return if (!grazelExtension.android.features.dataBinding) {
!project.hasDatabinding
} else {
true
}
}
}
/**
* Calculates if a project is migrateable based on dependencies configuration.
* For example, if all dependencies of the project are downloaded from supported repository types.
*/
@Singleton
internal class DependenciesMigrationCriteria @Inject constructor(
private val dependenciesDataSource: DependenciesDataSource
) : MigrationCriteria {
override fun canMigrate(project: Project): Boolean {
val hasPrivateDependencies = dependenciesDataSource.hasDepsFromUnsupportedRepositories(project)
val hasIgnoredArtifacts = dependenciesDataSource.hasIgnoredArtifacts(project)
return !hasPrivateDependencies && !hasIgnoredArtifacts
}
}