|
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.file.FileSystems; |
|
30 import java.nio.file.Files; |
|
31 import java.nio.file.Path; |
|
32 import java.nio.file.PathMatcher; |
|
33 import java.text.MessageFormat; |
|
34 import java.util.*; |
|
35 import java.util.function.Supplier; |
|
36 import java.util.stream.Collectors; |
|
37 import java.util.stream.Stream; |
|
38 |
|
39 /** |
|
40 * WiX tool. |
|
41 */ |
|
42 public enum WixTool { |
|
43 Candle, Light; |
|
44 |
|
45 static final class ToolInfo { |
|
46 ToolInfo(Path path, String version) { |
|
47 this.path = path; |
|
48 this.version = new DottedVersion(version); |
|
49 } |
|
50 |
|
51 final Path path; |
|
52 final DottedVersion version; |
|
53 } |
|
54 |
|
55 static Map<WixTool, ToolInfo> toolset() throws ConfigException { |
|
56 Map<WixTool, ToolInfo> toolset = new HashMap<>(); |
|
57 for (var tool : values()) { |
|
58 toolset.put(tool, tool.find()); |
|
59 } |
|
60 return toolset; |
|
61 } |
|
62 |
|
63 ToolInfo find() throws ConfigException { |
|
64 final Path toolFileName = IOUtils.addSuffix( |
|
65 Path.of(name().toLowerCase()), ".exe"); |
|
66 |
|
67 String[] version = new String[1]; |
|
68 ConfigException reason = createToolValidator(toolFileName, version).get(); |
|
69 if (version[0] != null) { |
|
70 if (reason == null) { |
|
71 // Found in PATH. |
|
72 return new ToolInfo(toolFileName, version[0]); |
|
73 } |
|
74 |
|
75 // Found in PATH, but something went wrong. |
|
76 throw reason; |
|
77 } |
|
78 |
|
79 for (var dir : findWixInstallDirs()) { |
|
80 Path path = dir.resolve(toolFileName); |
|
81 if (path.toFile().exists()) { |
|
82 reason = createToolValidator(path, version).get(); |
|
83 if (reason != null) { |
|
84 throw reason; |
|
85 } |
|
86 return new ToolInfo(path, version[0]); |
|
87 } |
|
88 } |
|
89 |
|
90 throw reason; |
|
91 } |
|
92 |
|
93 private static Supplier<ConfigException> createToolValidator(Path toolPath, |
|
94 String[] versionCtnr) { |
|
95 return new ToolValidator(toolPath) |
|
96 .setCommandLine("/?") |
|
97 .setMinimalVersion(MINIMAL_VERSION) |
|
98 .setToolNotFoundErrorHandler( |
|
99 (name, ex) -> new ConfigException( |
|
100 I18N.getString("error.no-wix-tools"), |
|
101 I18N.getString("error.no-wix-tools.advice"))) |
|
102 .setToolOldVersionErrorHandler( |
|
103 (name, version) -> new ConfigException( |
|
104 MessageFormat.format(I18N.getString( |
|
105 "message.wrong-tool-version"), name, |
|
106 version, MINIMAL_VERSION), |
|
107 I18N.getString("error.no-wix-tools.advice"))) |
|
108 .setVersionParser(output -> { |
|
109 versionCtnr[0] = ""; |
|
110 String firstLineOfOutput = output.findFirst().orElse(""); |
|
111 int separatorIdx = firstLineOfOutput.lastIndexOf(' '); |
|
112 if (separatorIdx == -1) { |
|
113 return null; |
|
114 } |
|
115 versionCtnr[0] = firstLineOfOutput.substring(separatorIdx + 1); |
|
116 return versionCtnr[0]; |
|
117 })::validate; |
|
118 } |
|
119 |
|
120 private final static String MINIMAL_VERSION = "3.0"; |
|
121 |
|
122 private final static Path PROGRAM_FILES = Path.of("C:\\Program Files"); |
|
123 private final static Path PROGRAM_FILES_X86 = Path.of("C:\\Program Files (x86)"); |
|
124 |
|
125 private static List<Path> findWixInstallDirs() { |
|
126 PathMatcher wixInstallDirMatcher = FileSystems.getDefault().getPathMatcher( |
|
127 "glob:WiX Toolset v*"); |
|
128 |
|
129 // Returns list of WiX install directories ordered by WiX version number. |
|
130 // Newer versions go first. |
|
131 return Stream.of(PROGRAM_FILES, PROGRAM_FILES_X86).map(path -> { |
|
132 List<Path> result; |
|
133 try (var paths = Files.walk(path, 1)) { |
|
134 result = paths.collect(Collectors.toList()); |
|
135 } catch (IOException ex) { |
|
136 Log.verbose(ex); |
|
137 result = Collections.emptyList(); |
|
138 } |
|
139 return result; |
|
140 }).flatMap(List::stream) |
|
141 .filter(path -> wixInstallDirMatcher.matches(path.getFileName())) |
|
142 .sorted(Comparator.comparing(Path::getFileName).reversed()) |
|
143 .map(path -> path.resolve("bin")) |
|
144 .collect(Collectors.toList()); |
|
145 } |
|
146 } |