From e8ac56902440704082470574abf0cd159f66293b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 20 Feb 2017 22:07:22 +0100 Subject: [PATCH] Get rid of commons-io Java 8 gives us everything we need to search for files, hence there is no need to use commons-io, remove it. Change-Id: Ieab83e52189992f68616e8246320ca7685a87cd5 Signed-off-by: Robert Varga --- yang/yang-maven-plugin/pom.xml | 5 --- .../yangtools/yang2sources/plugin/Util.java | 27 +------------- .../plugin/YangToSourcesProcessor.java | 37 +++++++++++++++---- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/yang/yang-maven-plugin/pom.xml b/yang/yang-maven-plugin/pom.xml index 04ef90d64f..2c7beb34a4 100644 --- a/yang/yang-maven-plugin/pom.xml +++ b/yang/yang-maven-plugin/pom.xml @@ -83,11 +83,6 @@ plexus-build-api - - commons-io - commons-io - - ${project.groupId} yang-maven-plugin-spi diff --git a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java index d897f109bd..3656c859b8 100644 --- a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java +++ b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java @@ -15,11 +15,9 @@ import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProces import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -28,7 +26,6 @@ import java.util.Map; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; @@ -55,32 +52,10 @@ final class Util { private Util() { } - static final String YANG_SUFFIX = "yang"; - private static final Logger LOG = LoggerFactory.getLogger(Util.class); - static Collection listFiles(final File root, final Collection excludedFiles) - throws FileNotFoundException { - if (!root.exists()) { - LOG.warn("{} YANG source directory {} not found. No code will be generated.", LOG_PREFIX, root); - - return Collections.emptyList(); - } - Collection result = new ArrayList<>(); - Collection yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true); - for (File f : yangFiles) { - if (excludedFiles.contains(f)) { - LOG.info("{} {} file excluded {}", LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(), f); - } else { - result.add(f); - } - } - - return result; - } - static List getClassPath(final MavenProject project) { - List dependencies = new ArrayList<>(); + final List dependencies = new ArrayList<>(); for (Artifact element : project.getArtifacts()) { File asFile = element.getFile(); if (isJar(asFile) || asFile.isDirectory()) { diff --git a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java index a708c412b9..ff274d19e2 100644 --- a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java +++ b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java @@ -17,17 +17,21 @@ import java.io.File; import java.io.IOException; import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; +import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; @@ -137,7 +141,7 @@ class YangToSourcesProcessor { * files in current project and optionally any jars/files in the * dependencies. */ - final Collection yangFilesInProject = Util.listFiles(yangFilesRootDir, excludedFiles); + final Collection yangFilesInProject = listFiles(yangFilesRootDir, excludedFiles); final Collection allFiles = new ArrayList<>(yangFilesInProject); if (inspectDependencies) { @@ -180,10 +184,10 @@ class YangToSourcesProcessor { return Optional.of(reactor); } catch (Exception e) { // MojoExecutionException is thrown since execution cannot continue - LOG.error("{} Unable to parse {} files from {}", LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir, e); + LOG.error("{} Unable to parse YANG files from {}", LOG_PREFIX, yangFilesRootDir, e); Throwable rootCause = Throwables.getRootCause(e); - throw new MojoExecutionException(LOG_PREFIX + " Unable to parse " + Util.YANG_SUFFIX + " files from " - + yangFilesRootDir, rootCause); + throw new MojoExecutionException(LOG_PREFIX + " Unable to parse YANG files from " + yangFilesRootDir, + rootCause); } } @@ -206,6 +210,22 @@ class YangToSourcesProcessor { return reactor.toContext(); } + private static Collection listFiles(final File root, final Collection excludedFiles) + throws IOException { + if (!root.isDirectory()) { + LOG.warn("{} YANG source directory {} not found. No code will be generated.", LOG_PREFIX, root); + return ImmutableList.of(); + } + + return Files.walk(root.toPath()).map(Path::toFile).filter(File::isFile).filter(f -> { + if (excludedFiles.contains(f)) { + LOG.info("{} YANG file excluded {}", LOG_PREFIX, f); + return false; + } + return true; + }).filter(f -> f.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)).collect(Collectors.toList()); + } + private static Collection toUniqueSources(final Collection sources) throws IOException { final Map byContent = new HashMap<>(); @@ -286,9 +306,10 @@ class YangToSourcesProcessor { LOG.debug("{} Folder: {} marked as resources for generator: {}", LOG_PREFIX, resourceBaseDir, codeGeneratorCfg.getCodeGeneratorClass()); - FileUtils.deleteDirectory(outputDir); - LOG.info("{} Succesfully deleted output directory {}", LOG_PREFIX, outputDir); - + if (outputDir.exists()) { + Files.walk(outputDir.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + LOG.info("{} Succesfully deleted output directory {}", LOG_PREFIX, outputDir); + } Collection generated = g.generateSources(context.getContext(), outputDir, context.getYangModules(), context::moduleToResourcePath); -- 2.36.6