Skip to content

Commit 337faee

Browse files
author
Xavier
committed
Implement includeContracts extension
1 parent 8500e4b commit 337faee

File tree

6 files changed

+105
-13
lines changed

6 files changed

+105
-13
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ buildscript {
2525
mavenCentral()
2626
}
2727
dependencies {
28-
classpath 'org.web3j:web3j-gradle-plugin:4.1.2'
28+
classpath 'org.web3j:web3j-gradle-plugin:4.1.3'
2929
}
3030
}
3131
@@ -39,7 +39,7 @@ build file:
3939

4040
```groovy
4141
plugins {
42-
id 'org.web3j' version '4.1.2'
42+
id 'org.web3j' version '4.1.3'
4343
}
4444
```
4545

@@ -85,6 +85,7 @@ The properties accepted by the DSL are listed in the following table:
8585
| `generatedPackageName` | `String` | `${group}.web3j` or `org.web3j.{0}` | Generated contract wrappers package. |
8686
| `generatedFilesBaseDir` | `String` | `$buildDir/generated/source/web3j` | Generated Java code output directory. |
8787
| `excludedContracts` | `String[]` | `[]` | Excluded contract names from wrapper generation. |
88+
| `includedContracts` | `String[]` | `[]` | Included contract names from wrapper generation. Has preference over `excludedContracts`. |
8889
| `useNativeJavaTypes` | `Boolean` | `true` | Generate smart contract wrappers using native Java types. |
8990

