Skip to content

Commit

Permalink
Parse "legacy" quarkiverse metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and yrodiere committed Nov 22, 2023
1 parent f7f6c52 commit 33dd203
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 28 deletions.
20 changes: 17 additions & 3 deletions src/main/java/io/quarkus/search/app/fetching/QuarkusIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ private Stream<Guide> localGuides() throws IOException {
}

private Stream<Guide> quarkiverseGuides() throws IOException {
return quarkiverseDirectories()
.flatMap(quarkiverse -> QuarkiverseMetadata.parseYamlMetadata(quarkiverse.path, quarkiverse.version)
.createQuarkiverseGuides());
return Stream.concat(
quarkiverseDirectories()
.flatMap(quarkiverse -> QuarkiverseMetadata.parseYamlMetadata(quarkiverse.path, quarkiverse.version)
.createQuarkiverseGuides()),
quarkiverseLegacyDirectories()
.flatMap(quarkiverse -> QuarkiverseMetadata
.parseYamlLegacyMetadata(quarkiverse.path, quarkiverse.version)
.createQuarkiverseGuides()));
}

@SuppressWarnings("resource")
Expand All @@ -124,6 +129,15 @@ private Stream<GuidesDirectory> quarkiverseDirectories() throws IOException {
.filter(Objects::nonNull);
}

private Stream<GuidesDirectory> quarkiverseLegacyDirectories() throws IOException {
return Files.list(directory.path().resolve("_data"))
.filter(p -> !Files.isDirectory(p) && p.getFileName().toString().startsWith("guides-"))
.map(p -> {
var version = p.getFileName().toString().replaceAll("guides-|\\.yaml", "").replace('-', '.');
return new GuidesDirectory(version, p);
});
}

record GuidesDirectory(String version, Path path) {
}

Expand Down
95 changes: 70 additions & 25 deletions src/main/java/io/quarkus/search/app/yml/QuarkiverseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -27,44 +29,87 @@ private QuarkiverseMetadata(Collection<Guide> guides) {

@SuppressWarnings("unchecked")
public static QuarkiverseMetadata parseYamlMetadata(Path quarkusYamlPath, String version) {
Map<String, Object> quarkusYaml;
try (InputStream inputStream = Files.newInputStream(quarkusYamlPath)) {
Yaml yaml = new Yaml();
quarkusYaml = yaml.load(inputStream);
} catch (IOException e) {
throw new IllegalStateException("Unable to load " + quarkusYamlPath, e);
}

Set<Guide> parsed = new HashSet<>();

try {
return parse(quarkusYamlPath, quarkusYaml -> {
Set<Guide> parsed = new HashSet<>();
for (Map.Entry<String, List<Map<String, Object>>> type : ((Map<String, List<Map<String, Object>>>) quarkusYaml
.get("types")).entrySet()) {
for (Map<String, Object> parsedGuide : type.getValue()) {
Guide guide = new Guide();
guide.type = type.getKey();
guide.title = toString(parsedGuide.get("title"));
guide.summary = toString(parsedGuide.get("summary"));
guide.origin = toString(parsedGuide.get("origin"));
Guide guide = createGuide(version, type.getKey(), parsedGuide, "summary");
guide.categories = toSet(parsedGuide.get("categories"));
guide.version = version;
guide.url = toString(parsedGuide.get("url"));
guide.htmlFullContentProvider = new UrlInputProvider(guide.url);
parsed.add(guide);
}
}
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);

return new QuarkiverseMetadata(parsed);
});
}

@SuppressWarnings("unchecked")
public static QuarkiverseMetadata parseYamlLegacyMetadata(Path quarkusYamlPath, String version) {
return parse(quarkusYamlPath, quarkusYaml -> {
String versionParameter = ("?quarkusDocVersion=" + version);
Map<String, Guide> parsed = new HashMap<>();
for (Map<String, Object> categoryObj : ((List<Map<String, Object>>) quarkusYaml.get("categories"))) {
String category = toString(categoryObj.get("cat-id"));
for (Map<String, Object> parsedGuide : ((List<Map<String, Object>>) categoryObj.get("guides"))) {
if (toString(parsedGuide.get("url")).startsWith("http")) {
// we only care about external quarkiverse guides at this point ^
Guide guide = createGuide(version, "guide", parsedGuide, "description");
// since we can have the same link to a quarkiverse guide in multiple versions of quarkus,
// we want to somehow make them different in their ID:
guide.url = guide.url + versionParameter;
guide.categories = Set.of(category);
Guide old = parsed.put(guide.url, guide);
if (old != null) {
guide.categories = combine(guide.categories, old.categories);
}
guide.categories = toSet(parsedGuide.get("categories"));
}
}
}

return new QuarkiverseMetadata(parsed.values());
});
}

private static Set<String> combine(Set<String> a, Set<String> b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
HashSet<String> result = new HashSet<>(a);
result.addAll(b);
return result;
}

private static QuarkiverseMetadata parse(Path quarkusYamlPath, Function<Map<String, Object>, QuarkiverseMetadata> parser) {
Map<String, Object> quarkusYaml;
try (InputStream inputStream = Files.newInputStream(quarkusYamlPath)) {
Yaml yaml = new Yaml();
quarkusYaml = yaml.load(inputStream);
} catch (IOException e) {
throw new IllegalStateException("Unable to load " + quarkusYamlPath, e);
}

return new QuarkiverseMetadata(parsed);
return parser.apply(quarkusYaml);
}

public Stream<Guide> createQuarkiverseGuides() {
return guides.stream();
private static Guide createGuide(String version, String type, Map<String, Object> parsedGuide, String summaryKey) {
Guide guide = new Guide();
guide.type = type;
guide.title = toString(parsedGuide.get("title"));
guide.summary = toString(parsedGuide.get(summaryKey));
guide.origin = toString(parsedGuide.get("origin"));
guide.version = version;
guide.url = toString(parsedGuide.get("url"));
guide.htmlFullContentProvider = new UrlInputProvider(guide.url);
return guide;
}

record Metadata(String title, String url, String summary, String type, String origin, Set<String> categories) {
public Stream<Guide> createQuarkiverseGuides() {
return guides.stream();
}

private static String toString(Object value) {
Expand Down

0 comments on commit 33dd203

Please sign in to comment.