author | herrick |
Fri, 18 Oct 2019 14:14:37 -0400 | |
branch | JDK-8200758-branch |
changeset 58696 | 61c44899b4eb |
parent 58670 | 6fb9e12d5595 |
permissions | -rw-r--r-- |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
1 |
/* |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
4 |
* |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
10 |
* |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
15 |
* accompanied this code). |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
16 |
* |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
20 |
* |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
23 |
* questions. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
24 |
*/ |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
25 |
package jdk.jpackage.internal; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
26 |
|
58302 | 27 |
import java.io.File; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
28 |
import java.io.IOException; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
29 |
import java.nio.file.Files; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
30 |
import java.nio.file.Path; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
31 |
import java.util.ArrayList; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
32 |
import java.util.Collection; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
33 |
import java.util.Collections; |
58302 | 34 |
import java.util.HashMap; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
35 |
import java.util.List; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
36 |
import java.util.Map; |
58302 | 37 |
import java.util.function.BiFunction; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
38 |
import java.util.stream.Collectors; |
58670
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
39 |
import java.util.stream.Stream; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
40 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
41 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
42 |
/** |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
43 |
* Group of paths. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
44 |
* Each path in the group is assigned a unique id. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
45 |
*/ |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
46 |
final class PathGroup { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
47 |
PathGroup(Map<Object, Path> paths) { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
48 |
entries = new HashMap<>(paths); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
49 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
50 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
51 |
Path getPath(Object id) { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
52 |
if (id == null) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
53 |
throw new NullPointerException(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
54 |
} |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
55 |
return entries.get(id); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
56 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
57 |
|
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
58 |
void setPath(Object id, Path path) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
59 |
if (path != null) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
60 |
entries.put(id, path); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
61 |
} else { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
62 |
entries.remove(id); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
63 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
64 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
65 |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
66 |
/** |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
67 |
* All configured entries. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
68 |
*/ |
58302 | 69 |
List<Path> paths() { |
70 |
return entries.values().stream().collect(Collectors.toList()); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
71 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
72 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
73 |
/** |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
74 |
* Root entries. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
75 |
*/ |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
76 |
List<Path> roots() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
77 |
// Sort by the number of path components in ascending order. |
58302 | 78 |
List<Map.Entry<Path, Path>> sorted = normalizedPaths().stream().sorted( |
79 |
(a, b) -> a.getKey().getNameCount() - b.getKey().getNameCount()).collect( |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
80 |
Collectors.toList()); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
81 |
|
58302 | 82 |
// Returns `true` if `a` is a parent of `b` |
83 |
BiFunction<Map.Entry<Path, Path>, Map.Entry<Path, Path>, Boolean> isParentOrSelf = (a, b) -> { |
|
84 |
return a == b || b.getKey().startsWith(a.getKey()); |
|
85 |
}; |
|
86 |
||
87 |
return sorted.stream().filter( |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
88 |
v -> v == sorted.stream().sequential().filter( |
58302 | 89 |
v2 -> isParentOrSelf.apply(v2, v)).findFirst().get()).map( |
90 |
v -> v.getValue()).collect(Collectors.toList()); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
91 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
92 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
93 |
long sizeInBytes() throws IOException { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
94 |
long reply = 0; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
95 |
for (Path dir : roots().stream().filter(f -> Files.isDirectory(f)).collect( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
96 |
Collectors.toList())) { |
58670
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
97 |
try (Stream<Path> stream = Files.walk(dir)) { |
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
98 |
reply += stream.filter(p -> Files.isRegularFile(p)).mapToLong( |
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
99 |
f -> f.toFile().length()).sum(); |
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
100 |
} |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
101 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
102 |
return reply; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
103 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
104 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
105 |
PathGroup resolveAt(Path root) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
106 |
return new PathGroup(entries.entrySet().stream().collect( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
107 |
Collectors.toMap(e -> e.getKey(), |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
108 |
e -> root.resolve(e.getValue())))); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
109 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
110 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
111 |
void copy(PathGroup dst) throws IOException { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
112 |
copy(this, dst, null, false); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
113 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
114 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
115 |
void move(PathGroup dst) throws IOException { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
116 |
copy(this, dst, null, true); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
117 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
118 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
119 |
void transform(PathGroup dst, TransformHandler handler) throws IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
120 |
copy(this, dst, handler, false); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
121 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
122 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
123 |
static interface Facade<T> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
124 |
PathGroup pathGroup(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
125 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
126 |
default Collection<Path> paths() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
127 |
return pathGroup().paths(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
128 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
129 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
130 |
default List<Path> roots() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
131 |
return pathGroup().roots(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
132 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
133 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
134 |
default long sizeInBytes() throws IOException { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
135 |
return pathGroup().sizeInBytes(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
136 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
137 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
138 |
T resolveAt(Path root); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
139 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
140 |
default void copy(Facade<T> dst) throws IOException { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
141 |
pathGroup().copy(dst.pathGroup()); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
142 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
143 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
144 |
default void move(Facade<T> dst) throws IOException { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
145 |
pathGroup().move(dst.pathGroup()); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
146 |
} |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
147 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
148 |
default void transform(Facade<T> dst, TransformHandler handler) throws |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
149 |
IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
150 |
pathGroup().transform(dst.pathGroup(), handler); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
151 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
152 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
153 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
154 |
static interface TransformHandler { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
155 |
public void copyFile(Path src, Path dst) throws IOException; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
156 |
public void createDirectory(Path dir) throws IOException; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
157 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
158 |
|
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
159 |
private static void copy(PathGroup src, PathGroup dst, |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
160 |
TransformHandler handler, boolean move) throws IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
161 |
List<Map.Entry<Path, Path>> copyItems = new ArrayList<>(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
162 |
List<Path> excludeItems = new ArrayList<>(); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
163 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
164 |
for (var id: src.entries.keySet()) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
165 |
Path srcPath = src.entries.get(id); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
166 |
if (dst.entries.containsKey(id)) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
167 |
copyItems.add(Map.entry(srcPath, dst.entries.get(id))); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
168 |
} else { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
169 |
excludeItems.add(srcPath); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
170 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
171 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
172 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
173 |
copy(move, copyItems, excludeItems, handler); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
174 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
175 |
|
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
176 |
private static void copy(boolean move, List<Map.Entry<Path, Path>> entries, |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
177 |
List<Path> excludePaths, TransformHandler handler) throws |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
178 |
IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
179 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
180 |
if (handler == null) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
181 |
handler = new TransformHandler() { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
182 |
@Override |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
183 |
public void copyFile(Path src, Path dst) throws IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
184 |
Files.createDirectories(dst.getParent()); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
185 |
if (move) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
186 |
Files.move(src, dst); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
187 |
} else { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
188 |
Files.copy(src, dst); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
189 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
190 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
191 |
|
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
192 |
@Override |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
193 |
public void createDirectory(Path dir) throws IOException { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
194 |
Files.createDirectories(dir); |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
195 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
196 |
}; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
197 |
} |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
198 |
|
58302 | 199 |
// destination -> source file mapping |
200 |
Map<Path, Path> actions = new HashMap<>(); |
|
201 |
for (var action: entries) { |
|
202 |
Path src = action.getKey(); |
|
203 |
Path dst = action.getValue(); |
|
204 |
if (src.toFile().isDirectory()) { |
|
58670
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
205 |
try (Stream<Path> stream = Files.walk(src)) { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
206 |
stream.sequential().forEach(path -> actions.put(dst.resolve( |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
207 |
src.relativize(path)).normalize(), path)); |
58670
6fb9e12d5595
8232280: close() is not called on return value of Files.walk()
herrick
parents:
58302
diff
changeset
|
208 |
} |
58302 | 209 |
} else { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
210 |
actions.put(dst.normalize(), src); |
58302 | 211 |
} |
212 |
} |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
213 |
|
58302 | 214 |
for (var action : actions.entrySet()) { |
215 |
Path dst = action.getKey(); |
|
216 |
Path src = action.getValue(); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
217 |
|
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
218 |
if (excludePaths.stream().anyMatch(src::startsWith)) { |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
219 |
continue; |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
220 |
} |
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
221 |
|
58302 | 222 |
if (src.equals(dst) || !src.toFile().exists()) { |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
223 |
continue; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
224 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
225 |
|
58302 | 226 |
if (src.toFile().isDirectory()) { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
227 |
handler.createDirectory(dst); |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
228 |
} else { |
58696
61c44899b4eb
8223325: Improve wix sources generated by jpackage
herrick
parents:
58670
diff
changeset
|
229 |
handler.copyFile(src, dst); |
58302 | 230 |
} |
231 |
} |
|
232 |
||
233 |
if (move) { |
|
234 |
// Delete source dirs. |
|
235 |
for (var entry: entries) { |
|
236 |
File srcFile = entry.getKey().toFile(); |
|
237 |
if (srcFile.isDirectory()) { |
|
238 |
IOUtils.deleteRecursive(srcFile); |
|
239 |
} |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
240 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
241 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
242 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
243 |
|
58302 | 244 |
private static Map.Entry<Path, Path> normalizedPath(Path v) { |
245 |
final Path normalized; |
|
246 |
if (!v.isAbsolute()) { |
|
247 |
normalized = Path.of("./").resolve(v.normalize()); |
|
248 |
} else { |
|
249 |
normalized = v.normalize(); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
250 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
251 |
|
58302 | 252 |
return Map.entry(normalized, v); |
253 |
} |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
254 |
|
58302 | 255 |
private List<Map.Entry<Path, Path>> normalizedPaths() { |
256 |
return entries.values().stream().map(PathGroup::normalizedPath).collect( |
|
257 |
Collectors.toList()); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
258 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
259 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
260 |
private final Map<Object, Path> entries; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff
changeset
|
261 |
} |