Skip to content

Commit

Permalink
mavenIndex search 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Wu committed Aug 16, 2017
1 parent 32d102e commit da2f85f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
Expand Up @@ -4,6 +4,8 @@ import com.intellij.notification.NotificationType
import com.intellij.openapi.project.Project
import groovy.json.JsonSlurper
import org.jetbrains.idea.maven.indices.MavenArtifactSearcher
import org.jetbrains.idea.maven.indices.MavenProjectIndicesManager
import org.jetbrains.idea.maven.model.MavenArtifactInfo
import org.jetbrains.plugins.gradle.integrations.maven.MavenRepositoriesHolder
import java.io.InputStream
import java.net.HttpURLConnection
Expand Down Expand Up @@ -37,6 +39,8 @@ class ArtifactInfo(groupId: String, artifactId: String, version: String = "", re
}

class SearchParam {
val groupId: String
val artifactId: String
val id: String
val q: String
val mq: String
Expand All @@ -46,13 +50,17 @@ class SearchParam {

constructor(text: String) {
if (text.startsWith("c:", true)) {
groupId = ""
artifactId = ""
id = text.substringAfter("c:").trim()
q = text.trim()
mq = text.trim()
nq = text.trim()
advancedSearch = "c"
failContent = "<a href='http://search.maven.org/#search|gav|1|c:\"$id\"'>search in mavenCentral</a>"
} else if (text.startsWith("fc:", true)) {
groupId = ""
artifactId = ""
id = text.substringAfter("fc:").trim()
q = text.trim()
mq = text.trim()
Expand All @@ -63,13 +71,15 @@ class SearchParam {
advancedSearch = ""
val list = split(text)
if (list.size in (2..3)) {
val groupId = list[0].trim()
val artifactId = list[1].trim()
groupId = list[0].trim()
artifactId = list[1].trim()
this.id = "$groupId${if (artifactId.isEmpty()) "" else ":$artifactId"}"
this.q = "g=$groupId${if (artifactId.isEmpty()) "*" else "&a=$artifactId*"}"
this.mq = "g:\"$groupId\"${if (artifactId.isEmpty()) "" else "+AND+a:\"$artifactId\""}"
this.nq = "g=$groupId${if (artifactId.isEmpty()) "" else "&a=$artifactId"}"
} else {
groupId = ""
artifactId = ""
this.id = text.trim()
this.q = "q=${text.trim()}*"
this.mq = "a:\"${text.trim()}\""
Expand All @@ -81,8 +91,8 @@ class SearchParam {

constructor(groupIdParam: String, artifactIdParam: String) {
advancedSearch = ""
val groupId = groupIdParam.trim()
val artifactId = artifactIdParam.trim()
groupId = groupIdParam.trim()
artifactId = artifactIdParam.trim()
this.id = "$groupId${if (artifactId.isEmpty()) "" else ":$artifactId"}"
this.q = "g=$groupId${if (artifactId.isEmpty()) "*" else "&a=$artifactId*"}"
this.mq = "g:\"$groupId\"${if (artifactId.isEmpty()) "" else "+AND+a:\"$artifactId\""}"
Expand Down Expand Up @@ -178,7 +188,7 @@ class GradleArtifactSearcher {
result = searchByClassNameInMavenCentral(searchParam, project)
} else {

result = searchInMavenRepositories(searchParam, project)
result = searchInMavenIndexs(searchParam, project)
// result = searchInNexus(searchParam, project)
// result = searchInJcenter(searchParam, project)
}
Expand Down Expand Up @@ -226,16 +236,38 @@ class GradleArtifactSearcher {
return result
}

private fun searchInMavenRepositories(searchParam: SearchParam, project: Project): MutableList<ArtifactInfo> {
private fun searchInMavenIndexs(searchParam: SearchParam, project: Project): MutableList<ArtifactInfo> {
val result: MutableList<ArtifactInfo> = mutableListOf()
MavenRepositoriesHolder.getInstance(project).checkNotIndexedRepositories()
val searcher = MavenArtifactSearcher()
val searchResults = searcher.search(project, searchParam.id, 1000)
for (searchResult in searchResults) {
for (artifactInfo in searchResult.versions) {
result.add(ArtifactInfo(artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.version, "mavenCentral", "Apache"))
val m = MavenProjectIndicesManager.getInstance(project)
if (searchParam.groupId.isNotEmpty()) {
if (searchParam.artifactId.isEmpty())
m.groupIds.mapTo(result) { ArtifactInfo(it, "", "", "mavenCentral", "Apache") }
else {
m.getArtifactIds(searchParam.groupId).forEach {
if (it == searchParam.artifactId) {
m.getVersions(searchParam.groupId, it).sortedWith(kotlin.Comparator<String> { o1, o2 ->
compareVersion(o2, o1)
}).forEach { version ->
result.add(ArtifactInfo(searchParam.groupId, it, version, "mavenCentral", "Apache"))
}
} else {
result.add(ArtifactInfo(searchParam.groupId, it, "", "mavenCentral", "Apache"))
}
}
}
} else {
val searcher = MavenArtifactSearcher()
val searchResults = searcher.search(project, searchParam.id, 1000)
searchResults
.flatMap {
it.versions.sortedWith(kotlin.Comparator<MavenArtifactInfo> { o1, o2 ->
compareVersion(o2.version, o1.version)
})
}
.mapTo(result) { ArtifactInfo(it.groupId, it.artifactId, it.version, "mavenCentral", "Apache") }
}

return result
}

Expand Down
Expand Up @@ -50,19 +50,23 @@ class ImportMavenRepositoriesTask(project: Project) : ReadTask() {
if (ApplicationManager.getApplication().isUnitTestMode) return

// val remoteRepository = MavenRemoteRepository("my", null, "http://127.0.0.1:8081/remote-repos/", null, null, null)
MavenRepositoriesHolder.getInstance(myProject).update(setOf( mavenCentralRemoteRepository))
val repositoriesHolder = MavenRepositoriesHolder.getInstance(myProject)
val remoteRepositories = mutableSetOf<MavenRemoteRepository>()
remoteRepositories.addAll(repositoriesHolder.remoteRepositories)
remoteRepositories.add(mavenCentralRemoteRepository)
repositoriesHolder.update(remoteRepositories)
MavenProjectIndicesManager.getInstance(myProject).scheduleUpdateIndicesList(Consumer<List<MavenIndex>> { indexes ->
if (myProject.isDisposed) return@Consumer

val repositoriesWithEmptyIndex = indexes.stream()
.filter({ index ->
index.updateTimestamp == -1L &&
index.failureMessage == null &&
MavenRepositoriesHolder.getInstance(myProject).contains(index.repositoryPathOrUrl)
repositoriesHolder.contains(index.repositoryPathOrUrl)
})
.map(MavenIndex::getRepositoryPathOrUrl)
.collect(Collectors.toList<String>())
MavenRepositoriesHolder.getInstance(myProject).updateNotIndexedUrls(repositoriesWithEmptyIndex)
repositoriesHolder.updateNotIndexedUrls(repositoriesWithEmptyIndex)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Expand Up @@ -5,8 +5,8 @@
<vendor email="piterwu@outlook.com"
url="https://github.com/bestwu/gradle-dependencies-plugins-helper-plugin">piterwu
</vendor>
<depends>org.jetbrains.idea.maven</depends>
<depends>org.jetbrains.plugins.gradle</depends>
<depends>org.jetbrains.plugins.maven</depends>
<depends>org.intellij.groovy</depends>
<depends>org.jetbrains.kotlin</depends>

Expand Down

0 comments on commit da2f85f

Please sign in to comment.