Search in sources :

Example 1 with MapOutputHandler

use of io.micronaut.starter.io.MapOutputHandler 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 2 with MapOutputHandler

use of io.micronaut.starter.io.MapOutputHandler in project micronaut-starter by micronaut-projects.

the class FeatureDiffer method produceDiff.

/**
 * Produces a Diff for the given arguments.
 * @param projectGenerator The project generator
 * @param generatorContext The generator context
 * @param consoleOutput The console output
 * @throws Exception If something does wrong
 */
public void produceDiff(ProjectGenerator projectGenerator, GeneratorContext generatorContext, ConsoleOutput consoleOutput) throws Exception {
    MapOutputHandler outputHandler = new MapOutputHandler();
    Project project = generatorContext.getProject();
    ApplicationType applicationType = generatorContext.getApplicationType();
    projectGenerator.generate(applicationType, project, new Options(generatorContext.getLanguage(), generatorContext.getTestFramework(), generatorContext.getBuildTool(), generatorContext.getJdkVersion()), generatorContext.getOperatingSystem(), Collections.emptyList(), outputHandler, ConsoleOutput.NOOP);
    Map<String, String> oldProject = outputHandler.getProject();
    outputHandler = new MapOutputHandler();
    projectGenerator.generate(applicationType, project, outputHandler, generatorContext);
    Map<String, String> newProject = outputHandler.getProject();
    for (Map.Entry<String, String> entry : newProject.entrySet()) {
        String oldFile = oldProject.remove(entry.getKey());
        if (entry.getValue() == null) {
            continue;
        }
        List<String> oldFileLines = oldFile == null ? Collections.emptyList() : toLines(oldFile);
        String newFile = entry.getValue();
        List<String> newFileLines = toLines(newFile);
        Patch<String> diff = DiffUtils.diff(oldFileLines, newFileLines);
        List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(entry.getKey(), entry.getKey(), oldFileLines, diff, 3);
        if (!unifiedDiff.isEmpty()) {
            for (String delta : unifiedDiff) {
                if (delta.startsWith("+")) {
                    consoleOutput.green(delta);
                } else if (delta.startsWith("-")) {
                    consoleOutput.red(delta);
                } else {
                    consoleOutput.out(delta);
                }
            }
            consoleOutput.out("\n");
        }
    }
    for (Map.Entry<String, String> entry : oldProject.entrySet()) {
        if (entry.getValue() == null) {
            continue;
        }
        List<String> oldFileLines = toLines(entry.getValue());
        Patch<String> diff = DiffUtils.diff(oldFileLines, Collections.emptyList());
        List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(entry.getKey(), entry.getKey(), oldFileLines, diff, 3);
        if (!unifiedDiff.isEmpty()) {
            for (String delta : unifiedDiff) {
                if (delta.startsWith("+")) {
                    consoleOutput.green(delta);
                } else if (delta.startsWith("-")) {
                    consoleOutput.red(delta);
                } else {
                    consoleOutput.out(delta);
                }
            }
            consoleOutput.out("\n");
        }
    }
}
Also used : Project(io.micronaut.starter.application.Project) ApplicationType(io.micronaut.starter.application.ApplicationType) Options(io.micronaut.starter.options.Options) MapOutputHandler(io.micronaut.starter.io.MapOutputHandler) Map(java.util.Map)

Aggregations

Project (io.micronaut.starter.application.Project)2 MapOutputHandler (io.micronaut.starter.io.MapOutputHandler)2 Options (io.micronaut.starter.options.Options)2 Get (io.micronaut.http.annotation.Get)1 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)1 ApplicationType (io.micronaut.starter.application.ApplicationType)1 IOException (java.io.IOException)1 Map (java.util.Map)1