|
34 | 34 | import static io.wcm.devops.conga.plugins.aem.postprocessor.ContentPackageOptions.PROPERTY_PACKAGE_THUMBNAIL_IMAGE; |
35 | 35 | import static io.wcm.devops.conga.plugins.aem.postprocessor.ContentPackageOptions.PROPERTY_PACKAGE_VERSION; |
36 | 36 |
|
| 37 | +import java.io.File; |
37 | 38 | import java.io.IOException; |
38 | 39 | import java.io.InputStream; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.nio.file.Paths; |
39 | 43 | import java.util.ArrayList; |
40 | 44 | import java.util.List; |
41 | 45 | import java.util.Map; |
| 46 | +import java.util.Objects; |
| 47 | +import java.util.regex.Matcher; |
| 48 | +import java.util.regex.Pattern; |
42 | 49 | import java.util.stream.Collectors; |
43 | 50 |
|
44 | 51 | import org.apache.commons.lang3.BooleanUtils; |
@@ -348,25 +355,70 @@ private static Map<String, Object> getOptionalPropMap(Map<String, Object> option |
348 | 355 | /** |
349 | 356 | * Get binary files to be added to package. |
350 | 357 | * @param options Options |
| 358 | + * @param targetDir Target directory |
351 | 359 | * @return File list |
352 | 360 | * @throws IOException I/O exception |
353 | 361 | */ |
354 | | - public static List<ContentPackageBinaryFile> getFiles(Map<String, Object> options) throws IOException { |
| 362 | + public static List<ContentPackageBinaryFile> getFiles(Map<String, Object> options, File targetDir) throws IOException { |
355 | 363 | List<ContentPackageBinaryFile> files = new ArrayList<>(); |
356 | 364 |
|
357 | 365 | List<Map<String, Object>> fileDefinitions = getOptionalPropMapList(options, PROPERTY_PACKAGE_FILES); |
358 | 366 | if (fileDefinitions != null) { |
359 | 367 | for (Map<String, Object> fileDefinition : fileDefinitions) { |
360 | 368 | String file = getOptionalProp(fileDefinition, "file"); |
| 369 | + String fileMatch = getOptionalProp(fileDefinition, "fileMatch"); |
361 | 370 | String dir = getOptionalProp(fileDefinition, "dir"); |
362 | 371 | String url = getOptionalProp(fileDefinition, "url"); |
363 | 372 | String path = getMandatoryProp(fileDefinition, "path"); |
364 | 373 | boolean delete = BooleanUtils.toBoolean(getOptionalProp(fileDefinition, "delete")); |
365 | | - files.add(new ContentPackageBinaryFile(file, dir, url, path, delete)); |
| 374 | + |
| 375 | + if (fileMatch != null && file == null && url == null) { |
| 376 | + // add multiple (local) files matching with given regex |
| 377 | + files.addAll(getMatchingFiles(targetDir, fileMatch, dir, path, delete)); |
| 378 | + } |
| 379 | + else { |
| 380 | + // add single file |
| 381 | + files.add(new ContentPackageBinaryFile(file, dir, url, path, delete)); |
| 382 | + } |
366 | 383 | } |
367 | 384 | } |
368 | 385 |
|
369 | 386 | return files; |
370 | 387 | } |
371 | 388 |
|
| 389 | + private static List<ContentPackageBinaryFile> getMatchingFiles(File targetDir, String fileMatch, |
| 390 | + String dir, String path, boolean delete) throws IOException { |
| 391 | + File fileTargetDir = targetDir; |
| 392 | + if (StringUtils.isNotEmpty(dir)) { |
| 393 | + fileTargetDir = new File(targetDir, dir); |
| 394 | + } |
| 395 | + Path fileTargetPath = Paths.get(fileTargetDir.toURI()); |
| 396 | + String targetPathPrefix = normalizedAbsolutePath(fileTargetPath) + "/"; |
| 397 | + Pattern pattern = Pattern.compile(fileMatch); |
| 398 | + |
| 399 | + // collect all files below the target dir |
| 400 | + return Files.walk(Paths.get(fileTargetDir.toURI())) |
| 401 | + .filter(Files::isRegularFile) |
| 402 | + // strip off the target dir paths, keep only the relative path/file name |
| 403 | + .map(ContentPackageUtil::normalizedAbsolutePath) |
| 404 | + .map(file -> StringUtils.removeStart(file, targetPathPrefix)) |
| 405 | + // check if file matches with the regex, apply matching input groups to path |
| 406 | + .map(file -> { |
| 407 | + Matcher matcher = pattern.matcher(file); |
| 408 | + if (matcher.matches()) { |
| 409 | + String adaptedPath = matcher.replaceAll(path); |
| 410 | + return new ContentPackageBinaryFile(file, dir, null, adaptedPath, delete); |
| 411 | + } |
| 412 | + else { |
| 413 | + return null; |
| 414 | + } |
| 415 | + }) |
| 416 | + .filter(Objects::nonNull) |
| 417 | + .collect(Collectors.toList()); |
| 418 | + } |
| 419 | + |
| 420 | + private static String normalizedAbsolutePath(Path path) { |
| 421 | + return StringUtils.replace(path.toAbsolutePath().toString(), "\\", "/"); |
| 422 | + } |
| 423 | + |
372 | 424 | } |
0 commit comments