PackageManifest.java

package network.ike.docs.plugin.packages;

import network.ike.docs.plugin.registry.TopicRegistry;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * The manifest a library ships beside its registry,
 * {@code META-INF/ike/<groupId>/<artifactId>/manifest.yaml}
 * ({@code dev-adoc-packages}, IKE-Network/ike-issues#1341): generated by
 * the build from the POM and the tree, never kept by hand. It declares the
 * artifact's coordinates, the packages it ships with their kind, and where
 * its registry is, so that the goal that unpacks the union can refuse two
 * libraries that ship one package, naming both, and a tool can list the
 * libraries present by listing the metadata area.
 *
 * <pre>
 * coordinates: network.ike.lab:unlv-topics
 * packages:
 *   - name: network.ike.lab/aip
 *     kind: topics
 *   - name: network.ike.lab/unlv
 *     kind: library
 * registry: META-INF/ike/network.ike.lab/unlv-topics/topic-registry.yaml
 * </pre>
 *
 * @since 136
 */
public final class PackageManifest {

    /** The manifest's file name. */
    public static final String FILE_NAME = "manifest.yaml";

    private PackageManifest() {
    }

    /**
     * The manifest as an ordered map, ready for YAML.
     *
     * @param root           the AsciiDoc root
     * @param groupId        the library's group id
     * @param artifactId     the library's artifact id
     * @param libraryPackage the package {@code ike.adoc.library-package}
     *                       names as the library's own, or {@code null}
     * @return the manifest model
     * @throws IOException if the tree cannot be read
     */
    public static Map<String, Object> model(Path root, String groupId, String artifactId, String libraryPackage)
            throws IOException {
        return model(AdocPackages.packages(root, libraryPackage), groupId, artifactId);
    }

    /**
     * The manifest as an ordered map from packages already enumerated.
     *
     * @param packages   the packages, from {@link AdocPackages#packages}
     * @param groupId    the library's group id
     * @param artifactId the library's artifact id
     * @return the manifest model
     */
    public static Map<String, Object> model(List<AdocPackages.Package> packages, String groupId, String artifactId) {
        Map<String, Object> manifest = new LinkedHashMap<>();
        manifest.put("coordinates", groupId + ":" + artifactId);
        List<Object> list = new ArrayList<>();
        for (AdocPackages.Package p : packages) {
            Map<String, Object> entry = new LinkedHashMap<>();
            entry.put("name", p.name());
            entry.put("kind", p.kind().label());
            list.add(entry);
        }
        manifest.put("packages", list);
        manifest.put("registry", AdocPackages.metaDirectory(groupId, artifactId) + "/topic-registry.yaml");
        return manifest;
    }

    /**
     * Render the manifest as YAML.
     *
     * @param model the model from {@link #model}
     * @return the YAML text
     */
    public static String yaml(Map<String, Object> model) {
        return TopicRegistry.yaml(model, "manifest.yaml, generated by idoc:manifest from the pom and the tree. Do not edit.");
    }
}