|
| 1 | +package com.walmartlabs.concord.common; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ***** |
| 5 | + * Concord |
| 6 | + * ----- |
| 7 | + * Copyright (C) 2017 - 2025 Walmart Inc. |
| 8 | + * ----- |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * ===== |
| 21 | + */ |
| 22 | + |
| 23 | +import com.walmartlabs.concord.sdk.Constants; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.*; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public final class ResourceUtils { |
| 32 | + |
| 33 | + public static void copyResources(Path baseDir, List<String> includePatterns, Path destDir, CopyOption... options) throws IOException { |
| 34 | + copyResources(baseDir, includePatterns, List.of(), destDir, null, options); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Copies Concord resources from baseDir to destDir. |
| 39 | + * @param baseDir source |
| 40 | + * @param includePatterns list of paths (glob, regexes, file paths) to include |
| 41 | + * @param excludePatterns list of paths (glob, regexes, file paths) to exclude |
| 42 | + * @param destDir destination |
| 43 | + * @param visitor a FileVisitor to apply after each copied file (can be null) |
| 44 | + * @param options array of CopyOptions |
| 45 | + * @throws IOException |
| 46 | + */ |
| 47 | + public static void copyResources(Path baseDir, List<String> includePatterns, List<String> excludePatterns, Path destDir, FileVisitor visitor, CopyOption... options) throws IOException { |
| 48 | + var paths = findResources(baseDir, includePatterns, excludePatterns); |
| 49 | + for (var fileName : Constants.Files.PROJECT_ROOT_FILE_NAMES) { |
| 50 | + var p = baseDir.resolve(fileName); |
| 51 | + paths.add(p); |
| 52 | + } |
| 53 | + copy(paths, baseDir, destDir, visitor, options); |
| 54 | + } |
| 55 | + |
| 56 | + public static List<Path> findResources(Path baseDir, List<String> includePatterns) throws IOException { |
| 57 | + return findResources(baseDir, includePatterns, List.of()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Finds paths in baseDir that match includePatterns and do not match excludePatterns. |
| 62 | + * @param baseDir source |
| 63 | + * @param includePatterns list of paths (glob, regexes, file paths) to include |
| 64 | + * @param excludePatterns list of paths (glob, regexes, file paths) to exclude |
| 65 | + * @return list of absolute paths |
| 66 | + * @throws IOException |
| 67 | + */ |
| 68 | + public static List<Path> findResources(Path baseDir, List<String> includePatterns, List<String> excludePatterns) throws IOException { |
| 69 | + var result = new ArrayList<Path>(); |
| 70 | + |
| 71 | + var includeMatchers = includePatterns.stream().map(p -> parsePattern(baseDir, p)).toList(); |
| 72 | + var excludeMatchers = excludePatterns.stream().map(p -> parsePattern(baseDir, p)).toList(); |
| 73 | + |
| 74 | + try (var walker = Files.walk(baseDir)) { |
| 75 | + walker.filter(candidate -> !Files.isDirectory(candidate)) |
| 76 | + .filter(candidate -> |
| 77 | + includeMatchers.stream().anyMatch(m -> m.matches(candidate)) && |
| 78 | + excludeMatchers.stream().noneMatch(pattern -> pattern.matches(candidate))) |
| 79 | + .forEach(result::add); |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + private static void copy(Collection<Path> files, Path baseDir, Path dest, FileVisitor visitor, CopyOption... options) throws IOException { |
| 86 | + for (var f : files) { |
| 87 | + if (Files.notExists(f)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + var src = baseDir.relativize(f); |
| 92 | + var dst = dest.resolve(src); |
| 93 | + |
| 94 | + var dstParent = dst.getParent(); |
| 95 | + if (dstParent != null && Files.notExists(dstParent)) { |
| 96 | + Files.createDirectories(dstParent); |
| 97 | + } |
| 98 | + |
| 99 | + if (Files.isSymbolicLink(f)) { |
| 100 | + Path link = Files.readSymbolicLink(f); |
| 101 | + Path target = f.getParent().resolve(link).normalize(); |
| 102 | + |
| 103 | + if (!target.startsWith(baseDir)) { |
| 104 | + throw new IOException("Symlinks outside the base directory are not supported: " + baseDir + " -> " + target); |
| 105 | + } |
| 106 | + |
| 107 | + if (Files.notExists(target)) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + Files.createSymbolicLink(dst, link); |
| 112 | + } else { |
| 113 | + Files.copy(f, dst, options); |
| 114 | + } |
| 115 | + |
| 116 | + if (visitor != null) { |
| 117 | + visitor.visit(src, dst); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + static PathMatcher parsePattern(Path baseDir, String pattern) { |
| 123 | + pattern = pattern.trim(); |
| 124 | + |
| 125 | + String normalizedPattern; |
| 126 | + if (pattern.startsWith("glob:")) { |
| 127 | + normalizedPattern = "glob:" + concat(baseDir, pattern.substring("glob:".length())); |
| 128 | + } else if (pattern.startsWith("regex:")) { |
| 129 | + normalizedPattern = "regex:" + concat(baseDir, pattern.substring("regex:".length())); |
| 130 | + } else { |
| 131 | + normalizedPattern = "glob:" + concat(baseDir, pattern |
| 132 | + .replace("*", "\\*") |
| 133 | + .replace("{", "\\{") |
| 134 | + .replace("}", "\\}") |
| 135 | + .replace("[", "\\[") |
| 136 | + .replace("]", "\\]") |
| 137 | + .replace("!", "\\!") |
| 138 | + .replace("?", "\\?")); |
| 139 | + } |
| 140 | + |
| 141 | + return FileSystems.getDefault().getPathMatcher(normalizedPattern); |
| 142 | + } |
| 143 | + |
| 144 | + private static String concat(Path path, String str) { |
| 145 | + var separator = "/"; |
| 146 | + if (str.startsWith("/")) { |
| 147 | + separator = ""; |
| 148 | + } |
| 149 | + return path.toAbsolutePath() + separator + str; |
| 150 | + } |
| 151 | + |
| 152 | + private ResourceUtils() { |
| 153 | + } |
| 154 | +} |
0 commit comments