Search in sources :

Example 1 with Options

use of io.micronaut.starter.options.Options 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)

Example 2 with Options

use of io.micronaut.starter.options.Options in project micronaut-starter by micronaut-projects.

the class FeatureDiffCommand method call.

@Override
public Integer call() throws Exception {
    String appName = FileSystemOutputHandler.getDefaultBaseDirectory().getName();
    Project project = NameUtils.parse(config.getDefaultPackage() + "." + appName);
    Options options = new Options(config.getSourceLanguage(), config.getTestFramework(), config.getBuildTool());
    ApplicationType applicationType = config.getApplicationType();
    List<String> features = this.features;
    ProjectGenerator projectGenerator = this.projectGenerator;
    featureDiffer.produceDiff(projectGenerator, project, applicationType, options, getOperatingSystem(), features, this);
    return 0;
}
Also used : Project(io.micronaut.starter.application.Project) Options(io.micronaut.starter.options.Options) ApplicationType(io.micronaut.starter.application.ApplicationType) ProjectGenerator(io.micronaut.starter.application.generator.ProjectGenerator)

Example 3 with Options

use of io.micronaut.starter.options.Options in project micronaut-starter by micronaut-projects.

the class AbstractCreateController method createProjectGeneratorContext.

public GeneratorContext createProjectGeneratorContext(ApplicationType type, @Pattern(regexp = "[\\w\\d-_\\.]+") String name, @Nullable List<String> features, @Nullable BuildTool buildTool, @Nullable TestFramework testFramework, @Nullable Language lang, @Nullable JdkVersion javaVersion, @Nullable @Header(HttpHeaders.USER_AGENT) String userAgent) {
    Project project;
    try {
        project = NameUtils.parse(name);
    } catch (IllegalArgumentException e) {
        throw new HttpStatusException(HttpStatus.BAD_REQUEST, "Invalid project name: " + e.getMessage());
    }
    GeneratorContext generatorContext;
    try {
        generatorContext = projectGenerator.createGeneratorContext(type, project, new Options(lang, testFramework != null ? testFramework.toTestFramework() : null, buildTool == null ? BuildTool.GRADLE : buildTool, javaVersion != null ? javaVersion : JdkVersion.JDK_8), getOperatingSystem(userAgent), features != null ? features : Collections.emptyList(), ConsoleOutput.NOOP);
        try {
            eventPublisher.publishEvent(new ApplicationGeneratingEvent(generatorContext));
        } catch (Exception e) {
            LOG.warn("Error firing application generated event: " + e.getMessage(), e);
        }
    } catch (IllegalArgumentException e) {
        throw new HttpStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
    }
    return generatorContext;
}
Also used : Project(io.micronaut.starter.application.Project) Options(io.micronaut.starter.options.Options) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) GeneratorContext(io.micronaut.starter.application.generator.GeneratorContext) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) ApplicationGeneratingEvent(io.micronaut.starter.api.event.ApplicationGeneratingEvent)

Example 4 with Options

use of io.micronaut.starter.options.Options in project micronaut-starter by micronaut-projects.

the class PreviewController method previewApp.

/**
 * Previews the contents of a generated application..
 * @param type The application type The application type
 * @param name The name of the application The name of the application
 * @param features The features The chosen features
 * @param build The build type (optional, defaults to Gradle)
 * @param test The test framework (optional, defaults to JUnit)
 * @param lang The language (optional, defaults to Java)
 * @return A preview of the application contents.
 */
