Skip to content

Commit

Permalink
Added basic implementation for kotlin project generation.
Browse files Browse the repository at this point in the history
Further refactoring.
  • Loading branch information
AlexandrouR committed Jan 24, 2020
1 parent 8159ad4 commit bbb1421
Show file tree
Hide file tree
Showing 28 changed files with 784 additions and 119 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/web3j/console/project/BaseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T extends BaseBuilder> BaseBuilder withSolidityFile(final String solidityImportPath) {
this.solidityImportPath = solidityImportPath;
return this;
}

public BaseBuilder withWalletProvider(boolean withWalletProvider) {
public <T extends BaseBuilder> BaseBuilder withWalletProvider(boolean withWalletProvider) {
this.withWallet = withWalletProvider;
return this;
}

public BaseBuilder withSampleCode(boolean withSampleCode) {
public <T extends BaseBuilder> BaseBuilder withSampleCode(boolean withSampleCode) {
this.withSampleCode = withSampleCode;
return this;
}

public BaseBuilder withTests(boolean withTests) {
public <T extends BaseBuilder> BaseBuilder withTests(boolean withTests) {
this.withTests = withTests;
return this;
}

public BaseBuilder withFatJar(boolean withFatJar) {
public <T extends BaseBuilder> BaseBuilder withFatJar(boolean withFatJar) {
this.withFatJar = withFatJar;
return this;
}

public BaseBuilder withCommand(String command) {
public <T extends BaseBuilder> BaseBuilder withCommand(String command) {
this.command = command;
return this;
}

public BaseBuilder withProjectName(String projectName) {
public <T extends BaseBuilder> BaseBuilder withProjectName(String projectName) {
this.projectName = projectName;
return this;
}

public BaseBuilder withPackageName(String packageName) {
public <T extends BaseBuilder> BaseBuilder withPackageName(String packageName) {
this.packageName = packageName;
return this;
}

public BaseBuilder withRootDirectory(String rootDirectory) {
public <T extends BaseBuilder> BaseBuilder withRootDirectory(String rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

public BaseProject build() throws Exception {
public <T extends BaseProject> BaseProject build() throws Exception {
final ProjectStructure projectStructure =
new JavaProjectStructure(rootDirectory, packageName, projectName);
return new BaseProject(
Expand Down
60 changes: 22 additions & 38 deletions src/main/java/org/web3j/console/project/BaseProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand Down Expand Up @@ -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),
Expand All @@ -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();
Expand All @@ -162,7 +146,7 @@ private void generateWallet()
projectStructure.getWalletPath());
}

private TemplateProvider getTemplateProvider() {
public TemplateProvider getTemplateProvider() {
TemplateBuilder templateBuilder =
new TemplateBuilder()
.withProjectNameReplacement(projectStructure.projectName)
Expand Down
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.java;
package org.web3j.console.project;

import java.io.File;
import java.io.PrintWriter;
Expand All @@ -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;
Expand Down
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.java;
package org.web3j.console.project;

import java.io.File;
import java.util.Optional;
Expand All @@ -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;

Expand Down
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.java;
package org.web3j.console.project;

import java.util.ArrayList;
import java.util.List;
Expand Down
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.java;
package org.web3j.console.project;

import java.io.File;
import java.util.Optional;
Expand All @@ -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;

Expand Down
29 changes: 25 additions & 4 deletions src/main/java/org/web3j/console/project/ProjectStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -83,7 +96,7 @@ public void createMainDirectory() {
}

public void createTestDirectory() {
createDirectory(testPath);
createDirectory(pathToTestDirectory);
}

public void createSolidityDirectory() {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit bbb1421

Please sign in to comment.