|
| 1 | +package io.javaoperatorsdk.boostrapper; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import org.apache.commons.io.FileUtils; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import io.javaoperatorsdk.bootstrapper.Versions; |
| 16 | + |
| 17 | +import com.github.mustachejava.DefaultMustacheFactory; |
| 18 | +import com.github.mustachejava.MustacheFactory; |
| 19 | + |
| 20 | +public class Bootstrapper { |
| 21 | + |
| 22 | + private static final Logger log = LoggerFactory.getLogger(Bootstrapper.class); |
| 23 | + |
| 24 | + private MustacheFactory mustacheFactory = new DefaultMustacheFactory(); |
| 25 | + |
| 26 | + private static final List<String> TOP_LEVEL_STATIC_FILES = |
| 27 | + List.of(".gitignore", "README.md"); |
| 28 | + private static final List<String> JAVA_FILES = |
| 29 | + List.of("CustomResource.java", "Reconciler.java", |
| 30 | + "Spec.java", "Status.java"); |
| 31 | + |
| 32 | + public void create(File targetDir, String groupId, String artifactId) { |
| 33 | + try { |
| 34 | + log.info("Generating project to: {}", targetDir.getPath()); |
| 35 | + var projectDir = new File(targetDir, artifactId); |
| 36 | + FileUtils.forceMkdir(projectDir); |
| 37 | + addStaticFiles(projectDir); |
| 38 | + addTemplatedFiles(projectDir, groupId, artifactId); |
| 39 | + addJavaFiles(projectDir, groupId, artifactId); |
| 40 | + addResourceFiles(projectDir, groupId, artifactId); |
| 41 | + } catch (IOException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void addResourceFiles(File projectDir, String groupId, String artifactId) { |
| 47 | + try { |
| 48 | + var target = new File(projectDir, "src/main/resources"); |
| 49 | + FileUtils.forceMkdir(target); |
| 50 | + addTemplatedFile(target, "log4j2.xml", groupId, artifactId, target, null); |
| 51 | + } catch (IOException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void addJavaFiles(File projectDir, String groupId, String artifactId) { |
| 57 | + try { |
| 58 | + var packages = groupId.replace(".", File.separator); |
| 59 | + var targetDir = new File(projectDir, "src/main/java/" + packages); |
| 60 | + FileUtils.forceMkdir(targetDir); |
| 61 | + var classFileNamePrefix = artifactClassId(artifactId); |
| 62 | + JAVA_FILES.forEach(f -> addTemplatedFile(projectDir, f, groupId, artifactId, targetDir, |
| 63 | + classFileNamePrefix + f)); |
| 64 | + |
| 65 | + addTemplatedFile(projectDir, "Runner.java", groupId, artifactId, targetDir, null); |
| 66 | + addTemplatedFile(projectDir, "ConfigMapDependentResource.java", groupId, artifactId, |
| 67 | + targetDir, null); |
| 68 | + } catch (IOException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + private void addTemplatedFiles(File projectDir, String groupId, String artifactId) { |
| 75 | + addTemplatedFile(projectDir, "pom.xml", groupId, artifactId); |
| 76 | + addTemplatedFile(projectDir, "k8s/test-resource.yaml", groupId, artifactId); |
| 77 | + } |
| 78 | + |
| 79 | + private void addTemplatedFile(File projectDir, String fileName, String groupId, |
| 80 | + String artifactId) { |
| 81 | + addTemplatedFile(projectDir, fileName, groupId, artifactId, null, null); |
| 82 | + } |
| 83 | + |
| 84 | + private void addTemplatedFile(File projectDir, String fileName, String groupId, String artifactId, |
| 85 | + File targetDir, String targetFileName) { |
| 86 | + try { |
| 87 | + var values = Map.of("groupId", groupId, "artifactId", artifactId, |
| 88 | + "artifactClassId", artifactClassId(artifactId), |
| 89 | + "josdkVersion", Versions.JOSDK, |
| 90 | + "fabric8Version", Versions.KUBERNETES_CLIENT); |
| 91 | + |
| 92 | + var mustache = mustacheFactory.compile("templates/" + fileName); |
| 93 | + var targetFile = new File(targetDir == null ? projectDir : targetDir, |
| 94 | + targetFileName == null ? fileName : targetFileName); |
| 95 | + FileUtils.forceMkdir(targetFile.getParentFile()); |
| 96 | + var writer = new FileWriter(targetFile); |
| 97 | + mustache.execute(writer, values); |
| 98 | + writer.flush(); |
| 99 | + } catch (IOException e) { |
| 100 | + throw new RuntimeException(e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void addStaticFiles(File projectDir) { |
| 105 | + TOP_LEVEL_STATIC_FILES.forEach(f -> addStaticFile(projectDir, f)); |
| 106 | + } |
| 107 | + |
| 108 | + private void addStaticFile(File targetDir, String fileName) { |
| 109 | + addStaticFile(targetDir, fileName, null); |
| 110 | + } |
| 111 | + |
| 112 | + private void addStaticFile(File targetDir, String fileName, String subDir) { |
| 113 | + String sourcePath = subDir == null ? "/static/" : "/static/" + subDir; |
| 114 | + String path = sourcePath + fileName; |
| 115 | + try (var is = Bootstrapper.class.getResourceAsStream(path)) { |
| 116 | + targetDir = subDir == null ? targetDir : new File(targetDir, subDir); |
| 117 | + if (subDir != null) { |
| 118 | + FileUtils.forceMkdir(targetDir); |
| 119 | + } |
| 120 | + FileUtils.copyInputStreamToFile(is, new File(targetDir, fileName)); |
| 121 | + } catch (IOException e) { |
| 122 | + throw new RuntimeException("File path: " + path, e); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + public static String artifactClassId(String artifactId) { |
| 128 | + var parts = artifactId.split("-"); |
| 129 | + return Arrays.stream(parts).map(p -> p.substring(0, 1) |
| 130 | + .toUpperCase() + p.substring(1)) |
| 131 | + .collect(Collectors.joining("")); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments