author | herrick |
Wed, 23 Oct 2019 10:37:54 -0400 | |
branch | JDK-8200758-branch |
changeset 58762 | 0fe62353385b |
parent 58696 | 61c44899b4eb |
child 58890 | 6539ad1d90aa |
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; |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
29 |
import java.nio.file.*; |
58696 | 30 |
import java.text.MessageFormat; |
31 |
import java.util.*; |
|
32 |
import java.util.function.Supplier; |
|
33 |
import java.util.stream.Collectors; |
|
34 |
import java.util.stream.Stream; |
|
35 |
||
36 |
/** |
|
37 |
* WiX tool. |
|
38 |
*/ |
|
39 |
public enum WixTool { |
|
40 |
Candle, Light; |
|
41 |
||
42 |
static final class ToolInfo { |
|
43 |
ToolInfo(Path path, String version) { |
|
44 |
this.path = path; |
|
45 |
this.version = new DottedVersion(version); |
|
46 |
} |
|
47 |
||
48 |
final Path path; |
|
49 |
final DottedVersion version; |
|
50 |
} |
|
51 |
||
52 |
static Map<WixTool, ToolInfo> toolset() throws ConfigException { |
|
53 |
Map<WixTool, ToolInfo> toolset = new HashMap<>(); |
|
54 |
for (var tool : values()) { |
|
55 |
toolset.put(tool, tool.find()); |
|
56 |
} |
|
57 |
return toolset; |
|
58 |
} |
|
59 |
||
60 |
ToolInfo find() throws ConfigException { |
|
61 |
final Path toolFileName = IOUtils.addSuffix( |
|
62 |
Path.of(name().toLowerCase()), ".exe"); |
|
63 |
||
64 |
String[] version = new String[1]; |
|
65 |
ConfigException reason = createToolValidator(toolFileName, version).get(); |
|
66 |
if (version[0] != null) { |
|
67 |
if (reason == null) { |
|
68 |
// Found in PATH. |
|
69 |
return new ToolInfo(toolFileName, version[0]); |
|
70 |
} |
|
71 |
||
72 |
// Found in PATH, but something went wrong. |
|
73 |
throw reason; |
|
74 |
} |
|
75 |
||
76 |
for (var dir : findWixInstallDirs()) { |
|
77 |
Path path = dir.resolve(toolFileName); |
|
78 |
if (path.toFile().exists()) { |
|
79 |
reason = createToolValidator(path, version).get(); |
|
80 |
if (reason != null) { |
|
81 |
throw reason; |
|
82 |
} |
|
83 |
return new ToolInfo(path, version[0]); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
throw reason; |
|
88 |
} |
|
89 |
||
90 |
private static Supplier<ConfigException> createToolValidator(Path toolPath, |
|
91 |
String[] versionCtnr) { |
|
92 |
return new ToolValidator(toolPath) |
|
93 |
.setCommandLine("/?") |
|
94 |
.setMinimalVersion(MINIMAL_VERSION) |
|
95 |
.setToolNotFoundErrorHandler( |
|
96 |
(name, ex) -> new ConfigException( |
|
97 |
I18N.getString("error.no-wix-tools"), |
|
98 |
I18N.getString("error.no-wix-tools.advice"))) |
|
99 |
.setToolOldVersionErrorHandler( |
|
100 |
(name, version) -> new ConfigException( |
|
101 |
MessageFormat.format(I18N.getString( |
|
102 |
"message.wrong-tool-version"), name, |
|
103 |
version, MINIMAL_VERSION), |
|
104 |
I18N.getString("error.no-wix-tools.advice"))) |
|
105 |
.setVersionParser(output -> { |
|
106 |
versionCtnr[0] = ""; |
|
107 |
String firstLineOfOutput = output.findFirst().orElse(""); |
|
108 |
int separatorIdx = firstLineOfOutput.lastIndexOf(' '); |
|
109 |
if (separatorIdx == -1) { |
|
110 |
return null; |
|
111 |
} |
|
112 |
versionCtnr[0] = firstLineOfOutput.substring(separatorIdx + 1); |
|
113 |
return versionCtnr[0]; |
|
114 |
})::validate; |
|
115 |
} |
|
116 |
||
117 |
private final static String MINIMAL_VERSION = "3.0"; |
|
118 |
||
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
119 |
static Path getSystemDir(String envVar, String knownDir) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
120 |
return Optional |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
121 |
.ofNullable(getEnvVariableAsPath(envVar)) |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
122 |
.orElseGet(() -> Optional |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
123 |
.ofNullable(getEnvVariableAsPath("SystemDrive")) |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
124 |
.orElseGet(() -> Path.of("C:")).resolve(knownDir)); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
125 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
126 |
|
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
127 |
private static Path getEnvVariableAsPath(String envVar) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
128 |
String path = System.getenv(envVar); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
129 |
if (path != null) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
130 |
try { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
131 |
return Path.of(path); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
132 |
} catch (InvalidPathException ex) { |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
133 |
Log.error(MessageFormat.format(I18N.getString( |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
134 |
"error.invalid-envvar"), envVar)); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
135 |
return null; |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
136 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
137 |
} |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
138 |
return null; |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
139 |
} |
58696 | 140 |
|
141 |
private static List<Path> findWixInstallDirs() { |
|
142 |
PathMatcher wixInstallDirMatcher = FileSystems.getDefault().getPathMatcher( |
|
143 |
"glob:WiX Toolset v*"); |
|
144 |
||
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
145 |
Path programFiles = getSystemDir("ProgramFiles", "Program Files"); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
146 |
Path programFilesX86 = getSystemDir("ProgramFiles(X86)", |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
147 |
"Program Files (x86)"); |
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
148 |
|
58696 | 149 |
// Returns list of WiX install directories ordered by WiX version number. |
150 |
// Newer versions go first. |
|
58762
0fe62353385b
8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
58696
diff
changeset
|
151 |
return Stream.of(programFiles, programFilesX86).map(path -> { |
58696 | 152 |
List<Path> result; |
153 |
try (var paths = Files.walk(path, 1)) { |
|
154 |
result = paths.collect(Collectors.toList()); |
|
155 |
} catch (IOException ex) { |
|
156 |
Log.verbose(ex); |
|
157 |
result = Collections.emptyList(); |
|
158 |
} |
|
159 |
return result; |
|
160 |
}).flatMap(List::stream) |
|
161 |
.filter(path -> wixInstallDirMatcher.matches(path.getFileName())) |
|
162 |
.sorted(Comparator.comparing(Path::getFileName).reversed()) |
|
163 |
.map(path -> path.resolve("bin")) |
|
164 |
.collect(Collectors.toList()); |
|
165 |
} |
|
166 |
} |