Testing bat files.
Project: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/commit/864dfc5b Tree: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/tree/864dfc5b Diff: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/diff/864dfc5b Branch: refs/heads/master Commit: 864dfc5b1987226ad3561c7c07e77b3ea9962b42 Parents: 9bea96e Author: Jan Lahoda <[email protected]> Authored: Sun Sep 17 10:18:02 2017 +0200 Committer: Jan Lahoda <[email protected]> Committed: Sun Sep 17 10:18:02 2017 +0200 ---------------------------------------------------------------------- convert/src/convert/CategorizeLicenses.java | 48 ++++++++++---------- convert/src/convert/Convert.java | 38 ++++++++++++++++ .../test/convert/CategorizeLicensesTest.java | 4 +- 3 files changed, 65 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/blob/864dfc5b/convert/src/convert/CategorizeLicenses.java ---------------------------------------------------------------------- diff --git a/convert/src/convert/CategorizeLicenses.java b/convert/src/convert/CategorizeLicenses.java index a492a17..ee22b8c 100644 --- a/convert/src/convert/CategorizeLicenses.java +++ b/convert/src/convert/CategorizeLicenses.java @@ -36,6 +36,7 @@ import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; public class CategorizeLicenses { @@ -123,7 +124,7 @@ public class CategorizeLicenses { } } - private static final Map<String, Function<String, Description>> extension2Convertor = new HashMap<>(); + private static final Map<String, Collection<Function<String, Description>>> extension2Convertor = new HashMap<>(); static { enterExtensions(code -> snipLicense(code, "/\\*+", "\\*+/", "^[ \t]*\\**[ \t]*", CommentType.JAVA), @@ -131,33 +132,30 @@ public class CategorizeLicenses { enterExtensions(code -> snipLicense(code, "<!--+", "-+->", "^[ \t]*(-[ \t]*)?", CommentType.XML), "html", "xsd", "xsl", "dtd", "settings", "wstcgrp", "wstcref", "wsgrp", "xml", "xslt"); - enterExtensions(code -> snipLicenseBundle(code, "#!.*"), "sh"); - enterExtensions(code -> snipLicenseBundle(code, null), "properties"); + enterExtensions(code -> snipLicenseBundle(code, "#!.*", "#", CommentType.PROPERTIES), "sh"); + enterExtensions(code -> snipLicenseBundle(code, null, "#", CommentType.PROPERTIES), "properties"); + enterExtensions(code -> snipLicenseBundle(code, null, "rem", CommentType.BAT1), "bat"); + enterExtensions(code -> snipLicenseBundle(code, null, "@rem", CommentType.BAT2), "bat"); } private static void enterExtensions(Function<String, Description> convertor, String... extensions) { for (String ext : extensions) { - extension2Convertor.put(ext, convertor); + extension2Convertor.computeIfAbsent(ext, x -> new ArrayList<>()).add(convertor); } } public static Description snipUnifiedLicenseOrNull(String code, Path file) { String fn = file.getFileName().toString(); String ext = fn.substring(fn.lastIndexOf('.') + 1); - Function<String, Description> preferredConvertor = extension2Convertor.get(ext); - Description desc = preferredConvertor != null ? preferredConvertor.apply(code) : null; - - if (desc == null) { - for (Function<String, Description> convertor : extension2Convertor.values()) { - desc = convertor.apply(code); - - if (desc != null) { - return desc; - } - } - } - - return desc; + return Stream.concat(extension2Convertor.getOrDefault(ext, Collections.emptyList()) + .stream(), + extension2Convertor.values() + .stream() + .flatMap(c -> c.stream())) + .map(c -> c.apply(code)) + .filter(desc -> desc != null) + .findFirst() + .orElse(null); } private static Description snipLicense(String code, String commentStart, String commentEnd, String normalizeLines, CommentType commentType) { @@ -176,7 +174,7 @@ public class CategorizeLicenses { return createUnifiedDescriptionOrNull(startM.start(), endM.end(), lic, commentType); } - public static Description snipLicenseBundle(String code, String firstLinePattern) { + public static Description snipLicenseBundle(String code, String firstLinePattern, String commentMarker, CommentType commentType) { StringBuilder res = new StringBuilder(); boolean firstLine = true; int start = -1; @@ -193,8 +191,8 @@ public class CategorizeLicenses { continue; if (firstLine && line.trim().isEmpty()) continue; - if (line.startsWith("#")) { - String part = line.substring(1).trim(); + if (line.startsWith(commentMarker)) { + String part = line.substring(commentMarker.length()).trim(); if (firstLine && part.isEmpty()) continue; if (firstLine) { @@ -207,10 +205,10 @@ public class CategorizeLicenses { end = next; } } else { - return createUnifiedDescriptionOrNull(start, end, res.toString(), CommentType.PROPERTIES); + return createUnifiedDescriptionOrNull(start, end, res.toString(), commentType); } } - return createUnifiedDescriptionOrNull(start, end, res.toString(), CommentType.PROPERTIES); + return createUnifiedDescriptionOrNull(start, end, res.toString(), commentType); } private static Description createUnifiedDescriptionOrNull(int start, int end, String lic, CommentType commentType) { @@ -249,6 +247,8 @@ public class CategorizeLicenses { public enum CommentType { JAVA, XML, - PROPERTIES; + PROPERTIES, + BAT1, + BAT2; } } http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/blob/864dfc5b/convert/src/convert/Convert.java ---------------------------------------------------------------------- diff --git a/convert/src/convert/Convert.java b/convert/src/convert/Convert.java index 1cd72f2..240c5a6 100644 --- a/convert/src/convert/Convert.java +++ b/convert/src/convert/Convert.java @@ -138,6 +138,42 @@ public class Convert { "# specific language governing permissions and limitations\n" + "# under the License.\n"; + private static final String BAT1_OUTPUT = + "rem Licensed to the Apache Software Foundation (ASF) under one\n" + + "rem or more contributor license agreements. See the NOTICE file\n" + + "rem distributed with this work for additional information\n" + + "rem regarding copyright ownership. The ASF licenses this file\n" + + "rem to you under the Apache License, Version 2.0 (the\n" + + "rem \"License\"); you may not use this file except in compliance\n" + + "rem with the License. You may obtain a copy of the License at\n" + + "rem\n" + + "rem http://www.apache.org/licenses/LICENSE-2.0\n" + + "rem\n" + + "rem Unless required by applicable law or agreed to in writing,\n" + + "rem software distributed under the License is distributed on an\n" + + "rem \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" + + "rem KIND, either express or implied. See the License for the\n" + + "rem specific language governing permissions and limitations\n" + + "rem under the License.\n"; + + private static final String BAT2_OUTPUT = + "@rem Licensed to the Apache Software Foundation (ASF) under one\n" + + "@rem or more contributor license agreements. See the NOTICE file\n" + + "@rem distributed with this work for additional information\n" + + "@rem regarding copyright ownership. The ASF licenses this file\n" + + "@rem to you under the Apache License, Version 2.0 (the\n" + + "@rem \"License\"); you may not use this file except in compliance\n" + + "@rem with the License. You may obtain a copy of the License at\n" + + "@rem\n" + + "@rem http://www.apache.org/licenses/LICENSE-2.0\n" + + "@rem\n" + + "@rem Unless required by applicable law or agreed to in writing,\n" + + "@rem software distributed under the License is distributed on an\n" + + "@rem \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" + + "@rem KIND, either express or implied. See the License for the\n" + + "@rem specific language governing permissions and limitations\n" + + "@rem under the License.\n"; + public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Use: Convert <source-directory>"); @@ -176,6 +212,8 @@ public class Convert { case JAVA: outputLicense = JAVA_OUTPUT; break; case XML: outputLicense = XML_OUTPUT; break; case PROPERTIES: outputLicense = BUNDLE_OUTPUT; break; + case BAT1: outputLicense = BAT1_OUTPUT; break; + case BAT2: outputLicense = BAT2_OUTPUT; break; default: System.err.println("cannot rewrite: " + file); return ; http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/blob/864dfc5b/convert/test/convert/CategorizeLicensesTest.java ---------------------------------------------------------------------- diff --git a/convert/test/convert/CategorizeLicensesTest.java b/convert/test/convert/CategorizeLicensesTest.java index e77e4f9..960b2a3 100644 --- a/convert/test/convert/CategorizeLicensesTest.java +++ b/convert/test/convert/CategorizeLicensesTest.java @@ -41,7 +41,9 @@ public class CategorizeLicensesTest { "\n"; CategorizeLicenses.Description desc = CategorizeLicenses.snipLicenseBundle(code, - null); + null, + "#", + CategorizeLicenses.CommentType.PROPERTIES); assertEquals("CDDL\nlic", desc.header); assertEquals("#CDDL\n#\n#lic\n", code.substring(desc.start, desc.end)); }
