Search in sources :

Example 1 with BeanIntrospection

use of io.micronaut.core.beans.BeanIntrospection in project micronaut-starter by micronaut-projects.

the class CodeGenConfig method load.

public static CodeGenConfig load(BeanContext beanContext, File directory, ConsoleOutput consoleOutput) {
    File micronautCli = new File(directory, "micronaut-cli.yml");
    if (micronautCli.exists()) {
        try (InputStream inputStream = Files.newInputStream(micronautCli.toPath())) {
            Yaml yaml = new Yaml();
            Map<String, Object> map = new LinkedHashMap<>();
            Iterable<Object> objects = yaml.loadAll(inputStream);
            Iterator<Object> i = objects.iterator();
            if (i.hasNext()) {
                while (i.hasNext()) {
                    Object object = i.next();
                    if (object instanceof Map) {
                        map.putAll((Map) object);
                    }
                }
            }
            BeanIntrospection<CodeGenConfig> introspection = BeanIntrospection.getIntrospection(CodeGenConfig.class);
            CodeGenConfig codeGenConfig = introspection.instantiate();
            introspection.getBeanProperties().forEach(bp -> {
                Object value = map.get(bp.getName());
                if (value != null) {
                    bp.convertAndSet(codeGenConfig, value);
                }
            });
            if (map.containsKey("profile")) {
                codeGenConfig.legacy = true;
                String profile = map.get("profile").toString();
                if (profile.equals("service")) {
                    codeGenConfig.setApplicationType(ApplicationType.DEFAULT);
                } else if (profile.equals("cli")) {
                    codeGenConfig.setApplicationType(ApplicationType.CLI);
                } else if (profile.equals("function-aws") || profile.equals("function-aws-alexa")) {
                    codeGenConfig.setApplicationType(ApplicationType.FUNCTION);
                } else if (profile.equals("grpc")) {
                    codeGenConfig.setApplicationType(ApplicationType.GRPC);
                } else if (profile.equals("kafka") || profile.equals("rabbitmq")) {
                    codeGenConfig.setApplicationType(ApplicationType.MESSAGING);
                } else {
                    return null;
                }
                AvailableFeatures availableFeatures = beanContext.getBean(AvailableFeatures.class, Qualifiers.byName(codeGenConfig.getApplicationType().getName()));
                if (new File(directory, "build.gradle").exists()) {
                    codeGenConfig.setBuildTool(BuildTool.GRADLE);
                } else if (new File(directory, "build.gradle.kts").exists()) {
                    codeGenConfig.setBuildTool(BuildTool.GRADLE_KOTLIN);
                } else if (new File(directory, "pom.xml").exists()) {
                    codeGenConfig.setBuildTool(BuildTool.MAVEN);
                } else {
                    return null;
                }
                codeGenConfig.setFeatures(availableFeatures.getAllFeatures().filter(f -> f instanceof DefaultFeature).map(DefaultFeature.class::cast).filter(f -> f.shouldApply(codeGenConfig.getApplicationType(), new Options(codeGenConfig.getSourceLanguage(), codeGenConfig.getTestFramework(), codeGenConfig.getBuildTool(), VersionInfo.getJavaVersion()), new HashSet<>())).map(Feature::getName).collect(Collectors.toList()));
                consoleOutput.warning("This project is using Micronaut CLI v2 but is still using the v1 micronaut-cli.yml format");
                consoleOutput.warning("To replace the configuration with the new format, run `mn update-cli-config`");
            }
            return codeGenConfig;
        } catch (IOException e) {
        }
    }
    return null;
}
Also used : Options(io.micronaut.starter.options.Options) VersionInfo(io.micronaut.starter.util.VersionInfo) java.util(java.util) BuildTool(io.micronaut.starter.options.BuildTool) Files(java.nio.file.Files) BeanContext(io.micronaut.context.BeanContext) ApplicationType(io.micronaut.starter.application.ApplicationType) FileSystemOutputHandler(io.micronaut.starter.io.FileSystemOutputHandler) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) TestFramework(io.micronaut.starter.options.TestFramework) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) Introspected(io.micronaut.core.annotation.Introspected) ConsoleOutput(io.micronaut.starter.io.ConsoleOutput) io.micronaut.starter.feature(io.micronaut.starter.feature) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Language(io.micronaut.starter.options.Language) InputStream(java.io.InputStream) Options(io.micronaut.starter.options.Options) InputStream(java.io.InputStream) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) File(java.io.File)

Aggregations

BeanContext (io.micronaut.context.BeanContext)1 Introspected (io.micronaut.core.annotation.Introspected)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 ApplicationType (io.micronaut.starter.application.ApplicationType)1 io.micronaut.starter.feature (io.micronaut.starter.feature)1 ConsoleOutput (io.micronaut.starter.io.ConsoleOutput)1 FileSystemOutputHandler (io.micronaut.starter.io.FileSystemOutputHandler)1 BuildTool (io.micronaut.starter.options.BuildTool)1 Language (io.micronaut.starter.options.Language)1 Options (io.micronaut.starter.options.Options)1 TestFramework (io.micronaut.starter.options.TestFramework)1 VersionInfo (io.micronaut.starter.util.VersionInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Yaml (org.yaml.snakeyaml.Yaml)1