Search in sources :

Example 1 with LibraryTagsList

use of com.chimbori.hermitcrab.schema.library.LibraryTagsList in project lite-apps by chimbori.

the class TagsCollector method updateTagsJson.

public static void updateTagsJson() throws IOException {
    Gson gson = GsonInstance.getPrettyPrinter();
    // Read the list of all known tags from the tags.json file. In case we discover any new tags,
    // we will add them to this file, taking care not to overwrite those that already exist.
    LibraryTagsList tagsGson = LibraryTagsList.fromGson(gson, new FileReader(FilePaths.LITE_APPS_TAGS_JSON));
    Map<String, LibraryTag> globalTags = new HashMap<>();
    File[] liteAppDirs = FilePaths.LITE_APPS_SRC_DIR.listFiles();
    for (File liteAppDirectory : liteAppDirs) {
        if (!liteAppDirectory.isDirectory()) {
            // Probably a temporary file, like .DS_Store.
            continue;
        }
        File manifestJsonFile = new File(liteAppDirectory, FilePaths.MANIFEST_JSON_FILE_NAME);
        if (!manifestJsonFile.exists()) {
            throw new MissingManifestException(liteAppDirectory.getName());
        }
        Manifest manifest;
        try {
            manifest = gson.fromJson(new FileReader(manifestJsonFile), Manifest.class);
        } catch (JsonSyntaxException e) {
            System.err.println("Failed to parse JSON: " + liteAppDirectory.getName());
            throw e;
        }
        // For all tags applied to this manifest, check if they exist in the global tags list.
        if (manifest.tags != null) {
            for (String tagName : manifest.tags) {
                LibraryTag tag = globalTags.get(tagName);
                if (tag == null) {
                    // If this is the first time we are seeing this tag, create a new JSONArray to hold its contents.
                    LibraryTag newTag = new LibraryTag(tagName);
                    globalTags.put(tagName, newTag);
                    tagsGson.addTag(newTag);
                }
            }
        }
    }
    // Write the tags to JSON
    FileUtils.writeFile(FilePaths.LITE_APPS_TAGS_JSON, tagsGson.toJson(gson));
}
Also used : LibraryTag(com.chimbori.hermitcrab.schema.library.LibraryTag) JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) LibraryTagsList(com.chimbori.hermitcrab.schema.library.LibraryTagsList) Manifest(com.chimbori.hermitcrab.schema.manifest.Manifest) File(java.io.File)

Example 2 with LibraryTagsList

use of com.chimbori.hermitcrab.schema.library.LibraryTagsList in project lite-apps by chimbori.

the class LibraryGenerator method generateLibraryData.

/**
 * Individual manifest.json files do not contain any information about the organization of the
 * Lite Apps in the Library (e.g. categories, order within category, whether it should be
 * displayed or not. This metadata is stored in a separate index.json file. To minimize
 * duplication & to preserve a single source of truth, this file does not contain actual URLs
 * or anything about a Lite App other than its name (same as the directory name).
 * <p>
 * This generator tool combines the basic organizational metadata from index.json & detailed
 * Lite Apps data from * / manifest.json files. It outputs the Library Data JSON file,
 * which is used as the basis for generating the Hermit Library page at
 * https://hermit.chimbori.com/library.
 */
public static void generateLibraryData() throws IOException {
    Gson gson = GsonInstance.getPrettyPrinter();
    // Read the list of all known tags from the tags.json file. In case we discover any new tags,
    // we will add them to this file, taking care not to overwrite those that already exist.
    LibraryTagsList globalTags = LibraryTagsList.fromGson(gson, new FileReader(FilePaths.LITE_APPS_TAGS_JSON));
    Library outputLibrary = new Library(globalTags);
    File[] liteAppDirs = FilePaths.LITE_APPS_SRC_DIR.listFiles();
    for (File liteAppDirectory : liteAppDirs) {
        if (!liteAppDirectory.isDirectory()) {
            // Probably a temporary file, like .DS_Store.
            continue;
        }
        File iconsDirectory = new File(liteAppDirectory, FilePaths.ICONS_DIR_NAME);
        String appName = liteAppDirectory.getName();
        File manifestJsonFile = new File(liteAppDirectory, FilePaths.MANIFEST_JSON_FILE_NAME);
        if (!manifestJsonFile.exists()) {
            throw new MissingManifestException(appName);
        }
        // Create an entry for this Lite App to be put in the directory index file.
        Manifest manifest = gson.fromJson(new FileReader(manifestJsonFile), Manifest.class);
        LibraryApp outputApp = new LibraryApp();
        outputApp.url = manifest.startUrl;
        outputApp.name = appName;
        outputApp.theme_color = manifest.themeColor;
        outputApp.priority = manifest.priority;
        // Set user-agent from the settings stored in the Lite App’s manifest.json.
        String userAgent = manifest.hermitSettings != null ? manifest.hermitSettings.userAgent : null;
        if (USER_AGENT_DESKTOP.equals(userAgent)) {
            outputApp.user_agent = USER_AGENT_DESKTOP;
        }
        outputLibrary.addAppToCategories(outputApp, manifest.tags);
        // Resize the icon to be suitable for the Web, and copy it to the Web-accessible icons directory.
        File thumbnailImage = new File(FilePaths.LIBRARY_ICONS_DIR, appName + FilePaths.ICON_EXTENSION);
        if (!thumbnailImage.exists()) {
            Thumbnails.of(new File(iconsDirectory, IconFile.FAVICON_FILE.fileName)).outputQuality(1.0f).useOriginalFormat().size(LIBRARY_ICON_SIZE, LIBRARY_ICON_SIZE).imageType(BufferedImage.TYPE_INT_ARGB).toFile(thumbnailImage);
        }
    }
    FileUtils.writeFile(FilePaths.LIBRARY_JSON, outputLibrary.toJson(gson));
}
Also used : Gson(com.google.gson.Gson) FileReader(java.io.FileReader) Library(com.chimbori.hermitcrab.schema.library.Library) LibraryApp(com.chimbori.hermitcrab.schema.library.LibraryApp) LibraryTagsList(com.chimbori.hermitcrab.schema.library.LibraryTagsList) Manifest(com.chimbori.hermitcrab.schema.manifest.Manifest) File(java.io.File) IconFile(com.chimbori.hermitcrab.schema.manifest.IconFile)

Aggregations

LibraryTagsList (com.chimbori.hermitcrab.schema.library.LibraryTagsList)2 Manifest (com.chimbori.hermitcrab.schema.manifest.Manifest)2 Gson (com.google.gson.Gson)2 File (java.io.File)2 FileReader (java.io.FileReader)2 Library (com.chimbori.hermitcrab.schema.library.Library)1 LibraryApp (com.chimbori.hermitcrab.schema.library.LibraryApp)1 LibraryTag (com.chimbori.hermitcrab.schema.library.LibraryTag)1 IconFile (com.chimbori.hermitcrab.schema.manifest.IconFile)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 HashMap (java.util.HashMap)1