Skip to content

Commit 66ee676

Browse files
authored
Merge pull request #32 from AlexandrouR/kotlinProject
Builder patter refactoring.
2 parents 26e2eec + b701769 commit 66ee676

File tree

58 files changed

+2752
-789
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2752
-789
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ gradle-app.setting
2525
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
2626
!gradle-wrapper.jar
2727

28-
# Cache of project
28+
# Cache of javaProject
2929
.gradletasknamecache
3030

3131
# Work around https://youtrack.jetbrains.com/issue/IDEA-116898

src/main/java/org/web3j/console/Runner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public static void main(String[] args) throws Exception {
6969
TruffleJsonFunctionWrapperGenerator.run(tail(args));
7070
break;
7171
case COMMAND_NEW:
72-
ProjectCreator.main(args);
72+
ProjectCreator.main(tail(args));
7373
break;
7474
case COMMAND_IMPORT:
75-
ProjectImporter.main(args);
75+
ProjectImporter.main(tail(args));
7676
break;
7777
case "version":
7878
Console.exitSuccess(
@@ -86,7 +86,7 @@ public static void main(String[] args) throws Exception {
8686
ContractAuditor.main(tail(args));
8787
break;
8888
case COMMAND_GENERATE_TESTS:
89-
UnitTestCreator.main(args);
89+
UnitTestCreator.main(tail(args));
9090
break;
9191
default:
9292
Console.exitError(USAGE);
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2020 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.console.project;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.security.InvalidAlgorithmParameterException;
18+
import java.security.NoSuchAlgorithmException;
19+
import java.security.NoSuchProviderException;
20+
21+
import org.web3j.codegen.Console;
22+
import org.web3j.console.project.templates.TemplateProvider;
23+
import org.web3j.console.project.utils.ProgressCounter;
24+
import org.web3j.console.project.utils.ProjectUtils;
25+
import org.web3j.crypto.CipherException;
26+
27+
public abstract class AbstractProject<T extends AbstractProject<T>> {
28+
private T project;
29+
30+
protected final boolean withTests;
31+
protected final boolean withFatJar;
32+
protected final boolean withWallet;
33+
protected final boolean withSampleCode;
34+
protected final String command;
35+
protected final String solidityImportPath;
36+
protected final ProjectStructure projectStructure;
37+
protected ProjectWallet projectWallet;
38+
protected ProgressCounter progressCounter = new ProgressCounter(true);
39+
40+
protected abstract T getProjectInstance();
41+
42+
protected AbstractProject(
43+
boolean withTests,
44+
boolean withFatJar,
45+
boolean withWallet,
46+
boolean withSampleCode,
47+
String command,
48+
String solidityImportPath,
49+
ProjectStructure projectStructure) {
50+
this.withTests = withTests;
51+
this.withFatJar = withFatJar;
52+
this.withWallet = withWallet;
53+
this.withSampleCode = withSampleCode;
54+
this.command = command;
55+
this.solidityImportPath = solidityImportPath;
56+
this.projectStructure = projectStructure;
57+
this.project = getProjectInstance();
58+
}
59+
60+
public ProjectStructure getProjectStructure() {
61+
return project.projectStructure;
62+
}
63+
64+
public ProjectWallet getProjectWallet() {
65+
return project.projectWallet;
66+
}
67+
68+
protected void buildGradleProject(final String pathToDirectory)
69+
throws IOException, InterruptedException {
70+
if (!isWindows()) {
71+
setExecutable(pathToDirectory, "gradlew");
72+
executeBuild(
73+
new File(pathToDirectory), new String[] {"bash", "-c", "./gradlew build -q"});
74+
} else {
75+
setExecutable(pathToDirectory, "gradlew.bat");
76+
executeBuild(
77+
new File(pathToDirectory),
78+
new String[] {"cmd.exe", "/c", "gradlew.bat build -q"});
79+
}
80+
}
81+
82+
private boolean isWindows() {
83+
return System.getProperty("os.name").toLowerCase().startsWith("windows");
84+
}
85+
86+
private void setExecutable(final String pathToDirectory, final String gradlew) {
87+
final File f = new File(pathToDirectory + File.separator + gradlew);
88+
final boolean isExecutable = f.setExecutable(true);
89+
}
90+
91+
private void executeBuild(final File workingDir, final String[] command)
92+
throws InterruptedException, IOException {
93+
executeProcess(workingDir, command);
94+
}
95+
96+
private void executeProcess(File workingDir, String[] command)
97+
throws InterruptedException, IOException {
98+
int exitCode =
99+
new ProcessBuilder(command)
100+
.directory(workingDir)
101+
.redirectErrorStream(true)
102+
.redirectOutput(File.createTempFile("w3j", "cli"))
103+
.start()
104+
.waitFor();
105+
if (exitCode != 0) {
106+
Console.exitError("Could not build project.");
107+
}
108+
}
109+
110+
protected void createFatJar(String pathToDirectory) throws IOException, InterruptedException {
111+
if (!isWindows()) {
112+
executeProcess(
113+
new File(pathToDirectory),
114+
new String[] {"bash", "./gradlew", "shadowJar", "-q"});
115+
} else {
116+
executeProcess(
117+
new File(pathToDirectory),
118+
new String[] {"cmd.exe", "/c", "./gradlew.bat shadowJar", "-q"});
119+
}
120+
}
121+
122+
protected void generateWallet()
123+
throws CipherException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
124+
NoSuchProviderException, IOException {
125+
projectStructure.createWalletDirectory();
126+
projectWallet =
127+
new ProjectWallet(
128+
ProjectUtils.generateWalletPassword(), projectStructure.getWalletPath());
129+
ProjectWriter.writeResourceFile(
130+
projectWallet.getWalletPassword(),
131+
projectWallet.getPasswordFileName(),
132+
projectStructure.getWalletPath());
133+
}
134+
135+
protected void generateTopLevelDirectories(ProjectStructure projectStructure) {
136+
projectStructure.createMainDirectory();
137+
projectStructure.createTestDirectory();
138+
projectStructure.createSolidityDirectory();
139+
projectStructure.createWrapperDirectory();
140+
}
141+
142+
public void createProject()
143+
throws IOException, InterruptedException, NoSuchAlgorithmException,
144+
NoSuchProviderException, InvalidAlgorithmParameterException, CipherException {
145+
generateTopLevelDirectories(projectStructure);
146+
if (withWallet) {
147+
generateWallet();
148+
}
149+
getTemplateProvider().generateFiles(projectStructure);
150+
progressCounter.processing("Creating " + projectStructure.projectName);
151+
buildGradleProject(projectStructure.getProjectRoot());
152+
153+
if (withTests) {
154+
generateTests(projectStructure);
155+
}
156+
if (withFatJar) {
157+
createFatJar(projectStructure.getProjectRoot());
158+
}
159+
progressCounter.setLoading(false);
160+
}
161+
162+
protected abstract TemplateProvider getTemplateProvider();
163+
164+
protected abstract void generateTests(ProjectStructure projectStructure) throws IOException;
165+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2020 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.console.project;
14+
15+
public abstract class AbstractProjectBuilder<T extends AbstractProjectBuilder<T>> {
16+
private T builder;
17+
18+
protected String solidityImportPath;
19+
protected boolean withWallet;
20+
protected boolean withTests;
21+
protected String projectName;
22+
protected String packageName;
23+
protected String rootDirectory;
24+
protected boolean withSampleCode;
25+
protected boolean withFatJar;
26+
27+
protected abstract T getBuilderInstance();
28+
29+
protected AbstractProjectBuilder() {
30+
this.builder = getBuilderInstance();
31+
}
32+
33+
public T withSolidityFile(final String solidityImportPath) {
34+
builder.solidityImportPath = solidityImportPath;
35+
return builder;
36+
}
37+
38+
public T withWalletProvider(boolean withWalletProvider) {
39+
builder.withWallet = withWalletProvider;
40+
return this.builder;
41+
}
42+
43+
public T withSampleCode(boolean withSampleCode) {
44+
builder.withSampleCode = withSampleCode;
45+
return this.builder;
46+
}
47+
48+
public T withTests(boolean withTests) {
49+
builder.withTests = withTests;
50+
return this.builder;
51+
}
52+
53+
public T withFatJar(boolean withFatJar) {
54+
builder.withFatJar = withFatJar;
55+
return this.builder;
56+
}
57+
58+
public T withProjectName(String projectName) {
59+
builder.projectName = projectName;
60+
return this.builder;
61+
}
62+
63+
public T withPackageName(String packageName) {
64+
builder.packageName = packageName;
65+
return this.builder;
66+
}
67+
68+
public T withRootDirectory(String rootDirectory) {
69+
builder.rootDirectory = rootDirectory;
70+
return this.builder;
71+
}
72+
}

src/main/java/org/web3j/console/project/InteractiveOptions.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import static org.web3j.codegen.Console.exitError;
2424
import static org.web3j.console.project.utils.ProjectUtils.deleteFolder;
2525

26-
class InteractiveOptions {
26+
public class InteractiveOptions {
2727
static Scanner scanner = new Scanner(System.in);
2828

29-
static String getProjectName() {
29+
public static String getProjectName() {
3030
print("Please enter the project name [Web3App]:");
3131
String projectName = getUserInput();
3232
if (projectName.trim().isEmpty()) {
@@ -38,7 +38,7 @@ static String getProjectName() {
3838
return projectName;
3939
}
4040

41-
static String getPackageName() {
41+
public static String getPackageName() {
4242
print("Please enter the package name for your project [io.web3j]:");
4343
String packageName = getUserInput();
4444
if (packageName.trim().isEmpty()) {
@@ -50,7 +50,7 @@ static String getPackageName() {
5050
return packageName;
5151
}
5252

53-
static Optional<String> getProjectDestination(final String projectName) {
53+
public static Optional<String> getProjectDestination(final String projectName) {
5454
print(
5555
"Please enter the destination of your project ["
5656
+ System.getProperty("user.dir")
@@ -69,7 +69,7 @@ static Optional<String> getProjectDestination(final String projectName) {
6969
return projectDest.isEmpty() ? Optional.empty() : Optional.of(projectDest);
7070
}
7171

72-
static Optional<String> getGeneratedWrapperLocation() {
72+
public static Optional<String> getGeneratedWrapperLocation() {
7373
print(
7474
"Please enter the path of the generated contract wrappers ["
7575
+ String.join(
@@ -97,7 +97,7 @@ static Optional<String> getGeneratedWrapperLocation() {
9797
: Optional.of(pathToTheWrappers);
9898
}
9999

100-
public static Optional<String> setGeneratedTestLocation() {
100+
public static Optional<String> setGeneratedTestLocationJava() {
101101
print(
102102
"Where would you like to save your tests ["
103103
+ String.join(
@@ -111,13 +111,27 @@ public static Optional<String> setGeneratedTestLocation() {
111111
: Optional.of(outputPath);
112112
}
113113

114+
public static Optional<String> setGeneratedTestLocationKotlin() {
115+
print(
116+
"Where would you like to save your tests ["
117+
+ String.join(
118+
separator, System.getProperty("user.dir"), "src", "test", "kotlin")
119+
+ "]");
120+
String outputPath = getUserInput();
121+
return outputPath.isEmpty()
122+
? Optional.of(
123+
String.join(
124+
separator, System.getProperty("user.dir"), "src", "test", "kotlin"))
125+
: Optional.of(outputPath);
126+
}
127+
114128
public static boolean userWantsTests() {
115129
print("Would you like to generate unit test for your solidity contracts [Y/n] ? ");
116130
String userAnswer = getUserInput();
117131
return userAnswer.trim().toLowerCase().equals("y") || userAnswer.trim().equals("");
118132
}
119133

120-
static String getSolidityProjectPath() {
134+
public static String getSolidityProjectPath() {
121135
System.out.println("Please enter the path to your solidity file/folder [Required Field]: ");
122136
return getUserInput();
123137
}

0 commit comments

Comments
 (0)