/* * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang2sources.plugin; import static java.util.Objects.requireNonNull; import com.google.common.base.Stopwatch; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator; import org.opendaylight.yangtools.yang2sources.spi.BuildContextAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonatype.plexus.build.incremental.BuildContext; @NonNullByDefault final class CodeGeneratorTask extends GeneratorTask { private static final Logger LOG = LoggerFactory.getLogger(CodeGeneratorTask.class); private final File outputDir; CodeGeneratorTask(final CodeGeneratorTaskFactory factory, final ContextHolder context, final File outputDir) { super(factory, context); this.outputDir = requireNonNull(outputDir); } @Override Collection execute(final CodeGeneratorTaskFactory factory, final ContextHolder modelContext, final BuildContext buildContext) throws IOException { final Stopwatch watch = Stopwatch.createStarted(); final BasicCodeGenerator gen = factory.generator(); final boolean mark; if (gen instanceof BuildContextAware) { ((BuildContextAware)gen).setBuildContext(buildContext); mark = false; } else { mark = true; } LOG.debug("Sources will be generated to {}", outputDir); Collection sources = gen.generateSources(modelContext.getContext(), outputDir, modelContext.getYangModules(), modelContext); if (sources == null) { sources = List.of(); } LOG.debug("Sources generated by {}: {}", gen.getClass(), sources); LOG.info("Sources generated by {}: {} in {}", gen.getClass(), sources.size(), watch); if (mark) { sources.forEach(buildContext::refresh); } return sources; } }