Skip to content

Commit

Permalink
Changes update checker to look at current version vs latest version i…
Browse files Browse the repository at this point in the history
…nstead of using an update boolean
  • Loading branch information
josh-richardson committed Nov 23, 2019
1 parent 872755a commit faeda63
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
31 changes: 18 additions & 13 deletions src/main/java/org/web3j/console/config/CliConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static CliConfig initializeDefaultConfig(File configFile) throws IOExcep
Version.getVersion(),
defaultServicesUrl,
UUID.randomUUID().toString(),
false,
Version.getVersion(),
null);
}

Expand All @@ -58,6 +58,18 @@ public static CliConfig getConfig(File configFile) throws IOException {
}
}

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,
Expand Down Expand Up @@ -98,22 +110,23 @@ public static OS determineOS() {
private String version;
private String servicesUrl;
private String clientId;
private String latestVersion;
private String updatePrompt;

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

private boolean updateAvailable;
private String updatePrompt;


public String getVersion() {
return version;
Expand All @@ -127,14 +140,6 @@ public String getClientId() {
return clientId;
}

public boolean isUpdateAvailable() {
return updateAvailable;
}

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

public String getUpdatePrompt() {
return updatePrompt;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/web3j/console/update/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public Updater(CliConfig config) {
public void promptIfUpdateAvailable() {
if (config.isUpdateAvailable()) {
System.out.println(
String.format("A new Web3j update is available: %s", config.getUpdatePrompt()));
String.format(
"A new Web3j update is available. To update, run: %s",
config.getUpdatePrompt()));
}
}

Expand Down Expand Up @@ -66,9 +68,9 @@ public void onlineUpdateCheck() {
.getAsJsonObject()
.get("latest")
.getAsJsonObject();
String currentVersion = rootObj.get("version").getAsString();
if (!currentVersion.equals(Version.getVersion())) {
config.setUpdateAvailable(true);
String latestVersion = rootObj.get("version").getAsString();
if (!latestVersion.equals(Version.getVersion())) {
config.setLatestVersion(latestVersion);
config.setUpdatePrompt(
rootObj.get(
CliConfig.determineOS() == CliConfig.OS.WINDOWS
Expand Down
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) {

}
}
}
26 changes: 14 additions & 12 deletions src/test/java/org/web3j/console/project/UpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void testUpdateCheckWorksSuccessfullyWhenUpdateAvailable(String version) throws
Version.getVersion(),
"http://localhost:8081",
UUID.randomUUID().toString(),
false,
Version.getVersion(),
null)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));

Expand All @@ -81,7 +81,7 @@ void testUpdateCheckWorksSuccessfullyWhenUpdateAvailable(String version) throws
config.getVersion(),
config.getServicesUrl(),
config.getClientId(),
config.isUpdateAvailable(),
config.getLatestVersion(),
config.getUpdatePrompt()));
Files.write(
tempWeb3jSettingsPath,
Expand All @@ -95,22 +95,24 @@ void testUpdateCheckWorksSuccessfullyWhenUpdateAvailable(String version) throws

Updater updater = new Updater(config);

String validUpdateResponse =
String.format(
"{\n"
+ " \"latest\": {\n"
+ " \"version\": \"%s\",\n"
+ " \"install_unix\": \"curl -L get.web3j.io | sh\",\n"
+ " \"install_win\": \"Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/web3j/web3j-installer/master/installer.ps1'))\"\n"
+ " }\n"
+ "}",
version);

stubFor(
post(urlPathMatching("/api/v1/versioning/versions/"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(
String.format(
"{\n"
+ " \"latest\": {\n"
+ " \"version\": \"%s\",\n"
+ " \"install_win\": \"curl -L get.web3j.io | sh\",\n"
+ " \"install_unix\": \"Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/web3j/web3j-installer/master/installer.ps1'))\"\n"
+ " }\n"
+ "}",
version))));
.withBody(validUpdateResponse)));
updater.onlineUpdateCheck();

verify(postRequestedFor(urlEqualTo("/api/v1/versioning/versions/")));
Expand Down

0 comments on commit faeda63

Please sign in to comment.