Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: extract guide generation core code #1490

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
46 changes: 46 additions & 0 deletions buildSrc/micronaut-guides-core/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions buildSrc/micronaut-guides-core/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'micronaut-guides-core'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides
package io.micronaut.guides.core

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.micronaut.guides
package io.micronaut.guides.core

import com.fizzed.rocker.Rocker
import groovy.transform.CompileStatic
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -23,6 +25,7 @@ class GuideMetadata {
List<String> tags
List<Category> categories
LocalDate publicationDate
List<String> clouds

boolean publish
String base
Expand Down Expand Up @@ -60,7 +63,7 @@ class GuideMetadata {
}
}
}
Set<String> categoriesAsTags = this.categories.collect { cat -> cat.name().toLowerCase() } as Set
Set<String> categoriesAsTags = this.categories.collect { cat -> cat.getName().toLowerCase().replace(" ","-") } as Set
tagsList.addAll(categoriesAsTags)
tagsList as List<String>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<JdkVersion> 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<String, Object> 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
Expand Down Expand Up @@ -87,7 +97,7 @@ class GuideProjectGenerator implements AutoCloseable {

List<Category> 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) {
Expand All @@ -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'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides
package io.micronaut.guides.core

import groovy.transform.CompileStatic

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides
package io.micronaut.guides.core

import groovy.transform.CompileStatic
import io.micronaut.starter.options.JdkVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.micronaut.guides.core;

import io.micronaut.core.order.Ordered;

public interface Category extends Ordered {
String getName();
int getOrder();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.micronaut.guides.core;

public interface CategoryProvider {
Category[] getAllCategories();
Category findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides;
package io.micronaut.guides.core;

import java.io.IOException;
import java.nio.file.FileVisitResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> features,
@Nullable BuildTool buildTool,
@Nullable TestFramework testFramework,
@Nullable Language lang,
@Nullable JdkVersion javaVersion) throws IOException {
throw new UnsupportedOperationException("No custom GuideGenerator implementation found");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> features,
@Nullable BuildTool buildTool,
@Nullable TestFramework testFramework,
@Nullable Language lang,
@Nullable JdkVersion javaVersion) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading