1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21
22 import java.util.Collection;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.function.Function;
29 import java.util.stream.Collectors;
30
31 import org.apache.commons.lang3.Strings;
32
33 import io.wcm.devops.conga.plugins.aem.maven.model.InstallableFile;
34
35 final class RunModeUtil {
36
37 static final String RUNMODE_AUTHOR = "author";
38 static final String RUNMODE_PUBLISH = "publish";
39
40 private RunModeUtil() {
41
42 }
43
44
45
46
47
48
49 public static boolean isAuthorAndPublish(InstallableFile file) {
50 Set<String> runModes = mapVariantsToRunModes(file.getVariants());
51 return (!runModes.contains(RUNMODE_AUTHOR) && !runModes.contains(RUNMODE_PUBLISH))
52 || (runModes.contains(RUNMODE_AUTHOR) && runModes.contains(RUNMODE_PUBLISH));
53 }
54
55
56
57
58
59
60 public static boolean isOnlyAuthor(InstallableFile file) {
61 Set<String> runModes = mapVariantsToRunModes(file.getVariants());
62 return runModes.contains(RUNMODE_AUTHOR) && !runModes.contains(RUNMODE_PUBLISH);
63 }
64
65
66
67
68
69
70 public static boolean isOnlyPublish(InstallableFile file) {
71 Set<String> runModes = mapVariantsToRunModes(file.getVariants());
72 return runModes.contains(RUNMODE_PUBLISH) && !runModes.contains(RUNMODE_AUTHOR);
73 }
74
75 private static Set<String> mapVariantsToRunModes(Collection<String> variants) {
76 return variants.stream()
77 .map(RunModeUtil::mapVariantToRunMode)
78 .collect(Collectors.toSet());
79 }
80
81
82
83
84
85
86
87 private static String mapVariantToRunMode(String variant) {
88 if ("aem-author".equals(variant)) {
89 return RUNMODE_AUTHOR;
90 }
91 else if ("aem-publish".equals(variant)) {
92 return RUNMODE_PUBLISH;
93 }
94 return variant;
95 }
96
97
98
99
100
101
102
103
104
105
106 public static <T extends InstallableFile, S extends FileSet<T>> Collection<S> eliminateAuthorPublishDuplicates(
107 List<S> fileSets, Function<String, S> fileSetFactory) {
108 Map<String, S> result = new LinkedHashMap<>();
109 fileSets.forEach(fileSet -> fileSet.getEnvironmentRunModes().forEach(environmentRunMode -> {
110 FileSet<T> resultFileSet = result.computeIfAbsent(environmentRunMode, fileSetFactory);
111 fileSet.getFiles().forEach(file -> {
112 Optional<T> existingFile = resultFileSet.getFiles().stream()
113 .filter(item -> isSameFileNameHash(item, file))
114 .findFirst();
115 if (existingFile.isPresent()) {
116
117 existingFile.get().getVariants().addAll(file.getVariants());
118 }
119 else {
120 resultFileSet.getFiles().add(file);
121 }
122 });
123 }));
124
125 result.values().forEach(
126 fileSet -> fileSet.getFiles().forEach(file -> removeAuthorPublishRunModeIfBothPresent(file.getVariants())));
127 return result.values();
128 }
129
130 private static boolean isSameFileNameHash(InstallableFile file1, InstallableFile file2) {
131 if (!Strings.CS.equals(file1.getFile().getName(), file2.getFile().getName())) {
132 return false;
133 }
134 return file1.getHashCode().equals(file2.getHashCode());
135 }
136
137
138
139
140
141 private static void removeAuthorPublishRunModeIfBothPresent(Set<String> runModes) {
142 if (runModes.contains(RUNMODE_AUTHOR) && runModes.contains(RUNMODE_PUBLISH)) {
143 runModes.remove(RUNMODE_AUTHOR);
144 runModes.remove(RUNMODE_PUBLISH);
145 }
146 }
147
148 }