Search in sources :

Example 1 with FileGeneratorException

use of org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException in project yangtools by opendaylight.

the class YangToSourcesProcessor method generateSources.

/**
 * Call generate on every generator from plugin configuration.
 */
private Set<File> generateSources(final ContextHolder context, final Collection<GeneratorTaskFactory> generators, final YangParserConfiguration parserConfig) throws MojoFailureException {
    final Builder<File> allFiles = ImmutableSet.builder();
    for (GeneratorTaskFactory factory : generators) {
        if (!parserConfig.equals(factory.parserConfig())) {
            continue;
        }
        final Stopwatch sw = Stopwatch.createStarted();
        final GeneratorTask<?> task = factory.createTask(project, context);
        LOG.debug("{} Task {} initialized in {}", LOG_PREFIX, task, sw);
        final Collection<File> files;
        try {
            files = task.execute(buildContext);
        } catch (FileGeneratorException | IOException e) {
            throw new MojoFailureException(LOG_PREFIX + " Generator " + factory + " failed", e);
        }
        LOG.debug("{} Sources generated by {}: {}", LOG_PREFIX, factory.generatorName(), files);
        final int fileCount;
        if (files != null) {
            fileCount = files.size();
            allFiles.addAll(files);
        } else {
            fileCount = 0;
        }
        LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, factory.generatorName(), fileCount, sw);
    }
    return allFiles.build();
}
Also used : FileGeneratorException(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException) Stopwatch(com.google.common.base.Stopwatch) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) File(java.io.File)

Example 2 with FileGeneratorException

use of org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException in project yangtools by opendaylight.

the class YangToSourcesProcessor method instantiateGenerators.

private List<GeneratorTaskFactory> instantiateGenerators() throws MojoExecutionException, MojoFailureException {
    final List<GeneratorTaskFactory> generators = new ArrayList<>(codeGeneratorArgs.size());
    for (CodeGeneratorArg arg : codeGeneratorArgs) {
        generators.add(CodeGeneratorTaskFactory.create(arg));
        LOG.info("{} Code generator instantiated from {}", LOG_PREFIX, arg.getCodeGeneratorClass());
    }
    // Search for available FileGenerator implementations
    final Map<String, FileGeneratorFactory> factories = Maps.uniqueIndex(ServiceLoader.load(FileGeneratorFactory.class), FileGeneratorFactory::getIdentifier);
    // Assign instantiate FileGenerators with appropriate configurate
    for (Entry<String, FileGeneratorFactory> entry : factories.entrySet()) {
        final String id = entry.getKey();
        FileGeneratorArg arg = fileGeneratorArgs.get(id);
        if (arg == null) {
            LOG.debug("{} No configuration for {}, using empty", LOG_PREFIX, id);
            arg = new FileGeneratorArg(id);
        }
        try {
            generators.add(FileGeneratorTaskFactory.of(entry.getValue(), arg));
        } catch (FileGeneratorException e) {
            throw new MojoExecutionException("File generator " + id + " failed", e);
        }
        LOG.info("{} Code generator {} instantiated", LOG_PREFIX, id);
    }
    return generators;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileGeneratorFactory(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory) FileGeneratorException(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException) CodeGeneratorArg(org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg) ArrayList(java.util.ArrayList)

Example 3 with FileGeneratorException

use of org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException in project yangtools by opendaylight.

the class FileGeneratorTask method transientDirectory.

private File transientDirectory(final GeneratedFileType fileType) throws FileGeneratorException {
    final File ret;
    if (GeneratedFileType.SOURCE.equals(fileType)) {
        ret = transientDirectory("generated-sources");
        project.addCompileSourceRoot(ret.toString());
    } else if (GeneratedFileType.RESOURCE.equals(fileType)) {
        ret = transientDirectory("generated-resources");
        project.addResource(createResouce(ret));
    } else {
        throw new FileGeneratorException("Unknown generated file type " + fileType);
    }
    return ret;
}
Also used : FileGeneratorException(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException) File(java.io.File) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)

Aggregations

FileGeneratorException (org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException)3 File (java.io.File)2 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 FileGeneratorFactory (org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory)1 GeneratedFile (org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)1 CodeGeneratorArg (org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg)1