This is an automated email from the ASF dual-hosted git repository.

tzimanyi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git


The following commit(s) were added to refs/heads/main by this push:
     new da09dab6fb [kie-issues#839] Remove comments script - ignore 
nonexistant files (#5675)
da09dab6fb is described below

commit da09dab6fb90be53b6b43bc144bf0e697809af5b
Author: Tibor Zimányi <[email protected]>
AuthorDate: Tue Feb 6 15:22:08 2024 +0100

    [kie-issues#839] Remove comments script - ignore nonexistant files (#5675)
---
 drools-util/pom.xml                                |  4 +++
 .../java/org/drools/util/RemoveCommentsMain.java   | 37 ++++++++++++++++++----
 .../java/org/drools/util/RemoveCommentsTest.java   |  2 +-
 3 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/drools-util/pom.xml b/drools-util/pom.xml
index 7bc2377c5f..3fd81109dd 100644
--- a/drools-util/pom.xml
+++ b/drools-util/pom.xml
@@ -41,6 +41,10 @@
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.eclipse.microprofile.config</groupId>
             <artifactId>microprofile-config-api</artifactId>
diff --git a/drools-util/src/main/java/org/drools/util/RemoveCommentsMain.java 
b/drools-util/src/main/java/org/drools/util/RemoveCommentsMain.java
index 014a1b23bf..024320a8b2 100644
--- a/drools-util/src/main/java/org/drools/util/RemoveCommentsMain.java
+++ b/drools-util/src/main/java/org/drools/util/RemoveCommentsMain.java
@@ -1,27 +1,50 @@
 package org.drools.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.util.stream.Collectors;
 
 public class RemoveCommentsMain {
 
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(RemoveCommentsMain.class);
+
     public static void main(String... args) {
-        for (String fileName : args) {
-            try {
-                Files.write(Path.of(fileName), 
removeComments(fileName).getBytes());
-            } catch (IOException e) {
-                throw new RuntimeException(e);
+        final String ignoreMissingFilesArgument = args[0];
+        final boolean ignoreMissingFiles = 
Boolean.parseBoolean(ignoreMissingFilesArgument);
+        for (int i = 0; i < args.length; i++) {
+            // If the ignoreMissingFiles argument is defined, skip it in this 
iteration.
+            if ((ignoreMissingFiles || 
"false".equalsIgnoreCase(ignoreMissingFilesArgument)) && i == 0) {
+                continue;
+            } else {
+                try {
+                    final String fileName = args[i];
+                    final String result = removeComments(fileName, 
ignoreMissingFiles);
+                    if (result != null) {
+                        Files.write(Path.of(fileName), result.getBytes());
+                    }
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
             }
         }
     }
 
-    static String removeComments(String fileName) {
+    static String removeComments(String fileName, final boolean 
ignoreMissingFiles) {
         try (var lines = Files.lines(Path.of(fileName))) {
             return lines.filter(line -> 
!line.startsWith("#")).collect(Collectors.joining("\n"));
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            // Ignore non-existant files.
+            if (ignoreMissingFiles && e instanceof NoSuchFileException) {
+                LOGGER.info("Ignoring file that doesn't exist: " + fileName);
+                return null;
+            } else {
+                throw new RuntimeException(e);
+            }
         }
     }
 
diff --git a/drools-util/src/test/java/org/drools/util/RemoveCommentsTest.java 
b/drools-util/src/test/java/org/drools/util/RemoveCommentsTest.java
index 91bbceba3d..1b529b5acc 100644
--- a/drools-util/src/test/java/org/drools/util/RemoveCommentsTest.java
+++ b/drools-util/src/test/java/org/drools/util/RemoveCommentsTest.java
@@ -8,7 +8,7 @@ public class RemoveCommentsTest {
 
     @Test
     public void test() {
-        String result = 
RemoveCommentsMain.removeComments("src/test/resources/commented.properties");
+        String result = 
RemoveCommentsMain.removeComments("src/test/resources/commented.properties", 
false);
         String expected = 
"provides-capabilities=org.drools.drl\ndeployment-artifact=org.drools\\:drools-quarkus-deployment\\:999-SNAPSHOT";
         assertEquals(expected, result);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to