-
Notifications
You must be signed in to change notification settings - Fork 499
/
MavenDependenciesListCheck.kt
190 lines (167 loc) · 6.43 KB
/
MavenDependenciesListCheck.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.oppia.android.scripts.license
import com.google.protobuf.TextFormat
import org.oppia.android.scripts.common.CommandExecutor
import org.oppia.android.scripts.common.CommandExecutorImpl
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import org.oppia.android.scripts.proto.MavenDependency
/**
* The main entrypoint for verifying the list of third-party maven dependencies in
* maven_dependencies.textproto is up-to-date.
*
* Usage:
* bazel run //scripts:maven_dependencies_list_check -- <path_to_root>
* <path_to_maven_install_json> <path_to_maven_dependencies_pb>
*
*
* Arguments:
* - path_to_root: directory path to the root of the Oppia Android repository.
* - path_to_maven_install_json: relative path to the maven_install.json file.
* - path_to_maven_dependencies_pb: relative path to the maven_dependencies.pb file.
*
* Example:
* bazel run //scripts:maven_dependencies_list_check -- $(pwd)
* third_party/maven_install.json scripts/assets/maven_dependencies.pb
*/
fun main(args: Array<String>) {
ScriptBackgroundCoroutineDispatcher().use { scriptBgDispatcher ->
MavenDependenciesListCheck(LicenseFetcherImpl(), scriptBgDispatcher).main(args)
}
}
/**
* Wrapper class to pass [LicenseFetcher] and [CommandExecutor] to be utilized by the the main
* method.
*/
class MavenDependenciesListCheck(
private val licenseFetcher: LicenseFetcher,
scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher,
private val commandExecutor: CommandExecutor = CommandExecutorImpl(scriptBgDispatcher)
) {
/**
* Verifies that the list of third-party maven dependencies in maven_dependnecies.textproto is
* up-to-date.
*/
fun main(args: Array<String>) {
val pathToRoot = args[0]
val pathToMavenInstallJson = "$pathToRoot/${args[1]}"
val pathToMavenDependenciesPb = args[2]
val mavenDependenciesRetriever = MavenDependenciesRetriever(
pathToRoot,
licenseFetcher,
commandExecutor
)
val bazelQueryDepsList =
mavenDependenciesRetriever.retrieveThirdPartyMavenDependenciesList()
val mavenInstallDepsList = mavenDependenciesRetriever.getDependencyListFromMavenInstall(
pathToMavenInstallJson,
bazelQueryDepsList
)
val dependenciesListFromPom =
mavenDependenciesRetriever
.retrieveDependencyListFromPom(mavenInstallDepsList)
.mavenDependencyList
val dependenciesListFromTextProto =
mavenDependenciesRetriever
.retrieveMavenDependencyList(pathToMavenDependenciesPb)
val updatedDependenciesList =
mavenDependenciesRetriever.addChangesFromTextProto(
dependenciesListFromPom,
dependenciesListFromTextProto
)
val manuallyUpdatedLicenses =
mavenDependenciesRetriever
.retrieveManuallyUpdatedLicensesSet(updatedDependenciesList)
val finalDependenciesList =
mavenDependenciesRetriever.updateMavenDependenciesList(
updatedDependenciesList,
manuallyUpdatedLicenses
)
val redundantDependencies = findRedundantDependencies(
finalDependenciesList,
dependenciesListFromTextProto
)
val missindDependencies = findMissingDependencies(
finalDependenciesList,
dependenciesListFromTextProto
)
if (redundantDependencies.isNotEmpty() || missindDependencies.isNotEmpty()) {
printErrorMessage()
if (redundantDependencies.isNotEmpty()) {
println("Redundant dependencies that need to be removed:\n")
printDependenciesList(redundantDependencies)
}
if (missindDependencies.isNotEmpty()) {
println("Missing dependencies that need to be added:\n")
printDependenciesList(missindDependencies)
}
println(
"""
Refer to https://github.com/oppia/oppia-android/wiki/Updating-Maven-Dependencies for more details.
""".trimIndent()
)
}
if (redundantDependencies.isNotEmpty() && missindDependencies.isNotEmpty()) {
throw Exception("Redundant and missing dependencies in maven_dependencies.textproto")
} else if (redundantDependencies.isNotEmpty()) {
throw Exception("Redundant dependencies in maven_dependencies.textproto")
} else if (missindDependencies.isNotEmpty()) {
throw Exception("Missing dependencies in maven_dependencies.textproto")
}
val brokenLicenses = mavenDependenciesRetriever.getAllBrokenLicenses(finalDependenciesList)
if (brokenLicenses.isNotEmpty()) {
val licenseToDependencyMap =
mavenDependenciesRetriever
.findFirstDependenciesWithBrokenLicenses(
finalDependenciesList,
brokenLicenses
)
printErrorMessage()
println("Licenses that need to be updated:\n")
brokenLicenses.forEach {
println(
"""
license_name: ${it.licenseName}
original_link: ${it.originalLink}
verified_link_case: ${it.verifiedLinkCase}
is_original_link_invalid: ${it.isOriginalLinkInvalid}
First dependency that should be updated with the license: ${licenseToDependencyMap[it]}
""".trimIndent() + "\n\n"
)
}
throw Exception("Licenses details are not completed")
}
val dependenciesWithoutAnyLinks =
mavenDependenciesRetriever.getDependenciesThatNeedIntervention(finalDependenciesList)
if (dependenciesWithoutAnyLinks.isNotEmpty()) {
printErrorMessage()
println("Dependencies with invalid or no license links:\n")
dependenciesWithoutAnyLinks.forEach { dependency ->
println(dependency)
}
throw Exception("License links are invalid or not available for some dependencies")
}
println("\nmaven_dependencies.textproto is up-to-date.")
}
private fun printErrorMessage() {
println(
"Errors were encountered. Please run script GenerateMavenDependenciesList.kt to fix.\n"
)
}
private fun findRedundantDependencies(
dependenciesList: List<MavenDependency>,
updatedDependenciesList: List<MavenDependency>
): List<MavenDependency> {
return updatedDependenciesList - dependenciesList
}
private fun findMissingDependencies(
dependenciesList: List<MavenDependency>,
updatedDependenciesList: List<MavenDependency>
): List<MavenDependency> {
return dependenciesList - updatedDependenciesList
}
private fun printDependenciesList(dependencyList: List<MavenDependency>) {
dependencyList.forEach { dep ->
TextFormat.printer().print(dep, System.out)
println()
}
}
}