Search in sources :

Example 1 with VersionInfo

use of com.github.mob41.osumer.io.VersionInfo in project osumer by mob41.

the class Updater method getPerVersionPerBranchLatestVersion.

public UpdateInfo getPerVersionPerBranchLatestVersion() throws DebuggableException {
    final String thisVersion = Osumer.OSUMER_VERSION;
    final String buildBranch = Osumer.OSUMER_BRANCH;
    final int buildNum = Osumer.OSUMER_BUILD_NUM;
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new DebuggableException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array:
    // index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    if (versionsJson.isNull(thisVersion)) {
        throw new NoSuchVersionException(json.toString(5), "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", sourceKey, thisVersion);
    }
    buildsArr = versionsJson.getJSONArray(thisVersion);
    if (buildsArr.length() < buildNum) {
        throw new NoSuchBuildNumberException(json.toString(5), "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", "Get latest build number from JSONArray", buildNum);
    }
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    if ((verJson.isNull("ended") || !verJson.getBoolean("ended")) && (!buildBranch.equals(sourceKey) || latest != buildNum)) {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, latest, webLink, exeLink, jarLink, false, false);
    }
    // As the version is ended, we are finding a new version here
    Iterator<String> it = versionsJson.keys();
    String key;
    String upgradeNode = null;
    while (it.hasNext()) {
        key = it.next();
        switch(compareVersion(thisVersion, key)) {
            case -2:
                break;
            case -1:
                if ((upgradeNode != null && compareVersion(upgradeNode, key) == -1) || upgradeNode == null) {
                    upgradeNode = key;
                }
                break;
            case 0:
                break;
            case 1:
                break;
        }
    }
    JSONArray upgradeNodeArr = null;
    int upgradedBuildNum = -1;
    if (upgradeNode != null) {
        upgradeNodeArr = versionsJson.getJSONArray(upgradeNode);
        upgradedBuildNum = upgradeNodeArr.length();
    }
    if (upgradedBuildNum == 0) {
        throw new NoBuildsForVersionException(json.toString(5), "Get Upgrade Node", "Validate build info JSON", "Return VersionInfo");
    }
    if (upgradeNode != null && upgradedBuildNum != -1) {
        JSONObject upgradedVerJson = upgradeNodeArr.getJSONObject(upgradedBuildNum - 1);
        String webLink = upgradedVerJson.isNull("web_link") ? null : upgradedVerJson.getString("web_link");
        String exeLink = upgradedVerJson.isNull("exe_link") ? null : upgradedVerJson.getString("exe_link");
        String jarLink = upgradedVerJson.isNull("jar_link") ? null : upgradedVerJson.getString("jar_link");
        String desc = upgradedVerJson.isNull("desc") ? null : upgradedVerJson.getString("desc");
        return new UpdateInfo(desc, upgradeNode, updateSource, upgradedBuildNum, webLink, exeLink, jarLink, false, true);
    } else {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, buildNum, webLink, exeLink, jarLink, true, false);
    }
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) NoSuchVersionException(com.github.mob41.osumer.exceptions.NoSuchVersionException) JSONObject(org.json.JSONObject) NoSuchBuildNumberException(com.github.mob41.osumer.exceptions.NoSuchBuildNumberException) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray) NoBuildsForVersionException(com.github.mob41.osumer.exceptions.NoBuildsForVersionException)

Example 2 with VersionInfo

use of com.github.mob41.osumer.io.VersionInfo in project osumer by mob41.

the class Updater method getLatestVersion.

public UpdateInfo getLatestVersion() throws DebuggableException {
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new DebuggableException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array: index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    VersionInfo thisInfo = new VersionInfo(Osumer.OSUMER_VERSION, Osumer.OSUMER_BRANCH, Osumer.OSUMER_BUILD_NUM);
    Iterator<String> it = versionsJson.keys();
    String last = null;
    String key;
    while (it.hasNext()) {
        key = it.next();
        if (last != null && compareVersion(key, last) != 1) {
            continue;
        } else {
            last = key;
        }
    }
    if (last == null) {
        return null;
    }
    buildsArr = versionsJson.getJSONArray(last);
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
    String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
    String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
    String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
    boolean isThisVersion = thisInfo != null && last.equals(thisInfo.getVersion()) && latest == thisInfo.getBuildNum() && getBranchStr(updateSource).equals(thisInfo.getBranch());
    if (thisInfo != null) {
        System.out.println(last + " to " + thisInfo.getVersion());
        System.out.println(latest + " to " + thisInfo.getBuildNum());
        System.out.println(getBranchStr(updateSource) + " to " + thisInfo.getBranch());
    } else {
        System.out.println("ThisInfoNull");
    }
    System.out.println("IsThisVer: " + isThisVersion);
    return new UpdateInfo(desc, last, updateSource, latest, webLink, exeLink, jarLink, isThisVersion, !isThisVersion);
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) VersionInfo(com.github.mob41.osumer.io.VersionInfo) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) JSONObject(org.json.JSONObject) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray)

Aggregations

DebuggableException (com.github.mob41.organdebug.exceptions.DebuggableException)2 InvalidSourceIntegerException (com.github.mob41.osumer.exceptions.InvalidSourceIntegerException)2 NoSuchSourceException (com.github.mob41.osumer.exceptions.NoSuchSourceException)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 NoBuildsForVersionException (com.github.mob41.osumer.exceptions.NoBuildsForVersionException)1 NoSuchBuildNumberException (com.github.mob41.osumer.exceptions.NoSuchBuildNumberException)1 NoSuchVersionException (com.github.mob41.osumer.exceptions.NoSuchVersionException)1 VersionInfo (com.github.mob41.osumer.io.VersionInfo)1