diff --git a/build.gradle b/build.gradle index 7516c5a4f2d..8d9ed224b53 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ import io.micronaut.guides.GuidesPlugin import io.micronaut.guides.IndexGenerator import io.micronaut.guides.TestScriptGenerator import io.micronaut.guides.ThemeProcessor -import io.micronaut.guides.GitDiffUtils +import io.micronaut.guides.core.GitDiffUtils plugins { id 'groovy' diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 7e440804801..28bf7e87f4d 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -19,6 +19,8 @@ dependencies { annotationProcessor(platform("io.micronaut.platform:micronaut-platform:$micronautVersion")) annotationProcessor("io.micronaut:micronaut-inject-java") + implementation subprojects + implementation(platform("io.micronaut.platform:micronaut-platform:$micronautVersion")) implementation("io.micronaut.starter:micronaut-starter-api:$micronautVersion") implementation("io.micronaut:micronaut-inject-java") diff --git a/buildSrc/micronaut-guides-core/build.gradle b/buildSrc/micronaut-guides-core/build.gradle new file mode 100644 index 00000000000..530aceb68c8 --- /dev/null +++ b/buildSrc/micronaut-guides-core/build.gradle @@ -0,0 +1,46 @@ +plugins { + id "java-gradle-plugin" + id 'groovy' +} + +ext { + micronautVersion = new File(projectDir.parentFile, 'version.txt').text.trim() + micronautDocsVersion = '1.0.25' +} + +repositories { + mavenCentral() + maven { url "https://repo.grails.org/grails/core" } + maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" } +} + +dependencies { + annotationProcessor(platform("io.micronaut.platform:micronaut-platform:$micronautVersion")) + annotationProcessor("io.micronaut:micronaut-inject-java") + annotationProcessor("io.micronaut.jsonschema:micronaut-json-schema-processor") + + testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + + implementation("org.mockito:mockito-core:5.13.0") + + implementation("io.micronaut.jsonschema:micronaut-json-schema-annotations") + implementation(platform("io.micronaut.platform:micronaut-platform:$micronautVersion")) + implementation("io.micronaut.starter:micronaut-starter-api:$micronautVersion") + implementation("io.micronaut:micronaut-inject-java") + implementation('org.apache.commons:commons-compress:1.26.0') + implementation "io.micronaut.docs:micronaut-docs-asciidoc-extensions:$micronautDocsVersion", { + exclude group: "org.codehaus.groovy" + } +} + +// For Groovy +tasks.withType(GroovyCompile).configureEach { + options.compilerArgs.add("-Amicronaut.jsonschema.baseUri=http://json-schema.org/draft-04/schema#") // (1) +} + +tasks.named('test', Test) { + useJUnitPlatform() + + debug true +} \ No newline at end of file diff --git a/buildSrc/micronaut-guides-core/settings.gradle b/buildSrc/micronaut-guides-core/settings.gradle new file mode 100644 index 00000000000..84dbbf3a15f --- /dev/null +++ b/buildSrc/micronaut-guides-core/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'micronaut-guides-core' \ No newline at end of file diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GitDiffUtils.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GitDiffUtils.groovy similarity index 96% rename from buildSrc/src/main/groovy/io/micronaut/guides/GitDiffUtils.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GitDiffUtils.groovy index d0e370b1ba9..4869a6d09a5 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GitDiffUtils.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GitDiffUtils.groovy @@ -1,4 +1,4 @@ -package io.micronaut.guides +package io.micronaut.guides.core import groovy.transform.CompileStatic import groovy.util.logging.Slf4j diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GuideAsciidocGenerator.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideAsciidocGenerator.groovy similarity index 99% rename from buildSrc/src/main/groovy/io/micronaut/guides/GuideAsciidocGenerator.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideAsciidocGenerator.groovy index 7f9566f543b..916687d0515 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GuideAsciidocGenerator.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideAsciidocGenerator.groovy @@ -1,4 +1,4 @@ -package io.micronaut.guides +package io.micronaut.guides.core import com.fizzed.rocker.Rocker import groovy.transform.CompileStatic @@ -6,7 +6,7 @@ import groovy.transform.Memoized import io.micronaut.context.ApplicationContext import io.micronaut.core.annotation.NonNull import io.micronaut.core.util.StringUtils -import io.micronaut.guides.GuideMetadata.App +import io.micronaut.guides.core.GuideMetadata.App import io.micronaut.starter.api.TestFramework import io.micronaut.starter.build.dependencies.Coordinate import io.micronaut.starter.build.dependencies.PomDependencyVersionResolver diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GuideMetadata.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideMetadata.groovy similarity index 96% rename from buildSrc/src/main/groovy/io/micronaut/guides/GuideMetadata.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideMetadata.groovy index fa6e217a985..addefeb2c66 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GuideMetadata.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideMetadata.groovy @@ -1,16 +1,18 @@ -package io.micronaut.guides +package io.micronaut.guides.core import groovy.transform.Canonical import groovy.transform.CompileStatic -import groovy.transform.EqualsAndHashCode import groovy.transform.ToString +import io.micronaut.jsonschema.JsonSchema import io.micronaut.starter.api.TestFramework import io.micronaut.starter.application.ApplicationType import io.micronaut.starter.options.BuildTool import io.micronaut.starter.options.Language + import java.time.LocalDate +@JsonSchema @ToString(includeNames = true) @CompileStatic class GuideMetadata { @@ -23,6 +25,7 @@ class GuideMetadata { List tags List categories LocalDate publicationDate + List clouds boolean publish String base @@ -60,7 +63,7 @@ class GuideMetadata { } } } - Set categoriesAsTags = this.categories.collect { cat -> cat.name().toLowerCase() } as Set + Set categoriesAsTags = this.categories.collect { cat -> cat.getName().toLowerCase().replace(" ","-") } as Set tagsList.addAll(categoriesAsTags) tagsList as List } diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GuideProjectGenerator.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideProjectGenerator.groovy similarity index 95% rename from buildSrc/src/main/groovy/io/micronaut/guides/GuideProjectGenerator.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideProjectGenerator.groovy index e1011272f40..bf32518549e 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GuideProjectGenerator.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuideProjectGenerator.groovy @@ -1,13 +1,14 @@ -package io.micronaut.guides +package io.micronaut.guides.core import groovy.json.JsonSlurper import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import groovy.transform.Memoized import io.micronaut.context.ApplicationContext +import io.micronaut.context.env.MapPropertySource import io.micronaut.core.annotation.NonNull import io.micronaut.core.annotation.Nullable -import io.micronaut.guides.GuideMetadata.App +import io.micronaut.guides.core.GuideMetadata.App import io.micronaut.starter.api.TestFramework import io.micronaut.starter.application.ApplicationType import io.micronaut.starter.options.BuildTool @@ -42,17 +43,26 @@ class GuideProjectGenerator implements AutoCloseable { private static final Pattern GROOVY_JAVA_OR_KOTLIN = ~/.*\.java|.*\.groovy|.*\.kt/ private static final Logger LOG = LoggerFactory.getLogger(this) - private static final String APP_NAME = 'micronautguide' - private static final String BASE_PACKAGE = 'example.micronaut' + private static String APP_NAME + private static String BASE_PACKAGE private static final List JDK_VERSIONS_SUPPORTED_BY_GRAALVM = [JDK_17, JDK_21] public static final String LICENSEHEADER = "LICENSEHEADER" private final ApplicationContext applicationContext - private final GuidesGenerator guidesGenerator + private final GuideGenerator guidesGenerator + private static CategoryProvider categoryProvider - GuideProjectGenerator() { - applicationContext = ApplicationContext.run() - guidesGenerator = applicationContext.getBean(GuidesGenerator) + GuideProjectGenerator(String appName, String basePackage, CategoryProvider categoryProvider) { + APP_NAME = appName + BASE_PACKAGE = basePackage + this.categoryProvider = categoryProvider + + Map properties = new HashMap<>(); + properties.put("micronaut.io.watch.paths", "classpath:*"); + + applicationContext = ApplicationContext.run(MapPropertySource.of("guide-generator", properties)) + guidesGenerator = applicationContext.findBean(GuideGenerator.class) + .orElseThrow(() -> new RuntimeException("No GuideGenerator implementation found")); } @Override @@ -87,7 +97,7 @@ class GuideProjectGenerator implements AutoCloseable { List categories = [] for (String c : config.categories) { - Category cat = Category.values().find { it.toString() == c } + Category cat = categoryProvider.findByName(c); if (cat) { categories << cat } else if (publish && !cat) { @@ -104,6 +114,7 @@ class GuideProjectGenerator implements AutoCloseable { tags: config.tags, categories: categories, publicationDate: publish ? LocalDate.parse(config.publicationDate) : null, + clouds: config.clouds, publish: publish, base: config.base, languages: config.languages ?: ['java', 'groovy', 'kotlin'], diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GuidesSection.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuidesSection.groovy similarity index 80% rename from buildSrc/src/main/groovy/io/micronaut/guides/GuidesSection.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuidesSection.groovy index 741ede66728..76c7412f5c5 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GuidesSection.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/GuidesSection.groovy @@ -1,4 +1,4 @@ -package io.micronaut.guides +package io.micronaut.guides.core import groovy.transform.CompileStatic diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/Utils.groovy b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/Utils.groovy similarity index 98% rename from buildSrc/src/main/groovy/io/micronaut/guides/Utils.groovy rename to buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/Utils.groovy index 1d92be621df..54384ed0741 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/Utils.groovy +++ b/buildSrc/micronaut-guides-core/src/main/groovy/io/micronaut/guides/core/Utils.groovy @@ -1,4 +1,4 @@ -package io.micronaut.guides +package io.micronaut.guides.core import groovy.transform.CompileStatic import io.micronaut.starter.options.JdkVersion diff --git a/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/Category.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/Category.java new file mode 100644 index 00000000000..bb8a7a3f114 --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/Category.java @@ -0,0 +1,8 @@ +package io.micronaut.guides.core; + +import io.micronaut.core.order.Ordered; + +public interface Category extends Ordered { + String getName(); + int getOrder(); +} diff --git a/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CategoryProvider.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CategoryProvider.java new file mode 100644 index 00000000000..d38727aadff --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CategoryProvider.java @@ -0,0 +1,6 @@ +package io.micronaut.guides.core; + +public interface CategoryProvider { + Category[] getAllCategories(); + Category findByName(String name); +} diff --git a/buildSrc/src/main/java/io/micronaut/guides/CopyFileVisitor.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CopyFileVisitor.java similarity index 97% rename from buildSrc/src/main/java/io/micronaut/guides/CopyFileVisitor.java rename to buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CopyFileVisitor.java index 25474cc6eb6..5c3faa80568 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/CopyFileVisitor.java +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/CopyFileVisitor.java @@ -1,4 +1,4 @@ -package io.micronaut.guides; +package io.micronaut.guides.core; import java.io.IOException; import java.nio.file.FileVisitResult; diff --git a/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultGuideGenerator.groovy b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultGuideGenerator.groovy new file mode 100644 index 00000000000..0e3dd11d7f4 --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultGuideGenerator.groovy @@ -0,0 +1,30 @@ +package io.micronaut.guides.core + +import io.micronaut.core.annotation.NonNull +import io.micronaut.core.annotation.Nullable +import io.micronaut.starter.api.TestFramework +import io.micronaut.starter.application.ApplicationType +import io.micronaut.starter.application.generator.GeneratorContext +import io.micronaut.starter.options.BuildTool +import io.micronaut.starter.options.JdkVersion +import io.micronaut.starter.options.Language +import jakarta.inject.Singleton +import jakarta.validation.constraints.NotNull +import jakarta.validation.constraints.Pattern; + +@Singleton +public class DefaultGuideGenerator implements GuideGenerator { + @Override + public void generateAppIntoDirectory( + @NonNull File directory, + @NotNull ApplicationType type, + @NotNull String packageAndName, + @Nullable String framework, + @Nullable List features, + @Nullable BuildTool buildTool, + @Nullable TestFramework testFramework, + @Nullable Language lang, + @Nullable JdkVersion javaVersion) throws IOException { + throw new UnsupportedOperationException("No custom GuideGenerator implementation found"); + } +} \ No newline at end of file diff --git a/buildSrc/src/main/java/io/micronaut/guides/DefaultPomDependencyVersionResolverReplacement.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultPomDependencyVersionResolverReplacement.java similarity index 97% rename from buildSrc/src/main/java/io/micronaut/guides/DefaultPomDependencyVersionResolverReplacement.java rename to buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultPomDependencyVersionResolverReplacement.java index 66f45822a55..2cba2554415 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/DefaultPomDependencyVersionResolverReplacement.java +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DefaultPomDependencyVersionResolverReplacement.java @@ -1,4 +1,4 @@ -package io.micronaut.guides; +package io.micronaut.guides.core; import io.micronaut.context.annotation.Replaces; import io.micronaut.core.annotation.NonNull; diff --git a/buildSrc/src/main/java/io/micronaut/guides/DependencyLines.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DependencyLines.java similarity index 99% rename from buildSrc/src/main/java/io/micronaut/guides/DependencyLines.java rename to buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DependencyLines.java index 5b10222b538..b85c6753074 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/DependencyLines.java +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/DependencyLines.java @@ -1,4 +1,4 @@ -package io.micronaut.guides; +package io.micronaut.guides.core; import io.micronaut.core.util.StringUtils; import io.micronaut.starter.options.BuildTool; diff --git a/buildSrc/src/main/java/io/micronaut/guides/GenerateGuideZip.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GenerateGuideZip.java similarity index 98% rename from buildSrc/src/main/java/io/micronaut/guides/GenerateGuideZip.java rename to buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GenerateGuideZip.java index 20ebea05c14..c5d54268368 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/GenerateGuideZip.java +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GenerateGuideZip.java @@ -1,4 +1,4 @@ -package io.micronaut.guides; +package io.micronaut.guides.core; import org.apache.commons.compress.archivers.zip.UnixStat; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; diff --git a/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuideGenerator.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuideGenerator.java new file mode 100644 index 00000000000..79c8bfeaced --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuideGenerator.java @@ -0,0 +1,45 @@ +package io.micronaut.guides.core; + +import io.micronaut.core.annotation.NonNull; +import io.micronaut.core.annotation.Nullable; +import io.micronaut.starter.api.TestFramework; +import io.micronaut.starter.application.ApplicationType; +import io.micronaut.starter.application.generator.GeneratorContext; +import io.micronaut.starter.application.generator.ProjectGenerator; +import io.micronaut.starter.options.BuildTool; +import io.micronaut.starter.options.JdkVersion; +import io.micronaut.starter.options.Language; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public interface GuideGenerator { + + /** + * Generates an application into a given directory. + * + * @param directory The directory where the generated application will be saved. + * @param type The type of application to generate. + * @param packageAndName The full name of the package and the main class of the application. + * @param framework Optional parameter specifying the testing framework to use. + * @param features Optional list of additional features to include in the application. + * @param buildTool Optional parameter specifying the build tool to use. + * @param testFramework Optional parameter specifying the testing framework to use. + * @param lang Optional parameter specifying the programming language to use. + * @param javaVersion Optional parameter specifying the version of Java to use. + * @throws IOException If there is an error while generating the application or saving it to the directory. + */ + public void generateAppIntoDirectory( + @NonNull File directory, + @NotNull ApplicationType type, + @NotNull String packageAndName, + @Nullable String framework, + @Nullable List features, + @Nullable BuildTool buildTool, + @Nullable TestFramework testFramework, + @Nullable Language lang, + @Nullable JdkVersion javaVersion) throws IOException; +} diff --git a/buildSrc/src/main/java/io/micronaut/guides/GuidesOption.java b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuidesOption.java similarity index 97% rename from buildSrc/src/main/java/io/micronaut/guides/GuidesOption.java rename to buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuidesOption.java index 019c31b11b5..fbc4e1dd64d 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/GuidesOption.java +++ b/buildSrc/micronaut-guides-core/src/main/java/io/micronaut/guides/core/GuidesOption.java @@ -1,4 +1,4 @@ -package io.micronaut.guides; +package io.micronaut.guides.core; import io.micronaut.core.annotation.Introspected; import io.micronaut.core.annotation.NonNull; diff --git a/buildSrc/micronaut-guides-core/src/test/groovy/GuideAsciidocGeneratorTest.java b/buildSrc/micronaut-guides-core/src/test/groovy/GuideAsciidocGeneratorTest.java new file mode 100644 index 00000000000..8ad3e6a926c --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/test/groovy/GuideAsciidocGeneratorTest.java @@ -0,0 +1,129 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import io.micronaut.guides.core.GuideMetadata; +import io.micronaut.guides.core.GuideAsciidocGenerator; +import io.micronaut.starter.api.TestFramework; +import io.micronaut.starter.options.JdkVersion; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class GuideAsciidocGeneratorTest { + + @TempDir + Path tempDir; + + private GuideMetadata metadata; + private File inputDir; + private File asciidocDir; + private File projectDir; + + @BeforeEach + void setUp() throws IOException { + metadata = new GuideMetadata(); + metadata.setSlug("test-guide"); + metadata.setTitle("Test Guide"); + metadata.setIntro("This is a test guide"); + metadata.setAuthors(new HashSet<>(Arrays.asList("John Doe", "Jane Smith"))); + metadata.setAsciidoctor("guide.adoc"); + + inputDir = tempDir.resolve("input").toFile(); + asciidocDir = tempDir.resolve("asciidoc").toFile(); + projectDir = tempDir.resolve("project").toFile(); + + inputDir.mkdirs(); + asciidocDir.mkdirs(); + projectDir.mkdirs(); + + // Create a sample guide.adoc file + File guideFile = new File(inputDir, "guide.adoc"); + Files.write(guideFile.toPath(), Arrays.asList( + "= Test Guide", + "", + "This is a test guide.", + "", + "include::common:license.adoc[]" + )); + + // Create a sample version.txt file + File versionFile = new File(projectDir, "version.txt"); + Files.write(versionFile.toPath(), "1.0.0".getBytes()); + } + + @Test + void testGenerate() throws IOException { + GuideAsciidocGenerator.generate(metadata, inputDir, asciidocDir, projectDir); + + // Check if the output file is created + File outputFile = new File(asciidocDir, "test-guide-gradle-java.adoc"); + assertTrue(outputFile.exists()); + + // Read the content of the generated file + List lines = Files.readAllLines(outputFile.toPath()); + + // Verify the content + assertTrue(lines.contains("= Test Guide")); + assertTrue(lines.contains("This is a test guide.")); + assertTrue(lines.contains("include::common:license.adoc[]")); + assertTrue(lines.stream().anyMatch(line -> line.contains("@micronaut@"))); + assertTrue(lines.stream().anyMatch(line -> line.contains("@authors@"))); + } + + @Test + void testGenerateWithMaximumJavaVersion() { + metadata.setMaximumJavaVersion(8); + JdkVersion currentJdk = JdkVersion.valueOf("JDK_17"); + + GuideAsciidocGenerator.generate(metadata, inputDir, asciidocDir, projectDir); + + if (currentJdk.majorVersion() > 8) { + // Check that no file is generated when current JDK is higher than maximum + File outputFile = new File(asciidocDir, "test-guide-gradle-java.adoc"); + assertFalse(outputFile.exists()); + } else { + // Check that file is generated when current JDK is not higher than maximum + File outputFile = new File(asciidocDir, "test-guide-gradle-java.adoc"); + assertTrue(outputFile.exists()); + } + } + + @Test + void testMainPath() { + String result = GuideAsciidocGenerator.mainPath("myapp", "MyClass"); + assertEquals("myapp/src/main/@lang@/example/micronaut/MyClass.@languageextension@", result); + + result = GuideAsciidocGenerator.mainPath("", "MyClass"); + assertEquals("src/main/@lang@/example/micronaut/MyClass.@languageextension@", result); + } + + @Test + void testTestPath() { + String result = GuideAsciidocGenerator.testPath("myapp", "MyClassTest", TestFramework.JUNIT); + assertEquals("myapp/src/test/@lang@/example/micronaut/MyClassTest.@languageextension@", result); + + result = GuideAsciidocGenerator.testPath("myapp", "MyClassTest", TestFramework.SPOCK); + assertEquals("myapp/src/test/@lang@/example/micronaut/MyClassSpec.@languageextension@", result); + + result = GuideAsciidocGenerator.testPath("", "MyClassTest", TestFramework.JUNIT); + assertEquals("src/test/@lang@/example/micronaut/MyClassTest.@languageextension@", result); + } + +// @Test +// void testResolveAsciidoctorLanguage() { +// assertEquals("yaml", GuideAsciidocGenerator.resolveAsciidoctorLanguage("config.yml")); +// assertEquals("yaml", GuideAsciidocGenerator.resolveAsciidoctorLanguage("config.yaml")); +// assertEquals("html", GuideAsciidocGenerator.resolveAsciidoctorLanguage("index.html")); +// assertEquals("html", GuideAsciidocGenerator.resolveAsciidoctorLanguage("template.vm")); +// assertEquals("html", GuideAsciidocGenerator.resolveAsciidoctorLanguage("template.hbs")); +// assertEquals("xml", GuideAsciidocGenerator.resolveAsciidoctorLanguage("config.xml")); +// assertEquals("java", GuideAsciidocGenerator.resolveAsciidoctorLanguage("MyClass.java")); +// } +} \ No newline at end of file diff --git a/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideMetadataTest.groovy b/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideMetadataTest.groovy new file mode 100644 index 00000000000..b209e494573 --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideMetadataTest.groovy @@ -0,0 +1,102 @@ +package io.micronaut.guides.core + +import io.micronaut.guides.core.GuideMetadata +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import io.micronaut.starter.api.TestFramework; +import io.micronaut.starter.application.ApplicationType; +import io.micronaut.starter.options.BuildTool; +import io.micronaut.starter.options.Language; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.HashSet; + +import static org.junit.jupiter.api.Assertions.*; + +class GuideMetadataTest { + + private GuideMetadata guideMetadata; + + @BeforeEach + void setUp() { + guideMetadata = new GuideMetadata(); + guideMetadata.setSlug("test-guide"); + guideMetadata.setTitle("Test Guide"); + guideMetadata.setAuthors(new HashSet<>(Arrays.asList("Author1", "Author2"))); + guideMetadata.setTags(Arrays.asList("tag1", "tag2")); + List categories = new LinkedList(); + categories.add(TestCategory.TEST_CATEGORY) + guideMetadata.setCategories(categories); + guideMetadata.setPublicationDate(LocalDate.now()); + + GuideMetadata.App app = new GuideMetadata.App(); + app.setName("TestApp"); + app.setApplicationType(ApplicationType.DEFAULT); + app.setVisibleFeatures(Arrays.asList("graalvm", "micronaut-validation")); + app.setJavaFeatures(Arrays.asList("java-feature")); + app.setKotlinFeatures(Arrays.asList("kotlin-feature")); + app.setGroovyFeatures(Arrays.asList("groovy-feature")); + + guideMetadata.setApps(Arrays.asList(app)); + } + + @Test + void testGetTags() { + List tags = guideMetadata.getTags(); + System.out.println(tags); + assertTrue(tags.containsAll(Arrays.asList("tag1", "tag2","graalvm","java-feature","kotlin-feature","groovy-feature","test-category","validation"))); + assertEquals(8, tags.size()); + } + + @Test + void testShouldSkip() { + guideMetadata.setSkipGradleTests(true); + guideMetadata.setSkipMavenTests(false); + + assertTrue(guideMetadata.shouldSkip(BuildTool.GRADLE)); + assertFalse(guideMetadata.shouldSkip(BuildTool.MAVEN)); + assertFalse(guideMetadata.shouldSkip(BuildTool.GRADLE_KOTLIN)); + } + + @Test + void testShouldSkipWithLanguage() { + GuideMetadata.Skip skip = new GuideMetadata.Skip(BuildTool.GRADLE, Language.JAVA); + guideMetadata.setSkips(new HashSet<>(Arrays.asList(skip))); + + assertTrue(guideMetadata.shouldSkip(BuildTool.GRADLE, Language.JAVA)); + assertFalse(guideMetadata.shouldSkip(BuildTool.MAVEN, Language.JAVA)); + assertFalse(guideMetadata.shouldSkip(BuildTool.GRADLE, Language.KOTLIN)); + } + + @Test + void testAppGetFeatures() { + GuideMetadata.App app = guideMetadata.getApps().get(0); + + List javaFeatures = app.getFeatures(Language.JAVA); + assertTrue(javaFeatures.containsAll(Arrays.asList("java-feature"))); + + List kotlinFeatures = app.getFeatures(Language.KOTLIN); + assertTrue(kotlinFeatures.containsAll(Arrays.asList("kotlin-feature"))); + + List groovyFeatures = app.getFeatures(Language.GROOVY); + assertTrue(groovyFeatures.containsAll(Arrays.asList("groovy-feature"))); + } + + @Test + void testAppGetVisibleFeatures() { + GuideMetadata.App app = guideMetadata.getApps().get(0); + + List javaVisibleFeatures = app.getVisibleFeatures(Language.JAVA); + assertEquals(3, javaVisibleFeatures.size()); + assertTrue(javaVisibleFeatures.containsAll(Arrays.asList("graalvm", "micronaut-validation", "java-feature"))); + + List kotlinVisibleFeatures = app.getVisibleFeatures(Language.KOTLIN); + assertEquals(3, kotlinVisibleFeatures.size()); + assertTrue(kotlinVisibleFeatures.containsAll(Arrays.asList("graalvm", "micronaut-validation", "kotlin-feature"))); + + List groovyVisibleFeatures = app.getVisibleFeatures(Language.GROOVY); + assertEquals(3, groovyVisibleFeatures.size()); + assertTrue(groovyVisibleFeatures.containsAll(Arrays.asList("graalvm", "micronaut-validation", "groovy-feature"))); + } +} \ No newline at end of file diff --git a/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideProjectGeneratorTest.groovy b/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideProjectGeneratorTest.groovy new file mode 100644 index 00000000000..b52b8fc7356 --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/test/groovy/io/micronaut/guides/core/GuideProjectGeneratorTest.groovy @@ -0,0 +1,131 @@ +package io.micronaut.guides.core + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import io.micronaut.context.ApplicationContext +import io.micronaut.starter.options.BuildTool; +import io.micronaut.starter.options.Language; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations + +import java.nio.file.Path; +import java.time.LocalDate + +class GuideProjectGeneratorTest { + + @TempDir + Path tempDir; + + @Mock + private ApplicationContext applicationContext; + + @Mock + private GuideGenerator guidesGenerator; + + @Mock + private CategoryProvider categoryProvider; + + private GuideProjectGenerator guideProjectGenerator; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + // Mock the ApplicationContext + when(applicationContext.getBean(GuideGenerator.class)).thenReturn(guidesGenerator); + + // Mock the CategoryProvider + Category mockCategory = mock(Category.class); + when(mockCategory.getName()).thenReturn("Mock Category"); + when(categoryProvider.getAllCategories()).thenReturn(new Category[]{mockCategory}); + when(categoryProvider.findByName(anyString())).thenReturn(mockCategory); + + // Create GuideProjectGenerator with the mock CategoryProvider + guideProjectGenerator = new GuideProjectGenerator("micronautguide", "example.micronaut", categoryProvider); + } + + @Test + void testParseGuideMetadata() { + File dir = tempDir.resolve("test-guide").toFile(); + dir.mkdirs(); + String metadataConfigName = "metadata.json"; + + // Create a mock JSON content + String jsonContent = "{" + + "\"title\": \"Test Guide\"," + + "\"intro\": \"This is a test guide\"," + + "\"authors\": [\"John Doe\"]," + + "\"tags\": [\"test\"]," + + "\"categories\": [\"CLOUD\"]," + + "\"publicationDate\": \"2023-09-25\"," + + "\"publish\": true" + + "}"; + + // Create a real file with the JSON content + File configFile = new File(dir, metadataConfigName); + try (FileWriter writer = new FileWriter(configFile)) { + writer.write(jsonContent); + } + + // Use Mockito's spy to allow some real method calls + File spyConfigFile = spy(configFile); + + // Call the method + Optional result = GuideProjectGenerator.parseGuideMetadata(dir, metadataConfigName); + + // Verify the result + assertTrue(result.isPresent()); + GuideMetadata metadata = result.get(); + + assertEquals("test-guide.adoc", metadata.getAsciidoctor()); + assertEquals("test-guide", metadata.getSlug()); + assertEquals("Test Guide", metadata.getTitle()); + assertEquals("This is a test guide", metadata.getIntro()); + assertEquals(Collections.singleton("John Doe"), metadata.getAuthors()); + assertEquals(Arrays.asList("test","mock-category"), metadata.getTags()); + assertEquals(LocalDate.parse("2023-09-25"), metadata.getPublicationDate()); + assertTrue(metadata.isPublish()); + } + + @Test + void testGuidesOptions() { + GuideMetadata metadata = new GuideMetadata(); + metadata.setBuildTools(Arrays.asList("gradle", "maven")); + metadata.setLanguages(Arrays.asList("java", "kotlin")); + metadata.setTestFramework("junit"); + metadata.skips = new HashSet<>(); + + List options = GuideProjectGenerator.guidesOptions(metadata); + + assertEquals(4, options.size()); + assertTrue(options.stream().anyMatch(o -> o.getBuildTool() == BuildTool.GRADLE && o.getLanguage() == Language.JAVA)); + assertTrue(options.stream().anyMatch(o -> o.getBuildTool() == BuildTool.GRADLE && o.getLanguage() == Language.KOTLIN)); + assertTrue(options.stream().anyMatch(o -> o.getBuildTool() == BuildTool.MAVEN && o.getLanguage() == Language.JAVA)); + assertTrue(options.stream().anyMatch(o -> o.getBuildTool() == BuildTool.MAVEN && o.getLanguage() == Language.KOTLIN)); + } + + @Test + void testDeleteEveryFileButSources(@TempDir Path tempDir) throws Exception { + // Create some test files + File javaFile = tempDir.resolve("Test.java").toFile(); + File groovyFile = tempDir.resolve("Test.groovy").toFile(); + File kotlinFile = tempDir.resolve("Test.kt").toFile(); + File textFile = tempDir.resolve("test.txt").toFile(); + + javaFile.createNewFile(); + groovyFile.createNewFile(); + kotlinFile.createNewFile(); + textFile.createNewFile(); + + GuideProjectGenerator.deleteEveryFileButSources(tempDir.toFile()); + + assertTrue(javaFile.exists()); + assertTrue(groovyFile.exists()); + assertTrue(kotlinFile.exists()); + assertFalse(textFile.exists()); + } +} \ No newline at end of file diff --git a/buildSrc/micronaut-guides-core/src/test/java/io/micronaut/guides/core/TestCategory.java b/buildSrc/micronaut-guides-core/src/test/java/io/micronaut/guides/core/TestCategory.java new file mode 100644 index 00000000000..1902f9716e3 --- /dev/null +++ b/buildSrc/micronaut-guides-core/src/test/java/io/micronaut/guides/core/TestCategory.java @@ -0,0 +1,27 @@ +package io.micronaut.guides.core; + +import io.micronaut.starter.feature.function.Cloud; + +public enum TestCategory implements Category { + TEST_CATEGORY("Test Category", 1), + CLOUD("Cloud",2); + + private final String name; + private final int order; + + TestCategory(String name, int order) { + this.name = name; + this.order = order; + } + + + @Override + public String getName() { + return name; + } + + @Override + public int getOrder() { + return order; + } +} diff --git a/buildSrc/settings.gradle b/buildSrc/settings.gradle new file mode 100644 index 00000000000..72ce50a76cc --- /dev/null +++ b/buildSrc/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'buildSrc' +include 'micronaut-guides-core' \ No newline at end of file diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/GuidesPlugin.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/GuidesPlugin.groovy index 52510d69343..d7d0b3702a3 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/GuidesPlugin.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/GuidesPlugin.groovy @@ -2,7 +2,11 @@ package io.micronaut.guides import groovy.transform.CompileStatic import io.micronaut.core.util.CollectionUtils -import io.micronaut.guides.GuideMetadata.App +import io.micronaut.guides.core.GuideMetadata +import io.micronaut.guides.core.GuideMetadata.App +import io.micronaut.guides.core.GuideProjectGenerator +import io.micronaut.guides.core.GuidesOption +import io.micronaut.guides.core.Utils import io.micronaut.guides.tasks.AsciidocGenerationTask import io.micronaut.guides.tasks.GuidesIndexGradleTask import io.micronaut.guides.tasks.SampleProjectGenerationTask @@ -27,7 +31,7 @@ import java.nio.file.Paths import java.util.function.Predicate import java.util.stream.Collectors -import static io.micronaut.guides.GuideProjectGenerator.DEFAULT_APP_NAME +import static io.micronaut.guides.core.GuideProjectGenerator.DEFAULT_APP_NAME import static io.micronaut.starter.options.BuildTool.MAVEN @CompileStatic @@ -49,7 +53,7 @@ class GuidesPlugin implements Plugin { @Override void apply(Project project) { - GuideProjectGenerator projectGenerator = new GuideProjectGenerator() + GuideProjectGenerator projectGenerator = new GuideProjectGenerator('micronautguide','example.micronaut', new MicronautCategory.MicronautCategoryProvider()) Directory guidesDir = project.layout.projectDirectory.dir("guides") Provider codeDir = project.layout.buildDirectory.dir("code") Properties testProps = guidesDir.file("tests.properties").asFile.withInputStream { inputStream -> diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/IndexGenerator.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/IndexGenerator.groovy index d0cb835d6f2..7d07d35f70c 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/IndexGenerator.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/IndexGenerator.groovy @@ -5,6 +5,12 @@ import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import io.micronaut.core.order.OrderUtil import io.micronaut.core.order.Ordered +import io.micronaut.guides.core.Category +import io.micronaut.guides.core.GuideMetadata +import io.micronaut.guides.core.GuideProjectGenerator +import io.micronaut.guides.core.GuidesOption +import io.micronaut.guides.core.GuidesSection +import io.micronaut.guides.core.Utils import io.micronaut.starter.options.BuildTool import io.micronaut.starter.options.Language @@ -47,7 +53,8 @@ class IndexGenerator { [new GuidesSection(category: tag.title, metadatas: tagMetadatas)], tag.title) } - Ordered[] categories = Category.values() + MicronautCategory.MicronautCategoryProvider categoryProvider = new MicronautCategory.MicronautCategoryProvider(); + Ordered[] categories = categoryProvider.getAllCategories(); OrderUtil.sort(categories) List sections = [] for (Ordered obj : categories) { @@ -329,52 +336,52 @@ class IndexGenerator { if (obj instanceof Category) { Category cat = (Category) obj switch (cat) { - case Category.GCP: + case MicronautCategory.GCP: return 'https://micronaut.io/wp-content/uploads/2021/02/Googlecloud.svg' - case Category.AWS: + case MicronautCategory.AWS: return 'https://micronaut.io/wp-content/uploads/2020/12/aws.svg' - case Category.AZURE: + case MicronautCategory.AZURE: return 'https://micronaut.io/wp-content/uploads/2020/12/Azure.svg' - case Category.CACHE: + case MicronautCategory.CACHE: return 'https://micronaut.io/wp-content/uploads/2020/12/cache.svg' - case Category.DATA_ACCESS: + case MicronautCategory.DATA_ACCESS: return 'https://micronaut.io/wp-content/uploads/2020/11/dataaccess.svg' - case Category.SERVICE_DISCOVERY: + case MicronautCategory.SERVICE_DISCOVERY: return 'https://micronaut.io/wp-content/uploads/2020/12/Service_Discovery.svg' - case Category.SECURITY: - case Category.AUTHORIZATION_CODE: - case Category.CLIENT_CREDENTIALS: - case Category.SECRETS_MANAGER: + case MicronautCategory.SECURITY: + case MicronautCategory.AUTHORIZATION_CODE: + case MicronautCategory.CLIENT_CREDENTIALS: + case MicronautCategory.SECRETS_MANAGER: return 'https://micronaut.io/wp-content/uploads/2020/12/Security.svg' - case Category.MESSAGING: + case MicronautCategory.MESSAGING: return 'https://micronaut.io/wp-content/uploads/2020/11/Messaging.svg' - case Category.DISTRIBUTED_TRACING: + case MicronautCategory.DISTRIBUTED_TRACING: return 'https://micronaut.io/wp-content/uploads/2020/12/Distributed_Tracing.svg' - case Category.GETTING_STARTED: + case MicronautCategory.GETTING_STARTED: return 'https://micronaut.io/wp-content/uploads/2020/11/Misc.svg' - case Category.ORACLE_CLOUD: + case MicronautCategory.ORACLE_CLOUD: return 'https://micronaut.io/wp-content/uploads/2021/05/Oracle-1.svg' - case Category.API: + case MicronautCategory.API: return 'https://micronaut.io/wp-content/uploads/2020/11/API.svg' - case Category.EMAIL: + case MicronautCategory.EMAIL: return 'https://micronaut.io/wp-content/uploads/2022/02/email.svg' - case Category.TEST: + case MicronautCategory.TEST: return 'https://micronaut.io/wp-content/uploads/2020/11/Build.svg' - case Category.KOTLIN: + case MicronautCategory.KOTLIN: return 'https://micronaut.io/wp-content/uploads/2021/05/Kotlin.svg' default: @@ -386,7 +393,7 @@ class IndexGenerator { private static String category(Object cat) { String h1 = cat instanceof Category ? - '' + cat.toString() + '' : + '' + cat.toString() + '' : cat.toString().replace("_", " ") '
' + '
' + diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/TestScriptGenerator.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/TestScriptGenerator.groovy index ef1aced09b4..6a046ef61c9 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/TestScriptGenerator.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/TestScriptGenerator.groovy @@ -1,8 +1,14 @@ package io.micronaut.guides import groovy.transform.CompileStatic +import io.micronaut.guides.core.GuideMetadata +import io.micronaut.guides.core.GuideMetadata.App +import io.micronaut.guides.core.GuideProjectGenerator +import io.micronaut.guides.core.GuidesOption +import io.micronaut.guides.core.Utils + import java.util.stream.Collectors -import static io.micronaut.guides.GuideProjectGenerator.DEFAULT_APP_NAME +import static io.micronaut.guides.core.GuideProjectGenerator.DEFAULT_APP_NAME import static io.micronaut.starter.options.BuildTool.GRADLE import static io.micronaut.starter.options.BuildTool.MAVEN import io.micronaut.starter.api.TestFramework @@ -98,7 +104,7 @@ exit 0 testScript.executable = true } - static boolean supportsNativeTest(GuideMetadata.App app, GuidesOption guidesOption) { + static boolean supportsNativeTest(App app, GuidesOption guidesOption) { isMicronautFramework(app) && guidesOption.buildTool.isGradle() && // right now we don't support Maven native tests without adding a profile supportsNativeTest(guidesOption.language) && @@ -106,7 +112,7 @@ exit 0 } - static boolean isMicronautFramework(GuideMetadata.App app) { + static boolean isMicronautFramework(App app) { !app.framework || app.framework == "Micronaut" } @@ -156,7 +162,7 @@ kill_kotlin_daemon () { bashScript << """\ cd $folder """ - for (GuideMetadata.App app : metadata.apps) { + for (App app : metadata.apps) { if (metadata.shouldSkip(buildTool)) { continue } diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/ThemeProcessor.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/ThemeProcessor.groovy index 9658cdf0860..0c34afb3dec 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/ThemeProcessor.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/ThemeProcessor.groovy @@ -1,5 +1,10 @@ package io.micronaut.guides +import io.micronaut.guides.core.GuideMetadata +import io.micronaut.guides.core.GuideProjectGenerator +import io.micronaut.guides.core.GuidesOption +import io.micronaut.guides.core.Utils + import java.util.regex.Pattern class ThemeProcessor { diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/AsciidocGenerationTask.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/AsciidocGenerationTask.groovy index 46478d491a1..21a3e9c85dd 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/AsciidocGenerationTask.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/AsciidocGenerationTask.groovy @@ -1,8 +1,8 @@ package io.micronaut.guides.tasks import groovy.transform.CompileStatic -import io.micronaut.guides.GuideAsciidocGenerator -import io.micronaut.guides.GuideMetadata +import io.micronaut.guides.core.GuideAsciidocGenerator +import io.micronaut.guides.core.GuideMetadata import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/GuidesIndexGradleTask.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/GuidesIndexGradleTask.groovy index 072132fa953..55b475448b6 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/GuidesIndexGradleTask.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/GuidesIndexGradleTask.groovy @@ -1,7 +1,7 @@ package io.micronaut.guides.tasks import groovy.transform.CompileStatic -import io.micronaut.guides.GuideMetadata +import io.micronaut.guides.core.GuideMetadata import io.micronaut.guides.IndexGenerator import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/NativeTestScriptTask.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/NativeTestScriptTask.groovy index d3420c0f1a8..ec58d7d5914 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/NativeTestScriptTask.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/NativeTestScriptTask.groovy @@ -1,7 +1,7 @@ package io.micronaut.guides.tasks import groovy.transform.CompileStatic -import io.micronaut.guides.GuideMetadata +import io.micronaut.guides.core.GuideMetadata import io.micronaut.guides.TestScriptGenerator import org.gradle.api.DefaultTask import org.gradle.api.file.RegularFileProperty diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/SampleProjectGenerationTask.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/SampleProjectGenerationTask.groovy index 5112adcb93f..457280fa3b6 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/SampleProjectGenerationTask.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/SampleProjectGenerationTask.groovy @@ -1,8 +1,8 @@ package io.micronaut.guides.tasks import groovy.transform.CompileStatic -import io.micronaut.guides.GuideMetadata -import io.micronaut.guides.GuideProjectGenerator +import io.micronaut.guides.core.GuideMetadata +import io.micronaut.guides.core.GuideProjectGenerator import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property diff --git a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/TestScriptTask.groovy b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/TestScriptTask.groovy index 88baa61aea2..3b2ea1ca3f1 100644 --- a/buildSrc/src/main/groovy/io/micronaut/guides/tasks/TestScriptTask.groovy +++ b/buildSrc/src/main/groovy/io/micronaut/guides/tasks/TestScriptTask.groovy @@ -1,7 +1,7 @@ package io.micronaut.guides.tasks import groovy.transform.CompileStatic -import io.micronaut.guides.GuideMetadata +import io.micronaut.guides.core.GuideMetadata import io.micronaut.guides.TestScriptGenerator import org.gradle.api.DefaultTask import org.gradle.api.file.RegularFileProperty diff --git a/buildSrc/src/main/java/io/micronaut/guides/Category.java b/buildSrc/src/main/java/io/micronaut/guides/MicronautCategory.java similarity index 66% rename from buildSrc/src/main/java/io/micronaut/guides/Category.java rename to buildSrc/src/main/java/io/micronaut/guides/MicronautCategory.java index d03f8d06682..90b76db9840 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/Category.java +++ b/buildSrc/src/main/java/io/micronaut/guides/MicronautCategory.java @@ -1,8 +1,12 @@ package io.micronaut.guides; -import io.micronaut.core.order.Ordered; +import groovy.lang.Singleton; +import io.micronaut.guides.core.Category; +import io.micronaut.guides.core.CategoryProvider; -public enum Category implements Ordered { +import java.util.Arrays; + +public enum MicronautCategory implements Category { GETTING_STARTED("Getting Started", 1), BEYOND_THE_BASICS("Beyond the Basics", 2), DISTRIBUTION("Distribution", 3), @@ -44,21 +48,37 @@ public enum Category implements Ordered { SPRING_BOOT_TO_MICRONAUT("Spring Boot to Micronaut Framework", 38), BUILDING_A_REST_API("Building a REST API - Spring Boot to Micronaut Framework", 39); - private final String val; + private final String name; private final int order; - Category(String val, int order) { - this.val = val; + MicronautCategory(String name, int order) { + this.name = name; this.order = order; } @Override - public String toString() { - return val; + public String getName() { + return name; } @Override public int getOrder() { return order; } + + @Singleton + public static class MicronautCategoryProvider implements CategoryProvider { + @Override + public Category[] getAllCategories() { + return MicronautCategory.values(); + } + + @Override + public Category findByName(String name) { + return Arrays.stream(MicronautCategory.values()) + .filter(c -> c.getName().equals(name)) + .findFirst() + .orElse(null); + } + } } diff --git a/buildSrc/src/main/java/io/micronaut/guides/GuidesGenerator.java b/buildSrc/src/main/java/io/micronaut/guides/MicronautGuideGenerator.java similarity index 57% rename from buildSrc/src/main/java/io/micronaut/guides/GuidesGenerator.java rename to buildSrc/src/main/java/io/micronaut/guides/MicronautGuideGenerator.java index b87b2623fcc..ed90a18783f 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/GuidesGenerator.java +++ b/buildSrc/src/main/java/io/micronaut/guides/MicronautGuideGenerator.java @@ -2,6 +2,7 @@ import io.micronaut.core.annotation.NonNull; import io.micronaut.core.annotation.Nullable; +import io.micronaut.guides.core.GuideGenerator; import io.micronaut.http.exceptions.HttpStatusException; import io.micronaut.starter.api.TestFramework; import io.micronaut.starter.application.ApplicationType; @@ -30,16 +31,42 @@ import static io.micronaut.starter.options.BuildTool.GRADLE; import static io.micronaut.starter.options.JdkVersion.JDK_8; +/** + * The GuidesGenerator class generates Micronaut applications based on provided parameters and saves them to a specified directory. + */ @Singleton -public class GuidesGenerator { - private static final Logger LOG = LoggerFactory.getLogger(GuidesGenerator.class); +public class MicronautGuideGenerator implements GuideGenerator { + private static final Logger LOG = LoggerFactory.getLogger(MicronautGuideGenerator.class); + /** + * An instance of the ProjectGenerator interface responsible for creating projects. + */ private final ProjectGenerator projectGenerator; - public GuidesGenerator(ProjectGenerator projectGenerator) { + /** + * Constructor for GuidesGenerator. + * + * @param projectGenerator An instance of the ProjectGenerator interface. + */ + public MicronautGuideGenerator(ProjectGenerator projectGenerator) { this.projectGenerator = projectGenerator; } + /** + * Generates an application into a given directory. + * + * @param directory The directory where the generated application will be saved. + * @param type The type of application to generate. + * @param packageAndName The full name of the package and the main class of the application. + * @param framework Optional parameter specifying the testing framework to use. + * @param features Optional list of additional features to include in the application. + * @param buildTool Optional parameter specifying the build tool to use. + * @param testFramework Optional parameter specifying the testing framework to use. + * @param lang Optional parameter specifying the programming language to use. + * @param javaVersion Optional parameter specifying the version of Java to use. + * @throws IOException If there is an error while generating the application or saving it to the directory. + */ + @Override public void generateAppIntoDirectory( @NonNull File directory, @NotNull ApplicationType type, @@ -62,6 +89,20 @@ public void generateAppIntoDirectory( } } + /** + * Creates a GeneratorContext object containing all necessary information about the application being generated. + * + * @param type The type of application to generate. + * @param packageAndName The full name of the package and the main class of the application. + * @param framework Optional parameter specifying the testing framework to use. + * @param features Optional list of additional features to include in the application. + * @param buildTool Optional parameter specifying the build tool to use. + * @param testFramework Optional parameter specifying the testing framework to use. + * @param lang Optional parameter specifying the programming language to use. + * @param javaVersion Optional parameter specifying the version of Java to use. + * @return A GeneratorContext object populated with the provided parameters. + * @throws IllegalArgumentException If the provided packageAndName does not conform to the expected format. + */ GeneratorContext createProjectGeneratorContext( ApplicationType type, @Pattern(regexp = "[\\w\\d-_\\.]+") String packageAndName, diff --git a/buildSrc/version.txt b/buildSrc/version.txt new file mode 100644 index 00000000000..c78c4964cac --- /dev/null +++ b/buildSrc/version.txt @@ -0,0 +1 @@ +4.6.2 diff --git a/guide_schema.json b/guide_schema.json new file mode 100644 index 00000000000..37512a8453e --- /dev/null +++ b/guide_schema.json @@ -0,0 +1,166 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "minimumJavaVersion": { + "type": "integer" + }, + "prerequesites": { + "type": "array", + "items": { + "type": "string" + } + }, + "clouds": { + "type": "array", + "items": { + "type": "string" + } + }, + "skipGradleTests": { + "type": "boolean" + }, + "intro": { + "type": "string" + }, + "skipMavenTests": { + "type": "boolean" + }, + "features": { + "type": "array", + "items": { + "type": "string" + } + }, + "publicationDate": { + "type": "string" + }, + "asciidoctor": { + "type": "string" + }, + "languages": { + "type": "array", + "items": { + "type": "string" + } + }, + "tags": { + "type": "array", + "items": { + "type": "string" + } + }, + "buildTools": { + "type": "array", + "items": { + "type": "string" + } + }, + "testFramework": { + "type": "string" + }, + "zipIncludes": { + "type": "array", + "items": { + "type": "string" + } + }, + "categories": { + "type": "array", + "items": { + "type": "string" + } + }, + "slug": { + "type": "string" + }, + "publish": { + "type": "boolean" + }, + "title": { + "type": "string" + }, + "authors": { + "type": "array", + "items": { + "type": "string" + } + }, + "maximumJavaVersion": { + "type": "integer" + }, + "base": { + "type": "string" + }, + "env": { + "type": "object", + "properties": { + "AWS_SECRET_ACCESS_KEY": { + "type": "string" + }, + "AWS_REGION": { + "type": "string" + }, + "AWS_ACCESS_KEY_ID": { + "type": "string" + } + } + }, + "apps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "invisibleFeatures": { + "type": "array", + "items": { + "type": "string" + } + }, + "kotlinFeatures": { + "type": "array", + "items": { + "type": "string" + } + }, + "validateLicense": { + "type": "boolean" + }, + "testFramework": { + "type": "string" + }, + "javaFeatures": { + "type": "array", + "items": { + "type": "string" + } + }, + "framework": { + "type": "string" + }, + "name": { + "type": "string" + }, + "features": { + "type": "array", + "items": { + "type": "string" + } + }, + "packageName": { + "type": "string" + }, + "excludeTest": { + "type": "array", + "items": { + "type": "string" + } + }, + "applicationType": { + "type": "string" + } + } + } + } + } +} \ No newline at end of file