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 package io.spring.initializr.web
import groovy.json.JsonBuilder import groovy.json.JsonBuilder
import io.spring.initializr.metadata.DependenciesCapability
import io.spring.initializr.metadata.Dependency import io.spring.initializr.metadata.Dependency
import io.spring.initializr.metadata.InitializrMetadataProvider import io.spring.initializr.metadata.InitializrMetadataProvider
import io.spring.initializr.util.Version import io.spring.initializr.util.Version
@@ -40,38 +39,41 @@ class UiController {
@Autowired @Autowired
protected InitializrMetadataProvider metadataProvider protected InitializrMetadataProvider metadataProvider
@RequestMapping(value = "/ui/dependencies" , produces = ["application/json"]) @RequestMapping(value = "/ui/dependencies", produces = ["application/json"])
String dependencies(@RequestParam(required = false) String version) { String dependencies(@RequestParam(required = false) String version) {
DependenciesCapability dependenciesCapability = metadataProvider.get().dependencies def dependencyGroups = metadataProvider.get().dependencies.content
List<Dependency> allDependencies = dependenciesCapability.content.collectMany { it.content } def content = []
if (version) { Version v = version ? Version.parse(version) : null
Version v = Version.parse(version) dependencyGroups.each { g ->
def filteredDependencies = allDependencies.findAll { d -> g.content.each { d ->
if (d.versionRange) { if (v && d.versionRange) {
return VersionRange.parse(d.versionRange).match(v) if (VersionRange.parse(d.versionRange).match(v)) {
content << new DependencyItem(g.name, d)
} }
return true
}
writeDependencies(filteredDependencies)
} else { } 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(); JsonBuilder json = new JsonBuilder();
json { json {
dependencies deps.collect { d -> dependencies items.collect { d ->
mapDependency(d) mapDependency(d)
} }
} }
json.toString() json.toString()
} }
private static mapDependency(Dependency d) { private static mapDependency(DependencyItem item) {
def result = [:] def result = [:]
Dependency d = item.dependency
result.id = d.id result.id = d.id
result.name = d.name result.name = d.name
result.group = item.group
if (d.description) { if (d.description) {
result.description = d.description result.description = d.description
} }
@@ -84,4 +86,14 @@ class UiController {
result 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", "id": "web",
"name": "Web", "name": "Web",
"description": "Web dependency description" "description": "Web dependency description",
"group": "Core"
}, },
{ {
"id": "security", "id": "security",
"name": "Security" "name": "Security",
"group": "Core"
}, },
{ {
"id": "data-jpa", "id": "data-jpa",
"name": "Data JPA" "name": "Data JPA",
"group": "Core"
}, },
{ {
"id": "org.acme:foo", "id": "org.acme:foo",
"name": "Foo", "name": "Foo",
"group": "Other",
"keywords": "thefoo,dafoo", "keywords": "thefoo,dafoo",
"weight": 42 "weight": 42
}, },
{ {
"id": "org.acme:bar", "id": "org.acme:bar",
"name": "Bar" "name": "Bar",
"group": "Other"
}, },
{ {
"id": "my-api", "id": "my-api",
"name": "My API" "name": "My API",
"group": "Other"
} }
] ]
} }

View File

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