9091
The `generatedPackageName` is evaluated as a [message format](https://docs.oracle.com/javase/6/docs/api/index.html?java/text/MessageFormat.html)

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=org.web3j
2-
version=4.1.2
2+
version=4.1.3

src/main/java/org/web3j/gradle/plugin/GenerateContractWrappers.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class GenerateContractWrappers extends SourceTask {
3030
@Optional
3131
private List<String> excludedContracts;
3232

33+
@Input
34+
@Optional
35+
private List<String> includedContracts;
36+
3337
@Inject
3438
public GenerateContractWrappers(final WorkerExecutor executor) {
3539
this.executor = executor;
@@ -46,7 +50,7 @@ void generateContractWrappers() {
4650
final String contractName = contractAbi.getName()
4751
.replaceAll("\\.abi", "");
4852

49-
if (excludedContracts == null || !excludedContracts.contains(contractName)) {
53+
if (shouldGenerateContract(contractName)) {
5054
final String packageName = MessageFormat.format(
5155
getGeneratedJavaPackageName(), contractName.toLowerCase());
5256

@@ -62,6 +66,14 @@ void generateContractWrappers() {
6266
}
6367
}
6468

69+
private boolean shouldGenerateContract(final String contractName) {
70+
if (includedContracts == null || includedContracts.isEmpty()) {
71+
return excludedContracts == null || !excludedContracts.contains(contractName);
72+
} else {
73+
return includedContracts.contains(contractName);
74+
}
75+
}
76+
6577
// Getters and setters
6678
public String getGeneratedJavaPackageName() {
6779
return generatedJavaPackageName;
@@ -87,5 +99,11 @@ public void setExcludedContracts(final List<String> excludedContracts) {
8799
this.excludedContracts = excludedContracts;
88100
}
89101

102+
public List<String> getIncludedContracts() {
103+
return includedContracts;
104+
}
90105

106+
public void setIncludedContracts(final List<String> includedContracts) {
107+
this.includedContracts = includedContracts;
108+
}
91109
}

src/main/java/org/web3j/gradle/plugin/Web3jExtension.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public class Web3jExtension {
4141
*/
4242
private List<String> excludedContracts;
4343

44+
/**
45+
* Included contract names from wrapper generation.
46+
*/
47+
private List<String> includedContracts;
48+
4449
public String getGeneratedPackageName() {
4550
return generatedPackageName;
4651
}
@@ -75,20 +80,29 @@ public void setExcludedContracts(final List<String> excludedContracts) {
7580
this.excludedContracts = excludedContracts;
7681
}
7782

83+
public List<String> getIncludedContracts() {
84+
return includedContracts;
85+
}
86+
87+
public void setIncludedContracts(final List<String> includedContracts) {
88+
this.includedContracts = includedContracts;
89+
}
90+
7891
public Web3jExtension(final Project project) {
7992
generatedFilesBaseDir = project.getBuildDir().getAbsolutePath()
80-
+ "/generated/source/web3j";
93+
+ "/generated/source/" + NAME;
8194

8295
// Use the project's group name in generated package
8396
final String projectGroup = project.getGroup().toString();
8497
if (!projectGroup.isEmpty()) {
85-
generatedPackageName = projectGroup + ".web3j";
98+
generatedPackageName = projectGroup + "." + NAME;
8699
} else {
87100
generatedPackageName = DEFAULT_GENERATED_PACKAGE;
88101
}
89102

90103
useNativeJavaTypes = true;
91104
excludedContracts = new ArrayList<>();
105+
includedContracts = new ArrayList<>();
92106
}
93107

94108
}

src/main/java/org/web3j/gradle/plugin/Web3jPlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private void configure(final Project project, final SourceSet sourceSet) {
8080

8181
// Set task excluded contracts
8282
task.setExcludedContracts(extension.getExcludedContracts());
83+
task.setIncludedContracts(extension.getIncludedContracts());
8384

8485
task.dependsOn(project.getTasks().withType(SolidityCompile.class)
8586
.named("compile" + srcSetName + "Solidity"));

src/test/java/org/web3j/gradle/plugin/Web3jPluginTest.java

+65-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package org.web3j.gradle.plugin;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.nio.file.Files;
7+
38
import org.gradle.testkit.runner.BuildResult;
49
import org.gradle.testkit.runner.GradleRunner;
510
import org.junit.Before;
611
import org.junit.Rule;
712
import org.junit.Test;
813
import org.junit.rules.TemporaryFolder;
914

10-
import java.io.File;
11-
import java.io.IOException;
12-
import java.net.URL;
13-
import java.nio.file.Files;
14-
1515
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
1616
import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE;
17-
import static org.junit.Assert.*;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
1821

1922
public class Web3jPluginTest {
2023

@@ -35,7 +38,7 @@ public void setup() throws IOException {
3538
}
3639

3740
@Test
38-
public void generateContractWrappers() throws IOException {
41+
public void generateContractWrappersExcluding() throws IOException {
3942
final String buildFileContent = "plugins {\n" +
4043
" id 'org.web3j'\n" +
4144
"}\n" +
@@ -89,4 +92,59 @@ public void generateContractWrappers() throws IOException {
8992
assertEquals(UP_TO_DATE, upToDate.task(":generateContractWrappers").getOutcome());
9093
}
9194

95+
@Test
96+
public void generateContractWrappersIncluding() throws IOException {
97+
final String buildFileContent = "plugins {\n" +
98+
" id 'org.web3j'\n" +
99+
"}\n" +
100+
"web3j {\n" +
101+
" generatedPackageName = 'org.web3j.test'\n" +
102+
" includedContracts = ['StandardToken']\n" +
103+
"}\n" +
104+
"sourceSets {\n" +
105+
" main {\n" +
106+
" solidity {\n" +
107+
" srcDir {" +
108+
" '" + sourceDir.getAbsolutePath() + "'\n" +
109+
" }\n" +
110+
" }\n" +
111+
" }\n" +
112+
"}\n" +
113+
"repositories {\n" +
114+
" mavenCentral()\n" +
115+
"}\n" +
116+
"dependencies {\n" +
117+
" implementation \"org.web3j:core:4.1.1\"\n" +
118+
"}\n";
119+
120+
Files.write(buildFile.toPath(), buildFileContent.getBytes());
121+
122+
final GradleRunner gradleRunner = GradleRunner.create()
123+
.withProjectDir(testProjectDir.getRoot())
124+
.withArguments("build")
125+
.withPluginClasspath()
126+
.forwardOutput();
127+
128+
final BuildResult success = gradleRunner.build();
129+
assertNotNull(success.task(":generateContractWrappers"));
130+
assertEquals(SUCCESS, success.task(":generateContractWrappers").getOutcome());
131+
132+
final File web3jContractsDir = new File(testProjectDir.getRoot(),
133+
"build/generated/source/web3j/main/java");
134+
135+
final File generatedContract = new File(web3jContractsDir,
136+
"org/web3j/test/StandardToken.java");
137+
138+
assertTrue(generatedContract.exists());
139+
140+
final File excludedContract = new File(web3jContractsDir,
141+
"org/web3j/test/Token.java");
142+
143+
assertFalse(excludedContract.exists());
144+
145+
final BuildResult upToDate = gradleRunner.build();
146+
assertNotNull(upToDate.task(":generateContractWrappers"));
147+
assertEquals(UP_TO_DATE, upToDate.task(":generateContractWrappers").getOutcome());
148+
}
149+
92150
}

0 commit comments

Comments
 (0)