|
| 1 | +package io.micronaut.guides.core; |
| 2 | + |
| 3 | +import io.micronaut.context.exceptions.ConfigurationException; |
| 4 | +import io.micronaut.core.annotation.NonNull; |
| 5 | +import io.micronaut.core.annotation.Nullable; |
| 6 | +import io.micronaut.http.exceptions.HttpStatusException; |
| 7 | +import io.micronaut.starter.api.TestFramework; |
| 8 | +import io.micronaut.starter.application.ApplicationType; |
| 9 | +import io.micronaut.starter.application.Project; |
| 10 | +import io.micronaut.starter.application.generator.GeneratorContext; |
| 11 | +import io.micronaut.starter.application.generator.ProjectGenerator; |
| 12 | +import io.micronaut.starter.io.ConsoleOutput; |
| 13 | +import io.micronaut.starter.io.FileSystemOutputHandler; |
| 14 | +import io.micronaut.starter.options.BuildTool; |
| 15 | +import io.micronaut.starter.options.JdkVersion; |
| 16 | +import io.micronaut.starter.options.Language; |
| 17 | +import io.micronaut.starter.options.Options; |
| 18 | +import io.micronaut.starter.util.NameUtils; |
| 19 | +import jakarta.inject.Singleton; |
| 20 | +import jakarta.validation.constraints.NotNull; |
| 21 | +import jakarta.validation.constraints.Pattern; |
| 22 | +import org.gradle.api.JavaVersion; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static io.micronaut.core.util.StringUtils.EMPTY_STRING; |
| 35 | +import static io.micronaut.http.HttpStatus.BAD_REQUEST; |
| 36 | +import static io.micronaut.starter.options.BuildTool.GRADLE; |
| 37 | +import static io.micronaut.starter.options.JdkVersion.JDK_8; |
| 38 | + |
| 39 | +@Singleton |
| 40 | +public class DefaultGuideProjectGenerator implements GuideProjectGenerator { |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(DefaultGuideProjectGenerator.class); |
| 42 | + private final GuidesConfiguration guidesConfiguration; |
| 43 | + private final ProjectGenerator projectGenerator; |
| 44 | + |
| 45 | + DefaultGuideProjectGenerator(GuidesConfiguration guidesConfiguration, |
| 46 | + ProjectGenerator projectGenerator) { |
| 47 | + this.guidesConfiguration = guidesConfiguration; |
| 48 | + this.projectGenerator = projectGenerator; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void generate(@NotNull @NonNull File outputDirectory, @NotNull @NonNull Guide guide) throws IOException { |
| 53 | + if (!outputDirectory.exists()) { |
| 54 | + throw new ConfigurationException("Output directory does not exist"); |
| 55 | + } |
| 56 | + if (!outputDirectory.isDirectory()) { |
| 57 | + throw new ConfigurationException("Output directory must be a directory"); |
| 58 | + } |
| 59 | + |
| 60 | + JdkVersion javaVersion = GuideGenerationUtils.resolveJdkVersion(guidesConfiguration, guide); |
| 61 | + if (guide.maximumJavaVersion() != null && javaVersion.majorVersion() > guide.maximumJavaVersion()) { |
| 62 | + if (LOG.isTraceEnabled()) { |
| 63 | + LOG.trace("not generating project for {}, JDK {} > {}", guide.slug(), javaVersion.majorVersion(), guide.maximumJavaVersion()); |
| 64 | + } |
| 65 | + return; |
| 66 | + } |
| 67 | + List<GuidesOption> guidesOptionList = GuideGenerationUtils.guidesOptions(guide,LOG); |
| 68 | + for (GuidesOption guidesOption : guidesOptionList) { |
| 69 | + generate(outputDirectory, guide, guidesOption, javaVersion); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void generate(@NonNull File outputDirectory, |
| 74 | + @NonNull Guide guide, |
| 75 | + @NonNull GuidesOption guidesOption, |
| 76 | + @NonNull JdkVersion javaVersion) throws IOException { |
| 77 | + for (App app : guide.apps()) { |
| 78 | + generate(outputDirectory, guide, guidesOption, javaVersion, app); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void generate(@NonNull File outputDirectory, |
| 83 | + @NonNull Guide guide, |
| 84 | + @NonNull GuidesOption guidesOption, |
| 85 | + @NonNull JdkVersion javaVersion, |
| 86 | + @NonNull App app) throws IOException { |
| 87 | + List<String> appFeatures = new ArrayList<>(GuideUtils.getAppFeatures(app, guidesOption.getLanguage())); |
| 88 | + if (!guidesConfiguration.getJdkVersionsSupportedByGraalvm().contains(javaVersion)) { |
| 89 | + appFeatures.remove("graalvm"); |
| 90 | + } |
| 91 | + |
| 92 | + if (guidesOption.getTestFramework() == TestFramework.SPOCK) { |
| 93 | + appFeatures.remove("mockito"); |
| 94 | + } |
| 95 | + |
| 96 | + // typical guides use 'default' as name, multi-project guides have different modules |
| 97 | + String folder = MacroUtils.getSourceDir(guide.slug(), guidesOption); |
| 98 | + |
| 99 | + String appName = app.name().equals(guidesConfiguration.getDefaultAppName()) ? EMPTY_STRING : app.name(); |
| 100 | + |
| 101 | + Path destinationPath = Paths.get(outputDirectory.getAbsolutePath(), folder, appName); |
| 102 | + File destination = destinationPath.toFile(); |
| 103 | + destination.mkdir(); |
| 104 | + |
| 105 | + String packageAndName = guidesConfiguration.getPackageName() + '.' + app.name(); |
| 106 | + GeneratorContext generatorContext = createProjectGeneratorContext(app.applicationType(), |
| 107 | + packageAndName, |
| 108 | + app.framework(), |
| 109 | + appFeatures, |
| 110 | + guidesOption.getBuildTool(), |
| 111 | + app.testFramework() != null ? app.testFramework() : guidesOption.getTestFramework(), |
| 112 | + guidesOption.getLanguage(), |
| 113 | + javaVersion); |
| 114 | + try { |
| 115 | + projectGenerator.generate(app.applicationType(), |
| 116 | + generatorContext.getProject(), |
| 117 | + new FileSystemOutputHandler(destination, ConsoleOutput.NOOP), |
| 118 | + generatorContext); |
| 119 | + } catch (Exception e) { |
| 120 | + LOG.error("Error generating application: " + e.getMessage(), e); |
| 121 | + throw new IOException(e.getMessage(), e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private GeneratorContext createProjectGeneratorContext( |
| 126 | + ApplicationType type, |
| 127 | + @Pattern(regexp = "[\\w\\d-_\\.]+") String packageAndName, |
| 128 | + @Nullable String framework, |
| 129 | + @Nullable List<String> features, |
| 130 | + @Nullable BuildTool buildTool, |
| 131 | + @Nullable TestFramework testFramework, |
| 132 | + @Nullable Language lang, |
| 133 | + @Nullable JdkVersion javaVersion) throws IllegalArgumentException { |
| 134 | + Project project; |
| 135 | + try { |
| 136 | + project = NameUtils.parse(packageAndName); |
| 137 | + } catch (IllegalArgumentException e) { |
| 138 | + throw new HttpStatusException(BAD_REQUEST, "Invalid project name: " + e.getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + return projectGenerator.createGeneratorContext( |
| 142 | + type, |
| 143 | + project, |
| 144 | + new Options( |
| 145 | + lang, |
| 146 | + testFramework != null ? testFramework.toTestFramework() : null, |
| 147 | + buildTool == null ? GRADLE : buildTool, |
| 148 | + javaVersion != null ? javaVersion : JDK_8).withFramework(framework), |
| 149 | + null, |
| 150 | + features != null ? features : Collections.emptyList(), |
| 151 | + ConsoleOutput.NOOP |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
0 commit comments