@Get(uri = "/{type}/{name}{?features,lang,build,test,javaVersion}", produces = MediaType.APPLICATION_JSON)
@Override
public PreviewDTO previewApp(ApplicationType type, String name, @Nullable List<String> features, @Nullable BuildTool build, @Nullable TestFramework test, @Nullable Language lang, @Nullable JdkVersion javaVersion, @Parameter(hidden = true) RequestInfo requestInfo) throws IOException {
    try {
        Project project = NameUtils.parse(name);
        MapOutputHandler outputHandler = new MapOutputHandler();
        projectGenerator.generate(type, project, new Options(lang, test != null ? test.toTestFramework() : null, build == null ? BuildTool.GRADLE : build, javaVersion == null ? JdkVersion.JDK_8 : javaVersion), getOperatingSystem(requestInfo.getUserAgent()), features == null ? Collections.emptyList() : features, outputHandler, ConsoleOutput.NOOP);
        Map<String, String> contents = outputHandler.getProject();
        PreviewDTO previewDTO = new PreviewDTO(contents);
        previewDTO.addLink(Relationship.CREATE, requestInfo.link(Relationship.CREATE, type));
        previewDTO.addLink(Relationship.SELF, requestInfo.self());
        return previewDTO;
    } catch (IllegalArgumentException e) {
        throw new HttpStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        LOG.error("Error generating application: " + e.getMessage(), e);
        throw new IOException(e.getMessage(), e);
    }
}
Also used : Project(io.micronaut.starter.application.Project) Options(io.micronaut.starter.options.Options) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) IOException(java.io.IOException) MapOutputHandler(io.micronaut.starter.io.MapOutputHandler) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) IOException(java.io.IOException) Get(io.micronaut.http.annotation.Get)

Example 5 with Options

use of io.micronaut.starter.options.Options in project micronaut-starter by micronaut-projects.

the class ContextFactory method createFeatureContext.

public FeatureContext createFeatureContext(AvailableFeatures availableFeatures, List<String> selectedFeatures, ApplicationType applicationType, Options options, @Nullable OperatingSystem operatingSystem) {
    final Set<Feature> features = Collections.newSetFromMap(new IdentityHashMap<>(8));
    for (String name : selectedFeatures) {
        Feature feature = availableFeatures.findFeature(name).orElse(null);
        if (feature != null) {
            features.add(feature);
        } else {
            throw new IllegalArgumentException("The requested feature does not exist: " + name);
        }
    }
    Language language = determineLanguage(options.getLanguage(), features);
    Options newOptions = options.withLanguage(language);
    availableFeatures.getAllFeatures().filter(f -> f instanceof DefaultFeature).filter(f -> ((DefaultFeature) f).shouldApply(applicationType, newOptions, features)).forEach(features::add);
    featureValidator.validatePreProcessing(newOptions, applicationType, features);
    return new FeatureContext(newOptions, applicationType, operatingSystem, features);
}
Also used : FeatureValidator(io.micronaut.starter.feature.validation.FeatureValidator) Options(io.micronaut.starter.options.Options) FeatureContext(io.micronaut.starter.feature.FeatureContext) java.util(java.util) ConsoleOutput(io.micronaut.starter.io.ConsoleOutput) DefaultFeature(io.micronaut.starter.feature.DefaultFeature) Feature(io.micronaut.starter.feature.Feature) Nullable(edu.umd.cs.findbugs.annotations.Nullable) AvailableFeatures(io.micronaut.starter.feature.AvailableFeatures) GeneratorContext(io.micronaut.starter.application.generator.GeneratorContext) Language(io.micronaut.starter.options.Language) Singleton(javax.inject.Singleton) Options(io.micronaut.starter.options.Options) Language(io.micronaut.starter.options.Language) DefaultFeature(io.micronaut.starter.feature.DefaultFeature) DefaultFeature(io.micronaut.starter.feature.DefaultFeature) Feature(io.micronaut.starter.feature.Feature) FeatureContext(io.micronaut.starter.feature.FeatureContext)

Aggregations

Options (io.micronaut.starter.options.Options)6 Project (io.micronaut.starter.application.Project)4 ApplicationType (io.micronaut.starter.application.ApplicationType)3 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)2 GeneratorContext (io.micronaut.starter.application.generator.GeneratorContext)2 ConsoleOutput (io.micronaut.starter.io.ConsoleOutput)2 MapOutputHandler (io.micronaut.starter.io.MapOutputHandler)2 Language (io.micronaut.starter.options.Language)2 IOException (java.io.IOException)2 java.util (java.util)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 BeanContext (io.micronaut.context.BeanContext)1 Introspected (io.micronaut.core.annotation.Introspected)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 Get (io.micronaut.http.annotation.Get)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 ApplicationGeneratingEvent (io.micronaut.starter.api.event.ApplicationGeneratingEvent)1 ProjectGenerator (io.micronaut.starter.application.generator.ProjectGenerator)1 io.micronaut.starter.feature (io.micronaut.starter.feature)1 AvailableFeatures (io.micronaut.starter.feature.AvailableFeatures)1