├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── src
└── main
│ ├── java
│ └── com
│ │ └── webank
│ │ └── scaffold
│ │ ├── Main.java
│ │ ├── artifact
│ │ ├── Artifact.java
│ │ ├── dir
│ │ │ ├── ConfDir.java
│ │ │ ├── ContractsDir.java
│ │ │ ├── DirectoryArtifact.java
│ │ │ ├── GradleDir.java
│ │ │ ├── MainDir.java
│ │ │ ├── MainJavaDir.java
│ │ │ ├── MainResourceDir.java
│ │ │ ├── RootDir.java
│ │ │ ├── SrcDir.java
│ │ │ ├── TestDir.java
│ │ │ ├── TestJavaDir.java
│ │ │ └── root
│ │ │ │ ├── GradleRootDir.java
│ │ │ │ ├── MavenRootDir.java
│ │ │ │ └── RootDir.java
│ │ └── file
│ │ │ ├── ApplicationJava.java
│ │ │ ├── ApplicationProperties.java
│ │ │ ├── BcosConfigJava.java
│ │ │ ├── BuildGradle.java
│ │ │ ├── CommonResponseJava.java
│ │ │ ├── DemosTestJava.java
│ │ │ ├── Mvnw.java
│ │ │ ├── MvnwCmd.java
│ │ │ ├── Pom.java
│ │ │ ├── SdkBeanConfigJava.java
│ │ │ └── SettingsGradle.java
│ │ ├── builder
│ │ ├── ConstructorBoFileBuilder.java
│ │ ├── ContractConfigBuilder.java
│ │ ├── ContractConstantsBuilder.java
│ │ ├── FunctionBoFileBuilder.java
│ │ ├── JavaFileBuilder.java
│ │ ├── ServiceFileBuilder.java
│ │ ├── StructsFileBuilder.java
│ │ └── SystemConfigBuilder.java
│ │ ├── cmd
│ │ └── ScaffoldRunner.java
│ │ ├── compiler
│ │ └── ContractCompiler.java
│ │ ├── config
│ │ └── UserConfig.java
│ │ ├── constants
│ │ ├── DirNameConstants.java
│ │ ├── FileNameConstants.java
│ │ └── ReplaceConstants.java
│ │ ├── enums
│ │ └── ProjectType.java
│ │ ├── exception
│ │ └── ScaffoldException.java
│ │ ├── factory
│ │ └── ProjectFactory.java
│ │ ├── handler
│ │ ├── ContractHandler.java
│ │ ├── ServiceBeanConfigHandler.java
│ │ ├── ServicesHandler.java
│ │ └── SolidityTypeHandler.java
│ │ └── util
│ │ ├── ABIUtil.java
│ │ └── IOUtil.java
│ └── resources
│ └── templates
│ ├── Application.java.tpl
│ ├── BcosConfig.java.tpl
│ ├── CommonResponse.java.tpl
│ ├── Demos.java.tpl
│ ├── SdkBeanConfig.java.tpl
│ ├── TestConnection.java.tpl
│ ├── build.gradle.tpl
│ ├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar.tpl
│ │ └── gradle-wrapper.properties.tpl
│ ├── mvnw.cmd.tpl
│ ├── mvnw.tpl
│ ├── pom.xml.tpl
│ └── settings.gradle.tpl
└── tools
├── config.ini
├── contracts
├── HelloWorld.sol
└── KVTable.sol
├── run.bat
└── run.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle/
2 | build/
3 | .classpath
4 | bin/
5 | .settings/
6 | .project
7 | dist/
8 | tmp/
9 | /.gradle/
10 | *.lck
11 | .idea/
12 | out/
13 | demo/
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | [](https://www.apache.org/licenses/LICENSE-2.0.html)
3 |
4 |
5 | # 组件介绍
6 |
7 | 应用开发脚手架用于一键生成区块链应用开发工程,从而提高应用开发的效率。用户可将智能合约导入脚手架,即可生成对应的区块链应用开发工程,包含了合约函数对应的DAO(Data Access Object)层代码。用户可基于生成的项目补充自己的业务逻辑。
8 |
9 | ## 环境要求
10 |
11 | 在使用本组件前,请确认系统环境已安装相关依赖软件,清单如下:
12 |
13 | | 依赖软件 | 说明 | 备注 |
14 | | ---------- | ------------------------------------------------------------ | ---- |
15 | | Java | \>= JDK[1.8] | |
16 | | Git | 下载安装源码使用Git | |
17 |
18 |
19 | ## 文档
20 | - [**中文**](https://toolkit-doc.readthedocs.io/zh_CN/latest/docs/WeBankBlockchain-SmartDev-Scaffold/index.html)
21 | - [**快速开始**](https://toolkit-doc.readthedocs.io/zh_CN/latest/docs/WeBankBlockchain-SmartDev-Scaffold/quick_start.html)
22 |
23 |
24 | ## 贡献代码
25 | 欢迎参与本项目的社区建设:
26 | - 如项目对您有帮助,欢迎点亮我们的小星星(点击项目左上方Star按钮)。
27 | - 欢迎提交代码(Pull requests)。
28 | - [提问和提交BUG](https://github.com/WeBankBlockchain/SmartDev-Scaffold/issues)。
29 | - 如果发现代码存在安全漏洞,请在[这里](https://security.webank.com)上报。
30 |
31 |
32 | 
33 |
34 |
35 | ## License
36 | 
37 |
38 | 开源协议为[Apache License 2.0](http://www.apache.org/licenses/). 详情参考[LICENSE](../LICENSE)。
39 |
40 |
41 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.github.johnrengelman.shadow' version '6.1.0'
3 | id 'java'
4 | id 'eclipse'
5 | id 'idea'
6 | id 'application'
7 | }
8 |
9 | group 'com.webank'
10 | version '1.2.0'
11 |
12 | sourceCompatibility = 1.8
13 | targetCompatibility = 1.8
14 |
15 | def solcVersion = rootProject.hasProperty('solcVersion') ? solcVersion : "0.6.10.0"
16 |
17 | repositories {
18 | mavenCentral()
19 | maven { url "https://maven.aliyun.com/nexus/content/groups/public/"}
20 | maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
21 | maven { url "https://dl.bintray.com/ethereum/maven/" }
22 | mavenLocal()
23 | }
24 |
25 | def log4j_version="2.16.0"
26 | List logger = [
27 | "org.apache.logging.log4j:log4j-api:$log4j_version",
28 | "org.apache.logging.log4j:log4j-core:$log4j_version",
29 | "org.apache.logging.log4j:log4j-to-slf4j:$log4j_version",
30 | ]
31 |
32 | dependencies {
33 | compile logger
34 | implementation ('com.webank:solc-gradle-plugin:1.0.1')
35 | implementation ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:2.9.1')
36 | implementation group: 'info.picocli', name: 'picocli', version: '4.6.1'
37 | implementation ("org.slf4j:slf4j-api:1.7.30")
38 | implementation "org.fisco-bcos:solcJ:${solcVersion}"
39 | }
40 |
41 |
42 | shadowJar {
43 | mainClassName = 'com.webank.scaffold.Main'
44 | destinationDirectory = file('dist')
45 | archiveFileName = project.name+'.jar'
46 |
47 | exclude '**/*.DSA'
48 | exclude '**/*.RSA'
49 | exclude '**/*.SF'
50 | manifestContentCharset 'utf-8'
51 | metadataCharset 'utf-8'
52 |
53 | from("./"){
54 | include 'build.gradle'
55 | }
56 | }
57 |
58 | clean {
59 | println "delete ${projectDir}/dist"
60 | delete "${projectDir}/dist"
61 | }
62 |
63 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WeBankBlockchain/SmartDev-Scaffold/1a46d0d8f8bb1698f926918dc72cfcde93c8e33d/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'WeBankBlockchain-SmartDev-Scaffold'
2 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/Main.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold;
2 |
3 | import com.webank.scaffold.cmd.ScaffoldRunner;
4 | import com.webank.scaffold.constants.FileNameConstants;
5 | import picocli.CommandLine;
6 |
7 | import java.io.InputStream;
8 |
9 | /**
10 | * @author aaronchu
11 | * @Description
12 | * @data 2021/01/20
13 | */
14 | public class Main {
15 |
16 | public static void main(String[] args) {
17 | CommandLine cmd = new CommandLine(new ScaffoldRunner());
18 | cmd.execute(args);
19 | }
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/Artifact.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact;
2 |
3 | import java.io.File;
4 |
5 | public interface Artifact {
6 |
7 | void generate() throws Exception;
8 |
9 | default boolean isDirectory(){
10 | return false;
11 | }
12 |
13 | default File toFile(){
14 | return new File(getParentDir(), getName());
15 | }
16 |
17 | File getParentDir();
18 |
19 | String getName();
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/ConfDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.constants.DirNameConstants;
4 |
5 | import java.io.File;
6 |
7 | /**
8 | * @author aaronchu
9 | * @Description
10 | * @data 2021/01/19
11 | */
12 | public class ConfDir extends DirectoryArtifact {
13 |
14 | public ConfDir(File path) {
15 | super(path);
16 | }
17 |
18 | @Override
19 | protected void doGenerateSubContents() throws Exception {
20 | }
21 |
22 | @Override
23 | public String getName() {
24 | return DirNameConstants.CONF_DIR;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/ContractsDir.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.artifact.dir;
17 |
18 | import java.io.File;
19 |
20 | import org.apache.commons.io.FileUtils;
21 |
22 | import com.webank.scaffold.config.UserConfig;
23 | import com.webank.scaffold.constants.DirNameConstants;
24 | import com.webank.scaffold.exception.ScaffoldException;
25 |
26 | /**
27 | * ContractsDir
28 | *
29 | * @Description:
30 | * @Author: grayson
31 | * @Version 1.0
32 | * @Date: 2021-04-07 11:12
33 | **/
34 | public class ContractsDir extends DirectoryArtifact {
35 |
36 | private UserConfig config;
37 |
38 | public ContractsDir(File parentDir, UserConfig config) {
39 | super(parentDir);
40 | this.config = config;
41 | }
42 |
43 | @Override
44 | protected void doGenerateSubContents() throws Exception {
45 | File file = FileUtils.getFile(config.getSolidityDir());
46 | if(checkContractsDir(file)){
47 | FileUtils.copyDirectory(file, this.toFile());
48 | }
49 | }
50 |
51 | @Override
52 | public String getName() {
53 | return DirNameConstants.SOL_Dir;
54 | }
55 |
56 | private boolean checkContractsDir(File file) throws Exception {
57 | if(!file.isDirectory() || file.listFiles().length == 0){
58 | throw new ScaffoldException("contracts directory must not be empty : " + file.getName());
59 | }
60 | for (File solFile : file.listFiles()) {
61 | if(!solFile.getName().endsWith(".sol")){
62 | throw new ScaffoldException(solFile.getName() + " is not contract file");
63 | }
64 | }
65 | return true;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/DirectoryArtifact.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.exception.ScaffoldException;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | *
10 | * @author aaronchu
11 | * @Description
12 | * @data 2021/01/15
13 | */
14 | public abstract class DirectoryArtifact implements Artifact {
15 |
16 | protected File parentDir;
17 |
18 | public DirectoryArtifact(File parentDir){
19 | this.parentDir = parentDir;
20 | }
21 |
22 | @Override
23 | public final void generate() throws Exception {
24 | File f = this.toFile();
25 | if(!f.mkdirs()){
26 | throw new ScaffoldException(f.getAbsolutePath() + " directory has exist");
27 | }
28 | this.doGenerateSubContents();
29 | }
30 |
31 | protected abstract void doGenerateSubContents() throws Exception;
32 |
33 | @Override
34 | public final boolean isDirectory() {
35 | return true;
36 | }
37 |
38 | @Override
39 | public final File getParentDir(){
40 | return this.parentDir;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/GradleDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.config.UserConfig;
4 | import com.webank.scaffold.constants.DirNameConstants;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 | import org.apache.commons.io.FileUtils;
9 | import org.apache.commons.io.IOUtils;
10 |
11 | import java.io.File;
12 | import java.io.FileOutputStream;
13 | import java.io.InputStream;
14 | import java.io.OutputStream;
15 | import java.util.HashMap;
16 | import java.util.Map;
17 |
18 | /**
19 | * @author aaronchu
20 | * @Description
21 | * @data 2021/04/06
22 | */
23 | public class GradleDir extends DirectoryArtifact {
24 |
25 | private UserConfig config;
26 |
27 | public GradleDir(File parentDir, UserConfig config) {
28 | super(parentDir);
29 | this.config = config;
30 | }
31 |
32 | @Override
33 | protected void doGenerateSubContents() throws Exception {
34 | /**
35 | * 1. Create gradle wrapper directory
36 | */
37 | File wrapperDir = new File(this.toFile(), "wrapper");
38 | wrapperDir.mkdirs();
39 |
40 | /**
41 | * 2. Copy gradle wrapper jar
42 | */
43 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
44 | try(InputStream jarIn = classLoader.getResourceAsStream(FileNameConstants.TEMPLATE_GRADLE_WRAPPER_JAR);
45 | OutputStream jarOut = new FileOutputStream(new File(wrapperDir, FileNameConstants.GRADLE_WRAPPER_JAR))
46 | ){
47 | IOUtils.copy(jarIn, jarOut);
48 | }
49 |
50 | /**
51 | * 3. Generate gradle properties file
52 | */
53 | Map map = new HashMap<>();
54 | map.put(ReplaceConstants.GRADLE, config.getGradle());
55 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_GRADLE_WRAPPER_PROPERTIES, map, new File(wrapperDir, FileNameConstants.GRADLE_WRAPPER_PROPERTIES));
56 | }
57 |
58 | @Override
59 | public String getName() {
60 | return DirNameConstants.GRADLE;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/MainDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.config.UserConfig;
4 | import com.webank.scaffold.constants.DirNameConstants;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * @author aaronchu
10 | * @Description
11 | * @data 2021/01/19
12 | */
13 | public class MainDir extends DirectoryArtifact {
14 |
15 | private UserConfig config;
16 |
17 | public MainDir(File parentPath, UserConfig config) {
18 | super(parentPath);
19 | this.config = config;
20 | }
21 |
22 | /**
23 | * Generate sub directories: contracts,abi,bin,smbin,java
24 | * @throws Exception
25 | */
26 | @Override
27 | protected void doGenerateSubContents() throws Exception {
28 |
29 | // process contracts dir
30 | ContractsDir contractsDir = new ContractsDir(this.toFile(), config);
31 | contractsDir.generate();
32 |
33 | // process resource dir
34 | MainResourceDir resources = new MainResourceDir(this.toFile(), config);
35 | resources.generate();
36 |
37 | // process java code dir
38 | MainJavaDir javas = new MainJavaDir(this.toFile(), resources.getAbiDir(), this.config);
39 | javas.generate();
40 | }
41 |
42 | @Override
43 | public String getName() {
44 | return DirNameConstants.MAIN_DIR;
45 | }
46 |
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/MainJavaDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import java.io.File;
4 | import java.util.List;
5 |
6 | import com.webank.scaffold.artifact.file.BcosConfigJava;
7 | import com.webank.scaffold.builder.*;
8 | import org.apache.commons.io.FileUtils;
9 |
10 | import com.webank.scaffold.artifact.file.ApplicationJava;
11 | import com.webank.scaffold.artifact.file.CommonResponseJava;
12 | import com.webank.scaffold.artifact.file.SdkBeanConfigJava;
13 | import com.webank.scaffold.config.UserConfig;
14 | import com.webank.scaffold.constants.DirNameConstants;
15 | import com.webank.scaffold.constants.FileNameConstants;
16 | import com.webank.scaffold.util.ABIUtil;
17 | import com.webank.scaffold.util.IOUtil;
18 |
19 | /**
20 | * @author aaronchu
21 | * @author grayson
22 | * @Description
23 | * @data 2021/03/02
24 | */
25 | public class MainJavaDir extends DirectoryArtifact {
26 |
27 | private File abiDir;
28 | private UserConfig config;
29 |
30 | public MainJavaDir(File parentDir, File abi, UserConfig config) {
31 | super(parentDir);
32 | this.abiDir = abi;
33 | this.config = config;
34 | }
35 |
36 | @Override
37 | protected void doGenerateSubContents() throws Exception {
38 | handleUtil();
39 | handleSystemConfig();
40 | handleBcosConfig();
41 | handleContractConfig();
42 | handleContractConstants();
43 | handleBOAndService();
44 | handleApplication();
45 | handleSdkBeanConfig();
46 | handleCommonResponse();
47 | }
48 |
49 | private void handleContractConstants() throws Exception{
50 | List contractList = ABIUtil.contracts(abiDir, this.config.getNeed());
51 | ContractConstantsBuilder constantsBuilder = new ContractConstantsBuilder(this.config, contractList);
52 | File javaDir = this.toFile();
53 | constantsBuilder.generateJavaFile(FileNameConstants.CONSTANT_PKG_POSTFIX, javaDir);
54 | }
55 |
56 | private void handleApplication() throws Exception{
57 | String pkg = config.getGroup() + "." + config.getArtifact();
58 | ApplicationJava applicationJava = new ApplicationJava(IOUtil.convertPackageToFile(this.toFile(),pkg), config);
59 | applicationJava.generate();
60 | }
61 |
62 | private void handleUtil() throws Exception{
63 | //hook
64 | }
65 |
66 | private void handleBOAndService() throws Exception{
67 | File javaDir = this.toFile();
68 | List contractList = ABIUtil.contracts(abiDir, this.config.getNeed());
69 | for(String contractName :contractList){
70 | File abiFile = new File(abiDir, contractName + FileNameConstants.ABI_FILE_POSTFIX);
71 | String abiStr = FileUtils.readFileToString(abiFile);
72 |
73 | // create constructor bo
74 | ConstructorBoFileBuilder cbBuilder = new ConstructorBoFileBuilder(contractName, abiStr, this.config);
75 | cbBuilder.generateJavaFile(FileNameConstants.BO_PKG_POSTFIX, javaDir);
76 |
77 | // create struct bo
78 | StructsFileBuilder structsBuilder = new StructsFileBuilder(abiStr, this.config);
79 | structsBuilder.generateJavaFile(FileNameConstants.BO_PKG_POSTFIX, javaDir);
80 |
81 | // create function bo
82 | FunctionBoFileBuilder fbBuilder = new FunctionBoFileBuilder(contractName, abiStr, this.config);
83 | fbBuilder.generateJavaFile(FileNameConstants.BO_PKG_POSTFIX, javaDir);
84 |
85 | //Build service
86 | ServiceFileBuilder sfBuilder = new ServiceFileBuilder(contractName, abiStr, this.config);
87 | sfBuilder.generateJavaFile(FileNameConstants.SERVICE_PKG_POSTFIX, javaDir);
88 | }
89 | }
90 |
91 | private void handleSystemConfig() throws Exception {
92 | SystemConfigBuilder systemConfigBuilder = new SystemConfigBuilder(config);
93 | systemConfigBuilder.generateJavaFile(FileNameConstants.CONFIG_PKG_POSTFIX, this.toFile());
94 | }
95 |
96 | private void handleBcosConfig() throws Exception {
97 | String configPackage = config.getGroup() + "." + config.getArtifact() + FileNameConstants.CONFIG_PKG_POSTFIX;;
98 | BcosConfigJava bcosConfigJava = new BcosConfigJava(IOUtil.convertPackageToFile(this.toFile(),configPackage), config);
99 | bcosConfigJava.generate();
100 | }
101 |
102 | private void handleContractConfig() throws Exception {
103 | List contracts = ABIUtil.contracts(this.abiDir, this.config.getNeed());
104 | ContractConfigBuilder contractConfigBuilder = new ContractConfigBuilder(config, contracts);
105 | contractConfigBuilder.generateJavaFile(FileNameConstants.CONFIG_PKG_POSTFIX, this.toFile());
106 | }
107 |
108 | private void handleSdkBeanConfig() throws Exception{
109 | String configPackage = config.getGroup() + "." + config.getArtifact() + FileNameConstants.CONFIG_PKG_POSTFIX;;
110 | SdkBeanConfigJava sdkBeanConfigJava = new SdkBeanConfigJava(IOUtil.convertPackageToFile(this.toFile(),configPackage), config);
111 | sdkBeanConfigJava.generate();
112 | }
113 |
114 | private void handleCommonResponse() throws Exception{
115 | String utilsPackage = config.getGroup() + "." + config.getArtifact() + FileNameConstants.MODEL_FILE_POSTFIX;;
116 | CommonResponseJava commonResponseJava = new CommonResponseJava(IOUtil.convertPackageToFile(this.toFile(),utilsPackage), config);
117 | commonResponseJava.generate();
118 | }
119 |
120 | @Override
121 | public String getName() {
122 | return DirNameConstants.JAVA_DIR;
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/MainResourceDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import java.io.File;
4 |
5 | import com.webank.scaffold.artifact.file.ApplicationProperties;
6 | import com.webank.scaffold.compiler.ContractCompiler;
7 | import com.webank.scaffold.config.UserConfig;
8 | import com.webank.scaffold.constants.DirNameConstants;
9 | import com.webank.scaffold.util.ABIUtil;
10 |
11 | /**
12 | *
13 | * @author aaronchu
14 | * @Description
15 | * @data 2021/03/02
16 | */
17 | public class MainResourceDir extends DirectoryArtifact {
18 |
19 | private UserConfig config;
20 | private File abiDir;
21 |
22 | public MainResourceDir(File parentDir, UserConfig config) {
23 | super(parentDir);
24 | this.config = config;
25 | this.abiDir = new File(this.toFile(), DirNameConstants.ABI_DIR);
26 | }
27 |
28 | @Override
29 | protected void doGenerateSubContents() throws Exception {
30 |
31 | //1. create Abi And Bin
32 | ContractCompiler compiler = new ContractCompiler(this.toFile(), new File(this.config.getSolidityDir()));
33 | compiler.compile();
34 |
35 | //2. create conf file
36 | ConfDir confDir = new ConfDir(this.toFile());
37 | confDir.generate();
38 |
39 | //3. create Application.properties
40 | ApplicationProperties applicationProperties = new ApplicationProperties(
41 | this.toFile(),
42 | ABIUtil.contracts(new File(this.toFile(), DirNameConstants.ABI_DIR), this.config.getNeed()));
43 | applicationProperties.generate();
44 | }
45 |
46 | @Override
47 | public String getName() {
48 | return DirNameConstants.RESOURCE;
49 | }
50 |
51 | /**
52 | * @return the config
53 | */
54 | public UserConfig getConfig() {
55 | return config;
56 | }
57 |
58 | /**
59 | * @param config the config to set
60 | */
61 | public void setConfig(UserConfig config) {
62 | this.config = config;
63 | }
64 |
65 | /**
66 | * @return the abiDir
67 | */
68 | public File getAbiDir() {
69 | return abiDir;
70 | }
71 |
72 | /**
73 | * @param abiDir the abiDir to set
74 | */
75 | public void setAbiDir(File abiDir) {
76 | this.abiDir = abiDir;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/RootDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 |
6 | import org.apache.commons.io.FileUtils;
7 |
8 | import com.webank.scaffold.artifact.file.BuildGradle;
9 | import com.webank.scaffold.artifact.file.SettingsGradle;
10 | import com.webank.scaffold.config.UserConfig;
11 |
12 | /**
13 | * @author aaronchu
14 | * @author grayson
15 | * @Description
16 | * @data 2021/01/19
17 | */
18 | public class RootDir extends DirectoryArtifact {
19 |
20 | private UserConfig config;
21 |
22 | public RootDir(File basePath, UserConfig config) {
23 | super(basePath);
24 | this.config = config;
25 | }
26 |
27 | @Override
28 | public String getName() {
29 | return this.config.getArtifact();
30 | }
31 |
32 | @Override
33 | public void doGenerateSubContents() throws Exception {
34 |
35 | SrcDir srcDir = new SrcDir(this.toFile(), config);
36 | srcDir.generate();
37 |
38 | BuildGradle buildGradle = new BuildGradle(this.toFile(), config);
39 | buildGradle.generate();
40 |
41 | SettingsGradle settingsGradle = new SettingsGradle(this.toFile(), config);
42 | settingsGradle.generate();
43 |
44 | GradleDir gradle = new GradleDir(this.toFile(), this.config);
45 | gradle.generate();
46 | }
47 |
48 | public void clean() throws IOException {
49 | FileUtils.forceDelete(this.toFile());
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/SrcDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.config.UserConfig;
4 | import com.webank.scaffold.constants.DirNameConstants;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * @author aaronchu
10 | * @Description
11 | * @data 2021/01/19
12 | */
13 | public class SrcDir extends DirectoryArtifact {
14 |
15 | private UserConfig config;
16 |
17 | public SrcDir(File parentPath, UserConfig config) {
18 | super(parentPath);
19 | this.config = config;
20 | }
21 |
22 | @Override
23 | protected void doGenerateSubContents() throws Exception {
24 |
25 | MainDir mainDir = new MainDir(this.toFile(), config);
26 | mainDir.generate();
27 |
28 | TestDir testDir = new TestDir(this.toFile(), config);
29 | testDir.generate();
30 | }
31 |
32 | @Override
33 | public String getName() {
34 | return DirNameConstants.SRC_DIR;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/TestDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.config.UserConfig;
4 |
5 | import java.io.File;
6 |
7 | /**
8 | * @author aaronchu
9 | * @Description
10 | * @data 2021/01/20
11 | */
12 | public class TestDir extends DirectoryArtifact {
13 |
14 | private UserConfig config;
15 |
16 | public TestDir(File parentDir, UserConfig config) {
17 | super(parentDir);
18 | this.config = config;
19 | }
20 |
21 | @Override
22 | protected void doGenerateSubContents() throws Exception {
23 | TestJavaDir testJavaDir = new TestJavaDir(this.toFile(), this.config);
24 | testJavaDir.generate();
25 | }
26 |
27 | @Override
28 | public String getName() {
29 | return "test";
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/TestJavaDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir;
2 |
3 | import com.webank.scaffold.artifact.file.DemosTestJava;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.util.IOUtil;
6 |
7 | import java.io.File;
8 |
9 | /**
10 | * @author aaronchu
11 | * @Description
12 | * @data 2021/01/20
13 | */
14 | public class TestJavaDir extends DirectoryArtifact {
15 |
16 | private UserConfig config;
17 |
18 | public TestJavaDir(File parentDir, UserConfig userConfig) {
19 | super(parentDir);
20 | this.config = userConfig;
21 | }
22 |
23 | @Override
24 | protected void doGenerateSubContents() throws Exception {
25 | /**
26 | * 1. Build sub directory due to package name
27 | */
28 | handleDemos();
29 | }
30 |
31 | @Override
32 | public String getName() {
33 | return "java";
34 | }
35 |
36 | private void handleDemos() throws Exception{
37 | File javaDir = this.toFile();
38 | String utilsPackage = config.getGroup() + "." + config.getArtifact();
39 | DemosTestJava demosTestJava = new DemosTestJava(IOUtil.convertPackageToFile(javaDir,utilsPackage), config);
40 | demosTestJava.generate();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/root/GradleRootDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir.root;
2 |
3 | import com.webank.scaffold.artifact.dir.GradleDir;
4 | import com.webank.scaffold.artifact.file.BuildGradle;
5 | import com.webank.scaffold.artifact.file.SettingsGradle;
6 | import com.webank.scaffold.config.UserConfig;
7 |
8 | import java.io.File;
9 |
10 | /**
11 | * @author aaronchu
12 | * @Description
13 | * @date 2021/08/10
14 | */
15 | public class GradleRootDir extends RootDir {
16 |
17 | public GradleRootDir(File basePath, UserConfig config) {
18 | super(basePath, config);
19 | }
20 |
21 | @Override
22 | public void doGenerateSubContents() throws Exception {
23 | super.doGenerateSubContents();
24 |
25 | BuildGradle buildGradle = new BuildGradle(this.toFile(), config);
26 | buildGradle.generate();
27 |
28 | SettingsGradle settingsGradle = new SettingsGradle(this.toFile(), config);
29 | settingsGradle.generate();
30 |
31 | GradleDir gradle = new GradleDir(this.toFile(), this.config);
32 | gradle.generate();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/root/MavenRootDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir.root;
2 |
3 | import com.webank.scaffold.artifact.file.BuildGradle;
4 | import com.webank.scaffold.artifact.file.Mvnw;
5 | import com.webank.scaffold.artifact.file.MvnwCmd;
6 | import com.webank.scaffold.artifact.file.Pom;
7 | import com.webank.scaffold.config.UserConfig;
8 |
9 | import java.io.File;
10 |
11 | /**
12 | * @author aaronchu
13 | * @Description
14 | * @date 2021/08/10
15 | */
16 | public class MavenRootDir extends RootDir {
17 |
18 | public MavenRootDir(File basePath, UserConfig config) {
19 | super(basePath, config);
20 | }
21 |
22 | @Override
23 | public void doGenerateSubContents() throws Exception {
24 | super.doGenerateSubContents();
25 |
26 | Pom pom = new Pom(this.toFile(), config);
27 | pom.generate();
28 |
29 | Mvnw mvnw = new Mvnw(this.toFile(), config);
30 | mvnw.generate();
31 |
32 | MvnwCmd mvnwCmd = new MvnwCmd(this.toFile(), config);
33 | mvnwCmd.generate();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/dir/root/RootDir.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.dir.root;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 |
6 | import com.webank.scaffold.artifact.dir.DirectoryArtifact;
7 | import com.webank.scaffold.artifact.dir.GradleDir;
8 | import com.webank.scaffold.artifact.dir.SrcDir;
9 | import org.apache.commons.io.FileUtils;
10 |
11 | import com.webank.scaffold.artifact.file.BuildGradle;
12 | import com.webank.scaffold.artifact.file.SettingsGradle;
13 | import com.webank.scaffold.config.UserConfig;
14 |
15 | /**
16 | * @author aaronchu
17 | * @author grayson
18 | * @Description
19 | * @data 2021/01/19
20 | */
21 | public abstract class RootDir extends DirectoryArtifact {
22 |
23 | protected UserConfig config;
24 |
25 | public RootDir(File basePath, UserConfig config) {
26 | super(basePath);
27 | this.config = config;
28 | }
29 |
30 | @Override
31 | public String getName() {
32 | return this.config.getArtifact();
33 | }
34 |
35 | @Override
36 | public void doGenerateSubContents() throws Exception {
37 |
38 | SrcDir srcDir = new SrcDir(this.toFile(), config);
39 | srcDir.generate();
40 | }
41 |
42 | public void clean() throws IOException {
43 | FileUtils.forceDelete(this.toFile());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/ApplicationJava.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.ReplaceConstants;
6 | import com.webank.scaffold.constants.FileNameConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Author grayson
16 | * @Description
17 | * @data 2021/03/02
18 | */
19 | public class ApplicationJava implements Artifact {
20 |
21 | private File parentDir;
22 | private UserConfig config;
23 |
24 | public ApplicationJava(File parent, UserConfig config){
25 | this.parentDir = parent;
26 | this.config = config;
27 | }
28 |
29 | @Override
30 | public void generate() throws Exception {
31 | this.parentDir.mkdirs();
32 | String pkg = config.getGroup() + "." + config.getArtifact();
33 | Map map = new HashMap<>();
34 | map.put(ReplaceConstants.PACKAGE, pkg);
35 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_APPLICATION, map, this.toFile());
36 | }
37 |
38 | @Override
39 | public File getParentDir() {
40 | return this.parentDir;
41 | }
42 |
43 | @Override
44 | public String getName() {
45 | return FileNameConstants.APPLICATION_JAVA;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/ApplicationProperties.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.constants.FileNameConstants;
5 | import org.fisco.bcos.sdk.utils.StringUtils;
6 |
7 | import java.io.BufferedWriter;
8 | import java.io.File;
9 | import java.io.FileWriter;
10 | import java.io.IOException;
11 | import java.util.List;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Description
16 | * @data 2021/03/02
17 | */
18 | public class ApplicationProperties implements Artifact {
19 |
20 | private File parentDir;
21 | private List contracts;
22 |
23 | public ApplicationProperties(File parentDir, List contracts){
24 | this.parentDir = parentDir;
25 | this.contracts = contracts;
26 | }
27 |
28 | @Override
29 | public void generate() throws Exception {
30 | try(BufferedWriter writer = new BufferedWriter(new FileWriter(this.toFile()))){
31 | writeJavaSdkKeys(writer);
32 | writeSystemConfigKeys(writer);
33 | writeContractKeys(writer);
34 | writeSpringBootKeys(writer);
35 | writer.flush();
36 | }
37 | }
38 | private void writeJavaSdkKeys(BufferedWriter writer) throws IOException {
39 | writer.write("### Java sdk configuration\n");
40 | writer.write("cryptoMaterial.certPath=conf\n");
41 | writer.write("network.peers[0]=127.0.0.1:20200\n");
42 | writer.write("network.peers[1]=127.0.0.1:20201\n");
43 | writer.newLine();
44 | }
45 |
46 | private void writeSystemConfigKeys(BufferedWriter writer) throws IOException {
47 | writer.write("### System configuration\n");
48 | writer.write("system.groupId=1\n");
49 | writer.write("system.hexPrivateKey=\n");
50 | writer.newLine();
51 | }
52 |
53 | private void writeContractKeys(BufferedWriter writer) throws IOException {
54 | writer.write("### Contract configuration\n");
55 | for(String contractName : contracts){
56 | contractName = StringUtils.lowercaseFirstLetter(contractName);
57 | writer.write("contract."+contractName+"Address=\n");
58 | }
59 | writer.newLine();
60 | }
61 |
62 | private void writeSpringBootKeys(BufferedWriter writer) throws IOException {
63 | writer.write("### Springboot configuration\n");
64 | writer.write("server.port=8080\n");
65 | writer.write("server.session.timeout=60\n");
66 | writer.write("banner.charset=UTF-8\n");
67 | writer.write("spring.jackson.date-format=yyyy-MM-dd HH:mm:ss\n");
68 | writer.write("spring.jackson.time-zone=GMT+8\n");
69 | writer.newLine();
70 | }
71 |
72 | @Override
73 | public File getParentDir() {
74 | return this.parentDir;
75 | }
76 |
77 | @Override
78 | public String getName() {
79 | return FileNameConstants.APP_PRO;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/BcosConfigJava.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Description
16 | * @data 2021/05/13
17 | */
18 | public class BcosConfigJava implements Artifact {
19 |
20 | private File parentDir;
21 | private UserConfig config;
22 |
23 | public BcosConfigJava(File parentDir, UserConfig config){
24 | this.parentDir = parentDir;
25 | this.config = config;
26 | }
27 | @Override
28 | public void generate() throws Exception {
29 | this.parentDir.mkdirs();
30 | String pkg = config.getGroup() + "." + config.getArtifact()+ ".config";
31 | Map map = new HashMap<>();
32 | map.put(ReplaceConstants.PACKAGE, pkg);
33 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_BCOS_CONFIG, map, this.toFile());
34 | }
35 |
36 | @Override
37 | public File getParentDir() {
38 | return parentDir;
39 | }
40 |
41 | @Override
42 | public String getName() {
43 | return FileNameConstants.BCOS_CONFIG;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/BuildGradle.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.ReplaceConstants;
6 | import com.webank.scaffold.constants.FileNameConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * Generate build.gradle file
15 | * @author aaronchu
16 | * @author grayson
17 | * @Description
18 | * @data 2021/01/15
19 | */
20 | public class BuildGradle implements Artifact {
21 |
22 | private File parent;
23 | private UserConfig config;
24 |
25 | public BuildGradle(File parent, UserConfig config){
26 | this.parent = parent;
27 | this.config = config;
28 | }
29 |
30 | @Override
31 | public void generate() throws Exception {
32 | Map map = new HashMap<>();
33 | map.put(ReplaceConstants.GENERATOR_GROUP, config.getGroup());
34 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_BUILD_GRADLE, map, this.toFile());
35 | }
36 |
37 | @Override
38 | public File getParentDir() {
39 | return this.parent;
40 | }
41 |
42 | @Override
43 | public String getName() {
44 | return FileNameConstants.BUILD_GRADLE_FILE;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/CommonResponseJava.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | public class CommonResponseJava implements Artifact {
14 |
15 | private File parentDir;
16 | private UserConfig config;
17 |
18 | public CommonResponseJava(File parentDir, UserConfig config){
19 | this.parentDir = parentDir;
20 | this.config = config;
21 | }
22 |
23 | @Override
24 | public void generate() throws Exception {
25 | this.parentDir.mkdirs();
26 | String pkg = config.getGroup() + "." + config.getArtifact() + ".model";
27 | Map map = new HashMap<>();
28 | map.put(ReplaceConstants.PACKAGE, pkg);
29 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_COMMONRESPONSE, map, this.toFile());
30 | }
31 |
32 | @Override
33 | public File getParentDir() {
34 | return parentDir;
35 | }
36 |
37 | @Override
38 | public String getName() {
39 | return FileNameConstants.COMMON_RESPONSE_JAVA;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/DemosTestJava.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | public class DemosTestJava implements Artifact {
14 |
15 | private File parentDir;
16 | private UserConfig config;
17 |
18 | public DemosTestJava(File parentDir, UserConfig config){
19 | this.parentDir = parentDir;
20 | this.config = config;
21 | }
22 |
23 | @Override
24 | public void generate() throws Exception {
25 | this.parentDir.mkdirs();
26 | String pkg = config.getGroup() + "." + config.getArtifact();
27 | Map map = new HashMap<>();
28 | map.put(ReplaceConstants.PACKAGE, pkg);
29 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_DEMOS, map, this.toFile());
30 | }
31 |
32 | @Override
33 | public File getParentDir() {
34 | return parentDir;
35 | }
36 |
37 | @Override
38 | public String getName() {
39 | return FileNameConstants.DEMOS_JAVA;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/Mvnw.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Description
16 | * @date 2021/08/10
17 | */
18 | public class Mvnw implements Artifact {
19 |
20 | private File parent;
21 | private UserConfig config;
22 |
23 | public Mvnw(File parent, UserConfig config){
24 | this.parent = parent;
25 | this.config = config;
26 | }
27 |
28 | @Override
29 | public void generate() throws Exception {
30 | Map map = new HashMap<>();
31 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_MVNW, map, this.toFile());
32 | }
33 |
34 | @Override
35 | public File getParentDir() {
36 | return this.parent;
37 | }
38 |
39 | @Override
40 | public String getName() {
41 | return FileNameConstants.MVNW_FILE;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/MvnwCmd.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.util.IOUtil;
7 |
8 | import java.io.File;
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | /**
13 | * @author aaronchu
14 | * @Description
15 | * @date 2021/08/10
16 | */
17 | public class MvnwCmd implements Artifact {
18 |
19 | private File parent;
20 | private UserConfig config;
21 |
22 | public MvnwCmd(File parent, UserConfig config){
23 | this.parent = parent;
24 | this.config = config;
25 | }
26 |
27 | @Override
28 | public void generate() throws Exception {
29 | Map map = new HashMap<>();
30 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_MVNW_CMD, map, this.toFile());
31 | }
32 |
33 | @Override
34 | public File getParentDir() {
35 | return this.parent;
36 | }
37 |
38 | @Override
39 | public String getName() {
40 | return FileNameConstants.MVNW_CMD_FILE;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/Pom.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Description
16 | * @date 2021/08/10
17 | */
18 | public class Pom implements Artifact {
19 | private File parent;
20 | private UserConfig config;
21 |
22 | public Pom(File parent, UserConfig config){
23 | this.parent = parent;
24 | this.config = config;
25 | }
26 |
27 | @Override
28 | public void generate() throws Exception {
29 | Map map = new HashMap<>();
30 | map.put(ReplaceConstants.GENERATOR_GROUP, config.getGroup());
31 | map.put(ReplaceConstants.GENERATOR_ARTIFACT, config.getArtifact());
32 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_POM, map, this.toFile());
33 | }
34 |
35 | @Override
36 | public File getParentDir() {
37 | return this.parent;
38 | }
39 |
40 | @Override
41 | public String getName() {
42 | return FileNameConstants.POM_FILE;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/SdkBeanConfigJava.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import com.webank.scaffold.constants.ReplaceConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @Description
16 | * @data 2021/03/03
17 | */
18 | public class SdkBeanConfigJava implements Artifact {
19 |
20 | private File parentDir;
21 | private UserConfig config;
22 |
23 | public SdkBeanConfigJava(File parentDir, UserConfig config){
24 | this.parentDir = parentDir;
25 | this.config = config;
26 | }
27 |
28 | @Override
29 | public void generate() throws Exception {
30 | this.parentDir.mkdirs();
31 | String pkg = config.getGroup() + "." + config.getArtifact() + ".config";
32 | Map map = new HashMap<>();
33 | map.put(ReplaceConstants.PACKAGE, pkg);
34 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_SDK_CONFIG, map, this.toFile());
35 | }
36 |
37 | @Override
38 | public File getParentDir() {
39 | return parentDir;
40 | }
41 |
42 | @Override
43 | public String getName() {
44 | return FileNameConstants.SDK_CONFIG;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/artifact/file/SettingsGradle.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.artifact.file;
2 |
3 | import com.webank.scaffold.artifact.Artifact;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.ReplaceConstants;
6 | import com.webank.scaffold.constants.FileNameConstants;
7 | import com.webank.scaffold.util.IOUtil;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * @author aaronchu
15 | * @author grayson
16 | * @Description
17 | * @data 2021/02/24
18 | */
19 | public class SettingsGradle implements Artifact {
20 |
21 | private File parent;
22 | private UserConfig config;
23 |
24 | public SettingsGradle(File parent, UserConfig config){
25 | this.parent = parent;
26 | this.config = config;
27 | }
28 |
29 | @Override
30 | public void generate() throws Exception {
31 | Map map = new HashMap<>();
32 | map.put(ReplaceConstants.GENERATOR_ARTIFACT, config.getArtifact());
33 | IOUtil.replaceAllStr(FileNameConstants.TEMPLATE_SETTING_GRADLE, map, this.toFile());
34 | }
35 |
36 | @Override
37 | public File getParentDir() {
38 | return this.parent;
39 | }
40 |
41 | @Override
42 | public String getName() {
43 | return FileNameConstants.SETTINGS_GRADLE_FILE;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/ConstructorBoFileBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.builder;
17 |
18 | import com.squareup.javapoet.TypeSpec;
19 | import com.webank.scaffold.config.UserConfig;
20 | import com.webank.scaffold.constants.FileNameConstants;
21 | import com.webank.scaffold.util.ABIUtil;
22 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
23 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinitionFactory;
24 | import org.fisco.bcos.sdk.abi.wrapper.ContractABIDefinition;
25 | import org.fisco.bcos.sdk.crypto.CryptoSuite;
26 |
27 | import java.util.Arrays;
28 | import java.util.List;
29 |
30 | /**
31 | * BoFileHandler
32 | *
33 | * @Description:
34 | * @Author: grayson
35 | * @Version 1.0
36 | * @Date: 2021-04-08 10:47
37 | **/
38 | public class ConstructorBoFileBuilder implements JavaFileBuilder {
39 |
40 | private String contractName;
41 | private String abiStr;
42 | private UserConfig config;
43 |
44 | public ConstructorBoFileBuilder(String contractName, String abiStr, UserConfig config){
45 | this.contractName = contractName;
46 | this.abiStr = abiStr;
47 | this.config = config;
48 | }
49 |
50 | @Override
51 | public String getJavaFilePackage(String relativePackage) {
52 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
53 | }
54 |
55 | @Override
56 | public List buildTypeSpec(String fullPkg) {
57 |
58 | // 1.Get Constructor ABIDefinition
59 | ABIDefinitionFactory factory = new ABIDefinitionFactory(new CryptoSuite(0));
60 | ContractABIDefinition rootAbi = factory.loadABI(abiStr);
61 | ABIDefinition constructorAbi = rootAbi.getConstructor();
62 |
63 | // 2.Returns null if no constructor defined(this is the common case)
64 | if(constructorAbi == null || constructorAbi.getInputs().isEmpty()) return null;
65 |
66 | // 3.Build TypeSpec
67 | String className = this.contractName + FileNameConstants.CTOR;
68 | return Arrays.asList(ABIUtil.buildBOType(className, constructorAbi.getInputs()));
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/ContractConfigBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.builder;
17 |
18 | import com.squareup.javapoet.AnnotationSpec;
19 | import com.squareup.javapoet.ClassName;
20 | import com.squareup.javapoet.TypeSpec;
21 | import com.webank.scaffold.config.UserConfig;
22 | import com.webank.scaffold.constants.FileNameConstants;
23 | import lombok.Data;
24 | import org.fisco.bcos.sdk.utils.StringUtils;
25 |
26 | import javax.lang.model.element.Modifier;
27 | import java.util.Arrays;
28 | import java.util.List;
29 |
30 | /**
31 | * ContractConfigHander
32 | *
33 | * @Description:
34 | * @Author: grayson
35 | * @Version 1.0
36 | * @Date: 2021-04-08 09:33
37 | **/
38 | public class ContractConfigBuilder implements JavaFileBuilder {
39 |
40 | private List contracts;
41 | private UserConfig config;
42 |
43 | public ContractConfigBuilder(UserConfig config, List contracts){
44 | this.contracts = contracts;
45 | this.config = config;
46 | }
47 |
48 | @Override
49 | public String getJavaFilePackage(String relativePackage) {
50 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
51 | }
52 |
53 | @Override
54 | public List buildTypeSpec(String fullPkg) {
55 | //1. Basic initializations
56 | ClassName contractConfigClass = ClassName.get(fullPkg, FileNameConstants.CONTRACT_CONFIG);
57 | TypeSpec.Builder contractConfigBuilder = TypeSpec.classBuilder(contractConfigClass)
58 | .addModifiers(Modifier.PUBLIC)
59 | .addAnnotation(Data.class)
60 | .addAnnotation(ClassName.get("org.springframework.context.annotation","Configuration"))
61 | .addAnnotation(
62 | AnnotationSpec
63 | .builder(ClassName.get("org.springframework.boot.context.properties","ConfigurationProperties"))
64 | .addMember("prefix", "\"contract\"")
65 | .build());
66 |
67 | //2. Populate contract fields
68 | for (String contract : this.contracts) {
69 | String fieldName = StringUtils.lowercaseFirstLetter(contract) +"Address";
70 | contractConfigBuilder.addField(String.class, fieldName, Modifier.PRIVATE);
71 | }
72 | return Arrays.asList(contractConfigBuilder.build());
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/ContractConstantsBuilder.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.builder;
2 |
3 | import com.squareup.javapoet.*;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.DirNameConstants;
6 | import com.webank.scaffold.constants.FileNameConstants;
7 |
8 | import javax.lang.model.element.Modifier;
9 | import java.util.Arrays;
10 | import java.util.List;
11 |
12 | /**
13 | * @author aaronchu
14 | * @Description
15 | * @data 2021/05/12
16 | */
17 | public class ContractConstantsBuilder implements JavaFileBuilder {
18 |
19 | private List contracts;
20 | private UserConfig config;
21 |
22 |
23 | public ContractConstantsBuilder(UserConfig config, List contracts){
24 | this.contracts = contracts;
25 | this.config = config;
26 | }
27 |
28 | @Override
29 | public String getJavaFilePackage(String relativePackage) {
30 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
31 | }
32 |
33 | @Override
34 | public List buildTypeSpec(String fullPkg) {
35 | /**
36 | * 1. Create class header
37 | */
38 | TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(ClassName.get(fullPkg, FileNameConstants.CONTRACT_CONSTANTS))
39 | .addModifiers(Modifier.PUBLIC);
40 |
41 | /**
42 | * 2. Create fields
43 | */
44 | for(String contract:contracts){
45 | typeBuilder.addField(abiField(contract));
46 | typeBuilder.addField(binaryField(contract));
47 | typeBuilder.addField(gmBinaryField(contract));
48 | }
49 |
50 | /**
51 | * 3. Static blocks for field initializers
52 | */
53 | CodeBlock.Builder staticBlockBuilder = CodeBlock.builder()
54 | .beginControlFlow("try");
55 | String readUtilFullName =
56 | "org.apache.commons.io.IOUtils.toString(Thread.currentThread().getContextClassLoader().getResource(\"$L/$L\"))";
57 | for(String contract:contracts) {
58 | String abiFieldName = contract + FileNameConstants.ABI_POSTFIX;
59 | staticBlockBuilder.addStatement(abiFieldName + " = " + readUtilFullName, DirNameConstants.ABI_DIR, contract + ".abi");
60 | String binFieldName = contract + FileNameConstants.BINARY_POSTFIX;
61 | staticBlockBuilder.addStatement(binFieldName + " = " + readUtilFullName, DirNameConstants.BIN_DIR, contract + ".bin");
62 | String gmBinFieldName = contract + FileNameConstants.BINARY_GM_POSTFIX;
63 | staticBlockBuilder.addStatement(gmBinFieldName + " = " + readUtilFullName, DirNameConstants.SMBIN_DIR, contract + ".bin");
64 | }
65 |
66 | staticBlockBuilder
67 | .nextControlFlow("catch ($T e)", Exception.class)
68 | .addStatement("throw new $T(e)", RuntimeException.class)
69 | .endControlFlow();
70 | typeBuilder.addStaticBlock(staticBlockBuilder.build());
71 |
72 | return Arrays.asList(typeBuilder.build());
73 | }
74 |
75 | private FieldSpec abiField(String contract){
76 | String fieldName = contract+FileNameConstants.ABI_POSTFIX;
77 | FieldSpec fieldSpec = FieldSpec.builder(String.class, fieldName, Modifier.PUBLIC, Modifier.STATIC).build();
78 | return fieldSpec;
79 | }
80 |
81 | private FieldSpec binaryField(String contract){
82 | String fieldName = contract+FileNameConstants.BINARY_POSTFIX;
83 | FieldSpec fieldSpec = FieldSpec.builder(String.class, fieldName, Modifier.PUBLIC, Modifier.STATIC).build();
84 | return fieldSpec;
85 | }
86 |
87 | private FieldSpec gmBinaryField(String contract){
88 | String fieldName = contract+FileNameConstants.BINARY_GM_POSTFIX;
89 | FieldSpec fieldSpec = FieldSpec.builder(String.class, fieldName, Modifier.PUBLIC, Modifier.STATIC).build();
90 | return fieldSpec;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/FunctionBoFileBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.builder;
17 |
18 | import com.squareup.javapoet.TypeSpec;
19 | import com.webank.scaffold.config.UserConfig;
20 | import com.webank.scaffold.util.ABIUtil;
21 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
22 |
23 | import java.util.List;
24 | import java.util.Map;
25 | import java.util.stream.Collectors;
26 |
27 | /**
28 | * FunctionBoFileHandler
29 | *
30 | * @Description:
31 | * @Author: grayson
32 | * @Version 1.0
33 | * @Date: 2021-04-08 11:30
34 | **/
35 | public class FunctionBoFileBuilder implements JavaFileBuilder {
36 |
37 | private String contractName;
38 | private String abiStr;
39 | private UserConfig config;
40 |
41 | public FunctionBoFileBuilder(String contractName, String abiStr, UserConfig config){
42 | this.contractName = contractName;
43 | this.abiStr = abiStr;
44 | this.config = config;
45 | }
46 |
47 | @Override
48 | public String getJavaFilePackage(String relativePackage) {
49 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
50 | }
51 |
52 | @Override
53 | public List buildTypeSpec(String fullPkg) {
54 | Map result = ABIUtil.buildAbiDefAndTypeSpec(contractName,abiStr);
55 | return result.values().stream().collect(Collectors.toList());
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/JavaFileBuilder.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.builder;
2 |
3 | import com.squareup.javapoet.JavaFile;
4 | import com.squareup.javapoet.TypeSpec;
5 | import java.io.File;
6 | import java.io.IOException;
7 | import java.util.List;
8 |
9 | public interface JavaFileBuilder {
10 |
11 | String getJavaFilePackage(String relativePackage);
12 |
13 | List buildTypeSpec(String fullPkg);
14 |
15 | default void generateJavaFile(String relativePackage, File rootFile) throws IOException {
16 | String pkg = getJavaFilePackage(relativePackage);
17 | List typeSpecs = buildTypeSpec(pkg);
18 | if(typeSpecs == null) return;
19 | for (TypeSpec typeSpec: typeSpecs) {
20 | if(typeSpec != null){
21 | JavaFile file = JavaFile.builder(pkg, typeSpec).build();
22 | file.writeTo(rootFile);
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/ServiceFileBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.builder;
17 |
18 | import com.squareup.javapoet.TypeSpec;
19 | import com.webank.scaffold.config.UserConfig;
20 | import com.webank.scaffold.handler.ServicesHandler;
21 | import com.webank.scaffold.util.ABIUtil;
22 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
23 |
24 | import java.util.Arrays;
25 | import java.util.List;
26 | import java.util.Map;
27 |
28 | /**
29 | * ServiceFileHandler
30 | *
31 | * @Description:
32 | * @Author: grayson
33 | * @Version 1.0
34 | * @Date: 2021-04-08 15:44
35 | **/
36 | public class ServiceFileBuilder implements JavaFileBuilder {
37 |
38 | private String contractName;
39 | private String abiStr;
40 | private UserConfig config;
41 |
42 | public ServiceFileBuilder(String contractName, String abiStr, UserConfig config){
43 | this.contractName = contractName;
44 | this.abiStr = abiStr;
45 | this.config = config;
46 | }
47 |
48 | @Override
49 | public String getJavaFilePackage(String relativePackage) {
50 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
51 | }
52 |
53 | @Override
54 | public List buildTypeSpec(String fullPkg) {
55 | Map functions = ABIUtil.buildAbiDefAndTypeSpec(contractName, abiStr);
56 | ServicesHandler servicesHandler = new ServicesHandler(config);
57 | TypeSpec serviceType = servicesHandler.build(contractName, functions);
58 | return Arrays.asList(serviceType);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/StructsFileBuilder.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.builder;
2 |
3 | import com.squareup.javapoet.TypeSpec;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.handler.ContractHandler;
6 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
7 | import org.fisco.bcos.sdk.codegen.CodeGenUtils;
8 |
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * Convert struct into bos
14 | * @author yuzhichu
15 | */
16 | public class StructsFileBuilder implements JavaFileBuilder{
17 |
18 | private UserConfig config;
19 | private ContractHandler handler;
20 | private List abis;
21 |
22 | public StructsFileBuilder(String abiStr, UserConfig config) throws Exception{
23 | this.config = config;
24 | this.handler = new ContractHandler();
25 | this.abis = CodeGenUtils.loadContractAbiDefinition(abiStr);
26 | }
27 |
28 | //private List buildStructTypes(List functionDefinitions)
29 | @Override
30 | public String getJavaFilePackage(String relativePackage) {
31 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
32 | }
33 |
34 | @Override
35 | public List buildTypeSpec(String fullPkg) {
36 | try{
37 | return handler.buildStructTypes(abis);
38 | }
39 | catch (Exception ex){
40 | throw new RuntimeException(ex);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/builder/SystemConfigBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.builder;
17 |
18 | import com.squareup.javapoet.*;
19 | import com.webank.scaffold.config.UserConfig;
20 | import com.webank.scaffold.constants.FileNameConstants;
21 | import lombok.Data;
22 |
23 | import javax.lang.model.element.Modifier;
24 | import java.util.Arrays;
25 | import java.util.List;
26 |
27 | /**
28 | * SystemConfigBuilder
29 | *
30 | * @Description:
31 | * @Author: grayson
32 | * @Version 1.0
33 | * @Date: 2021-04-08 10:10
34 | **/
35 | public class SystemConfigBuilder implements JavaFileBuilder {
36 |
37 | private UserConfig config;
38 |
39 | public SystemConfigBuilder(UserConfig config){
40 | this.config = config;
41 | }
42 |
43 | @Override
44 | public String getJavaFilePackage(String relativePackage) {
45 | return config.getGroup() + "." + config.getArtifact() + relativePackage;
46 | }
47 |
48 | @Override
49 | public List buildTypeSpec(String fullPkg) {
50 | ClassName className = ClassName.get(fullPkg, FileNameConstants.SYSTEM_CONFIG);
51 | TypeSpec systemConfigBuilder = TypeSpec.classBuilder(className)
52 | .addModifiers(Modifier.PUBLIC)
53 | .addAnnotation(Data.class)
54 | .addAnnotation(ClassName.get("org.springframework.context.annotation","Configuration"))
55 | .addAnnotation(
56 | AnnotationSpec
57 | .builder(ClassName.get("org.springframework.boot.context.properties","ConfigurationProperties"))
58 | .addMember("prefix", "\"system\"")
59 | .build())
60 | .addField(
61 | FieldSpec
62 | .builder(
63 | TypeName.INT, "groupId", Modifier.PRIVATE)
64 | .initializer("1")
65 | .build())
66 | .addField(
67 | FieldSpec
68 | .builder(
69 | String.class, "hexPrivateKey", Modifier.PRIVATE)
70 | .build())
71 | .build();
72 |
73 | return Arrays.asList(systemConfigBuilder);
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/cmd/ScaffoldRunner.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.cmd;
2 |
3 |
4 | import com.webank.scaffold.enums.ProjectType;
5 | import com.webank.scaffold.factory.ProjectFactory;
6 | import picocli.CommandLine;
7 |
8 | /**
9 | * @author aaronchu
10 | * @Description
11 | * @data 2021/01/20
12 | */
13 |
14 | @CommandLine.Command(name = "ScaffoldRunner")
15 | public class ScaffoldRunner implements Runnable{
16 | @CommandLine.Option(names = {"-s", "--sol"}, required = true, description = "Required. Solidity contracts dir.")
17 | private String solidityDir;
18 |
19 | @CommandLine.Option(names = {"-g", "--group"}, required = true,defaultValue = "org.example",description = "Optional. Group name.")
20 | private String group;
21 |
22 | @CommandLine.Option(names = {"-a", "--artifact"}, required = true, defaultValue = "demo",description = "Optional. Artifact name.")
23 | private String artifact;
24 |
25 | @CommandLine.Option(names = {"-o", "--output"},required = true, defaultValue = "artifacts",description = "Optional. Output directory.")
26 | private String output;
27 |
28 | @CommandLine.Option(names = {"-n", "--need"}, required = false,defaultValue = "",description = "Optional. The contracts you need,for example Contract1,Contract2,Contract3")
29 | private String need;
30 |
31 | @CommandLine.Option(names = {"-t", "--type"}, required = false,defaultValue = "gradle",description = "Optional. Project type.")
32 | private String projectType;
33 |
34 | @CommandLine.Option(names = {"-gv", "--gradle"}, required = false,defaultValue = "6.3",description = "Optional. Gradle version.")
35 | private String gradle;
36 |
37 | @Override
38 | public void run() {
39 | ProjectFactory factory = new ProjectFactory(group, artifact, solidityDir, output, need, projectType, gradle);
40 | factory.createProject();
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/compiler/ContractCompiler.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.compiler;
2 |
3 | import com.webank.scaffold.constants.DirNameConstants;
4 | import com.webank.solc.plugin.compiler.CompileSolToJava;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * @author aaronchu
10 | * @Description
11 | * @data 2021/01/15
12 | */
13 | public class ContractCompiler {
14 |
15 | private File output;
16 | private File solDir;
17 |
18 | public ContractCompiler(File output, File solDir){
19 | this.output = output;
20 | this.solDir = solDir;
21 | }
22 |
23 | /**
24 | * Compile solidity and generates:abi、bin、smbin.
25 | * @throws Exception
26 | */
27 | public void compile() throws Exception{
28 |
29 | File abiDir = new File(this.output, DirNameConstants.ABI_DIR);
30 | File binDir = new File(this.output, DirNameConstants.BIN_DIR);
31 | File smBinDir = new File(this.output, DirNameConstants.SMBIN_DIR);
32 |
33 | CompileSolToJava compiler = new CompileSolToJava();
34 | String solName = "";//Compile everything
35 |
36 | compiler.compileSolToJava(
37 | solName,
38 | null,
39 | solDir,
40 | abiDir,
41 | binDir,
42 | smBinDir,
43 | null
44 | );
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/config/UserConfig.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.config;
2 |
3 | import com.webank.scaffold.enums.ProjectType;
4 |
5 | /**
6 | * Config files for users
7 | * @author aaronchu
8 | * @Description
9 | * @data 2021/01/15
10 | */
11 |
12 | public class UserConfig{
13 |
14 | private String group;
15 | private String artifact;
16 | private String solidityDir;
17 | private String outputDir;
18 | private String need;
19 | private String gradle;
20 | private ProjectType projectType;
21 |
22 | public String getGradle() {
23 | return gradle;
24 | }
25 |
26 | private UserConfig(){}
27 |
28 | public static UserConfig getInstance(String group, String artifact, String solidityDir, String outputDir, String need, ProjectType projectType, String gradle){
29 |
30 | UserConfig config = new UserConfig();
31 | config.setGroup(group);
32 | config.setArtifact(artifact);
33 | config.setSolidityDir(solidityDir);
34 | config.setOutputDir(outputDir);
35 | config.setNeed(need);
36 | config.setGradle(gradle);
37 | config.setProjectType(projectType);
38 | return config;
39 | }
40 |
41 | private void setGradle(String gradle) {
42 | this.gradle = gradle;
43 | }
44 |
45 | /**
46 | * @return the group
47 | */
48 | public String getGroup() {
49 | return group;
50 | }
51 |
52 | /**
53 | * @param group the group to set
54 | */
55 | public void setGroup(String group) {
56 | this.group = group;
57 | }
58 |
59 | /**
60 | * @return the artifact
61 | */
62 | public String getArtifact() {
63 | return artifact;
64 | }
65 |
66 | /**
67 | * @param artifact the artifact to set
68 | */
69 | public void setArtifact(String artifact) {
70 | this.artifact = artifact;
71 | }
72 |
73 | /**
74 | * @return the solidityDir
75 | */
76 | public String getSolidityDir() {
77 | return solidityDir;
78 | }
79 |
80 | /**
81 | * @param solidityDir the solidityDir to set
82 | */
83 | public void setSolidityDir(String solidityDir) {
84 | this.solidityDir = solidityDir;
85 | }
86 |
87 | /**
88 | * @return the outputDir
89 | */
90 | public String getOutputDir() {
91 | return outputDir;
92 | }
93 |
94 | /**
95 | * @param outputDir the outputDir to set
96 | */
97 | public void setOutputDir(String outputDir) {
98 | this.outputDir = outputDir;
99 | }
100 |
101 | /**
102 | * @return the need
103 | */
104 | public String getNeed() {
105 | return need;
106 | }
107 |
108 | /**
109 | * @param need the need to set
110 | */
111 | public void setNeed(String need) {
112 | this.need = need;
113 | }
114 |
115 |
116 | public ProjectType getProjectType() {
117 | return projectType;
118 | }
119 |
120 | public void setProjectType(ProjectType projectType) {
121 | this.projectType = projectType;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/constants/DirNameConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.constants;
17 |
18 | /**
19 | * DirNameConstants
20 | *
21 | * @Description:
22 | * @Author: grayson
23 | * @Version 1.0
24 | * @Date: 2021-04-07 14:41
25 | **/
26 | public class DirNameConstants {
27 |
28 | public static final String ABI_DIR = "abi";
29 | public static final String BIN_DIR = "bin/ecc";
30 | public static final String SMBIN_DIR = "bin/sm";
31 |
32 | public static final String RESOURCE = "resources";
33 | public static final String GRADLE = "gradle";
34 | public static final String CONF_DIR = "conf";
35 | public static final String SOL_Dir = "contracts";
36 | public static final String MAIN_DIR = "main";
37 | public static final String SRC_DIR = "src";
38 | public static final String JAVA_DIR = "java";
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/constants/FileNameConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.constants;
17 |
18 | /**
19 | * FileNameConstants
20 | *
21 | * @Description:
22 | * @Author: grayson
23 | * @Version 1.0
24 | * @Date: 2021-04-07 15:59
25 | **/
26 | public class FileNameConstants {
27 |
28 | public static final String TEMPLATE_POSTFIX = ".tpl";
29 |
30 | //template file name
31 | public static final String TEMPLATE_BUILD_GRADLE = "templates/build.gradle" + TEMPLATE_POSTFIX;
32 | public static final String TEMPLATE_POM = "templates/pom.xml" + TEMPLATE_POSTFIX;
33 | public static final String TEMPLATE_SETTING_GRADLE = "templates/settings.gradle" + TEMPLATE_POSTFIX;
34 | public static final String TEMPLATE_MVNW = "templates/mvnw" + TEMPLATE_POSTFIX;
35 | public static final String TEMPLATE_MVNW_CMD = "templates/mvnw.cmd" + TEMPLATE_POSTFIX;
36 | public static final String TEMPLATE_APPLICATION = "templates/Application.java" + TEMPLATE_POSTFIX;
37 | public static final String TEMPLATE_COMMONRESPONSE = "templates/CommonResponse.java" + TEMPLATE_POSTFIX;
38 | public static final String TEMPLATE_DEMOS = "templates/Demos.java" + TEMPLATE_POSTFIX;
39 | public static final String TEMPLATE_SDK_CONFIG = "templates/SdkBeanConfig.java" + TEMPLATE_POSTFIX;
40 | public static final String TEMPLATE_GRADLE_WRAPPER_JAR = "templates/gradle/wrapper/gradle-wrapper.jar" + TEMPLATE_POSTFIX;
41 | public static final String TEMPLATE_GRADLE_WRAPPER_PROPERTIES = "templates/gradle/wrapper/gradle-wrapper.properties"+TEMPLATE_POSTFIX;
42 | public static final String TEMPLATE_BCOS_CONFIG = "templates/BcosConfig.java"+TEMPLATE_POSTFIX;
43 |
44 | //generated file name
45 | public static final String BUILD_GRADLE_FILE = "build.gradle";
46 | public static final String POM_FILE = "pom.xml";
47 | public static final String APP_PRO = "application.properties";
48 | public static final String SETTINGS_GRADLE_FILE = "settings.gradle";
49 | public static final String MVNW_FILE = "mvnw";
50 | public static final String MVNW_CMD_FILE = "mvnw.cmd";
51 | public static final String APPLICATION_JAVA = "Application.java";
52 | public static final String COMMON_RESPONSE_JAVA = "CommonResponse.java";
53 | public static final String DEMOS_JAVA = "Demos.java";
54 | public static final String SDK_CONFIG = "SdkBeanConfig.java";
55 | public static final String GRADLE_WRAPPER_JAR = "gradle-wrapper.jar";
56 | public static final String GRADLE_WRAPPER_PROPERTIES = "gradle-wrapper.properties";
57 | public static final String BCOS_CONFIG = "BcosConfig.java";
58 | public static final String SYSTEM_CONFIG = "SystemConfig";
59 |
60 | public static final String CONTRACT_CONFIG = "ContractConfig";
61 | public static final String CONTRACT_CONSTANTS = "ContractConstants";
62 |
63 | //package or file postfix
64 | public static final String CONFIG_PKG_POSTFIX = ".config";
65 | public static final String UTILS_PKG_POSTFIX = ".utils";
66 | public static final String ABI_FILE_POSTFIX = ".abi";
67 | public static final String MODEL_FILE_POSTFIX = ".model";
68 | public static final String BO_PKG_POSTFIX = ".model.bo";
69 | public static final String SERVICE_PKG_POSTFIX = ".service";
70 | public static final String CONSTANT_PKG_POSTFIX = ".constants";
71 |
72 | public final static String CTOR = "CtorBO";
73 | public final static String INPUT = "InputBO";
74 | public final static String ABI_POSTFIX = "Abi";
75 | public final static String BINARY_POSTFIX = "Binary";
76 | public final static String BINARY_GM_POSTFIX = "GmBinary";
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/constants/ReplaceConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.constants;
17 |
18 | /**
19 | * ConfigOptConstants
20 | *
21 | * @Description:
22 | * @Author: grayson
23 | * @Version 1.0
24 | * @Date: 2021-04-06 18:53
25 | **/
26 | public class ReplaceConstants {
27 |
28 | public static String GENERATOR_ARTIFACT = "generator.artifact";
29 | public static String GENERATOR_GROUP = "generator.group";
30 | public static String PACKAGE = "package";
31 | public static String GRADLE = "gradle";
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/enums/ProjectType.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.enums;
2 |
3 | import lombok.Getter;
4 |
5 | /**
6 | * @author aaronchu
7 | * @Description
8 | * @date 2021/08/10
9 | */
10 |
11 | public enum ProjectType {
12 | Gradle("gradle"),
13 |
14 | Maven("maven");
15 |
16 | public String getName() {
17 | return name;
18 | }
19 |
20 | private String name;
21 |
22 | ProjectType(String name){
23 | this.name = name;
24 | }
25 |
26 | public static ProjectType nameOf(String projectType) {
27 | for(ProjectType projectTypeEnum:ProjectType.values()){
28 | if(projectTypeEnum.getName().equals(projectType.toLowerCase())){
29 | return projectTypeEnum;
30 | }
31 | }
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/exception/ScaffoldException.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.exception;
2 |
3 | /**
4 | * @author aaronchu
5 | * @Description
6 | * @data 2021/01/20
7 | */
8 | public class ScaffoldException extends RuntimeException{
9 |
10 | /** @Fields serialVersionUID : TODO */
11 | private static final long serialVersionUID = -4165654620608938012L;
12 |
13 | public ScaffoldException(Exception error){
14 | super(error);
15 | }
16 |
17 | public ScaffoldException(String message){
18 | super(message);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/factory/ProjectFactory.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.factory;
2 |
3 | import com.webank.scaffold.artifact.dir.root.GradleRootDir;
4 | import com.webank.scaffold.artifact.dir.root.MavenRootDir;
5 | import com.webank.scaffold.artifact.dir.root.RootDir;
6 | import com.webank.scaffold.config.UserConfig;
7 | import com.webank.scaffold.enums.ProjectType;
8 | import com.webank.scaffold.exception.ScaffoldException;
9 | import java.io.File;
10 |
11 | /**
12 | * @author aaronchu
13 | * @author grayson
14 | * @Description
15 | * @data 2021/01/20
16 | */
17 | public class ProjectFactory {
18 |
19 |
20 | private UserConfig config;
21 |
22 | public ProjectFactory(String group, String artifact, String solidityDir, String output, String need, String projectType, String gradle){
23 | this.config = UserConfig.getInstance(group, artifact, solidityDir, output, need, ProjectType.nameOf(projectType), gradle);
24 | }
25 |
26 | public boolean createProject(){
27 | //1. build project root object
28 |
29 | RootDir project = determineRootDir(config.getProjectType());
30 |
31 | //2. create sub content in project
32 | try{
33 | project.generate();
34 | System.out.println("Project created:" + project.toFile());
35 | }
36 | catch (Exception ex){
37 | ex.printStackTrace();
38 |
39 | // 3.Delete all project data in case of exception
40 | try{
41 | project.clean();
42 | }
43 | catch (Exception e){}
44 | }
45 | return true;
46 | }
47 |
48 |
49 | private RootDir determineRootDir(ProjectType projectType){
50 | if(ProjectType.Gradle.equals(projectType)){
51 | return new GradleRootDir(new File(config.getOutputDir()), config);
52 | }
53 | if(ProjectType.Maven.equals(projectType)){
54 | return new MavenRootDir(new File(config.getOutputDir()), config);
55 | }
56 | throw new IllegalArgumentException("Invalid project type:"+projectType);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/handler/ServiceBeanConfigHandler.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.handler;
2 |
3 | import com.squareup.javapoet.*;
4 | import com.webank.scaffold.config.UserConfig;
5 | import com.webank.scaffold.constants.FileNameConstants;
6 | import lombok.Data;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.fisco.bcos.sdk.client.Client;
9 | import org.fisco.bcos.sdk.utils.StringUtils;
10 |
11 | import javax.lang.model.element.Modifier;
12 | import java.io.File;
13 | import java.util.List;
14 |
15 | /**
16 | * @author aaronchu
17 | * @Description
18 | * @data 2021/03/03
19 | */
20 | public class ServiceBeanConfigHandler {
21 | private File javaRoot;
22 | private List contracts;
23 | private UserConfig config;
24 |
25 | public ServiceBeanConfigHandler(File javaRoot, List contracts, UserConfig config){
26 | this.javaRoot = javaRoot;
27 | this.contracts = contracts;
28 | this.config = config;
29 | }
30 |
31 | public void export() throws Exception{
32 | //1. Initializing
33 | String configPkg = config.getGroup() + "." + config.getArtifact() + ".config";
34 | TypeSpec.Builder configClassBuilder
35 | = initBuilder(configPkg);
36 |
37 | //2. Contract services beans
38 | String servicePkg = config.getGroup() + "." + config.getArtifact() + ".service";
39 | for(String contract: contracts){
40 | addBean(configClassBuilder, servicePkg, contract);
41 | }
42 |
43 | //3. Output
44 | JavaFile javaFile = JavaFile.builder(configPkg, configClassBuilder.build()).build();
45 | javaFile.writeTo(javaRoot);
46 | }
47 |
48 | private TypeSpec.Builder initBuilder(String configPkg){
49 |
50 | ClassName className = ClassName.get(configPkg, "ServiceBeanConfig");
51 | TypeSpec.Builder configClassBuilder
52 | = TypeSpec.classBuilder(className)
53 | .addModifiers(Modifier.PUBLIC)
54 | .addAnnotation(ClassName.get("org.springframework.context.annotation","Configuration"))
55 | .addAnnotation(Data.class)
56 | .addAnnotation(Slf4j.class)
57 | .addField(FieldSpec.builder(
58 | ClassName.get(configPkg, FileNameConstants.SYSTEM_CONFIG), "config", Modifier.PRIVATE)
59 | .addAnnotation(ClassName.get("org.springframework.beans.factory.annotation","Autowired"))
60 | .build());
61 | return configClassBuilder;
62 | }
63 |
64 | private void addBean(TypeSpec.Builder builder, String servicePackage, String contract){
65 | String serviceName = contract+"Service";
66 | ClassName serviceClass = ClassName.get(servicePackage, serviceName);
67 | builder.addMethod(
68 | MethodSpec.methodBuilder(StringUtils.lowercaseFirstLetter(contract)+"Service")
69 | .addModifiers(Modifier.PUBLIC)
70 | .returns(serviceClass)
71 | .addParameter(ParameterSpec.builder(Client.class, "client").build())
72 | .addException(Exception.class)
73 | .addAnnotation(ClassName.get("import org.springframework.context.annotation","Bean"))
74 | .addStatement("String address = config.getContract().get$LAddress()", contract)
75 | .addStatement("if (address == null) {log.warn(\"$L address not configured, so $L will not inject into spring\");}", contract, serviceName)
76 | .addStatement("return new $L(address, client)",serviceName)
77 | .build()
78 | );
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/handler/ServicesHandler.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.handler;
2 |
3 | import java.util.Arrays;
4 | import java.util.Map;
5 |
6 | import javax.lang.model.element.Modifier;
7 |
8 | import com.webank.scaffold.builder.ContractConstantsBuilder;
9 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
10 | import org.fisco.bcos.sdk.client.Client;
11 | import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
12 | import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
13 | import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
14 | import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
15 | import org.fisco.bcos.sdk.utils.StringUtils;
16 |
17 | import com.squareup.javapoet.AnnotationSpec;
18 | import com.squareup.javapoet.ClassName;
19 | import com.squareup.javapoet.FieldSpec;
20 | import com.squareup.javapoet.MethodSpec;
21 | import com.squareup.javapoet.TypeSpec;
22 | import com.webank.scaffold.config.UserConfig;
23 | import com.webank.scaffold.constants.DirNameConstants;
24 | import com.webank.scaffold.constants.FileNameConstants;
25 |
26 | import lombok.Data;
27 | import lombok.NoArgsConstructor;
28 |
29 | /**
30 | * @author aaronchu
31 | * @Description
32 | * @data 2021/01/27
33 | */
34 | public class ServicesHandler {
35 |
36 | private UserConfig config;
37 |
38 | public ServicesHandler(UserConfig config) {
39 | this.config = config;
40 | }
41 |
42 | public TypeSpec build(String contract, Map functionWithInputBO) {
43 |
44 | // 1. create service class
45 | ClassName serviceClassName = serviceClassName(contract);
46 | TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(serviceClassName).addModifiers(Modifier.PUBLIC)
47 | .addAnnotation(ClassName.get("org.springframework.stereotype", "Service"))
48 | .addAnnotation(NoArgsConstructor.class).addAnnotation(Data.class);
49 |
50 | // 2. Instance fields: String address|Client client |AssemblyTransactionProcessor processor
51 | typeBuilder = this.populateInstanceFields(contract, typeBuilder);
52 |
53 | // 3. Add post constructor
54 | typeBuilder = this.populateInitializer(contract, typeBuilder);
55 |
56 | // 4. Add method: transaction | call
57 | typeBuilder = this.populateMethods(contract, typeBuilder, functionWithInputBO);
58 | return typeBuilder.build();
59 | }
60 |
61 | private ClassName serviceClassName(String contract) {
62 | String pkg = config.getGroup() + "." + config.getArtifact();
63 | String serviceName = contract + "Service";
64 | return ClassName.get(pkg + ".service", serviceName);
65 | }
66 |
67 | private TypeSpec.Builder populateInstanceFields(String contract, TypeSpec.Builder typeBuilder) {
68 | FieldSpec addressField = FieldSpec.builder(String.class, "address").addModifiers(Modifier.PRIVATE)
69 | .addAnnotation(AnnotationSpec
70 | .builder(ClassName.get("org.springframework.beans.factory.annotation", "Value"))
71 | .addMember("value", "\"$${contract.$LAddress}\"", StringUtils.lowercaseFirstLetter(contract))
72 | .build())
73 | .build();
74 | FieldSpec clientField = FieldSpec.builder(Client.class, "client").addModifiers(Modifier.PRIVATE)
75 | .addAnnotation(ClassName.get("org.springframework.beans.factory.annotation", "Autowired")).build();
76 | FieldSpec processField = FieldSpec.builder(AssembleTransactionProcessor.class, "txProcessor").build();
77 | return typeBuilder.addField(addressField).addField(clientField).addField(processField);
78 | }
79 |
80 | private TypeSpec.Builder populateInitializer(String contract, TypeSpec.Builder typeBuilder) {
81 | MethodSpec.Builder txBuilder = MethodSpec.methodBuilder("init");
82 | txBuilder.addModifiers(Modifier.PUBLIC).addAnnotation(ClassName.get("javax.annotation", "PostConstruct"))
83 | .addException(Exception.class);
84 | txBuilder.addStatement(
85 | "this.txProcessor = $T.createAssembleTransactionProcessor(this.client, this.client.getCryptoSuite().getCryptoKeyPair())",
86 | TransactionProcessorFactory.class);
87 | typeBuilder.addMethod(txBuilder.build());
88 | return typeBuilder;
89 | }
90 |
91 | private TypeSpec.Builder populateMethods(String contract, TypeSpec.Builder typeBuilder,
92 | Map functionWithInputBO) {
93 |
94 | for (Map.Entry entrySet : functionWithInputBO.entrySet()) {
95 | ABIDefinition function = entrySet.getKey();
96 | TypeSpec inputBO = entrySet.getValue();
97 | if (!function.isConstant()) {
98 | typeBuilder.addMethod(getTransactionMethod(contract, function.getName(), inputBO));
99 | } else {
100 | typeBuilder.addMethod(getCallMethod(contract, function.getName(), inputBO));
101 | }
102 | }
103 |
104 | return typeBuilder;
105 | }
106 |
107 | private MethodSpec getTransactionMethod(String contract, String function, TypeSpec inputType) {
108 | ClassName constantsClass = constantsClassName();
109 | String abiField = contract+FileNameConstants.ABI_POSTFIX;
110 | String pkg = config.getGroup() + "." + config.getArtifact() + FileNameConstants.BO_PKG_POSTFIX;
111 | MethodSpec.Builder txBuilder = MethodSpec.methodBuilder(function);
112 | txBuilder.addModifiers(Modifier.PUBLIC).addException(Exception.class).returns(TransactionResponse.class);
113 | if (inputType != null) {
114 | txBuilder.addParameter(ClassName.get(pkg, inputType.name), "input").addStatement(
115 | "return this.txProcessor.sendTransactionAndGetResponse(this.address, $T.$L, \"$L\", input.toArgs())",
116 | constantsClass, abiField,function);
117 | } else {
118 | txBuilder.addStatement(
119 | "return this.txProcessor.sendTransactionAndGetResponse(this.address, $T.$L, \"$L\", $T.asList())",
120 | constantsClass, abiField, function, Arrays.class);
121 | }
122 | return txBuilder.build();
123 | }
124 |
125 | private MethodSpec getCallMethod(String contract, String function, TypeSpec inputType) {
126 | ClassName constantsClass = constantsClassName();
127 | String abiField = contract+FileNameConstants.ABI_POSTFIX;
128 | String pkg = config.getGroup() + "." + config.getArtifact() + FileNameConstants.BO_PKG_POSTFIX;
129 | MethodSpec.Builder txBuilder = MethodSpec.methodBuilder(function);
130 | txBuilder.addModifiers(Modifier.PUBLIC).addException(Exception.class).returns(CallResponse.class);
131 | if (inputType != null) {
132 | txBuilder.addParameter(ClassName.get(pkg, inputType.name), "input").addStatement(
133 | "return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, $T.$L, \"$L\", input.toArgs())",
134 | constantsClass, abiField, function);
135 | } else {
136 | txBuilder.addStatement(
137 | "return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, $T.$L, \"$L\", $T.asList())",
138 | constantsClass, abiField, function, Arrays.class);
139 | }
140 | return txBuilder.build();
141 | }
142 |
143 |
144 | private ClassName constantsClassName() {
145 | String pkg = new ContractConstantsBuilder(config, Arrays.asList()).getJavaFilePackage(FileNameConstants.CONSTANT_PKG_POSTFIX);
146 | String simpleName = "ContractConstants";
147 | return ClassName.get(pkg , simpleName);
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/handler/SolidityTypeHandler.java:
--------------------------------------------------------------------------------
1 | package com.webank.scaffold.handler;
2 |
3 | import com.squareup.javapoet.TypeName;
4 | import com.squareup.javapoet.TypeSpec;
5 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * @author aaronchu
12 | * @Description
13 | * @data 2021/01/26
14 | */
15 | public class SolidityTypeHandler {
16 |
17 |
18 | /**
19 | * Convert a strong typed solidity type.
20 | * uint256 -> Uint256.class
21 | * uint256[] -> DynamicArray[Uint256].class
22 | * uint256[2] ->StaticArray2[Uint256].class
23 | * uint256[][2] ->DynamicArray[StaticArray2[Uint256]].class
24 | * @param namedType
25 | * @return
26 | */
27 | public static TypeName convert(ABIDefinition.NamedType namedType){
28 | ContractHandler handler = new ContractHandler();
29 | try{
30 | //如果是结构体,那么返回结构体对应类型的名字,比如xxx.xxx.xxx.Entry
31 | if (!namedType.getComponents().isEmpty()){
32 | List list = new ArrayList<>();
33 | list.add(namedType);
34 | TypeName typeName = handler.buildTypeNames(list).get(0);
35 | return typeName;
36 | }
37 | TypeName solType = handler.buildTypeName(namedType.getType());
38 | TypeName nativeType = handler.getNativeType(solType);
39 | return nativeType;
40 | }catch (Exception ex){
41 | throw new RuntimeException(ex);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/webank/scaffold/util/ABIUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2019 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.webank.scaffold.util;
17 |
18 | import com.squareup.javapoet.*;
19 | import com.webank.scaffold.constants.FileNameConstants;
20 | import com.webank.scaffold.handler.SolidityTypeHandler;
21 | import lombok.AllArgsConstructor;
22 | import lombok.Data;
23 | import lombok.NoArgsConstructor;
24 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
25 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinitionFactory;
26 | import org.fisco.bcos.sdk.abi.wrapper.ContractABIDefinition;
27 | import org.fisco.bcos.sdk.crypto.CryptoSuite;
28 | import org.fisco.bcos.sdk.utils.StringUtils;
29 |
30 | import javax.lang.model.element.Modifier;
31 | import java.io.File;
32 | import java.util.*;
33 | import java.util.stream.Collectors;
34 |
35 | /**
36 | * ABIUtil
37 | *
38 | * @Description:
39 | * @Author: grayson
40 | * @Version 1.0
41 | * @Date: 2021-04-08 15:48
42 | **/
43 | public class ABIUtil {
44 |
45 | public static TypeSpec buildBOType(String className, List args){
46 |
47 | // 1.Check: No need to generate BO for functions with no args
48 | if(args.isEmpty()) return null;//No need
49 |
50 | // 2.metadata
51 | TypeSpec.Builder boBuilder = TypeSpec.classBuilder(className)
52 | .addModifiers(Modifier.PUBLIC)
53 | .addAnnotation(Data.class)
54 | .addAnnotation(NoArgsConstructor.class)
55 | .addAnnotation(AllArgsConstructor.class);
56 |
57 | // 3.Fields
58 | int argIndex = 0;
59 | List fixedArgs = new ArrayList<>();
60 | for(ABIDefinition.NamedType namedType: args){
61 | String argName = namedType.getName();
62 | if(argName == null || argName.isEmpty()){
63 | argName = "arg"+argIndex;
64 | }
65 | TypeName type = SolidityTypeHandler.convert(namedType);
66 | boBuilder.addField(type, argName, Modifier.PRIVATE);
67 | argIndex++;
68 | fixedArgs.add(argName);
69 | }
70 |
71 | // 4.Methods
72 | MethodSpec.Builder toArgsMethodBuilder = MethodSpec.methodBuilder("toArgs")
73 | .addModifiers(Modifier.PUBLIC)
74 | .returns(ListObject.class.getGenericInterfaces()[0])
75 | .addStatement("$T args = new $T()", List.class, ArrayList.class);
76 | for(String arg: fixedArgs){
77 | toArgsMethodBuilder.addStatement("args.add($L)", arg);
78 | }
79 | toArgsMethodBuilder.addStatement("return args");
80 | boBuilder.addMethod(toArgsMethodBuilder.build());
81 |
82 | TypeSpec boType = boBuilder.build();
83 | return boType;
84 | }
85 |
86 | public static Map buildAbiDefAndTypeSpec(String contractName, String abiStr) {
87 |
88 | ABIDefinitionFactory factory = new ABIDefinitionFactory(new CryptoSuite(0));
89 | ContractABIDefinition rootAbi = factory.loadABI(abiStr);
90 | Map> functions = rootAbi.getFunctions();
91 |
92 | Map result = new HashMap<>();
93 | for(Map.Entry> e: functions.entrySet()){
94 | List definitions = e.getValue();
95 | for(int i=0;i0?Integer.toString(i):"";
99 | String className = contractName
100 | + functionName
101 | + overloadMark
102 | + FileNameConstants.INPUT;
103 | TypeSpec inputType = buildBOType(className, abiDef.getInputs());
104 | result.put(abiDef, inputType);
105 | }
106 | }
107 | return result;
108 | }
109 |
110 | public static List contracts(File abiDir, String need){
111 | Set needContracts = Arrays.stream(need.split(",|;")).collect(Collectors.toSet());
112 | List contractList = Arrays.stream(abiDir.listFiles())
113 | .map(f->f.getName().split("\\.")[0])
114 | .filter(c-> (need==null||need.isEmpty()) || needContracts.contains(c))
115 | .collect(Collectors.toList());
116 | return contractList;
117 | }
118 |
119 | interface ListObject extends List