Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
First feature complete version of the updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ggalmazor committed Jan 18, 2019
1 parent 05aac33 commit f977c93
Show file tree
Hide file tree
Showing 27 changed files with 1,901 additions and 105 deletions.
36 changes: 25 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import static org.opendatakit.gradle.Util.getValue
import static org.opendatakit.gradle.Util.getVersionName
import static org.opendatakit.gradle.Util.makeExecutableJar

plugins {
id 'java'
Expand All @@ -20,31 +20,45 @@ repositories {
dependencies {
// CLI
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
compile group: 'de.vandermeer', name: 'asciitable', version: '0.3.2'

// HTTP
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.6'
testCompile group: 'com.github.dreamhead', name: 'moco-core', version: '0.12.0'

// JSON
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.8'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'

// Logging & reporting
runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile group: 'io.sentry', name: 'sentry', version: '1.7.16'

// Testing
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.1'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.3.1'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
}

buildConfig {
appName = 'ODK Aggregate Updater'
version = getVersionName()
clsName = 'BuildConfig'
packageName = 'org.opendatakit.briefcase.buildconfig'
buildConfigField 'Boolean', 'SENTRY_ENABLED', getValue(this, "sentry.enabled", "false")
buildConfigField 'String', 'SENTRY_DSN', getValue(this, "sentry.dsn", "https://b6f023a5f17e44d9a46447f8827e2a41:[email protected]/287258")
}

test {
useJUnitPlatform {
includeTags 'fast'
excludeTags 'slow'
jar {
manifest {
attributes "Main-Class": "org.opendatakit.aggregateupdater.Launcher"
}

from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

doLast {
makeExecutableJar("${buildDir}/libs/${archivesBaseName}-${version}.jar")
}
}
14 changes: 14 additions & 0 deletions buildSrc/src/main/groovy/org/opendatakit/gradle/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class Util {
println("Set ${path}:${key} to \"${value}\"")
}

static def execute(cmd) {
['bash', '-c', cmd].execute().waitFor()
}

static def makeExecutableJar(path) {
String jarPath = path.toString()
String executablePath = path.substring(0, path.lastIndexOf("."))

execute("echo \"#!/bin/sh\" > ${executablePath}")
execute("echo \"exec java -jar \\\$0 \"\\\$@\"\" >> ${executablePath}")
execute("cat ${jarPath} >> ${executablePath}")
execute("chmod +x ${executablePath}")
}

static String getValue(obj, key, defaultValue) {
if (obj.hasProperty(key))
obj.getProperty(key)
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'aggregate-updater'
rootProject.name = 'ODK-Aggregate-Updater'

16 changes: 16 additions & 0 deletions src/main/java/org/opendatakit/aggregateupdater/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package org.opendatakit.aggregateupdater;

import org.opendatakit.aggregateupdater.listversions.ListAvailableVersions;
import org.opendatakit.aggregateupdater.reused.http.CommonsHttp;
import org.opendatakit.aggregateupdater.reused.http.Http;
import org.opendatakit.aggregateupdater.update.UpdateOperation;
import org.opendatakit.cli.Cli;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Launcher {
private static final Logger log = LoggerFactory.getLogger(Launcher.class);

public static void main(String[] args) {
Http http = new CommonsHttp();

new Cli()
.register(ListAvailableVersions.build(http))
.register(UpdateOperation.build(http))
.onError(t -> {
log.error("Error executing ODK Aggregate Updater", t);
System.exit(1);
})
.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.opendatakit.aggregateupdater.listversions;

import static java.time.format.DateTimeFormatter.ofLocalizedDateTime;
import static java.time.format.FormatStyle.LONG;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.opendatakit.cli.Param.flag;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.opendatakit.aggregateupdater.releases.Release;
import org.opendatakit.aggregateupdater.releases.ReleaseQueries;
import org.opendatakit.aggregateupdater.reused.http.Http;
import org.opendatakit.cli.Args;
import org.opendatakit.cli.Console;
import org.opendatakit.cli.Operation;
import org.opendatakit.cli.Param;

public class ListAvailableVersions {

public static final Param<Void> INCLUDE_BETA_VERSIONS = Param.flag("ib", "include-beta", "Include beta versions");

public static Operation build(Http http) {
return Operation.of(
flag("l", "list", "List available versions"),
(console, args) -> execute(http, console, args),
emptyList(),
singletonList(INCLUDE_BETA_VERSIONS)
);
}

private static void execute(Http http, Console console, Args args) {
List<Release> releases = http.execute(ReleaseQueries.all()).orElse(emptyList());

if (releases.isEmpty()) {
console.out("No releases are available at this moment. Please try again after some time");
console.exit();
}

console.out("List of available releases:");
console.table(releases.stream()
.filter(Release::isNotLegacy)
.filter(Release::isUpdateable)
.filter(r -> args.has(INCLUDE_BETA_VERSIONS) || r.isNotBeta())
.sorted(((Comparator<Release>) Release::compareTo).reversed())
.map(r -> Arrays.asList(r.getVersion().toString(), r.getPublishedAt().format(ofLocalizedDateTime(LONG))))
.collect(Collectors.toList()), "Version", "Publish date");
}


}
59 changes: 59 additions & 0 deletions src/main/java/org/opendatakit/aggregateupdater/releases/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.opendatakit.aggregateupdater.releases;

import static org.opendatakit.aggregateupdater.reused.HttpHelpers.url;

import java.net.URL;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Optional;

public class Asset {
private final URL url;
private final int id;
private final String nodeId;
private final String name;
private final Optional<String> label;
private final Author uploader;
private final String contentType;
private final String state;
private final int size;
private final int downloadCount;
private final ZonedDateTime createdAt;
private final ZonedDateTime updatedAt;
private final URL browserDownloadUrl;

public Asset(URL url, int id, String nodeId, String name, Optional<String> label, Author uploader, String contentType, String state, int size, int downloadCount, ZonedDateTime createdAt, ZonedDateTime updatedAt, URL browserDownloadUrl) {
this.url = url;
this.id = id;
this.nodeId = nodeId;
this.name = name;
this.label = label;
this.uploader = uploader;
this.contentType = contentType;
this.state = state;
this.size = size;
this.downloadCount = downloadCount;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.browserDownloadUrl = browserDownloadUrl;
}

public static Asset from(Map<String, Object> json) {
return new Asset(
url((String) json.get("url")),
(Integer) json.get("id"),
(String) json.get("node_id"),
(String) json.get("name"),
Optional.ofNullable((String) json.get("label")).filter(s -> !s.equals("null")),
Author.from((Map<String, Object>) json.get("uploader")),
(String) json.get("content_type"),
(String) json.get("state"),
(Integer) json.get("size"),
(Integer) json.get("download_count"),
OffsetDateTime.parse((String) json.get("created_at")).toZonedDateTime(),
OffsetDateTime.parse((String) json.get("updated_at")).toZonedDateTime(),
url((String) json.get("browser_download_url"))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.opendatakit.aggregateupdater.releases;

import static org.opendatakit.aggregateupdater.reused.HttpHelpers.url;

import java.net.URL;
import java.util.Map;
import java.util.Optional;

public class Author {
private final String login;
private final int id;
private final String nodeId;
private final URL avatarUrl;
private final Optional<String> gravatarId;
private final URL url;
private final URL htmlUrl;
private final URL followersUrl;
private final URL followingUrl;
private final URL gistsUrl;
private final URL starredUrl;
private final URL subscriptionsUrl;
private final URL organizationsUrl;
private final URL reposUrl;
private final URL eventsUrl;
private final URL receivedEventsUrl;
private final String type;
private final boolean siteAdmin;

public Author(String login, int id, String nodeId, URL avatarUrl, Optional<String> gravatarId, URL url, URL htmlUrl, URL followersUrl, URL followingUrl, URL gistsUrl, URL starredUrl, URL subscriptionsUrl, URL organizationsUrl, URL reposUrl, URL eventsUrl, URL receivedEventsUrl, String type, boolean siteAdmin) {
this.login = login;
this.id = id;
this.nodeId = nodeId;
this.avatarUrl = avatarUrl;
this.gravatarId = gravatarId;
this.url = url;
this.htmlUrl = htmlUrl;
this.followersUrl = followersUrl;
this.followingUrl = followingUrl;
this.gistsUrl = gistsUrl;
this.starredUrl = starredUrl;
this.subscriptionsUrl = subscriptionsUrl;
this.organizationsUrl = organizationsUrl;
this.reposUrl = reposUrl;
this.eventsUrl = eventsUrl;
this.receivedEventsUrl = receivedEventsUrl;
this.type = type;
this.siteAdmin = siteAdmin;
}

public static Author from(Map<String, Object> json) {
return new Author(
(String) json.get("login"),
(Integer) json.get("id"),
(String) json.get("node_id"),
url((String) json.get("avatar_url")),
Optional.ofNullable((String) json.get("gravatar_id")),
url((String) json.get("url")),
url((String) json.get("html_url")),
url((String) json.get("followers_url")),
url((String) json.get("following_url")),
url((String) json.get("gists_url")),
url((String) json.get("starred_url")),
url((String) json.get("subscriptions_url")),
url((String) json.get("organizations_url")),
url((String) json.get("repos_url")),
url((String) json.get("events_url")),
url((String) json.get("received_events_url")),
(String) json.get("type"),
(Boolean) json.get("site_admin")
);
}
}
Loading

0 comments on commit f977c93

Please sign in to comment.