Search in sources :

Example 1 with LibraryTag

use of com.chimbori.hermitcrab.schema.library.LibraryTag 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)

Aggregations

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