author | herrick |
Wed, 23 Oct 2019 10:37:54 -0400 | |
branch | JDK-8200758-branch |
changeset 58762 | 0fe62353385b |
parent 58696 | 61c44899b4eb |
child 58887 | 920f6770d71c |
permissions | -rw-r--r-- |
58696 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jpackage.internal; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.charset.StandardCharsets; |
|
30 |
import java.nio.file.Path; |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
31 |
import java.text.MessageFormat; |
58696 | 32 |
import java.util.*; |
33 |
import java.util.function.*; |
|
34 |
import java.util.stream.Collectors; |
|
35 |
import java.util.stream.Stream; |
|
36 |
import javax.xml.stream.XMLStreamException; |
|
37 |
import javax.xml.stream.XMLStreamWriter; |
|
38 |
import jdk.jpackage.internal.IOUtils.XmlConsumer; |
|
39 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
|
40 |
import static jdk.jpackage.internal.WinMsiBundler.*; |
|
41 |
import static jdk.jpackage.internal.WindowsBundlerParam.MENU_GROUP; |
|
42 |
import static jdk.jpackage.internal.WindowsBundlerParam.WINDOWS_INSTALL_DIR; |
|
43 |
||
44 |
/** |
|
45 |
* Creates application WiX source files. |
|
46 |
*/ |
|
47 |
class WixSourcesBuilder { |
|
48 |
||
49 |
WixSourcesBuilder setWixVersion(DottedVersion v) { |
|
50 |
wixVersion = v; |
|
51 |
return this; |
|
52 |
} |
|
53 |
||
54 |
WixSourcesBuilder initFromParams(Path appImageRoot, |
|
55 |
Map<String, ? super Object> params) { |
|
56 |
Supplier<ApplicationLayout> appImageSupplier = () -> { |
|
57 |
if (StandardBundlerParam.isRuntimeInstaller(params)) { |
|
58 |
return ApplicationLayout.javaRuntime(); |
|
59 |
} else { |
|
60 |
return ApplicationLayout.platformAppImage(); |
|
61 |
} |
|
62 |
}; |
|
63 |
||
64 |
systemWide = MSI_SYSTEM_WIDE.fetchFrom(params); |
|
65 |
||
66 |
registryKeyPath = Path.of("Software", |
|
67 |
VENDOR.fetchFrom(params), |
|
68 |
APP_NAME.fetchFrom(params), |
|
69 |
VERSION.fetchFrom(params)).toString(); |
|
70 |
||
71 |
installDir = (systemWide ? PROGRAM_FILES : LOCAL_PROGRAM_FILES).resolve( |
|
72 |
WINDOWS_INSTALL_DIR.fetchFrom(params)); |
|
73 |
||
74 |
do { |
|
75 |
ApplicationLayout layout = appImageSupplier.get(); |
|
76 |
// Don't want AppImageFile.FILENAME in installed application. |
|
77 |
// Register it with app image at a role without a match in installed |
|
78 |
// app layout to exclude it from layout transformation. |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
79 |
layout.pathGroup().setPath(new Object(), |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
80 |
AppImageFile.getPathInAppImage(Path.of(""))); |
58696 | 81 |
|
82 |
// Want absolute paths to source files in generated WiX sources. |
|
83 |
// This is to handle scenario if sources would be processed from |
|
84 |
// differnt current directory. |
|
85 |
appImage = layout.resolveAt(appImageRoot.toAbsolutePath().normalize()); |
|
86 |
} while (false); |
|
87 |
||
88 |
installedAppImage = appImageSupplier.get().resolveAt(INSTALLDIR); |
|
89 |
||
90 |
shortcutFolders = new HashSet<>(); |
|
91 |
if (SHORTCUT_HINT.fetchFrom(params)) { |
|
92 |
shortcutFolders.add(ShortcutsFolder.Desktop); |
|
93 |
} |
|
94 |
if (MENU_HINT.fetchFrom(params)) { |
|
95 |
shortcutFolders.add(ShortcutsFolder.ProgramMenu); |
|
96 |
} |
|
97 |
||
98 |
if (StandardBundlerParam.isRuntimeInstaller(params)) { |
|
99 |
launcherPaths = Collections.emptyList(); |
|
100 |
} else { |
|
101 |
launcherPaths = AppImageFile.getLauncherNames(appImageRoot, params).stream() |
|
102 |
.map(name -> installedAppImage.launchersDirectory().resolve(name)) |
|
103 |
.map(WixSourcesBuilder::addExeSuffixToPath) |
|
104 |
.collect(Collectors.toList()); |
|
105 |
} |
|
106 |
||
107 |
programMenuFolderName = MENU_GROUP.fetchFrom(params); |
|
108 |
||
109 |
initFileAssociations(params); |
|
110 |
||
111 |
return this; |
|
112 |
} |
|
113 |
||
114 |
void createMainFragment(Path file) throws IOException { |
|
115 |
removeFolderItems = new HashMap<>(); |
|
116 |
defaultedMimes = new HashSet<>(); |
|
117 |
IOUtils.createXml(file, xml -> { |
|
118 |
xml.writeStartElement("Wix"); |
|
119 |
xml.writeDefaultNamespace("http://schemas.microsoft.com/wix/2006/wi"); |
|
120 |
xml.writeNamespace("util", |
|
121 |
"http://schemas.microsoft.com/wix/UtilExtension"); |
|
122 |
||
123 |
xml.writeStartElement("Fragment"); |
|
124 |
||
125 |
addFaComponentGroup(xml); |
|
126 |
||
127 |
addShortcutComponentGroup(xml); |
|
128 |
||
129 |
addFilesComponentGroup(xml); |
|
130 |
||
131 |
xml.writeEndElement(); // <Fragment> |
|
132 |
||
133 |
addIconsFragment(xml); |
|
134 |
||
135 |
xml.writeEndElement(); // <Wix> |
|
136 |
}); |
|
137 |
} |
|
138 |
||
139 |
void logWixFeatures() { |
|
140 |
if (wixVersion.compareTo("3.6") >= 0) { |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
141 |
Log.verbose(MessageFormat.format(I18N.getString( |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
142 |
"message.use-wix36-features"), wixVersion)); |
58696 | 143 |
} |
144 |
} |
|
145 |
||
146 |
private void normalizeFileAssociation(FileAssociation fa) { |
|
147 |
fa.launcherPath = addExeSuffixToPath( |
|
148 |
installedAppImage.launchersDirectory().resolve(fa.launcherPath)); |
|
149 |
||
150 |
if (fa.iconPath != null && !fa.iconPath.toFile().exists()) { |
|
151 |
fa.iconPath = null; |
|
152 |
} |
|
153 |
||
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
154 |
if (fa.iconPath != null) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
155 |
fa.iconPath = fa.iconPath.toAbsolutePath(); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
156 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
157 |
|
58696 | 158 |
// Filter out empty extensions. |
159 |
fa.extensions = fa.extensions.stream().filter(Predicate.not( |
|
160 |
String::isEmpty)).collect(Collectors.toList()); |
|
161 |
} |
|
162 |
||
163 |
private static Path addExeSuffixToPath(Path path) { |
|
164 |
return IOUtils.addSuffix(path, ".exe"); |
|
165 |
} |
|
166 |
||
167 |
private Path getInstalledFaIcoPath(FileAssociation fa) { |
|
168 |
String fname = String.format("fa_%s.ico", String.join("_", fa.extensions)); |
|
169 |
return installedAppImage.destktopIntegrationDirectory().resolve(fname); |
|
170 |
} |
|
171 |
||
172 |
private void initFileAssociations(Map<String, ? super Object> params) { |
|
173 |
associations = FileAssociation.fetchFrom(params).stream() |
|
174 |
.peek(this::normalizeFileAssociation) |
|
175 |
// Filter out file associations without extensions. |
|
176 |
.filter(fa -> !fa.extensions.isEmpty()) |
|
177 |
.collect(Collectors.toList()); |
|
178 |
||
179 |
associations.stream().filter(fa -> fa.iconPath != null).forEach(fa -> { |
|
180 |
// Need to add fa icon in the image. |
|
181 |
Object key = new Object(); |
|
182 |
appImage.pathGroup().setPath(key, fa.iconPath); |
|
183 |
installedAppImage.pathGroup().setPath(key, getInstalledFaIcoPath(fa)); |
|
184 |
}); |
|
185 |
} |
|
186 |
||
187 |
private static UUID createNameUUID(String str) { |
|
188 |
return UUID.nameUUIDFromBytes(str.getBytes(StandardCharsets.UTF_8)); |
|
189 |
} |
|
190 |
||
191 |
private static UUID createNameUUID(Path path, String role) { |
|
192 |
if (path.isAbsolute() || !ROOT_DIRS.contains(path.getName(0))) { |
|
193 |
throw throwInvalidPathException(path); |
|
194 |
} |
|
195 |
// Paths are case insensitive on Windows |
|
196 |
String keyPath = path.toString().toLowerCase(); |
|
197 |
if (role != null) { |
|
198 |
keyPath = role + "@" + keyPath; |
|
199 |
} |
|
200 |
return createNameUUID(keyPath); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Value for Id attribute of various WiX elements. |
|
205 |
*/ |
|
206 |
enum Id { |
|
207 |
File, |
|
208 |
Folder("dir"), |
|
209 |
Shortcut, |
|
210 |
ProgId, |
|
211 |
Icon, |
|
212 |
CreateFolder("mkdir"), |
|
213 |
RemoveFolder("rm"); |
|
214 |
||
215 |
Id() { |
|
216 |
this.prefix = name().toLowerCase(); |
|
217 |
} |
|
218 |
||
219 |
Id(String prefix) { |
|
220 |
this.prefix = prefix; |
|
221 |
} |
|
222 |
||
223 |
String of(Path path) { |
|
224 |
if (this == Folder && KNOWN_DIRS.contains(path)) { |
|
225 |
return path.getFileName().toString(); |
|
226 |
} |
|
227 |
||
228 |
String result = of(path, prefix, name()); |
|
229 |
||
230 |
if (this == Icon) { |
|
231 |
// Icon id constructed from UUID value is too long and triggers |
|
232 |
// CNDL1000 warning, so use Java hash code instead. |
|
233 |
result = String.format("%s%d", prefix, result.hashCode()).replace( |
|
234 |
"-", "_"); |
|
235 |
} |
|
236 |
||
237 |
return result; |
|
238 |
} |
|
239 |
||
240 |
private static String of(Path path, String prefix, String role) { |
|
241 |
Objects.requireNonNull(role); |
|
242 |
Objects.requireNonNull(prefix); |
|
243 |
return String.format("%s%s", prefix, |
|
244 |
createNameUUID(path, role).toString().replace("-", "")); |
|
245 |
} |
|
246 |
||
247 |
static String of(Path path, String prefix) { |
|
248 |
return of(path, prefix, prefix); |
|
249 |
} |
|
250 |
||
251 |
private final String prefix; |
|
252 |
} |
|
253 |
||
254 |
enum Component { |
|
255 |
File(cfg().file()), |
|
256 |
Shortcut(cfg().file().withRegistryKeyPath()), |
|
257 |
ProgId(cfg().file().withRegistryKeyPath()), |
|
258 |
CreateFolder(cfg().withRegistryKeyPath()), |
|
259 |
RemoveFolder(cfg().withRegistryKeyPath()); |
|
260 |
||
261 |
Component() { |
|
262 |
this.cfg = cfg(); |
|
263 |
this.id = Id.valueOf(name()); |
|
264 |
} |
|
265 |
||
266 |
Component(Config cfg) { |
|
267 |
this.cfg = cfg; |
|
268 |
this.id = Id.valueOf(name()); |
|
269 |
} |
|
270 |
||
271 |
UUID guidOf(Path path) { |
|
272 |
return createNameUUID(path, name()); |
|
273 |
} |
|
274 |
||
275 |
String idOf(Path path) { |
|
276 |
return id.of(path); |
|
277 |
} |
|
278 |
||
279 |
boolean isRegistryKeyPath() { |
|
280 |
return cfg.withRegistryKeyPath; |
|
281 |
} |
|
282 |
||
283 |
boolean isFile() { |
|
284 |
return cfg.isFile; |
|
285 |
} |
|
286 |
||
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
287 |
static void startElement(XMLStreamWriter xml, String componentId, |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
288 |
String componentGuid) throws XMLStreamException, IOException { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
289 |
xml.writeStartElement("Component"); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
290 |
xml.writeAttribute("Win64", "yes"); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
291 |
xml.writeAttribute("Id", componentId); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
292 |
xml.writeAttribute("Guid", componentGuid); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
293 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
294 |
|
58696 | 295 |
private static final class Config { |
296 |
Config withRegistryKeyPath() { |
|
297 |
withRegistryKeyPath = true; |
|
298 |
return this; |
|
299 |
} |
|
300 |
||
301 |
Config file() { |
|
302 |
isFile = true; |
|
303 |
return this; |
|
304 |
} |
|
305 |
||
306 |
private boolean isFile; |
|
307 |
private boolean withRegistryKeyPath; |
|
308 |
} |
|
309 |
||
310 |
private static Config cfg() { |
|
311 |
return new Config(); |
|
312 |
} |
|
313 |
||
314 |
private final Config cfg; |
|
315 |
private final Id id; |
|
316 |
}; |
|
317 |
||
318 |
private static void addComponentGroup(XMLStreamWriter xml, String id, |
|
319 |
List<String> componentIds) throws XMLStreamException, IOException { |
|
320 |
xml.writeStartElement("ComponentGroup"); |
|
321 |
xml.writeAttribute("Id", id); |
|
322 |
componentIds = componentIds.stream().filter(Objects::nonNull).collect( |
|
323 |
Collectors.toList()); |
|
324 |
for (var componentId : componentIds) { |
|
325 |
xml.writeStartElement("ComponentRef"); |
|
326 |
xml.writeAttribute("Id", componentId); |
|
327 |
xml.writeEndElement(); |
|
328 |
} |
|
329 |
xml.writeEndElement(); |
|
330 |
} |
|
331 |
||
332 |
private String addComponent(XMLStreamWriter xml, Path path, |
|
333 |
Component role, XmlConsumer xmlConsumer) throws XMLStreamException, |
|
334 |
IOException { |
|
335 |
||
336 |
final Path directoryRefPath; |
|
337 |
if (role.isFile()) { |
|
338 |
directoryRefPath = path.getParent(); |
|
339 |
} else { |
|
340 |
directoryRefPath = path; |
|
341 |
} |
|
342 |
||
343 |
xml.writeStartElement("DirectoryRef"); |
|
344 |
xml.writeAttribute("Id", Id.Folder.of(directoryRefPath)); |
|
345 |
||
346 |
final String componentId = "c" + role.idOf(path); |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
347 |
Component.startElement(xml, componentId, String.format("{%s}", |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
348 |
role.guidOf(path))); |
58696 | 349 |
|
350 |
boolean isRegistryKeyPath = !systemWide || role.isRegistryKeyPath(); |
|
351 |
if (isRegistryKeyPath) { |
|
352 |
addRegistryKeyPath(xml, directoryRefPath); |
|
353 |
if ((role.isFile() || (role == Component.CreateFolder |
|
354 |
&& !systemWide)) && !SYSTEM_DIRS.contains(directoryRefPath)) { |
|
355 |
xml.writeStartElement("RemoveFolder"); |
|
356 |
int counter = Optional.ofNullable(removeFolderItems.get( |
|
357 |
directoryRefPath)).orElse(Integer.valueOf(0)).intValue() + 1; |
|
358 |
removeFolderItems.put(directoryRefPath, counter); |
|
359 |
xml.writeAttribute("Id", String.format("%s_%d", Id.RemoveFolder.of( |
|
360 |
directoryRefPath), counter)); |
|
361 |
xml.writeAttribute("On", "uninstall"); |
|
362 |
xml.writeEndElement(); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
xml.writeStartElement(role.name()); |
|
367 |
if (role != Component.CreateFolder) { |
|
368 |
xml.writeAttribute("Id", role.idOf(path)); |
|
369 |
} |
|
370 |
||
371 |
if (!isRegistryKeyPath) { |
|
372 |
xml.writeAttribute("KeyPath", "yes"); |
|
373 |
} |
|
374 |
||
375 |
xmlConsumer.accept(xml); |
|
376 |
xml.writeEndElement(); |
|
377 |
||
378 |
xml.writeEndElement(); // <Component> |
|
379 |
xml.writeEndElement(); // <DirectoryRef> |
|
380 |
||
381 |
return componentId; |
|
382 |
} |
|
383 |
||
384 |
private void addFaComponentGroup(XMLStreamWriter xml) |
|
385 |
throws XMLStreamException, IOException { |
|
386 |
||
387 |
List<String> componentIds = new ArrayList<>(); |
|
388 |
for (var fa : associations) { |
|
389 |
componentIds.addAll(addFaComponents(xml, fa)); |
|
390 |
} |
|
391 |
addComponentGroup(xml, "FileAssociations", componentIds); |
|
392 |
} |
|
393 |
||
394 |
private void addShortcutComponentGroup(XMLStreamWriter xml) throws |
|
395 |
XMLStreamException, IOException { |
|
396 |
List<String> componentIds = new ArrayList<>(); |
|
397 |
Set<ShortcutsFolder> defineShortcutFolders = new HashSet<>(); |
|
398 |
for (var launcherPath : launcherPaths) { |
|
399 |
for (var folder : shortcutFolders) { |
|
400 |
String componentId = addShortcutComponent(xml, launcherPath, |
|
401 |
folder); |
|
402 |
if (componentId != null) { |
|
403 |
defineShortcutFolders.add(folder); |
|
404 |
componentIds.add(componentId); |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
for (var folder : defineShortcutFolders) { |
|
410 |
Path path = folder.getPath(this); |
|
411 |
componentIds.addAll(addRootBranch(xml, path)); |
|
412 |
||
413 |
if (!KNOWN_DIRS.contains(path)) { |
|
414 |
componentIds.add(addDirectoryCleaner(xml, path)); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
addComponentGroup(xml, "Shortcuts", componentIds); |
|
419 |
} |
|
420 |
||
421 |
private String addShortcutComponent(XMLStreamWriter xml, Path launcherPath, |
|
422 |
ShortcutsFolder folder) throws XMLStreamException, IOException { |
|
423 |
Objects.requireNonNull(folder); |
|
424 |
||
425 |
if (!INSTALLDIR.equals(launcherPath.getName(0))) { |
|
426 |
throw throwInvalidPathException(launcherPath); |
|
427 |
} |
|
428 |
||
429 |
String launcherBasename = IOUtils.replaceSuffix( |
|
430 |
launcherPath.getFileName(), "").toString(); |
|
431 |
||
432 |
Path shortcutPath = folder.getPath(this).resolve(launcherBasename); |
|
433 |
return addComponent(xml, shortcutPath, Component.Shortcut, unused -> { |
|
434 |
final Path icoFile = IOUtils.addSuffix( |
|
435 |
installedAppImage.destktopIntegrationDirectory().resolve( |
|
436 |
launcherBasename), ".ico"); |
|
437 |
||
438 |
xml.writeAttribute("Name", launcherBasename); |
|
439 |
xml.writeAttribute("WorkingDirectory", INSTALLDIR.toString()); |
|
440 |
xml.writeAttribute("Advertise", "no"); |
|
441 |
xml.writeAttribute("IconIndex", "0"); |
|
442 |
xml.writeAttribute("Target", String.format("[#%s]", |
|
443 |
Component.File.idOf(launcherPath))); |
|
444 |
xml.writeAttribute("Icon", Id.Icon.of(icoFile)); |
|
445 |
}); |
|
446 |
} |
|
447 |
||
448 |
private List<String> addFaComponents(XMLStreamWriter xml, |
|
449 |
FileAssociation fa) throws XMLStreamException, IOException { |
|
450 |
List<String> components = new ArrayList<>(); |
|
451 |
for (var extension: fa.extensions) { |
|
452 |
Path path = INSTALLDIR.resolve(String.format("%s_%s", extension, |
|
453 |
fa.launcherPath.getFileName())); |
|
454 |
components.add(addComponent(xml, path, Component.ProgId, unused -> { |
|
455 |
xml.writeAttribute("Description", fa.description); |
|
456 |
||
457 |
if (fa.iconPath != null) { |
|
458 |
xml.writeAttribute("Icon", Id.File.of(getInstalledFaIcoPath( |
|
459 |
fa))); |
|
460 |
xml.writeAttribute("IconIndex", "0"); |
|
461 |
} |
|
462 |
||
463 |
xml.writeStartElement("Extension"); |
|
464 |
xml.writeAttribute("Id", extension); |
|
465 |
xml.writeAttribute("Advertise", "no"); |
|
466 |
||
467 |
var mimeIt = fa.mimeTypes.iterator(); |
|
468 |
if (mimeIt.hasNext()) { |
|
469 |
String mime = mimeIt.next(); |
|
470 |
xml.writeAttribute("ContentType", mime); |
|
471 |
||
472 |
if (!defaultedMimes.contains(mime)) { |
|
473 |
xml.writeStartElement("MIME"); |
|
474 |
xml.writeAttribute("ContentType", mime); |
|
475 |
xml.writeAttribute("Default", "yes"); |
|
476 |
xml.writeEndElement(); |
|
477 |
defaultedMimes.add(mime); |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
xml.writeStartElement("Verb"); |
|
482 |
xml.writeAttribute("Id", "open"); |
|
483 |
xml.writeAttribute("Command", "Open"); |
|
484 |
xml.writeAttribute("Argument", "%1"); |
|
485 |
xml.writeAttribute("TargetFile", Id.File.of(fa.launcherPath)); |
|
486 |
xml.writeEndElement(); // <Verb> |
|
487 |
||
488 |
xml.writeEndElement(); // <Extension> |
|
489 |
})); |
|
490 |
} |
|
491 |
||
492 |
return components; |
|
493 |
} |
|
494 |
||
495 |
private List<String> addRootBranch(XMLStreamWriter xml, Path path) |
|
496 |
throws XMLStreamException, IOException { |
|
497 |
if (!ROOT_DIRS.contains(path.getName(0))) { |
|
498 |
throw throwInvalidPathException(path); |
|
499 |
} |
|
500 |
||
501 |
Function<Path, String> createDirectoryName = dir -> null; |
|
502 |
||
503 |
boolean sysDir = true; |
|
504 |
int levels = 1; |
|
505 |
var dirIt = path.iterator(); |
|
506 |
xml.writeStartElement("DirectoryRef"); |
|
507 |
xml.writeAttribute("Id", dirIt.next().toString()); |
|
508 |
||
509 |
path = path.getName(0); |
|
510 |
while (dirIt.hasNext()) { |
|
511 |
levels++; |
|
512 |
Path name = dirIt.next(); |
|
513 |
path = path.resolve(name); |
|
514 |
||
515 |
if (sysDir && !SYSTEM_DIRS.contains(path)) { |
|
516 |
sysDir = false; |
|
517 |
createDirectoryName = dir -> dir.getFileName().toString(); |
|
518 |
} |
|
519 |
||
520 |
final String directoryId; |
|
521 |
if (!sysDir && path.equals(installDir)) { |
|
522 |
directoryId = INSTALLDIR.toString(); |
|
523 |
} else { |
|
524 |
directoryId = Id.Folder.of(path); |
|
525 |
} |
|
526 |
xml.writeStartElement("Directory"); |
|
527 |
xml.writeAttribute("Id", directoryId); |
|
528 |
||
529 |
String directoryName = createDirectoryName.apply(path); |
|
530 |
if (directoryName != null) { |
|
531 |
xml.writeAttribute("Name", directoryName); |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
while (0 != levels--) { |
|
536 |
xml.writeEndElement(); |
|
537 |
} |
|
538 |
||
539 |
List<String> componentIds = new ArrayList<>(); |
|
540 |
while (!SYSTEM_DIRS.contains(path = path.getParent())) { |
|
541 |
componentIds.add(addRemoveDirectoryComponent(xml, path)); |
|
542 |
} |
|
543 |
||
544 |
return componentIds; |
|
545 |
} |
|
546 |
||
547 |
private String addRemoveDirectoryComponent(XMLStreamWriter xml, Path path) |
|
548 |
throws XMLStreamException, IOException { |
|
549 |
return addComponent(xml, path, Component.RemoveFolder, |
|
550 |
unused -> xml.writeAttribute("On", "uninstall")); |
|
551 |
} |
|
552 |
||
553 |
private List<String> addDirectoryHierarchy(XMLStreamWriter xml) |
|
554 |
throws XMLStreamException, IOException { |
|
555 |
||
556 |
Set<Path> allDirs = new HashSet<>(); |
|
557 |
Set<Path> emptyDirs = new HashSet<>(); |
|
558 |
appImage.transform(installedAppImage, new PathGroup.TransformHandler() { |
|
559 |
@Override |
|
560 |
public void copyFile(Path src, Path dst) throws IOException { |
|
561 |
Path dir = dst.getParent(); |
|
562 |
createDirectory(dir); |
|
563 |
emptyDirs.remove(dir); |
|
564 |
} |
|
565 |
||
566 |
@Override |
|
567 |
public void createDirectory(final Path dir) throws IOException { |
|
568 |
if (!allDirs.contains(dir)) { |
|
569 |
emptyDirs.add(dir); |
|
570 |
} |
|
571 |
||
572 |
Path it = dir; |
|
573 |
while (it != null && allDirs.add(it)) { |
|
574 |
it = it.getParent(); |
|
575 |
} |
|
576 |
||
577 |
it = dir; |
|
578 |
while ((it = it.getParent()) != null && emptyDirs.remove(it)); |
|
579 |
} |
|
580 |
}); |
|
581 |
||
582 |
List<String> componentIds = new ArrayList<>(); |
|
583 |
for (var dir : emptyDirs) { |
|
584 |
componentIds.add(addComponent(xml, dir, Component.CreateFolder, |
|
585 |
unused -> {})); |
|
586 |
} |
|
587 |
||
588 |
if (!systemWide) { |
|
589 |
// Per-user install requires <RemoveFolder> component in every |
|
590 |
// directory. |
|
591 |
for (var dir : allDirs.stream() |
|
592 |
.filter(Predicate.not(emptyDirs::contains)) |
|
593 |
.filter(Predicate.not(removeFolderItems::containsKey)) |
|
594 |
.collect(Collectors.toList())) { |
|
595 |
componentIds.add(addRemoveDirectoryComponent(xml, dir)); |
|
596 |
} |
|
597 |
} |
|
598 |
||
599 |
allDirs.remove(INSTALLDIR); |
|
600 |
for (var dir : allDirs) { |
|
601 |
xml.writeStartElement("DirectoryRef"); |
|
602 |
xml.writeAttribute("Id", Id.Folder.of(dir.getParent())); |
|
603 |
xml.writeStartElement("Directory"); |
|
604 |
xml.writeAttribute("Id", Id.Folder.of(dir)); |
|
605 |
xml.writeAttribute("Name", dir.getFileName().toString()); |
|
606 |
xml.writeEndElement(); |
|
607 |
xml.writeEndElement(); |
|
608 |
} |
|
609 |
||
610 |
componentIds.addAll(addRootBranch(xml, installDir)); |
|
611 |
||
612 |
return componentIds; |
|
613 |
} |
|
614 |
||
615 |
private void addFilesComponentGroup(XMLStreamWriter xml) |
|
616 |
throws XMLStreamException, IOException { |
|
617 |
||
618 |
List<Map.Entry<Path, Path>> files = new ArrayList<>(); |
|
619 |
appImage.transform(installedAppImage, new PathGroup.TransformHandler() { |
|
620 |
@Override |
|
621 |
public void copyFile(Path src, Path dst) throws IOException { |
|
622 |
files.add(Map.entry(src, dst)); |
|
623 |
} |
|
624 |
||
625 |
@Override |
|
626 |
public void createDirectory(final Path dir) throws IOException { |
|
627 |
} |
|
628 |
}); |
|
629 |
||
630 |
List<String> componentIds = new ArrayList<>(); |
|
631 |
for (var file : files) { |
|
632 |
Path src = file.getKey(); |
|
633 |
Path dst = file.getValue(); |
|
634 |
||
635 |
componentIds.add(addComponent(xml, dst, Component.File, unused -> { |
|
636 |
xml.writeAttribute("Source", src.normalize().toString()); |
|
637 |
Path name = dst.getFileName(); |
|
638 |
if (!name.equals(src.getFileName())) { |
|
639 |
xml.writeAttribute("Name", name.toString()); |
|
640 |
} |
|
641 |
})); |
|
642 |
} |
|
643 |
||
644 |
componentIds.addAll(addDirectoryHierarchy(xml)); |
|
645 |
||
646 |
componentIds.add(addDirectoryCleaner(xml, INSTALLDIR)); |
|
647 |
||
648 |
addComponentGroup(xml, "Files", componentIds); |
|
649 |
} |
|
650 |
||
651 |
private void addIconsFragment(XMLStreamWriter xml) throws |
|
652 |
XMLStreamException, IOException { |
|
653 |
||
654 |
PathGroup srcPathGroup = appImage.pathGroup(); |
|
655 |
PathGroup dstPathGroup = installedAppImage.pathGroup(); |
|
656 |
||
657 |
// Build list of copy operations for all .ico files in application image |
|
658 |
List<Map.Entry<Path, Path>> icoFiles = new ArrayList<>(); |
|
659 |
srcPathGroup.transform(dstPathGroup, new PathGroup.TransformHandler() { |
|
660 |
@Override |
|
661 |
public void copyFile(Path src, Path dst) throws IOException { |
|
662 |
if (src.getFileName().toString().endsWith(".ico")) { |
|
663 |
icoFiles.add(Map.entry(src, dst)); |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
@Override |
|
668 |
public void createDirectory(Path dst) throws IOException { |
|
669 |
} |
|
670 |
}); |
|
671 |
||
672 |
xml.writeStartElement("Fragment"); |
|
673 |
for (var icoFile : icoFiles) { |
|
674 |
xml.writeStartElement("Icon"); |
|
675 |
xml.writeAttribute("Id", Id.Icon.of(icoFile.getValue())); |
|
676 |
xml.writeAttribute("SourceFile", icoFile.getKey().toString()); |
|
677 |
xml.writeEndElement(); |
|
678 |
} |
|
679 |
xml.writeEndElement(); |
|
680 |
} |
|
681 |
||
682 |
private void addRegistryKeyPath(XMLStreamWriter xml, Path path) throws |
|
683 |
XMLStreamException, IOException { |
|
684 |
addRegistryKeyPath(xml, path, () -> "ProductCode", () -> "[ProductCode]"); |
|
685 |
} |
|
686 |
||
687 |
private void addRegistryKeyPath(XMLStreamWriter xml, Path path, |
|
688 |
Supplier<String> nameAttr, Supplier<String> valueAttr) throws |
|
689 |
XMLStreamException, IOException { |
|
690 |
||
691 |
String regRoot = USER_PROFILE_DIRS.stream().anyMatch(path::startsWith) |
|
692 |
|| !systemWide ? "HKCU" : "HKLM"; |
|
693 |
||
694 |
xml.writeStartElement("RegistryKey"); |
|
695 |
xml.writeAttribute("Root", regRoot); |
|
696 |
xml.writeAttribute("Key", registryKeyPath); |
|
697 |
if (wixVersion.compareTo("3.6") < 0) { |
|
698 |
xml.writeAttribute("Action", "createAndRemoveOnUninstall"); |
|
699 |
} |
|
700 |
xml.writeStartElement("RegistryValue"); |
|
701 |
xml.writeAttribute("Type", "string"); |
|
702 |
xml.writeAttribute("KeyPath", "yes"); |
|
703 |
xml.writeAttribute("Name", nameAttr.get()); |
|
704 |
xml.writeAttribute("Value", valueAttr.get()); |
|
705 |
xml.writeEndElement(); // <RegistryValue> |
|
706 |
xml.writeEndElement(); // <RegistryKey> |
|
707 |
} |
|
708 |
||
709 |
private String addDirectoryCleaner(XMLStreamWriter xml, Path path) throws |
|
710 |
XMLStreamException, IOException { |
|
711 |
if (wixVersion.compareTo("3.6") < 0) { |
|
712 |
return null; |
|
713 |
} |
|
714 |
||
715 |
// rm -rf |
|
716 |
final String baseId = Id.of(path, "rm_rf"); |
|
717 |
final String propertyId = baseId.toUpperCase(); |
|
718 |
final String componentId = ("c" + baseId); |
|
719 |
||
720 |
xml.writeStartElement("Property"); |
|
721 |
xml.writeAttribute("Id", propertyId); |
|
722 |
xml.writeStartElement("RegistrySearch"); |
|
723 |
xml.writeAttribute("Id", Id.of(path, "regsearch")); |
|
724 |
xml.writeAttribute("Root", systemWide ? "HKLM" : "HKCU"); |
|
725 |
xml.writeAttribute("Key", registryKeyPath); |
|
726 |
xml.writeAttribute("Type", "raw"); |
|
727 |
xml.writeAttribute("Name", propertyId); |
|
728 |
xml.writeEndElement(); // <RegistrySearch> |
|
729 |
xml.writeEndElement(); // <Property> |
|
730 |
||
731 |
xml.writeStartElement("DirectoryRef"); |
|
732 |
xml.writeAttribute("Id", INSTALLDIR.toString()); |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
733 |
Component.startElement(xml, componentId, "*"); |
58696 | 734 |
|
735 |
addRegistryKeyPath(xml, INSTALLDIR, () -> propertyId, () -> { |
|
736 |
// The following code converts a path to value to be saved in registry. |
|
737 |
// E.g.: |
|
738 |
// INSTALLDIR -> [INSTALLDIR] |
|
739 |
// TERGETDIR/ProgramFiles64Folder/foo/bar -> [ProgramFiles64Folder]foo/bar |
|
740 |
final Path rootDir = KNOWN_DIRS.stream() |
|
741 |
.sorted(Comparator.comparing(Path::getNameCount).reversed()) |
|
742 |
.filter(path::startsWith) |
|
743 |
.findFirst().get(); |
|
744 |
StringBuilder sb = new StringBuilder(); |
|
745 |
sb.append(String.format("[%s]", rootDir.getFileName().toString())); |
|
746 |
sb.append(rootDir.relativize(path).toString()); |
|
747 |
return sb.toString(); |
|
748 |
}); |
|
749 |
||
750 |
xml.writeStartElement( |
|
751 |
"http://schemas.microsoft.com/wix/UtilExtension", |
|
752 |
"RemoveFolderEx"); |
|
753 |
xml.writeAttribute("On", "uninstall"); |
|
754 |
xml.writeAttribute("Property", propertyId); |
|
755 |
xml.writeEndElement(); // <RemoveFolderEx> |
|
756 |
xml.writeEndElement(); // <Component> |
|
757 |
xml.writeEndElement(); // <DirectoryRef> |
|
758 |
||
759 |
return componentId; |
|
760 |
} |
|
761 |
||
762 |
private static IllegalArgumentException throwInvalidPathException(Path v) { |
|
763 |
throw new IllegalArgumentException(String.format("Invalid path [%s]", v)); |
|
764 |
} |
|
765 |
||
766 |
enum ShortcutsFolder { |
|
767 |
ProgramMenu(PROGRAM_MENU_PATH), |
|
768 |
Desktop(DESKTOP_PATH); |
|
769 |
||
770 |
private ShortcutsFolder(Path root) { |
|
771 |
this.root = root; |
|
772 |
} |
|
773 |
||
774 |
Path getPath(WixSourcesBuilder outer) { |
|
775 |
if (this == ProgramMenu) { |
|
776 |
return root.resolve(outer.programMenuFolderName); |
|
777 |
} |
|
778 |
return root; |
|
779 |
} |
|
780 |
||
781 |
private final Path root; |
|
782 |
} |
|
783 |
||
784 |
private DottedVersion wixVersion; |
|
785 |
||
786 |
private boolean systemWide; |
|
787 |
||
788 |
private String registryKeyPath; |
|
789 |
||
790 |
private Path installDir; |
|
791 |
||
792 |
private String programMenuFolderName; |
|
793 |
||
794 |
private List<FileAssociation> associations; |
|
795 |
||
796 |
private Set<ShortcutsFolder> shortcutFolders; |
|
797 |
||
798 |
private List<Path> launcherPaths; |
|
799 |
||
800 |
private ApplicationLayout appImage; |
|
801 |
private ApplicationLayout installedAppImage; |
|
802 |
||
803 |
private Map<Path, Integer> removeFolderItems; |
|
804 |
private Set<String> defaultedMimes; |
|
805 |
||
806 |
private final static Path TARGETDIR = Path.of("TARGETDIR"); |
|
807 |
||
808 |
private final static Path INSTALLDIR = Path.of("INSTALLDIR"); |
|
809 |
||
810 |
private final static Set<Path> ROOT_DIRS = Set.of(INSTALLDIR, TARGETDIR); |
|
811 |
||
812 |
private final static Path PROGRAM_MENU_PATH = TARGETDIR.resolve("ProgramMenuFolder"); |
|
813 |
||
814 |
private final static Path DESKTOP_PATH = TARGETDIR.resolve("DesktopFolder"); |
|
815 |
||
816 |
private final static Path PROGRAM_FILES = TARGETDIR.resolve("ProgramFiles64Folder"); |
|
817 |
||
818 |
private final static Path LOCAL_PROGRAM_FILES = TARGETDIR.resolve("LocalAppDataFolder"); |
|
819 |
||
820 |
private final static Set<Path> SYSTEM_DIRS = Set.of(TARGETDIR, |
|
821 |
PROGRAM_MENU_PATH, DESKTOP_PATH, PROGRAM_FILES, LOCAL_PROGRAM_FILES); |
|
822 |
||
823 |
private final static Set<Path> KNOWN_DIRS = Stream.of(Set.of(INSTALLDIR), |
|
824 |
SYSTEM_DIRS).flatMap(Set::stream).collect( |
|
825 |
Collectors.toUnmodifiableSet()); |
|
826 |
||
827 |
private final static Set<Path> USER_PROFILE_DIRS = Set.of(LOCAL_PROGRAM_FILES, |
|
828 |
PROGRAM_MENU_PATH, DESKTOP_PATH); |
|
829 |
||
830 |
private static final StandardBundlerParam<Boolean> MENU_HINT = |
|
831 |
new WindowsBundlerParam<>( |
|
832 |
Arguments.CLIOptions.WIN_MENU_HINT.getId(), |
|
833 |
Boolean.class, |
|
834 |
params -> false, |
|
835 |
// valueOf(null) is false, |
|
836 |
// and we actually do want null in some cases |
|
837 |
(s, p) -> (s == null || |
|
838 |
"null".equalsIgnoreCase(s))? true : Boolean.valueOf(s) |
|
839 |
); |
|
840 |
||
841 |
private static final StandardBundlerParam<Boolean> SHORTCUT_HINT = |
|
842 |
new WindowsBundlerParam<>( |
|
843 |
Arguments.CLIOptions.WIN_SHORTCUT_HINT.getId(), |
|
844 |
Boolean.class, |
|
845 |
params -> false, |
|
846 |
// valueOf(null) is false, |
|
847 |
// and we actually do want null in some cases |
|
848 |
(s, p) -> (s == null || |
|
849 |
"null".equalsIgnoreCase(s))? false : Boolean.valueOf(s) |
|
850 |
); |
|
851 |
} |