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

Implemented formatting changes in the CLI #24

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion gradle/jacoco/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ task jacocoRootTestReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
exclude: [
'org/web3j/abi/datatypes/generated/**',
'org/web3j/tuples/generated/**',
'org/web3j/ens/contracts/generated/**'
'org/web3j/ens/contracts/generated/**',
'org/gradle/**'
])
}))
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/web3j/console/project/InteractiveOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,34 @@ class InteractiveOptions {
static Scanner scanner = new Scanner(System.in);

static String getProjectName() {
print("Please enter the project name (Required Field):");
print("Please enter the project name [HelloWorld]:");
String projectName = getUserInput();
if (projectName.trim().isEmpty()) {
return "HelloWorld";
}
while (!InputVerifier.classNameIsValid(projectName)) {
projectName = getUserInput();
}
return projectName;
}

static String getPackageName() {
print("Please enter the package name for your project (Required Field): ");
print("Please enter the package name for your project [io.web3j]:");
String packageName = getUserInput();
if (packageName.trim().isEmpty()) {
return "io.web3j";
}
while (!InputVerifier.packageNameIsValid(packageName)) {
packageName = getUserInput();
}
return packageName;
}

static Optional<String> getProjectDestination(final String projectName) {
print("Please enter the destination of your project (Current by default): ");
print(
"Please enter the destination of your project ["
+ System.getProperty("user.dir")
+ "]: ");
final String projectDest = getUserInput();
final String projectPath = projectDest + separator + projectName;
if (new File(projectPath).exists()) {
Expand Down Expand Up @@ -79,7 +88,7 @@ static boolean userWantsTests() {
}

static String getSolidityProjectPath() {
System.out.println("Please enter the path to your solidity file/folder (Required Field): ");
System.out.println("Please enter the path to your solidity file/folder [Required Field]: ");
return getUserInput();
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/web3j/console/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import org.web3j.console.project.utills.InputVerifier;
import org.web3j.console.project.utills.ProgressCounter;

import static org.web3j.codegen.Console.exitError;

Expand Down Expand Up @@ -76,6 +78,8 @@ private boolean isWindows() {

private void executeCommand(final File workingDir, final String[] command)
throws InterruptedException, IOException {
ProgressCounter progressCounter = new ProgressCounter();
progressCounter.processing(true, "Creating " + projectStructure.getProjectName());
new ProcessBuilder(command)
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
Expand Down Expand Up @@ -127,7 +131,7 @@ public Project build() {
projectStructure.getWrapperPath() + File.separator + "gradle-wrapper.jar");
buildGradleProject(projectStructure.getProjectRoot());
} catch (final IOException | InterruptedException e) {
exitError("Looks like an error occurred: " + e.getMessage());
exitError("Looks like an error occurred: " + Arrays.toString(e.getStackTrace()));
AlexandrouR marked this conversation as resolved.
Show resolved Hide resolved
}
return new Project(this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/web3j/console/project/ProjectCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ private void generateTests() throws IOException {

private void onSuccess() {
exitSuccess(
"Project created with name: "
"\n"
+ projectStructure.getProjectName()
+ " at location: "
+ " has been created in"
+ projectStructure.getProjectRoot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -67,7 +68,7 @@ private void createProject() {
try {
new ProjectCreator(outputDir, packageName, projectName).generate();
} catch (final IOException e) {
exitError(e);
exitError("Could not generate project reason:" + Arrays.toString(e.getStackTrace()));
AlexandrouR marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.web3j.console.project;

import java.io.File;
import java.util.Arrays;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -63,7 +64,7 @@ private void createProject() {
new ProjectImporter(outputDir, packageName, projectName, solidityImportPath);
projectImporter.generate(generateTests);
} catch (final Exception e) {
exitError(e);
exitError("Could not generate project reason:" + Arrays.toString(e.getStackTrace()));
AlexandrouR marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public static boolean requiredArgsAreNotEmpty(final String... args) {

public static boolean classNameIsValid(final String className) {
if (!SourceVersion.isIdentifier(className) || SourceVersion.isKeyword(className)) {
System.out.println(className + " is not valid name.");
System.out.println(
className
+ " is not valid name. Please make sure that your project name complies with Java's class naming convention.");
return false;
}
return true;
Expand All @@ -38,7 +40,9 @@ public static boolean packageNameIsValid(final String packageName) {
String[] splitPackageName = packageName.split("[.]");
for (final String argument : splitPackageName) {
if (!SourceVersion.isIdentifier(argument) || SourceVersion.isKeyword(argument)) {
System.out.println(argument + " is not a valid package name.");
System.out.println(
argument
+ " is not a valid package name. Please make sure that your project package name complies with Java's package naming convention.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.project.utills;
AlexandrouR marked this conversation as resolved.
Show resolved Hide resolved

import java.io.IOException;

public class ProgressCounter {
public void processing(boolean loading, String message) {
Thread th =
new Thread(
() -> {
String anim = "|/―\\";
try {
System.out.write("\r|".getBytes());
int current = 0;
while (loading) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

on error or on success system.exit is called.

current++;
String data =
String.format(
"\r[ %s ] %s",
anim.charAt(current % anim.length()), message);
System.out.write(data.getBytes());
Thread.sleep(500);
}
System.out.write("\n".getBytes());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});
th.start();
}
}
23 changes: 23 additions & 0 deletions src/test/java/org/web3j/console/project/ProjectCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,27 @@ public void testWhenInteractiveAndFirstInputIsInvalidPackageName()
process.waitFor();
assertEquals(0, process.exitValue());
}

@Test
public void testWhenInteractiveAndOptionsAreDefault() throws IOException, InterruptedException {
final String[] args = {"new"};
Process process =
executeClassAsSubProcessAndReturnProcess(
ProjectCreator.class, Collections.emptyList(), Arrays.asList(args))
.start();
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
writer.write("", 0, 0);
writer.newLine();
writer.write("", 0, 0);
writer.newLine();
writer.write(tempDirPath, 0, tempDirPath.length());
writer.newLine();
writer.write("y", 0, "y".length());
writer.newLine();
writer.close();
process.waitFor();
assertEquals(0, process.exitValue());
assertTrue(new File(tempDirPath + separator + "HelloWorld").exists());
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/web3j/console/project/ProjectImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,29 @@ public void testWhenInteractiveAndFirstInputIsInvalidPackageName()
process.waitFor();
assertEquals(0, process.exitValue());
}

@Test
public void testWhenInteractiveAndDefaultOptions() throws IOException, InterruptedException {
final String[] args = {"import"};
Process process =
executeClassAsSubProcessAndReturnProcess(
ProjectImporter.class, Collections.emptyList(), Arrays.asList(args))
.start();
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
writer.write("", 0, "".length());
writer.newLine();
writer.write("", 0, "".length());
writer.newLine();
writer.write(formattedPath, 0, formattedPath.length());
writer.newLine();
writer.write(tempDirPath, 0, tempDirPath.length());
writer.newLine();
writer.write("n", 0, "n".length());
writer.newLine();
writer.close();
process.waitFor();
assertEquals(0, process.exitValue());
assertTrue(new File(tempDirPath + separator + "HelloWorld").exists());
}
}