--- /dev/null
+/*
+ * Copyright (c) 2023 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 java.io.IOException;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * A {@link StateStorage} based on {@link BuildContext}.
+ */
+@NonNullByDefault
+final class BuildContextStateStorage extends StateStorage {
+ private static final String STATE_KEY = YangToSourcesState.class.getName();
+
+ private final BuildContext buildContext;
+
+ BuildContextStateStorage(final BuildContext buildContext) {
+ this.buildContext = requireNonNull(buildContext);
+ }
+
+ @Override
+ @Nullable YangToSourcesState loadState() throws IOException {
+ final var value = buildContext.getValue(STATE_KEY);
+ if (value instanceof YangToSourcesState state) {
+ return state;
+ } else if (value == null) {
+ return null;
+ } else {
+ throw new IOException("Unexpected loaded state " + value);
+ }
+ }
+
+ @Override
+ void storeState(final YangToSourcesState state) {
+ buildContext.setValue(STATE_KEY, requireNonNull(state));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2023 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 java.io.IOException;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * A storage engine able to store and load {@link YangToSourcesState}.
+ */
+@NonNullByDefault
+abstract class StateStorage {
+ private static final Logger LOG = LoggerFactory.getLogger(StateStorage.class);
+
+ static StateStorage of(final BuildContext buildContext) {
+ // FIXME: detect no-op BuildContext and fall back to a file
+ LOG.debug("{} Using BuildContext to store state", YangToSourcesProcessor.LOG_PREFIX);
+ return new BuildContextStateStorage(buildContext);
+ }
+
+ abstract @Nullable YangToSourcesState loadState() throws IOException;
+
+ abstract void storeState(YangToSourcesState state) throws IOException;
+}
static final String LOG_PREFIX = "yang-to-sources:";
private static final String META_INF_STR = "META-INF";
private static final String YANG_STR = "yang";
- private static final String BUILD_CONTEXT_STATE_NAME = YangToSourcesProcessor.class.getName();
static final String META_INF_YANG_STRING = META_INF_STR + File.separator + YANG_STR;
static final String META_INF_YANG_STRING_JAR = META_INF_STR + "/" + YANG_STR;
private final boolean inspectDependencies;
private final BuildContext buildContext;
private final YangProvider yangProvider;
+ private final StateStorage stateStorage;
private YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
final Collection<File> excludedFiles, final List<FileGeneratorArg> fileGeneratorsArgs,
this.project = requireNonNull(project);
this.inspectDependencies = inspectDependencies;
this.yangProvider = requireNonNull(yangProvider);
+ stateStorage = StateStorage.of(buildContext);
parserFactory = DEFAULT_PARSER_FACTORY;
}
}
void execute() throws MojoExecutionException, MojoFailureException {
- var prevState = buildContext.getValue(BUILD_CONTEXT_STATE_NAME);
+ YangToSourcesState prevState;
+ try {
+ prevState = stateStorage.loadState();
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to restore execution state", e);
+ }
if (prevState == null) {
- LOG.debug("{} BuildContext did not provide state", LOG_PREFIX);
- // FIXME: look for persisted state and restore it
+ LOG.debug("{} no previous execution state present", LOG_PREFIX);
}
// Collect all files in the current project.
codeGenerators.stream()
.collect(ImmutableMap.toImmutableMap(GeneratorTaskFactory::getIdentifier, GeneratorTaskFactory::arg)),
new FileStateSet(ImmutableMap.copyOf(uniqueOutputFiles)));
- buildContext.setValue(BUILD_CONTEXT_STATE_NAME, outputState);
- if (buildContext.getValue(BUILD_CONTEXT_STATE_NAME) == null) {
- LOG.debug("{} BuildContext did not retain state, persisting", LOG_PREFIX);
- // FIXME: persist in target/ directory (there is a maven best practice where)
+
+ try {
+ stateStorage.storeState(outputState);
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to store execution state", e);
}
}