Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update checker #17

Merged
merged 17 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ext {
slf4jVersion = "1.7.+"
junitVersion = '5.+'
mockitoVersion = "3.+"
gsonVersion = "2.8.6"
}

test {
Expand All @@ -33,7 +34,8 @@ dependencies {
"org.web3j:contracts:$web3jVersion",
"org.web3j:core:$web3jVersion",
"org.web3j:crypto:$web3jVersion",
"org.web3j:hosted-providers:$web3jVersion"
"org.web3j:hosted-providers:$web3jVersion",
"com.google.code.gson:gson:$gsonVersion"

runtime "org.slf4j:slf4j-nop:$slf4jVersion"

Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Nov 11 23:03:14 GMT 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
14 changes: 14 additions & 0 deletions src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.web3j.codegen.Console;
import org.web3j.codegen.SolidityFunctionWrapperGenerator;
import org.web3j.codegen.TruffleJsonFunctionWrapperGenerator;
import org.web3j.console.config.CliConfig;
import org.web3j.console.project.ProjectCreator;
import org.web3j.console.project.ProjectImporter;
import org.web3j.console.update.Updater;
import org.web3j.utils.Version;

import static org.web3j.codegen.SolidityFunctionWrapperGenerator.COMMAND_SOLIDITY;
Expand All @@ -43,6 +45,18 @@ public class Runner {
public static void main(String[] args) throws Exception {
System.out.println(LOGO);

CliConfig config = CliConfig.getConfig();
Updater updater = new Updater(config);
if (!updater.updateCurrentlyAvailable()) {
new Thread(updater::onlineUpdateCheck).start();
josh-richardson marked this conversation as resolved.
Show resolved Hide resolved
} else {
System.out.println(
String.format(
"A Web3j update is available; please run the following command to update: %s",
config.getUpdatePrompt()));
}

Thread.sleep(20000);
if (args.length < 1) {
Console.exitError(USAGE);
} else {
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/org/web3j/console/config/CliConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2019 Web3 Labs LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.config;

import java.io.*;
import java.nio.file.Files;
import java.util.UUID;

import com.google.gson.Gson;

import org.web3j.utils.Version;

public class CliConfig {
private static File web3jHome = new File(System.getProperty("user.home"), ".web3j");
private static File web3jConfigFile = new File(web3jHome, ".config");

private static CliConfig initializeDefaultConfig(File configFile) throws IOException {
if (!web3jHome.exists() && !web3jHome.mkdirs()) {
throw new IOException("Failed to create Web3j home directory");
}
CliConfig defaultCliConfig = new CliConfig(UUID.randomUUID().toString(), false, null);
String jsonToWrite = new Gson().toJson(defaultCliConfig);
Files.writeString(configFile.toPath(), jsonToWrite);
return defaultCliConfig;
}

private static CliConfig getSavedConfig(File configFile) throws IOException {
String configContents = Files.readString(configFile.toPath());
return new Gson().fromJson(configContents, CliConfig.class);
}

public static CliConfig getConfig() throws IOException {
if (web3jConfigFile.exists()) {
return getSavedConfig(web3jConfigFile);
} else {
return initializeDefaultConfig(web3jConfigFile);
}
}

public enum OS {
DARWIN,
FREEBSD,
OPENBSD,
LINUX,
SOLARIS,
WINDOWS,
AIX,
UNKNOWN;

@Override
public String toString() {
return name().toLowerCase();
}
}

public static OS determineOS() {
String osName = System.getProperty("os.name").split(" ")[0];
if (osName.toLowerCase().startsWith("mac") || osName.toLowerCase().startsWith("darwin")) {
return OS.DARWIN;
} else if (osName.toLowerCase().startsWith("linux")) {
return OS.LINUX;
} else if (osName.toLowerCase().startsWith("sunos")
|| osName.toLowerCase().startsWith("solaris")) {
return OS.SOLARIS;
} else if (osName.toLowerCase().startsWith("aix")) {
return OS.AIX;
} else if (osName.toLowerCase().startsWith("openbsd")) {
return OS.OPENBSD;
} else if (osName.toLowerCase().startsWith("freebsd")) {
return OS.FREEBSD;
} else if (osName.toLowerCase().startsWith("windows")) {
return OS.WINDOWS;
} else {
return OS.UNKNOWN;
}
}

private final transient String version;
private String clientId;

private CliConfig() throws IOException {
version = Version.getVersion();
}

public CliConfig(String clientId, boolean updateAvailable, String updatePrompt)
throws IOException {
version = Version.getVersion();
this.clientId = clientId;
this.updateAvailable = updateAvailable;
this.updatePrompt = updatePrompt;
}

private boolean updateAvailable;
private String updatePrompt;

public String getVersion() {
return version;
}

public String getClientId() {
return clientId;
}

public boolean isUpdateAvailable() {
return updateAvailable;
}

public void setUpdateAvailable(boolean updateAvailable) {
this.updateAvailable = updateAvailable;
}

public String getUpdatePrompt() {
return updatePrompt;
}

public void setUpdatePrompt(String updatePrompt) {
this.updatePrompt = updatePrompt;
}

public void save() throws IOException {
String jsonToWrite = new Gson().toJson(this);
Files.writeString(web3jConfigFile.toPath(), jsonToWrite);
}
}
88 changes: 88 additions & 0 deletions src/main/java/org/web3j/console/update/Updater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2019 Web3 Labs LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.update;

import java.io.IOException;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import org.web3j.console.config.CliConfig;
import org.web3j.utils.Version;

public class Updater {

private static final String BASE_URL = "http://localhost:8000";

private CliConfig config;

public Updater(CliConfig config) {
this.config = config;
}

public boolean updateCurrentlyAvailable() {
if (config.isUpdateAvailable()) {
System.out.println(
String.format("A new Web3j update is available: %s", config.getUpdatePrompt()));
return true;
}
return false;
}

public void onlineUpdateCheck() {
OkHttpClient client = new OkHttpClient();

RequestBody updateBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("os", CliConfig.determineOS().toString())
.addFormDataPart("clientId", config.getClientId())
.build();

Request updateCheckRequest =
new okhttp3.Request.Builder()
.url(String.format("%s/api/v1/versioning/versions/", BASE_URL))
.post(updateBody)
.build();

try {
Response sendRawResponse = client.newCall(updateCheckRequest).execute();
if (sendRawResponse.code() == 200) {
JsonParser parser = new JsonParser();
JsonObject rootObj =
parser.parse(sendRawResponse.body().string())
.getAsJsonObject()
.get("latest")
.getAsJsonObject();
String currentVersion = rootObj.get("version").getAsString();
if (!currentVersion.equals(Version.getVersion())) {
config.setUpdateAvailable(true);
config.setUpdatePrompt(
rootObj.get(
CliConfig.determineOS() == CliConfig.OS.WINDOWS
? "install_win"
: "install_unix")
.getAsString());
config.save();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}