|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 - present - Yupiik SAS - https://www.yupiik.com |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the License is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 12 | + * KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.yupiik.bundlebee.core.command.impl; |
| 17 | + |
| 18 | +import io.yupiik.bundlebee.core.command.Executable; |
| 19 | +import io.yupiik.bundlebee.core.configuration.Description; |
| 20 | +import io.yupiik.bundlebee.core.descriptor.Manifest; |
| 21 | +import io.yupiik.bundlebee.core.qualifier.BundleBee; |
| 22 | +import io.yupiik.bundlebee.core.service.AlveolusHandler; |
| 23 | +import lombok.Data; |
| 24 | +import lombok.extern.java.Log; |
| 25 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 26 | + |
| 27 | +import javax.enterprise.context.Dependent; |
| 28 | +import javax.inject.Inject; |
| 29 | +import javax.json.JsonArrayBuilder; |
| 30 | +import javax.json.JsonBuilderFactory; |
| 31 | +import javax.json.bind.annotation.JsonbProperty; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.List; |
| 36 | +import java.util.concurrent.CompletionStage; |
| 37 | +import java.util.stream.Collector; |
| 38 | +import java.util.stream.Stream; |
| 39 | + |
| 40 | +import static java.util.Comparator.comparing; |
| 41 | +import static java.util.stream.Collectors.joining; |
| 42 | +import static java.util.stream.Collectors.toList; |
| 43 | + |
| 44 | +@Log |
| 45 | +@Dependent |
| 46 | +public class ListAlveoliCommand implements Executable { |
| 47 | + @Inject |
| 48 | + @Description("Root dependency to download to get the manifest. If set to `auto` it is assumed to be present in current classpath.") |
| 49 | + @ConfigProperty(name = "bundlebee.list-alveoli.from", defaultValue = "auto") |
| 50 | + private String from; |
| 51 | + |
| 52 | + @Inject |
| 53 | + @Description("Manifest to load to start to find the alveolus. This optional setting mainly enables to use dependencies easily. " + |
| 54 | + "Ignored if set to `skip`.") |
| 55 | + @ConfigProperty(name = "bundlebee.list-alveoli.manifest", defaultValue = "skip") |
| 56 | + private String manifest; |
| 57 | + |
| 58 | + @Inject |
| 59 | + @Description("`logger` means the standard bundlebee logging output stream else it is considered as a file path. " + |
| 60 | + "Note that if you end the filename with `.json` it will be formatted as json else just readable text.") |
| 61 | + @ConfigProperty(name = "bundlebee.list-alveoli.output", defaultValue = "logger") |
| 62 | + private String output; |
| 63 | + |
| 64 | + @Inject |
| 65 | + private AlveolusHandler visitor; |
| 66 | + |
| 67 | + @Inject |
| 68 | + @BundleBee |
| 69 | + private JsonBuilderFactory json; |
| 70 | + |
| 71 | + @Override |
| 72 | + public String name() { |
| 73 | + return "list-alveoli"; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String description() { |
| 78 | + return "Lists all found alveoli"; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public CompletionStage<?> execute() { |
| 83 | + return visitor.findManifest(from, manifest, null) |
| 84 | + .thenApply(m -> m == null || m.getAlveoli() == null ? |
| 85 | + visitor.findManifestsFromClasspath(null) |
| 86 | + .flatMap(it -> it.getAlveoli() == null ? Stream.empty() : it.getAlveoli().stream()) |
| 87 | + .collect(toList()) : |
| 88 | + m.getAlveoli()) |
| 89 | + .thenApply(items -> { |
| 90 | + switch (output) { |
| 91 | + case "logger": |
| 92 | + log.info(asText(items)); |
| 93 | + break; |
| 94 | + case "logger.json": |
| 95 | + log.info(asJson(items)); |
| 96 | + break; |
| 97 | + default: |
| 98 | + final var out = Path.of(output); |
| 99 | + try { |
| 100 | + if (out.getParent() != null) { |
| 101 | + Files.createDirectories(out.getParent()); |
| 102 | + } |
| 103 | + Files.writeString(out, output.endsWith(".json") ? |
| 104 | + asJson(items) : |
| 105 | + asText(items)); |
| 106 | + } catch (final IOException ioe) { |
| 107 | + throw new IllegalStateException(ioe); |
| 108 | + } |
| 109 | + } |
| 110 | + return items; |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private String asJson(final List<Manifest.Alveolus> items) { |
| 115 | + return json.createObjectBuilder() |
| 116 | + .add("items", items.stream() |
| 117 | + .sorted(comparing(Manifest.Alveolus::getName)) |
| 118 | + .map(it -> json.createObjectBuilder().add("name", it.getName()).build()) |
| 119 | + .collect(Collector.of( |
| 120 | + json::createArrayBuilder, |
| 121 | + JsonArrayBuilder::add, |
| 122 | + JsonArrayBuilder::addAll))) |
| 123 | + .build() |
| 124 | + .toString(); |
| 125 | + } |
| 126 | + |
| 127 | + private static String asText(final List<Manifest.Alveolus> a) { |
| 128 | + return a.isEmpty() ? |
| 129 | + "No alveolus found." : |
| 130 | + a.stream() |
| 131 | + .sorted(comparing(Manifest.Alveolus::getName)) |
| 132 | + .map(i -> "- " + i.getName()) |
| 133 | + .collect(joining("\n", "Found alveoli:\n", "\n")); |
| 134 | + } |
| 135 | + |
| 136 | + @Data |
| 137 | + public static class Item { |
| 138 | + @JsonbProperty |
| 139 | + private final String name; |
| 140 | + } |
| 141 | + |
| 142 | + @Data |
| 143 | + public static class Items { |
| 144 | + @JsonbProperty |
| 145 | + private final List<Item> item; |
| 146 | + } |
| 147 | +} |
0 commit comments