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