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 9 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
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ext {
junitVersion = '5.+'
mockitoVersion = "3.+"
ktlintVersion = "0.31.0"
gsonVersion = "2.8.6"
wireMockVersion = "2.25.1"
}

apply {
Expand Down Expand Up @@ -66,10 +68,13 @@ dependencies {
"javax.xml.bind:jaxb-api:2.2.11",
"com.sun.xml.bind:jaxb-core:2.2.11",
"com.sun.xml.bind:jaxb-impl:2.2.11",
"javax.activation:activation:1.1.1"
"javax.activation:activation:1.1.1",
"org.web3j:hosted-providers:$web3jVersion",
"com.google.code.gson:gson:$gsonVersion"

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

testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion",
"org.mockito:mockito-core:$mockitoVersion"
"org.mockito:mockito-core:$mockitoVersion",
"com.github.tomakehurst:wiremock-jre8:$wireMockVersion"
}
4 changes: 2 additions & 2 deletions gradle/repositories/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
mavenCentral()
jcenter()
maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
2 changes: 1 addition & 1 deletion gradle/spotless/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ spotless {
}
}

spotlessCheck.dependsOn('downloadJavaLicense', 'downloadFormatterProperties')
spotlessCheck.dependsOn('downloadJavaLicense', 'downloadFormatterProperties')
9 changes: 9 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,11 @@ public class Runner {
public static void main(String[] args) throws Exception {
System.out.println(LOGO);

CliConfig config = CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile());
Updater updater = new Updater(config);
updater.promptIfUpdateAvailable();
new Thread(updater::onlineUpdateCheck).start();
josh-richardson marked this conversation as resolved.
Show resolved Hide resolved

if (args.length < 1) {
Console.exitError(USAGE);
} else {
Expand Down Expand Up @@ -77,5 +84,7 @@ public static void main(String[] args) throws Exception {
Console.exitError(USAGE);
}
}

config.save();
}
}
153 changes: 153 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,153 @@
/*
* 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.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

import com.google.gson.Gson;

import org.web3j.utils.Version;

public class CliConfig {
private static final Path web3jConfigPath =
Paths.get(System.getProperty("user.home"), ".web3j", ".config");
private static final String defaultServicesUrl = "http://localhost:8000";

public static Path getWeb3jConfigPath() {
return web3jConfigPath;
}

private static CliConfig initializeDefaultConfig(File configFile) throws IOException {
File web3jHome = new File(configFile.getParent());
if (!web3jHome.exists() && !web3jHome.mkdirs()) {
throw new IOException("Failed to create Web3j home directory");
}
return new CliConfig(
Version.getVersion(),
defaultServicesUrl,
UUID.randomUUID().toString(),
Version.getVersion(),
null);
}

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

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

public String getLatestVersion() {
return latestVersion;
}

public void setLatestVersion(String latestVersion) {
this.latestVersion = latestVersion;
}

public boolean isUpdateAvailable() {
return !version.equals(latestVersion);
}

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].toLowerCase();
if (osName.startsWith("mac") || osName.startsWith("darwin")) {
return OS.DARWIN;
} else if (osName.startsWith("linux")) {
return OS.LINUX;
} else if (osName.startsWith("sunos") || osName.startsWith("solaris")) {
return OS.SOLARIS;
} else if (osName.startsWith("aix")) {
return OS.AIX;
} else if (osName.startsWith("openbsd")) {
return OS.OPENBSD;
} else if (osName.startsWith("freebsd")) {
return OS.FREEBSD;
} else if (osName.startsWith("windows")) {
return OS.WINDOWS;
} else {
return OS.UNKNOWN;
}
}

private String version;
private String servicesUrl;
private String clientId;
private String latestVersion;
private String updatePrompt;

public CliConfig(
String version,
String servicesUrl,
String clientId,
String latestVersion,
String updatePrompt) {
this.version = version;
this.servicesUrl = servicesUrl;
this.clientId = clientId;
this.latestVersion = latestVersion;
this.updatePrompt = updatePrompt;
}

public String getVersion() {
return version;
}

public String getServicesUrl() {
return servicesUrl;
}

public String getClientId() {
return clientId;
}

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.write(web3jConfigPath, jsonToWrite.getBytes(Charset.defaultCharset()));
}
}
86 changes: 86 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,86 @@
/*
* 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 CliConfig config;

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

public void promptIfUpdateAvailable() {
if (config.isUpdateAvailable()) {
System.out.println(
String.format(
"A new Web3j update is available. To update, run: %s",
config.getUpdatePrompt()));
}
}

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/", config.getServicesUrl()))
.post(updateBody)
.build();

try {
Response sendRawResponse = client.newCall(updateCheckRequest).execute();
if (sendRawResponse.code() == 200) {
JsonObject rootObj =
JsonParser.parseString(sendRawResponse.body().string())
.getAsJsonObject()
.get("latest")
.getAsJsonObject();
String latestVersion = rootObj.get("version").getAsString();
if (!latestVersion.equals(Version.getVersion())) {
config.setLatestVersion(latestVersion);
config.setUpdatePrompt(
rootObj.get(
CliConfig.determineOS() == CliConfig.OS.WINDOWS
? "install_win"
: "install_unix")
.getAsString());
config.save();
}
}
} catch (IOException ignored) {
}
}
}
5 changes: 2 additions & 3 deletions src/main/resources/Template.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package <package_name>;
public class <project_name> {

public static void main(String[] args) {

public static void main(String[]args) {

}
}
}
Comment on lines +4 to 7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly relevant to this PR, but fixes the issue where the generator generates Java with weird indentation.

Loading