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 all 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
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
21 changes: 15 additions & 6 deletions src/main/java/org/web3j/console/project/InteractiveOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,44 @@
import java.util.Optional;
import java.util.Scanner;

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

import static java.io.File.separator;
import static org.web3j.codegen.Console.exitError;
import static org.web3j.console.project.utills.ProjectUtils.deleteFolder;
import static org.web3j.console.project.utils.ProjectUtils.deleteFolder;

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
90 changes: 44 additions & 46 deletions src/main/java/org/web3j/console/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import java.io.File;
import java.io.IOException;

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

import static org.web3j.codegen.Console.exitError;
import org.web3j.console.project.utils.InputVerifier;
import org.web3j.console.project.utils.ProgressCounter;

public class Project {

Expand Down Expand Up @@ -76,6 +75,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 All @@ -84,51 +85,48 @@ private void executeCommand(final File workingDir, final String[] command)
.waitFor();
}

public Project build() {
try {
projectStructure.createDirectoryStructure();
final ProjectWriter projectWriter = new ProjectWriter();
projectWriter.writeResourceFile(
templateProvider.getMainJavaClass(),
InputVerifier.capitalizeFirstLetter(
projectStructure.getProjectName() + ".java"),
projectStructure.getMainPath());
projectWriter.writeResourceFile(
templateProvider.getGradleBuild(),
File.separator + "build.gradle",
projectStructure.getProjectRoot());
projectWriter.writeResourceFile(
templateProvider.getGradleSettings(),
File.separator + "settings.gradle",
projectStructure.getProjectRoot());
if (solidityImportPath == null) {
projectWriter.writeResourceFile(
templateProvider.getSolidityProject(),
"HelloWorld.sol",
projectStructure.getSolidityPath());
} else {
projectWriter.importSolidityProject(
solidityImportPath, projectStructure.getSolidityPath());
}
public Project build() throws IOException, InterruptedException {
projectStructure.createDirectoryStructure();
final ProjectWriter projectWriter = new ProjectWriter();
projectWriter.writeResourceFile(
templateProvider.getMainJavaClass(),
InputVerifier.capitalizeFirstLetter(
projectStructure.getProjectName() + ".java"),
projectStructure.getMainPath());
projectWriter.writeResourceFile(
templateProvider.getGradleBuild(),
File.separator + "build.gradle",
projectStructure.getProjectRoot());
projectWriter.writeResourceFile(
templateProvider.getGradleSettings(),
File.separator + "settings.gradle",
projectStructure.getProjectRoot());
if (solidityImportPath == null) {
projectWriter.writeResourceFile(
templateProvider.getGradlewWrapperSettings(),
File.separator + "gradle-wrapper.properties",
projectStructure.getWrapperPath());
projectWriter.writeResourceFile(
templateProvider.getGradlewScript(),
File.separator + "gradlew",
projectStructure.getProjectRoot());
projectWriter.writeResourceFile(
templateProvider.getGradlewBatScript(),
File.separator + "gradlew.bat",
projectStructure.getProjectRoot());
projectWriter.copyResourceFile(
templateProvider.getGradlewJar(),
projectStructure.getWrapperPath() + File.separator + "gradle-wrapper.jar");
buildGradleProject(projectStructure.getProjectRoot());
} catch (final IOException | InterruptedException e) {
exitError("Looks like an error occurred: " + e.getMessage());
templateProvider.getSolidityProject(),
"HelloWorld.sol",
projectStructure.getSolidityPath());
} else {
projectWriter.importSolidityProject(
solidityImportPath, projectStructure.getSolidityPath());
}
projectWriter.writeResourceFile(
templateProvider.getGradlewWrapperSettings(),
File.separator + "gradle-wrapper.properties",
projectStructure.getWrapperPath());
projectWriter.writeResourceFile(
templateProvider.getGradlewScript(),
File.separator + "gradlew",
projectStructure.getProjectRoot());
projectWriter.writeResourceFile(
templateProvider.getGradlewBatScript(),
File.separator + "gradlew.bat",
projectStructure.getProjectRoot());
projectWriter.copyResourceFile(
templateProvider.getGradlewJar(),
projectStructure.getWrapperPath() + File.separator + "gradle-wrapper.jar");
buildGradleProject(projectStructure.getProjectRoot());

return new Project(this);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/web3j/console/project/ProjectCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import picocli.CommandLine;

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

import static java.io.File.separator;
import static org.web3j.codegen.Console.exitError;
Expand Down 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,16 +14,18 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

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

import static org.web3j.codegen.Console.exitError;
import static org.web3j.console.project.InteractiveOptions.overrideExistingProject;
import static org.web3j.console.project.ProjectCreator.COMMAND_NEW;
import static org.web3j.console.project.utills.ProjectUtils.deleteFolder;
import static org.web3j.console.project.utils.ProjectUtils.deleteFolder;
import static picocli.CommandLine.Help.Visibility.ALWAYS;

@Command(name = COMMAND_NEW, mixinStandardHelpOptions = true, version = "4.0", sortOptions = false)
Expand Down Expand Up @@ -67,7 +69,9 @@ private void createProject() {
try {
new ProjectCreator(outputDir, packageName, projectName).generate();
} catch (final IOException e) {
exitError(e);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
exitError("Could not generate project reason:" + sw.toString());
}
}

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

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

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

import static org.web3j.codegen.Console.exitError;
import static org.web3j.console.project.InteractiveOptions.overrideExistingProject;
import static org.web3j.console.project.ProjectImporter.COMMAND_IMPORT;
import static org.web3j.console.project.utills.ProjectUtils.*;
import static org.web3j.console.project.utils.ProjectUtils.*;
import static picocli.CommandLine.Help.Visibility.ALWAYS;

@Command(name = COMMAND_IMPORT)
Expand Down Expand Up @@ -63,7 +65,9 @@ private void createProject() {
new ProjectImporter(outputDir, packageName, projectName, solidityImportPath);
projectImporter.generate(generateTests);
} catch (final Exception e) {
exitError(e);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
exitError("Could not generate project reason:" + sw.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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;
package org.web3j.console.project.utils;

import java.io.File;
import javax.lang.model.SourceVersion;
Expand All @@ -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
42 changes: 42 additions & 0 deletions src/main/java/org/web3j/console/project/utils/ProgressCounter.java
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.utils;

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) {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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;
package org.web3j.console.project.utils;

import java.io.File;
import java.io.IOException;
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/org/web3j/console/project/ProjectCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;

import org.web3j.console.project.utills.ClassExecutor;
import org.web3j.console.project.utils.ClassExecutor;

import static java.io.File.separator;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down 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());
}
}
Loading