diff --git a/src/main/java/org/web3j/console/Runner.java b/src/main/java/org/web3j/console/Runner.java index 42e5ccf..aac53b3 100644 --- a/src/main/java/org/web3j/console/Runner.java +++ b/src/main/java/org/web3j/console/Runner.java @@ -16,16 +16,18 @@ import org.web3j.codegen.SolidityFunctionWrapperGenerator; import org.web3j.codegen.TruffleJsonFunctionWrapperGenerator; import org.web3j.console.config.CliConfig; -import org.web3j.console.project.java.ProjectCreator; -import org.web3j.console.project.java.ProjectImporter; -import org.web3j.console.project.java.UnitTestCreator; +import org.web3j.console.project.ProjectCreator; +import org.web3j.console.project.ProjectImporter; +import org.web3j.console.project.java.JavaTestCreator; +import org.web3j.console.project.kotlin.ProjectCreatorKotlin; import org.web3j.console.update.Updater; import org.web3j.utils.Version; import static org.web3j.codegen.SolidityFunctionWrapperGenerator.COMMAND_SOLIDITY; -import static org.web3j.console.project.java.ProjectCreator.COMMAND_NEW; -import static org.web3j.console.project.java.ProjectImporter.COMMAND_IMPORT; -import static org.web3j.console.project.java.UnitTestCreator.COMMAND_GENERATE_TESTS; +import static org.web3j.console.project.ProjectCreator.COMMAND_NEW; +import static org.web3j.console.project.ProjectImporter.COMMAND_IMPORT; +import static org.web3j.console.project.java.JavaTestCreator.COMMAND_GENERATE_TESTS; +import static org.web3j.console.project.kotlin.ProjectCreatorKotlin.COMMAND_NEW_KOTLIN; import static org.web3j.utils.Collection.tail; /** Main entry point for running command line utilities. */ @@ -71,6 +73,9 @@ public static void main(String[] args) throws Exception { case COMMAND_NEW: ProjectCreator.main(args); break; + case COMMAND_NEW_KOTLIN: + ProjectCreatorKotlin.main(args); + break; case COMMAND_IMPORT: ProjectImporter.main(args); break; @@ -86,7 +91,7 @@ public static void main(String[] args) throws Exception { ContractAuditor.main(tail(args)); break; case COMMAND_GENERATE_TESTS: - UnitTestCreator.main(args); + JavaTestCreator.main(args); break; default: Console.exitError(USAGE); diff --git a/src/main/java/org/web3j/console/project/BaseBuilder.java b/src/main/java/org/web3j/console/project/BaseBuilder.java index 424e867..d39feba 100644 --- a/src/main/java/org/web3j/console/project/BaseBuilder.java +++ b/src/main/java/org/web3j/console/project/BaseBuilder.java @@ -16,62 +16,62 @@ public class BaseBuilder { - private String solidityImportPath; - private boolean withWallet; - private boolean withTests; - private String projectName; - private String packageName; - private String rootDirectory; - private boolean withSampleCode; - private boolean withFatJar; - private String command = "new"; + protected String solidityImportPath; + protected boolean withWallet; + protected boolean withTests; + protected String projectName; + protected String packageName; + protected String rootDirectory; + protected boolean withSampleCode; + protected boolean withFatJar; + protected String command = "new"; - public BaseBuilder withSolidityFile(final String solidityImportPath) { + public BaseBuilder withSolidityFile(final String solidityImportPath) { this.solidityImportPath = solidityImportPath; return this; } - public BaseBuilder withWalletProvider(boolean withWalletProvider) { + public BaseBuilder withWalletProvider(boolean withWalletProvider) { this.withWallet = withWalletProvider; return this; } - public BaseBuilder withSampleCode(boolean withSampleCode) { + public BaseBuilder withSampleCode(boolean withSampleCode) { this.withSampleCode = withSampleCode; return this; } - public BaseBuilder withTests(boolean withTests) { + public BaseBuilder withTests(boolean withTests) { this.withTests = withTests; return this; } - public BaseBuilder withFatJar(boolean withFatJar) { + public BaseBuilder withFatJar(boolean withFatJar) { this.withFatJar = withFatJar; return this; } - public BaseBuilder withCommand(String command) { + public BaseBuilder withCommand(String command) { this.command = command; return this; } - public BaseBuilder withProjectName(String projectName) { + public BaseBuilder withProjectName(String projectName) { this.projectName = projectName; return this; } - public BaseBuilder withPackageName(String packageName) { + public BaseBuilder withPackageName(String packageName) { this.packageName = packageName; return this; } - public BaseBuilder withRootDirectory(String rootDirectory) { + public BaseBuilder withRootDirectory(String rootDirectory) { this.rootDirectory = rootDirectory; return this; } - public BaseProject build() throws Exception { + public BaseProject build() throws Exception { final ProjectStructure projectStructure = new JavaProjectStructure(rootDirectory, packageName, projectName); return new BaseProject( diff --git a/src/main/java/org/web3j/console/project/BaseProject.java b/src/main/java/org/web3j/console/project/BaseProject.java index 427514d..bf4ac64 100644 --- a/src/main/java/org/web3j/console/project/BaseProject.java +++ b/src/main/java/org/web3j/console/project/BaseProject.java @@ -20,25 +20,23 @@ import org.web3j.codegen.Console; import org.web3j.commons.JavaVersion; -import org.web3j.console.project.java.UnitTestCreator; +import org.web3j.console.project.java.JavaTestCreator; import org.web3j.console.project.templates.TemplateBuilder; import org.web3j.console.project.templates.TemplateProvider; import org.web3j.console.project.utils.ProgressCounter; import org.web3j.console.project.utils.ProjectUtils; import org.web3j.crypto.CipherException; -import static java.io.File.separator; - public class BaseProject { - private final boolean withTests; - private final boolean withFatJar; - private final boolean withWallet; - private final boolean withSampleCode; - private final String command; - private final String solidityImportPath; - private final ProjectStructure projectStructure; - private ProjectWallet projectWallet; - private ProgressCounter progressCounter = new ProgressCounter(true); + protected final boolean withTests; + protected final boolean withFatJar; + protected final boolean withWallet; + protected final boolean withSampleCode; + protected final String command; + protected final String solidityImportPath; + protected final ProjectStructure projectStructure; + protected ProjectWallet projectWallet; + protected ProgressCounter progressCounter = new ProgressCounter(true); protected BaseProject( boolean withTests, @@ -65,7 +63,7 @@ public ProjectWallet getProjectWallet() { return this.projectWallet; } - private void buildGradleProject(final String pathToDirectory) + protected void buildGradleProject(final String pathToDirectory) throws IOException, InterruptedException { if (!isWindows()) { setExecutable(pathToDirectory, "gradlew"); @@ -107,7 +105,7 @@ private void executeProcess(File workingDir, String[] command) } } - private void createFatJar(String pathToDirectory) throws IOException, InterruptedException { + protected void createFatJar(String pathToDirectory) throws IOException, InterruptedException { if (!isWindows()) { executeProcess( new File(pathToDirectory), @@ -119,37 +117,23 @@ private void createFatJar(String pathToDirectory) throws IOException, Interrupte } } - private void generateTopLevelDirectories(ProjectStructure projectStructure) { + protected void generateTopLevelDirectories(ProjectStructure projectStructure) { projectStructure.createMainDirectory(); + System.out.println(projectStructure.getMainPath()); projectStructure.createTestDirectory(); projectStructure.createSolidityDirectory(); projectStructure.createWrapperDirectory(); } - private void generateTests(ProjectStructure projectStructure) throws IOException { - String wrapperPath = - String.join( - separator, - projectStructure.getRootDirectory(), - projectStructure.projectName, - "build", - "generated", - "source", - "web3j", - "main", - "java"); - String writePath = - String.join( - separator, - projectStructure.getRootDirectory(), - projectStructure.projectName, - "src", - "test", - "java"); - new UnitTestCreator(wrapperPath, writePath).generate(); + protected void generateTests(ProjectStructure projectStructure) throws IOException { + + new JavaTestCreator( + projectStructure.getGeneratedJavaWrappers(), + projectStructure.getPathToTestDirectory()) + .generate(); } - private void generateWallet() + protected void generateWallet() throws CipherException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException { projectStructure.createWalletDirectory(); @@ -162,7 +146,7 @@ private void generateWallet() projectStructure.getWalletPath()); } - private TemplateProvider getTemplateProvider() { + public TemplateProvider getTemplateProvider() { TemplateBuilder templateBuilder = new TemplateBuilder() .withProjectNameReplacement(projectStructure.projectName) diff --git a/src/main/java/org/web3j/console/project/java/ProjectCreator.java b/src/main/java/org/web3j/console/project/ProjectCreator.java similarity index 96% rename from src/main/java/org/web3j/console/project/java/ProjectCreator.java rename to src/main/java/org/web3j/console/project/ProjectCreator.java index 955ffd6..023f500 100644 --- a/src/main/java/org/web3j/console/project/java/ProjectCreator.java +++ b/src/main/java/org/web3j/console/project/ProjectCreator.java @@ -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.java; +package org.web3j.console.project; import java.io.File; import java.io.PrintWriter; @@ -21,9 +21,6 @@ import picocli.CommandLine; -import org.web3j.console.project.BaseBuilder; -import org.web3j.console.project.BaseProject; -import org.web3j.console.project.InteractiveOptions; import org.web3j.console.project.utils.InputVerifier; import static org.web3j.codegen.Console.exitError; diff --git a/src/main/java/org/web3j/console/project/java/ProjectCreatorCLIRunner.java b/src/main/java/org/web3j/console/project/ProjectCreatorCLIRunner.java similarity index 95% rename from src/main/java/org/web3j/console/project/java/ProjectCreatorCLIRunner.java rename to src/main/java/org/web3j/console/project/ProjectCreatorCLIRunner.java index 902021f..2368139 100644 --- a/src/main/java/org/web3j/console/project/java/ProjectCreatorCLIRunner.java +++ b/src/main/java/org/web3j/console/project/ProjectCreatorCLIRunner.java @@ -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.java; +package org.web3j.console.project; import java.io.File; import java.util.Optional; @@ -22,7 +22,7 @@ import static org.web3j.codegen.Console.exitError; import static org.web3j.console.project.InteractiveOptions.overrideExistingProject; -import static org.web3j.console.project.java.ProjectCreator.COMMAND_NEW; +import static org.web3j.console.project.ProjectCreator.COMMAND_NEW; import static org.web3j.console.project.utils.ProjectUtils.deleteFolder; import static picocli.CommandLine.Help.Visibility.ALWAYS; diff --git a/src/main/java/org/web3j/console/project/java/ProjectImporter.java b/src/main/java/org/web3j/console/project/ProjectImporter.java similarity index 98% rename from src/main/java/org/web3j/console/project/java/ProjectImporter.java rename to src/main/java/org/web3j/console/project/ProjectImporter.java index 4997345..dcc7615 100644 --- a/src/main/java/org/web3j/console/project/java/ProjectImporter.java +++ b/src/main/java/org/web3j/console/project/ProjectImporter.java @@ -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.java; +package org.web3j.console.project; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/org/web3j/console/project/java/ProjectImporterCLIRunner.java b/src/main/java/org/web3j/console/project/ProjectImporterCLIRunner.java similarity index 95% rename from src/main/java/org/web3j/console/project/java/ProjectImporterCLIRunner.java rename to src/main/java/org/web3j/console/project/ProjectImporterCLIRunner.java index f0619bd..6789f7b 100644 --- a/src/main/java/org/web3j/console/project/java/ProjectImporterCLIRunner.java +++ b/src/main/java/org/web3j/console/project/ProjectImporterCLIRunner.java @@ -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.java; +package org.web3j.console.project; import java.io.File; import java.util.Optional; @@ -22,7 +22,7 @@ import static org.web3j.codegen.Console.exitError; import static org.web3j.console.project.InteractiveOptions.overrideExistingProject; -import static org.web3j.console.project.java.ProjectImporter.COMMAND_IMPORT; +import static org.web3j.console.project.ProjectImporter.COMMAND_IMPORT; import static org.web3j.console.project.utils.ProjectUtils.*; import static picocli.CommandLine.Help.Visibility.ALWAYS; diff --git a/src/main/java/org/web3j/console/project/ProjectStructure.java b/src/main/java/org/web3j/console/project/ProjectStructure.java index 49d044d..736848c 100644 --- a/src/main/java/org/web3j/console/project/ProjectStructure.java +++ b/src/main/java/org/web3j/console/project/ProjectStructure.java @@ -16,15 +16,17 @@ public abstract class ProjectStructure { - protected final String packageName; - protected final String projectName; + public final String packageName; + public final String projectName; protected final String rootDirectory; protected final String projectRoot; - private final String testPath; + private final String pathToTestDirectory; private final String solidityPath; private final String mainPath; private final String wrapperPath; private final String walletPath; + private final String generatedJavaWrapper; + private final String testPath; protected ProjectStructure( final String rootDirectory, @@ -39,10 +41,21 @@ protected ProjectStructure( this.mainPath = generatePath(this.projectRoot, "src", "main", projectType, formattedPackageName); this.solidityPath = generatePath(this.projectRoot, "src", "main", "solidity"); + this.pathToTestDirectory = generatePath(this.projectRoot, "src", "test", projectType); this.testPath = generatePath(this.projectRoot, "src", "test", projectType, formattedPackageName); this.walletPath = generatePath(this.projectRoot, "src", "test", "resources", "wallet"); this.wrapperPath = generatePath(this.projectRoot, "gradle", "wrapper"); + this.generatedJavaWrapper = + generatePath( + this.rootDirectory, + projectName, + "build", + "generated", + "source", + "web3j", + "main", + "java"); } protected String generateRoot(final String path) { @@ -83,7 +96,7 @@ public void createMainDirectory() { } public void createTestDirectory() { - createDirectory(testPath); + createDirectory(pathToTestDirectory); } public void createSolidityDirectory() { @@ -106,10 +119,18 @@ public final String getProjectName() { return projectName; } + public final String getPathToTestDirectory() { + return pathToTestDirectory; + } + public final String getTestPath() { return testPath; } + public final String getGeneratedJavaWrappers() { + return generatedJavaWrapper; + } + public final String getSolidityPath() { return solidityPath; } diff --git a/src/main/java/org/web3j/console/project/java/UnitTestCLIRunner.java b/src/main/java/org/web3j/console/project/java/JavaTestCLIRunner.java similarity index 88% rename from src/main/java/org/web3j/console/project/java/UnitTestCLIRunner.java rename to src/main/java/org/web3j/console/project/java/JavaTestCLIRunner.java index ea86e3a..8fe0eb2 100644 --- a/src/main/java/org/web3j/console/project/java/UnitTestCLIRunner.java +++ b/src/main/java/org/web3j/console/project/java/JavaTestCLIRunner.java @@ -23,23 +23,23 @@ mixinStandardHelpOptions = true, version = "4.0", sortOptions = false) -public class UnitTestCLIRunner implements Runnable { +public class JavaTestCLIRunner implements Runnable { @CommandLine.Option( names = {"-i", "--java-wrapper-directory"}, description = "The class path of your generated wrapper.", required = true) - String javaWrapperDir; + public String javaWrapperDir; @CommandLine.Option( names = {"-o", "--output-directory"}, description = "The path where the unit tests will be generated.", required = true) - String unitTestOutputDir; + public String unitTestOutputDir; @Override public void run() { try { - new UnitTestCreator(javaWrapperDir, unitTestOutputDir).generate(); + new JavaTestCreator(javaWrapperDir, unitTestOutputDir).generate(); Console.exitSuccess( "Unit tests were generated successfully at location: " + unitTestOutputDir); } catch (IOException e) { diff --git a/src/main/java/org/web3j/console/project/java/UnitTestCreator.java b/src/main/java/org/web3j/console/project/java/JavaTestCreator.java similarity index 95% rename from src/main/java/org/web3j/console/project/java/UnitTestCreator.java rename to src/main/java/org/web3j/console/project/java/JavaTestCreator.java index 3f38ce5..3525408 100644 --- a/src/main/java/org/web3j/console/project/java/UnitTestCreator.java +++ b/src/main/java/org/web3j/console/project/java/JavaTestCreator.java @@ -26,13 +26,13 @@ import static org.web3j.utils.Collection.tail; -public class UnitTestCreator { +public class JavaTestCreator { public static final String COMMAND_GENERATE_TESTS = "generate-tests"; private final String writePath; private final String wrapperPath; - public UnitTestCreator(final String wrapperPath, final String writePath) { + public JavaTestCreator(final String wrapperPath, final String writePath) { this.writePath = writePath; this.wrapperPath = wrapperPath; } @@ -57,7 +57,7 @@ public static void main(String[] args) { args = listOfArgs.toArray(new String[0]); } } - CommandLine.run(new UnitTestCLIRunner(), args); + CommandLine.run(new JavaTestCLIRunner(), args); } public void generate() throws IOException { diff --git a/src/main/java/org/web3j/console/project/kotlin/KotlinBuilder.java b/src/main/java/org/web3j/console/project/kotlin/KotlinBuilder.java new file mode 100644 index 0000000..09365d5 --- /dev/null +++ b/src/main/java/org/web3j/console/project/kotlin/KotlinBuilder.java @@ -0,0 +1,33 @@ +/* + * Copyright 2020 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.kotlin; + +import org.web3j.console.project.BaseBuilder; +import org.web3j.console.project.BaseProject; + +public class KotlinBuilder extends BaseBuilder { + protected String command = "kotlin"; + + public BaseProject build() throws Exception { + final KotlinProjectStructure projectStructure = + new KotlinProjectStructure(super.rootDirectory, packageName, projectName); + return new KotlinProject( + withTests, + withFatJar, + withWallet, + withSampleCode, + command, + solidityImportPath, + projectStructure); + } +} diff --git a/src/main/java/org/web3j/console/project/kotlin/KotlinProject.java b/src/main/java/org/web3j/console/project/kotlin/KotlinProject.java new file mode 100644 index 0000000..924840d --- /dev/null +++ b/src/main/java/org/web3j/console/project/kotlin/KotlinProject.java @@ -0,0 +1,107 @@ +/* + * Copyright 2020 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.kotlin; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; + +import org.web3j.commons.JavaVersion; +import org.web3j.console.project.BaseProject; +import org.web3j.console.project.ProjectStructure; +import org.web3j.console.project.templates.TemplateBuilder; +import org.web3j.console.project.templates.kotlin.KotlinTemplateBuilder; +import org.web3j.console.project.templates.kotlin.KotlinTemplateProvider; +import org.web3j.crypto.CipherException; + +public class KotlinProject extends BaseProject { + protected KotlinProject( + boolean withTests, + boolean withFatJar, + boolean withWallet, + boolean withSampleCode, + String command, + String solidityImportPath, + ProjectStructure projectStructure) { + super( + withTests, + withFatJar, + withWallet, + withSampleCode, + command, + solidityImportPath, + projectStructure); + } + + public KotlinTemplateProvider getTemplateProvider() { + TemplateBuilder templateBuilder = + new KotlinTemplateBuilder() + .withProjectNameReplacement(projectStructure.projectName) + .withPackageNameReplacement(projectStructure.packageName) + .withGradleBatScript("gradlew.bat.template") + .withGradleScript("gradlew.template"); + if (projectWallet != null) { + + templateBuilder.withWalletNameReplacement(projectWallet.getWalletName()); + templateBuilder.withPasswordFileName(projectWallet.getPasswordFileName()); + } + if (command.equals("kotlin")) { + templateBuilder + .withGradleBuild( + JavaVersion.getJavaVersionAsDouble() < 11 + ? "build.gradle.template" + : "build.gradleJava11.template") + .withSolidityProject("HelloWorld.sol"); + + } else if (command.equals("import")) { + templateBuilder + .withGradleBuild( + JavaVersion.getJavaVersionAsDouble() < 11 + ? "build.gradleImport.template" + : "build.gradleImportJava11.template") + .withPathToSolidityFolder(solidityImportPath); + } + templateBuilder + .withGradleSettings("settings.gradle.template") + .withWrapperGradleSettings("gradlew-wrapper.properties.template") + .withGradlewWrapperJar("gradle-wrapper.jar"); + if (withSampleCode) { + templateBuilder.withMainJavaClass("Template-Kotlin"); + } else { + templateBuilder.withMainJavaClass("EmptyTemplate.java"); + } + + return (KotlinTemplateProvider) templateBuilder.build(); + } + + public void createProject() + throws IOException, InterruptedException, NoSuchAlgorithmException, + NoSuchProviderException, InvalidAlgorithmParameterException, CipherException { + generateTopLevelDirectories(projectStructure); + if (withWallet) { + generateWallet(); + } + getTemplateProvider().generateFiles(projectStructure); + progressCounter.processing("Creating " + projectStructure.projectName); + buildGradleProject(projectStructure.getProjectRoot()); + + if (withTests) { + generateTests(projectStructure); + } + if (withFatJar) { + createFatJar(projectStructure.getProjectRoot()); + } + progressCounter.setLoading(false); + } +} diff --git a/src/main/java/org/web3j/console/project/kotlin/KotlinTestCreator.java b/src/main/java/org/web3j/console/project/kotlin/KotlinTestCreator.java new file mode 100644 index 0000000..6c71a59 --- /dev/null +++ b/src/main/java/org/web3j/console/project/kotlin/KotlinTestCreator.java @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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.kotlin; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import picocli.CommandLine; + +import org.web3j.codegen.Console; +import org.web3j.codegen.unit.gen.ClassProvider; +import org.web3j.codegen.unit.gen.kotlin.KotlinClassGenerator; +import org.web3j.console.project.InteractiveOptions; +import org.web3j.console.project.java.JavaTestCLIRunner; + +import static org.web3j.console.project.java.JavaTestCreator.COMMAND_GENERATE_TESTS; +import static org.web3j.utils.Collection.tail; + +public class KotlinTestCreator { + private final String writePath; + private final String wrapperPath; + + public KotlinTestCreator(final String wrapperPath, final String writePath) { + this.writePath = writePath; + this.wrapperPath = wrapperPath; + } + + public static void main(String[] args) { + if (args.length > 0 && args[0].equals(COMMAND_GENERATE_TESTS)) { + args = tail(args); + if (args.length == 0) { + final List listOfArgs = new ArrayList<>(); + InteractiveOptions.getGeneratedWrapperLocation() + .ifPresent( + wrappersPath -> { + listOfArgs.add("-i"); + listOfArgs.add(wrappersPath); + }); + InteractiveOptions.setGeneratedTestLocation() + .ifPresent( + outputPath -> { + listOfArgs.add("-o"); + listOfArgs.add(outputPath); + }); + args = listOfArgs.toArray(new String[0]); + } + } + CommandLine.run(new JavaTestCLIRunner(), args); + } + + public void generate() throws IOException { + List compiledClasses = new ClassProvider(new File(wrapperPath)).getClasses(); + compiledClasses.forEach( + compiledClass -> { + try { + new KotlinClassGenerator( + compiledClass, + compiledClass + .getCanonicalName() + .substring( + 0, + compiledClass + .getCanonicalName() + .lastIndexOf(".")), + writePath) + .writeClass(); + } catch (IOException e) { + Console.exitError(e); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } +} diff --git a/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlin.java b/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlin.java new file mode 100644 index 0000000..0bcda2d --- /dev/null +++ b/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlin.java @@ -0,0 +1,119 @@ +/* + * Copyright 2020 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.kotlin; + +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import picocli.CommandLine; + +import org.web3j.console.project.BaseProject; +import org.web3j.console.project.InteractiveOptions; +import org.web3j.console.project.utils.InputVerifier; + +import static org.web3j.codegen.Console.exitError; +import static org.web3j.codegen.Console.exitSuccess; +import static org.web3j.utils.Collection.tail; + +public class ProjectCreatorKotlin { + public static final String COMMAND_NEW_KOTLIN = "kotlin"; + + private final String root; + private final String packageName; + private final String projectName; + + ProjectCreatorKotlin(final String root, final String packageName, final String projectName) { + this.projectName = projectName; + this.packageName = packageName; + this.root = root; + } + + public static void main(String[] args) { + final String projectName; + final List stringOptions = new ArrayList<>(); + if (args.length > 0 && args[0].equals(COMMAND_NEW_KOTLIN)) { + args = tail(args); + if (args.length == 0) { + stringOptions.add("-n"); + projectName = InteractiveOptions.getProjectName(); + stringOptions.add(projectName); + stringOptions.add("-p"); + stringOptions.add(InteractiveOptions.getPackageName()); + InteractiveOptions.getProjectDestination(projectName) + .ifPresent( + projectDest -> { + stringOptions.add("-o"); + stringOptions.add(projectDest); + }); + args = stringOptions.toArray(new String[0]); + } + } + CommandLine.run(new ProjectCreatorKotlinCLIRunner(), args); + } + + void generate( + boolean withTests, + Optional solidityFile, + boolean withWalletProvider, + boolean withFatJar, + boolean withSampleCode) { + try { + KotlinBuilder kotlinBuilder = + (KotlinBuilder) + new KotlinBuilder() + .withProjectName(this.projectName) + .withRootDirectory(this.root) + .withPackageName(this.packageName) + .withTests(withTests) + .withWalletProvider(withWalletProvider) + .withCommand(ProjectCreatorKotlin.COMMAND_NEW_KOTLIN) + .withSampleCode(withSampleCode) + .withFatJar(withFatJar); + solidityFile.map(File::getAbsolutePath).ifPresent(kotlinBuilder::withSolidityFile); + KotlinProject kotlinProject = (KotlinProject) kotlinBuilder.build(); + kotlinProject.createProject(); + onSuccess(kotlinProject); + } catch (final Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + exitError("\nCould not generate project reason: \n" + sw.toString()); + } + } + + private void onSuccess(BaseProject baseProject) { + String address = + baseProject.getProjectWallet() == null + ? "" + : ("\nYour wallet address is: " + + baseProject.getProjectWallet().getWalletAddress()); + + exitSuccess( + "\n" + + this.projectName + + " has been created in " + + this.root + + "\n" + + "To test your smart contracts (./src/test/java/io/web3j/generated/contracts/HelloWorldTest.java): ./gradlew test" + + "\n" + + "To run your Web3 app (./src/main/java/io/web3j/" + + InputVerifier.capitalizeFirstLetter(this.projectName) + + ".java): java -DNODE_URL= -jar ./build/libs/" + + InputVerifier.capitalizeFirstLetter(this.projectName) + + "-0.1.0-all.jar\nTo fund your wallet on the Rinkeby test network go to: https://rinkeby.faucet.epirus.io/" + + address); + } +} diff --git a/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlinCLIRunner.java b/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlinCLIRunner.java new file mode 100644 index 0000000..dc4b63a --- /dev/null +++ b/src/main/java/org/web3j/console/project/kotlin/ProjectCreatorKotlinCLIRunner.java @@ -0,0 +1,79 @@ +/* + * Copyright 2020 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.kotlin; + +import java.io.File; +import java.util.Optional; + +import picocli.CommandLine; + +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.kotlin.ProjectCreatorKotlin.COMMAND_NEW_KOTLIN; +import static org.web3j.console.project.utils.ProjectUtils.deleteFolder; +import static picocli.CommandLine.Help.Visibility.ALWAYS; + +@CommandLine.Command( + name = COMMAND_NEW_KOTLIN, + mixinStandardHelpOptions = true, + version = "4.0", + sortOptions = false) +public class ProjectCreatorKotlinCLIRunner implements Runnable { + @CommandLine.Option( + names = {"-o", "--output-dir"}, + description = "Destination base directory.", + required = false, + showDefaultValue = ALWAYS) + String outputDir = System.getProperty("user.dir"); + + @CommandLine.Option( + names = {"-p", "--package"}, + description = "Base package name.", + required = true) + String packageName; + + @CommandLine.Option( + names = {"-n", "--project-name"}, + description = "Project name.", + required = true) + String projectName; + + @Override + public void run() { + if (inputIsValid(projectName, packageName)) { + if (InputVerifier.projectExists(new File(projectName))) { + if (overrideExistingProject()) { + deleteFolder(new File(projectName).toPath()); + createProject(); + } else { + exitError("Project creation was canceled."); + } + } else { + createProject(); + } + } + } + + private void createProject() { + new ProjectCreatorKotlin(outputDir, packageName, projectName) + .generate(true, Optional.empty(), true, true, true); + } + + boolean inputIsValid(String... requiredArgs) { + return InputVerifier.requiredArgsAreNotEmpty(requiredArgs) + && InputVerifier.classNameIsValid(projectName) + && InputVerifier.packageNameIsValid(packageName); + } +} diff --git a/src/main/java/org/web3j/console/project/templates/TemplateBuilder.java b/src/main/java/org/web3j/console/project/templates/TemplateBuilder.java index 94468ed..8571ab4 100644 --- a/src/main/java/org/web3j/console/project/templates/TemplateBuilder.java +++ b/src/main/java/org/web3j/console/project/templates/TemplateBuilder.java @@ -12,20 +12,20 @@ */ package org.web3j.console.project.templates; -public class TemplateBuilder { - private String mainJavaClass; - private String gradleBuild; - private String gradleSettings; - private String gradlewWrapperSettings; - private String gradlewBatScript; - private String gradlewScript; - private String solidityProject; - private String gradlewWrapperJar; - private String packageNameReplacement; - private String projectNameReplacement; - private String passwordFileName; - private String walletNameReplacement; - private String pathToSolidityFolder; +public class TemplateBuilder> { + protected String mainJavaClass; + protected String gradleBuild; + protected String gradleSettings; + protected String gradlewWrapperSettings; + protected String gradlewBatScript; + protected String gradlewScript; + protected String solidityProject; + protected String gradlewWrapperJar; + protected String packageNameReplacement; + protected String projectNameReplacement; + protected String passwordFileName; + protected String walletNameReplacement; + protected String pathToSolidityFolder; public TemplateBuilder withMainJavaClass(String mainJavaClass) { this.mainJavaClass = mainJavaClass; @@ -92,7 +92,7 @@ public TemplateBuilder withWalletNameReplacement(String walletNameReplacement) { return this; } - public TemplateProvider build() { + public TemplateProvider build() { return new TemplateProvider( mainJavaClass, solidityProject, diff --git a/src/main/java/org/web3j/console/project/templates/TemplateProvider.java b/src/main/java/org/web3j/console/project/templates/TemplateProvider.java index 792cc03..8399732 100644 --- a/src/main/java/org/web3j/console/project/templates/TemplateProvider.java +++ b/src/main/java/org/web3j/console/project/templates/TemplateProvider.java @@ -21,8 +21,8 @@ public class TemplateProvider { private final String mainJavaClass; - private final String solidityContract; - private final String pathToSolidityFolder; + protected final String solidityContract; + protected final String pathToSolidityFolder; private final String gradleBuild; private final String gradleSettings; private final String gradlewWrapperSettings; @@ -34,7 +34,7 @@ public class TemplateProvider { private final String passwordFileName; private final String walletNameReplacement; - TemplateProvider( + protected TemplateProvider( final String mainJavaClass, final String solidityContract, final String pathToSolidityFolder, diff --git a/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateBuilder.java b/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateBuilder.java new file mode 100644 index 0000000..9659575 --- /dev/null +++ b/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateBuilder.java @@ -0,0 +1,35 @@ +/* + * Copyright 2020 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.templates.kotlin; + +import org.web3j.console.project.templates.TemplateBuilder; + +public class KotlinTemplateBuilder extends TemplateBuilder { + + public KotlinTemplateProvider build() { + return new KotlinTemplateProvider( + mainJavaClass, + solidityProject, + pathToSolidityFolder, + gradleBuild, + gradleSettings, + gradlewWrapperSettings, + gradlewBatScript, + gradlewScript, + gradlewWrapperJar, + packageNameReplacement, + projectNameReplacement, + passwordFileName, + walletNameReplacement); + } +} diff --git a/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateProvider.java b/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateProvider.java new file mode 100644 index 0000000..f68189d --- /dev/null +++ b/src/main/java/org/web3j/console/project/templates/kotlin/KotlinTemplateProvider.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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.templates.kotlin; + +import java.io.File; +import java.io.IOException; + +import org.web3j.console.project.ProjectStructure; +import org.web3j.console.project.ProjectWriter; +import org.web3j.console.project.templates.TemplateProvider; +import org.web3j.console.project.utils.InputVerifier; + +public class KotlinTemplateProvider extends TemplateProvider { + + KotlinTemplateProvider( + String mainJavaClass, + String solidityContract, + String pathToSolidityFolder, + String gradleBuild, + String gradleSettings, + String gradlewWrapperSettings, + String gradlewBatScript, + String gradlewScript, + String gradlewJar, + String packageNameReplacement, + String projectNameReplacement, + String passwordFileName, + String walletNameReplacement) { + super( + mainJavaClass, + solidityContract, + pathToSolidityFolder, + gradleBuild, + gradleSettings, + gradlewWrapperSettings, + gradlewBatScript, + gradlewScript, + gradlewJar, + packageNameReplacement, + projectNameReplacement, + passwordFileName, + walletNameReplacement); + } + + public void generateFiles(ProjectStructure projectStructure) throws IOException { + ProjectWriter.writeResourceFile( + loadMainJavaClass(), + InputVerifier.capitalizeFirstLetter(projectStructure.getProjectName() + ".kt"), + projectStructure.getMainPath()); + ProjectWriter.writeResourceFile( + loadGradleBuild(), "build.gradle", projectStructure.getProjectRoot()); + ProjectWriter.writeResourceFile( + loadGradleSettings(), "settings.gradle", projectStructure.getProjectRoot()); + if (solidityContract != null) + ProjectWriter.writeResourceFile( + loadSolidityContract(), "HelloWorld.sol", projectStructure.getSolidityPath()); + if (pathToSolidityFolder != null) { + ProjectWriter.importSolidityProject( + new File(pathToSolidityFolder), projectStructure.getSolidityPath()); + } + ProjectWriter.writeResourceFile( + loadGradlewWrapperSettings(), + "gradle-wrapper.properties", + projectStructure.getWrapperPath()); + ProjectWriter.writeResourceFile( + loadGradlewScript(), "gradlew", projectStructure.getProjectRoot()); + ProjectWriter.writeResourceFile( + loadGradlewBatScript(), "gradlew.bat", projectStructure.getProjectRoot()); + ProjectWriter.copyResourceFile( + getGradlewJar(), + projectStructure.getWrapperPath() + File.separator + "gradle-wrapper.jar"); + } +} diff --git a/src/main/resources/Template-Kotlin b/src/main/resources/Template-Kotlin new file mode 100644 index 0000000..efa78f9 --- /dev/null +++ b/src/main/resources/Template-Kotlin @@ -0,0 +1,99 @@ +package ; + +import .generated.contracts.HelloWorld; +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.web3j.crypto.CipherException +import org.web3j.crypto.Credentials +import org.web3j.crypto.WalletUtils +import org.web3j.protocol.Web3j +import org.web3j.protocol.http.HttpService +import org.web3j.tx.gas.ContractGasProvider +import org.web3j.tx.gas.DefaultGasProvider +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.nio.file.Paths + +class { + + private val log: Logger = LoggerFactory.getLogger( ::class.java) + private val NODE_URL = "NODE_URL" + + + fun main(args: Array) { + try { + val credentials: Credentials? = + loadCredentials("") + val web3j: Web3j? = createWeb3jService("") + val helloWorld: HelloWorld? = deployHelloWorld(web3j, credentials, DefaultGasProvider()) + callGreetMethod(helloWorld) + } catch (e: Exception) { + log.info(e.message) + } + + } + + @Throws(IOException::class, CipherException::class) + private fun loadCredentials(walletName: String): Credentials? { + val pathToProjectResources = java.lang.String.join( + File.separator, + System.getProperty("user.dir"), + "src", + "test", + "resources", + "wallet" + ) + val pathToWallet = + java.lang.String.join(File.separator, pathToProjectResources, walletName) + val pathToWalletPasswordFile = java.lang.String.join( + File.separator, + pathToProjectResources, + "" + ) + val file = File(pathToWalletPasswordFile) + log.info("Reading wallet password from resources.") + val password = String(Files.readAllBytes(Paths.get(file.toURI()))) + log.info("Loading wallet file: $walletName from resources.") + log.info("Creating credentials from wallet.") + return WalletUtils.loadCredentials(password, File(pathToWallet)) + } + + private fun createWeb3jService(url: String?): Web3j? { + val nodeURLProperty = System.getProperty(NODE_URL) + val nodeURLEnv = System.getenv(NODE_URL) + if (url == null || url.isEmpty()) { + if (nodeURLProperty == null || nodeURLProperty.isEmpty()) { + if (nodeURLEnv == null || nodeURLEnv.isEmpty()) { + log.info("Please make sure the node url is valid.") + log.info("You can edit the node url programmatically, use java -D$NODE_URL=\"\" or as an environmental variable e.g export $NODE_URL=\"\"") + System.exit(1) + } else { + log.info("Connecting to $nodeURLEnv") + return Web3j.build(HttpService(nodeURLEnv)) + } + } else { + log.info("Connecting to $nodeURLProperty") + return Web3j.build(HttpService(nodeURLProperty)) + } + } + log.info("Connecting to $url") + return Web3j.build(HttpService(url)) + } + + @Throws(Exception::class) + private fun deployHelloWorld( + web3j: Web3j?, + credentials: Credentials?, + contractGasProvider: ContractGasProvider? + ): HelloWorld? { + return HelloWorld.deploy(web3j, credentials, contractGasProvider, "Hello Blockchain World!").send() + } + + @Throws(Exception::class) + private fun callGreetMethod(helloWorld: HelloWorld?) { + log.info("Calling the greeting method of contract HelloWorld") + val response: String = helloWorld?.greeting()!!.send() + log.info("Contract returned: $response") + } +} \ No newline at end of file diff --git a/src/main/resources/build.gradle.template b/src/main/resources/build.gradle.template index 2a2d691..bdb3515 100644 --- a/src/main/resources/build.gradle.template +++ b/src/main/resources/build.gradle.template @@ -1,5 +1,6 @@ plugins { id 'java' + id 'org.jetbrains.kotlin.jvm' version '1.3.61' id 'application' id 'org.web3j' version '4.5.11' id "com.github.johnrengelman.shadow" version "5.1.0" @@ -34,6 +35,8 @@ dependencies { implementation("org.web3j:web3j-evm:$web3jVersion") testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + } jar { @@ -52,3 +55,19 @@ application { test { useJUnitPlatform() } + +compileKotlin { + kotlinOptions.jvmTarget = "1.8" + dependsOn 'generateContractWrappers' +} + +compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" + dependsOn 'generateTestContractWrappers' +} + +sourceSets { + main.kotlin.srcDirs += "${web3j.generatedFilesBaseDir}/main/java" + test.kotlin.srcDirs += "${web3j.generatedFilesBaseDir}/test/java" +} + diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectCreatorCLIRunnerTest.java b/src/test/java/org/web3j/console/project/BaseProjectCreatorCLIRunnerTest.java similarity index 98% rename from src/test/java/org/web3j/console/project/java/BaseProjectCreatorCLIRunnerTest.java rename to src/test/java/org/web3j/console/project/BaseProjectCreatorCLIRunnerTest.java index 8e6308a..ffac391 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectCreatorCLIRunnerTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectCreatorCLIRunnerTest.java @@ -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.java; +package org.web3j.console.project; import java.nio.file.Path; diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectCreatorTest.java b/src/test/java/org/web3j/console/project/BaseProjectCreatorTest.java similarity index 99% rename from src/test/java/org/web3j/console/project/java/BaseProjectCreatorTest.java rename to src/test/java/org/web3j/console/project/BaseProjectCreatorTest.java index 63511e8..365820d 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectCreatorTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectCreatorTest.java @@ -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.java; +package org.web3j.console.project; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectImporterCLIRunnerTest.java b/src/test/java/org/web3j/console/project/BaseProjectImporterCLIRunnerTest.java similarity index 98% rename from src/test/java/org/web3j/console/project/java/BaseProjectImporterCLIRunnerTest.java rename to src/test/java/org/web3j/console/project/BaseProjectImporterCLIRunnerTest.java index 2b2fa53..0836932 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectImporterCLIRunnerTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectImporterCLIRunnerTest.java @@ -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.java; +package org.web3j.console.project; import java.nio.file.Path; diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectImporterTest.java b/src/test/java/org/web3j/console/project/BaseProjectImporterTest.java similarity index 98% rename from src/test/java/org/web3j/console/project/java/BaseProjectImporterTest.java rename to src/test/java/org/web3j/console/project/BaseProjectImporterTest.java index a5e5d85..25b2df7 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectImporterTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectImporterTest.java @@ -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.java; +package org.web3j.console.project; import java.io.ByteArrayOutputStream; import java.io.File; diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectTest.java b/src/test/java/org/web3j/console/project/BaseProjectTest.java similarity index 94% rename from src/test/java/org/web3j/console/project/java/BaseProjectTest.java rename to src/test/java/org/web3j/console/project/BaseProjectTest.java index aaf282d..3e29594 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectTest.java @@ -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.java; +package org.web3j.console.project; import java.io.File; import java.nio.file.Path; @@ -19,9 +19,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.web3j.console.project.BaseBuilder; -import org.web3j.console.project.BaseProject; -import org.web3j.console.project.ProjectStructure; +import org.web3j.console.project.java.JavaProjectStructure; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -45,7 +43,7 @@ public void setUpProject(@TempDir Path tempDirPath) throws Exception { public void directoryCreationTest() { final boolean mainProjectDir = new File(projectStructure.getMainPath()).exists(); final boolean gradleWrapperDir = new File(projectStructure.getWrapperPath()).exists(); - final boolean testProjectDir = new File(projectStructure.getTestPath()).exists(); + final boolean testProjectDir = new File(projectStructure.getPathToTestDirectory()).exists(); final boolean solidityPath = new File(projectStructure.getSolidityPath()).exists(); assertTrue(mainProjectDir && gradleWrapperDir && testProjectDir && solidityPath); diff --git a/src/test/java/org/web3j/console/project/java/BaseProjectWriterTest.java b/src/test/java/org/web3j/console/project/BaseProjectWriterTest.java similarity index 97% rename from src/test/java/org/web3j/console/project/java/BaseProjectWriterTest.java rename to src/test/java/org/web3j/console/project/BaseProjectWriterTest.java index 32708c9..40659c4 100644 --- a/src/test/java/org/web3j/console/project/java/BaseProjectWriterTest.java +++ b/src/test/java/org/web3j/console/project/BaseProjectWriterTest.java @@ -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.java; +package org.web3j.console.project; import java.io.File; import java.io.IOException; @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.web3j.console.project.ProjectWriter; import org.web3j.console.project.templates.TemplateBuilder; import org.web3j.console.project.templates.TemplateProvider; diff --git a/src/test/java/org/web3j/console/project/java/UnitTestCreatorTest.java b/src/test/java/org/web3j/console/project/java/JavaTestCreatorTest.java similarity index 88% rename from src/test/java/org/web3j/console/project/java/UnitTestCreatorTest.java rename to src/test/java/org/web3j/console/project/java/JavaTestCreatorTest.java index ad8edfd..28188de 100644 --- a/src/test/java/org/web3j/console/project/java/UnitTestCreatorTest.java +++ b/src/test/java/org/web3j/console/project/java/JavaTestCreatorTest.java @@ -24,11 +24,12 @@ import org.junit.jupiter.api.io.TempDir; import picocli.CommandLine; +import org.web3j.console.project.ProjectImporter; import org.web3j.console.project.utils.ClassExecutor; import static java.io.File.separator; -public class UnitTestCreatorTest extends ClassExecutor { +public class JavaTestCreatorTest extends ClassExecutor { private String formattedPath = new File(String.join(separator, "src", "test", "resources", "Solidity")) .getAbsolutePath(); @@ -42,10 +43,10 @@ void setup(@TempDir Path temp) { @Test public void testThatCorrectArgumentsArePassed() { final String[] args = {"-i=" + formattedPath, "-o=" + tempDirPath}; - final UnitTestCLIRunner unitTestCLIRunner = new UnitTestCLIRunner(); - new CommandLine(unitTestCLIRunner).parseArgs(args); - assert unitTestCLIRunner.unitTestOutputDir.equals(tempDirPath); - assert unitTestCLIRunner.javaWrapperDir.equals(formattedPath); + final JavaTestCLIRunner javaTestCLIRunner = new JavaTestCLIRunner(); + new CommandLine(javaTestCLIRunner).parseArgs(args); + assert javaTestCLIRunner.unitTestOutputDir.equals(tempDirPath); + assert javaTestCLIRunner.javaWrapperDir.equals(formattedPath); } @Test @@ -76,7 +77,7 @@ public void verifyThatTestsAreGenerated() throws IOException, InterruptedExcepti final String[] unitTestsArgs = {"-i=" + pathToJavaWrappers, "-o=" + tempDirPath}; int testsExitCode = executeClassAsSubProcessAndReturnProcess( - UnitTestCreator.class, + JavaTestCreator.class, Collections.emptyList(), Arrays.asList(unitTestsArgs)) .inheritIO()