Expose group in /ui/dependencies

Expose the group of each dependency so that it can be added as a search
term.

See gh-145
This commit is contained in:
Stephane Nicoll
2015-10-01 12:13:36 +02:00
parent 5b062dd9c0
commit ac970139e8
3 changed files with 54 additions and 28 deletions

View File

@@ -17,7 +17,6 @@
package io.spring.initializr.web
import groovy.json.JsonBuilder
import io.spring.initializr.metadata.DependenciesCapability
import io.spring.initializr.metadata.Dependency
import io.spring.initializr.metadata.InitializrMetadataProvider
import io.spring.initializr.util.Version
@@ -40,38 +39,41 @@ class UiController {
@Autowired
protected InitializrMetadataProvider metadataProvider
@RequestMapping(value = "/ui/dependencies" , produces = ["application/json"])
@RequestMapping(value = "/ui/dependencies", produces = ["application/json"])
String dependencies(@RequestParam(required = false) String version) {
DependenciesCapability dependenciesCapability = metadataProvider.get().dependencies
List<Dependency> allDependencies = dependenciesCapability.content.collectMany { it.content }
if (version) {
Version v = Version.parse(version)
def filteredDependencies = allDependencies.findAll { d ->
if (d.versionRange) {
return VersionRange.parse(d.versionRange).match(v)
def dependencyGroups = metadataProvider.get().dependencies.content
def content = []
Version v = version ? Version.parse(version) : null
dependencyGroups.each { g ->
g.content.each { d ->
if (v && d.versionRange) {
if (VersionRange.parse(d.versionRange).match(v)) {
content << new DependencyItem(g.name, d)
}
return true
}
writeDependencies(filteredDependencies)
} else {
writeDependencies(allDependencies)
content << new DependencyItem(g.name, d)
}
}
}
writeDependencies(content)
}
private static String writeDependencies(List<Dependency> deps) {
private static String writeDependencies(List<DependencyItem> items) {
JsonBuilder json = new JsonBuilder();
json {
dependencies deps.collect { d ->
dependencies items.collect { d ->
mapDependency(d)
}
}
json.toString()
}
private static mapDependency(Dependency d) {
private static mapDependency(DependencyItem item) {
def result = [:]
Dependency d = item.dependency
result.id = d.id
result.name = d.name
result.group = item.group
if (d.description) {
result.description = d.description
}
@@ -84,4 +86,14 @@ class UiController {
result
}
private static class DependencyItem {
private final String group
private final Dependency dependency
DependencyItem(String group, Dependency dependency) {
this.group = group
this.dependency = dependency
}
}
}

View File

@@ -3,29 +3,35 @@
{
"id": "web",
"name": "Web",
"description": "Web dependency description"
"description": "Web dependency description",
"group": "Core"
},
{
"id": "security",
"name": "Security"
"name": "Security",
"group": "Core"
},
{
"id": "data-jpa",
"name": "Data JPA"
"name": "Data JPA",
"group": "Core"
},
{
"id": "org.acme:foo",
"name": "Foo",
"group": "Other",
"keywords": "thefoo,dafoo",
"weight": 42
},
{
"id": "org.acme:bar",
"name": "Bar"
"name": "Bar",
"group": "Other"
},
{
"id": "my-api",
"name": "My API"
"name": "My API",
"group": "Other"
}
]
}

View File

@@ -3,37 +3,45 @@
{
"id": "web",
"name": "Web",
"description": "Web dependency description"
"description": "Web dependency description",
"group": "Core"
},
{
"id": "security",
"name": "Security"
"name": "Security",
"group": "Core"
},
{
"id": "data-jpa",
"name": "Data JPA"
"name": "Data JPA",
"group": "Core"
},
{
"id": "org.acme:foo",
"name": "Foo",
"group": "Other",
"keywords": "thefoo,dafoo",
"weight": 42
},
{
"id": "org.acme:bar",
"name": "Bar"
"name": "Bar",
"group": "Other"
},
{
"id": "org.acme:biz",
"name": "Biz"
"name": "Biz",
"group": "Other"
},
{
"id": "org.acme:bur",
"name": "Bur"
"name": "Bur",
"group": "Other"
},
{
"id": "my-api",
"name": "My API"
"name": "My API",
"group": "Other"
}
]
}