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

feat: Add generate both flag #88

Merged
merged 1 commit into from
Jul 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
package org.web3j.gradle.plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.gradle.api.provider.Property;
import org.gradle.workers.WorkAction;
Expand All @@ -28,22 +31,30 @@ public void execute() {
final String typesFlag =
getParameters().getUseNativeJavaTypes().get() ? "--javaTypes" : "--solidityTypes";

SolidityFunctionWrapperGenerator.main(
new String[] {
"--abiFile",
getParameters().getContractAbi().get().getAbsolutePath(),
"--binFile",
getParameters().getContractBin().get().getAbsolutePath(),
"--outputDir",
getParameters().getOutputDir().get(),
"--package",
getParameters().getPackageName().get(),
"--contractName",
getParameters().getContractName().get(),
"--addressLength",
String.valueOf(getParameters().getAddressLength().get()),
typesFlag
});
final String generateBoth = getParameters().getGenerateBoth().get() ? "--generateBoth" : "";

List<String> arguments =
new ArrayList<>(
Arrays.asList(
"--abiFile",
getParameters().getContractAbi().get().getAbsolutePath(),
"--binFile",
getParameters().getContractBin().get().getAbsolutePath(),
"--outputDir",
getParameters().getOutputDir().get(),
"--package",
getParameters().getPackageName().get(),
"--contractName",
getParameters().getContractName().get(),
"--addressLength",
String.valueOf(getParameters().getAddressLength().get()),
typesFlag));

if (!generateBoth.isEmpty()) {
arguments.add(generateBoth);
}

SolidityFunctionWrapperGenerator.main(arguments.toArray(new String[0]));
}

public interface Parameters extends WorkParameters {
Expand All @@ -61,5 +72,7 @@ public interface Parameters extends WorkParameters {
Property<Integer> getAddressLength();

Property<Boolean> getUseNativeJavaTypes();

Property<Boolean> getGenerateBoth();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class GenerateContractWrappers extends SourceTask {

@Input @Optional private Integer addressLength;

@Input @Optional private Boolean generateBoth;

@Inject
public GenerateContractWrappers(final WorkerExecutor executor) {
this.executor = executor;
Expand Down Expand Up @@ -72,6 +74,7 @@ void generateContractWrappers() {
params.getPackageName().set(packageName);
params.getAddressLength().set(addressLength);
params.getUseNativeJavaTypes().set(useNativeJavaTypes);
params.getGenerateBoth().set(generateBoth);
});
}
}
Expand Down Expand Up @@ -125,4 +128,12 @@ public Integer getAddressLength() {
public void setAddressLength(final Integer addressLength) {
this.addressLength = addressLength;
}

public Boolean getGenerateBoth() {
return generateBoth;
}

public void setGenerateBoth(Boolean generateBoth) {
this.generateBoth = generateBoth;
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/web3j/gradle/plugin/Web3jExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Web3jExtension {
/** Generate smart contract wrappers using native Java types. */
private Boolean useNativeJavaTypes;

private Boolean generateBoth;

/** Excluded contract names from wrapper generation. */
private List<String> excludedContracts;

Expand Down Expand Up @@ -75,6 +77,14 @@ public void setUseNativeJavaTypes(final Boolean useNativeJavaTypes) {
this.useNativeJavaTypes = useNativeJavaTypes;
}

public Boolean getGenerateBoth() {
return generateBoth;
}

public void setGenerateBoth(Boolean generateBoth) {
this.generateBoth = generateBoth;
}

public List<String> getExcludedContracts() {
return excludedContracts;
}
Expand Down Expand Up @@ -110,6 +120,7 @@ public Web3jExtension(final Project project) {
excludedContracts = new ArrayList<>();
includedContracts = new ArrayList<>();
addressBitLength = Address.DEFAULT_LENGTH / Byte.SIZE;
generateBoth = false;
}

protected String getDefaultGeneratedPackageName(Project project) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/web3j/gradle/plugin/Web3jPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private void configure(final Project project, final SourceSet sourceSet) {
task.setGeneratedJavaPackageName(
extension.getGeneratedPackageName());
task.setUseNativeJavaTypes(extension.getUseNativeJavaTypes());
task.setGenerateBoth(extension.getGenerateBoth());
task.setGroup(Web3jExtension.NAME);

// Set task excluded contracts
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/web3j/gradle/plugin/Web3jPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,63 @@ public void generateContractWrappersIncluding() throws IOException {
+ "web3j {\n"
+ " generatedPackageName = 'org.web3j.test'\n"
+ " includedContracts = ['StandardToken']\n"
+ " generateBoth = true\n"
+ "}\n"
+ "sourceSets {\n"
+ " main {\n"
+ " solidity {\n"
+ " srcDir '"
+ sourceDir.toAbsolutePath()
+ "'\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "repositories {\n"
+ " mavenCentral()\n"
+ " maven {\n"
+ " url 'https://oss.sonatype.org/content/repositories/snapshots'\n"
+ " }\n"
+ "}\n";

Files.write(buildFile, buildFileContent.getBytes());

final GradleRunner gradleRunner =
GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
.withArguments("build")
.withPluginClasspath()
.forwardOutput();

final BuildResult success = gradleRunner.build();
assertNotNull(success.task(":generateContractWrappers"));
assertEquals(SUCCESS, success.task(":generateContractWrappers").getOutcome());

final Path web3jContractsDir =
testProjectDir.resolve("build/generated/sources/web3j/main/java");
final Path generatedContract =
web3jContractsDir.resolve("org/web3j/test/StandardToken.java");
assertTrue(Files.exists(generatedContract));

final Path excludedContract = web3jContractsDir.resolve("org/web3j/test/Token.java");
assertFalse(Files.exists(excludedContract));

final BuildResult upToDate = gradleRunner.build();
assertNotNull(upToDate.task(":generateContractWrappers"));
assertEquals(UP_TO_DATE, upToDate.task(":generateContractWrappers").getOutcome());
}

@Test
public void generateContractWrappersIncludingGenerateBothFalseUseNativeJava()
throws IOException {
final String buildFileContent =
"plugins {\n"
+ " id 'org.web3j'\n"
+ "}\n"
+ "web3j {\n"
+ " generatedPackageName = 'org.web3j.test'\n"
+ " includedContracts = ['StandardToken']\n"
+ " useNativeJavaTypes = true\n"
+ " generateBoth = false\n"
+ "}\n"
+ "sourceSets {\n"
+ " main {\n"
Expand Down
Loading