diff --git a/build.gradle b/build.gradle index b09f179..3a9a930 100644 --- a/build.gradle +++ b/build.gradle @@ -34,7 +34,7 @@ apply { download { src "https://raw.githubusercontent.com/web3j/build-tools/master/gradle/$buildScript/build.gradle" dest "$rootDir/gradle/$buildScript/build.gradle" - overwrite false + overwrite true quiet true onlyIfModified true } diff --git a/gradle.properties b/gradle.properties index b2c4ee6..bdd8434 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=4.5.15 +version=4.6.0-SNAPSHOT \ No newline at end of file diff --git a/gradle/repositories/build.gradle b/gradle/repositories/build.gradle index e7581f6..350db19 100644 --- a/gradle/repositories/build.gradle +++ b/gradle/repositories/build.gradle @@ -1,5 +1,4 @@ repositories { - mavenLocal() mavenCentral() jcenter() maven { url 'https://oss.sonatype.org/content/repositories/releases/' } diff --git a/src/main/java/org/web3j/console/Runner.java b/src/main/java/org/web3j/console/Runner.java index 05b72e3..6e0f1d4 100644 --- a/src/main/java/org/web3j/console/Runner.java +++ b/src/main/java/org/web3j/console/Runner.java @@ -48,7 +48,6 @@ 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(); @@ -97,7 +96,6 @@ public static void main(String[] args) throws Exception { } } config.save(); - // TODO change this with a no argument method when the update is done in web3j - Console.exitSuccess(""); + Console.exitSuccess(); } } diff --git a/src/main/java/org/web3j/console/account/AccountManager.java b/src/main/java/org/web3j/console/account/AccountManager.java index 3d246ce..dc0bdde 100644 --- a/src/main/java/org/web3j/console/account/AccountManager.java +++ b/src/main/java/org/web3j/console/account/AccountManager.java @@ -66,8 +66,13 @@ public void createAccount(String email) throws IOException { JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject(); if (responseJsonObj.get("token") == null) { + String tokenError = responseJsonObj.get("tokenError").getAsString(); - System.out.println(tokenError); + if (tokenError == null || tokenError.isEmpty()) { + System.out.println("Could not retrieve token. Try again later."); + } else { + System.out.println(tokenError); + } return; } String token = responseJsonObj.get("token").getAsString(); diff --git a/src/main/java/org/web3j/console/project/InteractiveOptions.java b/src/main/java/org/web3j/console/project/InteractiveOptions.java index 61b3fd3..fa49134 100644 --- a/src/main/java/org/web3j/console/project/InteractiveOptions.java +++ b/src/main/java/org/web3j/console/project/InteractiveOptions.java @@ -13,10 +13,14 @@ package org.web3j.console.project; import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.util.Optional; import java.util.Scanner; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import org.web3j.account.LocalWeb3jAccount; import org.web3j.console.project.utils.InputVerifier; import static java.io.File.separator; @@ -73,27 +77,27 @@ public static Optional getGeneratedWrapperLocation() { print( "Please enter the path of the generated contract wrappers [" + String.join( - separator, - System.getProperty("user.dir"), - "build", - "generated", - "source", - "web3j", - "main", - "java") + separator, + System.getProperty("user.dir"), + "build", + "generated", + "source", + "web3j", + "main", + "java") + "]"); String pathToTheWrappers = getUserInput(); return pathToTheWrappers.isEmpty() ? Optional.of( - String.join( - separator, - System.getProperty("user.dir"), - "build", - "generated", - "source", - "web3j", - "main", - "java")) + String.join( + separator, + System.getProperty("user.dir"), + "build", + "generated", + "source", + "web3j", + "main", + "java")) : Optional.of(pathToTheWrappers); } @@ -101,13 +105,13 @@ public static Optional setGeneratedTestLocationJava() { print( "Where would you like to save your tests [" + String.join( - separator, System.getProperty("user.dir"), "src", "test", "java") + separator, System.getProperty("user.dir"), "src", "test", "java") + "]"); String outputPath = getUserInput(); return outputPath.isEmpty() ? Optional.of( - String.join( - separator, System.getProperty("user.dir"), "src", "test", "java")) + String.join( + separator, System.getProperty("user.dir"), "src", "test", "java")) : Optional.of(outputPath); } @@ -115,13 +119,13 @@ public static Optional setGeneratedTestLocationKotlin() { print( "Where would you like to save your tests [" + String.join( - separator, System.getProperty("user.dir"), "src", "test", "kotlin") + separator, System.getProperty("user.dir"), "src", "test", "kotlin") + "]"); String outputPath = getUserInput(); return outputPath.isEmpty() ? Optional.of( - String.join( - separator, System.getProperty("user.dir"), "src", "test", "kotlin")) + String.join( + separator, System.getProperty("user.dir"), "src", "test", "kotlin")) : Optional.of(outputPath); } @@ -137,7 +141,6 @@ public static String getSolidityProjectPath() { } static String getUserInput() { - return scanner.nextLine(); } @@ -150,4 +153,24 @@ public static boolean overrideExistingProject() { String userAnswer = getUserInput(); return userAnswer.toLowerCase().equals("y"); } + + public static boolean userHasWeb3jAccount() throws IOException { + if (LocalWeb3jAccount.configExists()) { + ObjectNode objectNode = LocalWeb3jAccount.readConfigAsJson(); + return LocalWeb3jAccount.loginTokenExists(objectNode); + } + return false; + } + + public static boolean configFileExists() { + return LocalWeb3jAccount.configExists(); + } + + public static boolean userWantsWeb3jAccount() throws IOException { + + print("It looks like you don’t have a Web3j account, would you like to create one?"); + print("This will provide free access to the Ethereum network [Y/n]"); + String userAnswer = getUserInput(); + return userAnswer.toLowerCase().equals("y") || userAnswer.trim().equals(""); + } } diff --git a/src/main/java/org/web3j/console/project/ProjectCreator.java b/src/main/java/org/web3j/console/project/ProjectCreator.java index 8f20656..4258f8f 100644 --- a/src/main/java/org/web3j/console/project/ProjectCreator.java +++ b/src/main/java/org/web3j/console/project/ProjectCreator.java @@ -13,6 +13,7 @@ package org.web3j.console.project; import java.io.File; +import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; @@ -22,6 +23,8 @@ import org.jetbrains.annotations.NotNull; import picocli.CommandLine; +import org.web3j.console.account.AccountManager; +import org.web3j.console.config.CliConfig; import org.web3j.console.project.java.JavaBuilder; import org.web3j.console.project.java.JavaProjectCreatorCLIRunner; import org.web3j.console.project.kotlin.KotlinBuilder; @@ -48,7 +51,7 @@ public ProjectCreator(final String root, final String packageName, final String this.root = root; } - public static void main(String[] args) { + public static void main(String[] args) throws IOException { final List stringOptions = new ArrayList<>(); if (args.length > 0 && args[0].toLowerCase().equals(COMMAND_JAVA)) { args = tail(args); @@ -61,7 +64,8 @@ public static void main(String[] args) { } @NotNull - private static String[] getValues(String[] args, List stringOptions) { + private static String[] getValues(String[] args, List stringOptions) + throws IOException { String projectName; if (args.length == 0) { stringOptions.add("-n"); @@ -75,6 +79,22 @@ private static String[] getValues(String[] args, List stringOptions) { stringOptions.add("-o"); stringOptions.add(projectDest); }); + if (InteractiveOptions.configFileExists()) { + if (!InteractiveOptions.userHasWeb3jAccount()) { + if (InteractiveOptions.userWantsWeb3jAccount()) { + AccountManager.main( + CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile()), + new String[] {"create"}); + } + } + } else { + if (InteractiveOptions.userWantsWeb3jAccount()) { + AccountManager.main( + CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile()), + new String[] {"create"}); + } + } + args = stringOptions.toArray(new String[0]); } return args; diff --git a/src/test/java/org/web3j/console/project/UpdaterTest.java b/src/test/java/org/web3j/console/project/UpdaterTest.java index 4f9fbb3..96be6fb 100644 --- a/src/test/java/org/web3j/console/project/UpdaterTest.java +++ b/src/test/java/org/web3j/console/project/UpdaterTest.java @@ -86,7 +86,7 @@ private void testWorksWithVersion(String version) throws IOException { "http://localhost:8081", UUID.randomUUID().toString(), Version.getVersion(), - null) + null,null) .defaultAnswer(Mockito.CALLS_REAL_METHODS)); doAnswer( diff --git a/src/test/java/org/web3j/console/project/java/JavaProjectCreatorTest.java b/src/test/java/org/web3j/console/project/java/JavaProjectCreatorTest.java index cf61b80..0deab15 100644 --- a/src/test/java/org/web3j/console/project/java/JavaProjectCreatorTest.java +++ b/src/test/java/org/web3j/console/project/java/JavaProjectCreatorTest.java @@ -96,7 +96,7 @@ public void verifyThatTestsAreGenerated() { } @Test - public void testWithPicoCliWhenArgumentsAreEmpty() { + public void testWithPicoCliWhenArgumentsAreEmpty() throws IOException { final String[] args = {"--java", "-n=", "-p="}; ProjectCreator.main(args); assertEquals(