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;
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations