Revert "Get rid of commons-io" 00/62700/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Sep 2017 14:10:17 +0000 (16:10 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Sep 2017 14:11:56 +0000 (16:11 +0200)
This reverts commit e8ac56902440704082470574abf0cd159f66293b.

Change-Id: Iffb89d4c9f0572ead4c64e5249844eb875a93824
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-maven-plugin/pom.xml
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java

index 2c7beb34a49d995931b47c86e13f77d8d1d30fed..04ef90d64fc097d70a75f93e41c5dde275b5f1de 100644 (file)
             <artifactId>plexus-build-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>yang-maven-plugin-spi</artifactId>
index 3656c859b825b55f0be31b7eff417a7c8b341bb9..d897f109bd9180ede02b4e75ea1cbf77779f9da7 100644 (file)
@@ -15,9 +15,11 @@ 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;
@@ -26,6 +28,7 @@ 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;
@@ -52,10 +55,32 @@ final class Util {
     private Util() {
     }
 
+    static final String YANG_SUFFIX = "yang";
+
     private static final Logger LOG = LoggerFactory.getLogger(Util.class);
 
+    static Collection<File> listFiles(final File root, final Collection<File> 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<File> result = new ArrayList<>();
+        Collection<File> 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<File> getClassPath(final MavenProject project) {
-        final List<File> dependencies = new ArrayList<>();
+        List<File> dependencies = new ArrayList<>();
         for (Artifact element : project.getArtifacts()) {
             File asFile = element.getFile();
             if (isJar(asFile) || asFile.isDirectory()) {
index ff274d19e22403a57f76eee5f6e6013ee2cb598d..a708c412b9415ba58abe0dc4fc52835a2b89a860 100644 (file)
@@ -17,21 +17,17 @@ 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.opendaylight.yangtools.yang.common.YangConstants;
+import org.codehaus.plexus.util.FileUtils;
 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;
@@ -141,7 +137,7 @@ class YangToSourcesProcessor {
              * files in current project and optionally any jars/files in the
              * dependencies.
              */
-            final Collection<File> yangFilesInProject = listFiles(yangFilesRootDir, excludedFiles);
+            final Collection<File> yangFilesInProject = Util.listFiles(yangFilesRootDir, excludedFiles);
 
             final Collection<File> allFiles = new ArrayList<>(yangFilesInProject);
             if (inspectDependencies) {
@@ -184,10 +180,10 @@ class YangToSourcesProcessor {
             return Optional.of(reactor);
         } catch (Exception e) {
             // MojoExecutionException is thrown since execution cannot continue
-            LOG.error("{} Unable to parse YANG files from {}", LOG_PREFIX, yangFilesRootDir, e);
+            LOG.error("{} Unable to parse {} files from {}", LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir, e);
             Throwable rootCause = Throwables.getRootCause(e);
-            throw new MojoExecutionException(LOG_PREFIX + " Unable to parse YANG files from " + yangFilesRootDir,
-                rootCause);
+            throw new MojoExecutionException(LOG_PREFIX + " Unable to parse " + Util.YANG_SUFFIX + " files from "
+                    + yangFilesRootDir, rootCause);
         }
     }
 
@@ -210,22 +206,6 @@ class YangToSourcesProcessor {
         return reactor.toContext();
     }
 
-    private static Collection<File> listFiles(final File root, final Collection<File> 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<YangTextSchemaSource> toUniqueSources(final Collection<YangTextSchemaSource> sources)
             throws IOException {
         final Map<String, YangTextSchemaSource> byContent = new HashMap<>();
@@ -306,10 +286,9 @@ class YangToSourcesProcessor {
         LOG.debug("{} Folder: {} marked as resources for generator: {}", LOG_PREFIX, resourceBaseDir,
                 codeGeneratorCfg.getCodeGeneratorClass());
 
-        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);
-        }
+        FileUtils.deleteDirectory(outputDir);
+        LOG.info("{} Succesfully deleted output directory {}", LOG_PREFIX, outputDir);
+
         Collection<File> generated = g.generateSources(context.getContext(), outputDir, context.getYangModules(),
             context::moduleToResourcePath);