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