Search in sources :

Example 1 with FileGenerator

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

the class FileGeneratorTask method execute.

@Override
Collection<File> execute(final FileGeneratorTaskFactory factory, final ContextHolder modelContext, final BuildContext buildContext) throws FileGeneratorException, IOException {
    // Step one: determine what files are going to be generated
    final Stopwatch sw = Stopwatch.createStarted();
    final FileGenerator gen = factory.generator();
    final Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> generatedFiles = gen.generateFiles(modelContext.getContext(), modelContext.getYangModules(), modelContext);
    LOG.info("{}: Defined {} files in {}", suffix, generatedFiles.size(), sw);
    // Step two: create generation tasks for each target file and group them by parent directory
    sw.reset().start();
    final ListMultimap<File, WriteTask> dirs = MultimapBuilder.hashKeys().arrayListValues().build();
    for (Cell<GeneratedFileType, GeneratedFilePath, GeneratedFile> cell : generatedFiles.cellSet()) {
        final GeneratedFile file = cell.getValue();
        final String relativePath = cell.getColumnKey().getPath();
        final File target;
        switch(file.getLifecycle()) {
            case PERSISTENT:
                target = new File(persistentPath(cell.getRowKey()), relativePath);
                if (target.exists()) {
                    LOG.debug("Skipping existing persistent {}", target);
                    continue;
                }
                break;
            case TRANSIENT:
                target = new File(transientPath(cell.getRowKey()), relativePath);
                break;
            default:
                throw new IllegalStateException("Unsupported file type in " + file);
        }
        dirs.put(target.getParentFile(), new WriteTask(buildContext, target, cell.getValue()));
    }
    LOG.info("Sorted {} files into {} directories in {}", dirs.size(), dirs.keySet().size(), sw);
    // Step three: submit parent directory creation tasks (via parallelStream()) and wait for them to complete
    sw.reset().start();
    dirs.keySet().parallelStream().forEach(path -> {
        try {
            Files.createDirectories(path.toPath());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to create " + path, e);
        }
    });
    LOG.debug("Parent directories created in {}", sw);
    // Step four: submit all code generation tasks (via parallelStream()) and wait for them to complete
    sw.reset().start();
    final List<File> result = dirs.values().parallelStream().map(WriteTask::generateFile).collect(Collectors.toList());
    LOG.debug("Generated {} files in {}", result.size(), sw);
    return result;
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) FileGenerator(org.opendaylight.yangtools.plugin.generator.api.FileGenerator) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) File(java.io.File) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 IOException (java.io.IOException)1 FileGenerator (org.opendaylight.yangtools.plugin.generator.api.FileGenerator)1 GeneratedFile (org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)1 GeneratedFilePath (org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath)1 GeneratedFileType (org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType)1