author | herrick |
Thu, 06 Dec 2018 16:45:26 -0500 | |
branch | JDK-8200758-branch |
changeset 57062 | 044e7a644ee3 |
parent 57059 | 9bb2a4dc3af7 |
child 57067 | 9c17d779808e |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
2 |
* Copyright (c) 2012, 2018, 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 |
||
57039 | 26 |
package jdk.jpackage.internal; |
57038 | 27 |
|
57039 | 28 |
import jdk.jpackage.internal.*; |
29 |
import jdk.jpackage.internal.ConfigException; |
|
30 |
import jdk.jpackage.internal.Arguments; |
|
31 |
import jdk.jpackage.internal.UnsupportedPlatformException; |
|
32 |
import jdk.jpackage.internal.resources.WinResources; |
|
57038 | 33 |
|
34 |
import java.io.*; |
|
35 |
import java.nio.charset.Charset; |
|
36 |
import java.nio.file.Files; |
|
37 |
import java.text.MessageFormat; |
|
38 |
import java.util.*; |
|
39 |
import java.util.regex.Matcher; |
|
40 |
import java.util.regex.Pattern; |
|
41 |
||
57039 | 42 |
import static jdk.jpackage.internal.WindowsBundlerParam.*; |
57038 | 43 |
|
44 |
public class WinMsiBundler extends AbstractBundler { |
|
45 |
||
57059 | 46 |
private static final ResourceBundle I18N = ResourceBundle.getBundle( |
47 |
"jdk.jpackage.internal.resources.WinResources"); |
|
57038 | 48 |
|
49 |
public static final BundlerParamInfo<WinAppBundler> APP_BUNDLER = |
|
50 |
new WindowsBundlerParam<>( |
|
57059 | 51 |
I18N.getString("param.msi-bundler.name"), |
52 |
I18N.getString("param.msi-bundler.description"), |
|
57038 | 53 |
"win.app.bundler", |
54 |
WinAppBundler.class, |
|
55 |
params -> new WinAppBundler(), |
|
56 |
null); |
|
57 |
||
58 |
public static final BundlerParamInfo<Boolean> CAN_USE_WIX36 = |
|
59 |
new WindowsBundlerParam<>( |
|
60 |
I18N.getString("param.can-use-wix36.name"), |
|
61 |
I18N.getString("param.can-use-wix36.description"), |
|
62 |
"win.msi.canUseWix36", |
|
63 |
Boolean.class, |
|
64 |
params -> false, |
|
65 |
(s, p) -> Boolean.valueOf(s)); |
|
66 |
||
67 |
public static final BundlerParamInfo<File> CONFIG_ROOT = |
|
68 |
new WindowsBundlerParam<>( |
|
69 |
I18N.getString("param.config-root.name"), |
|
70 |
I18N.getString("param.config-root.description"), |
|
71 |
"configRoot", |
|
72 |
File.class, |
|
73 |
params -> { |
|
74 |
File imagesRoot = |
|
75 |
new File(BUILD_ROOT.fetchFrom(params), "windows"); |
|
76 |
imagesRoot.mkdirs(); |
|
77 |
return imagesRoot; |
|
78 |
}, |
|
79 |
(s, p) -> null); |
|
80 |
||
81 |
public static final BundlerParamInfo<File> MSI_IMAGE_DIR = |
|
82 |
new WindowsBundlerParam<>( |
|
83 |
I18N.getString("param.image-dir.name"), |
|
84 |
I18N.getString("param.image-dir.description"), |
|
85 |
"win.msi.imageDir", |
|
86 |
File.class, |
|
87 |
params -> { |
|
88 |
File imagesRoot = IMAGES_ROOT.fetchFrom(params); |
|
89 |
if (!imagesRoot.exists()) imagesRoot.mkdirs(); |
|
90 |
return new File(imagesRoot, "win-msi.image"); |
|
91 |
}, |
|
92 |
(s, p) -> null); |
|
93 |
||
94 |
public static final BundlerParamInfo<File> WIN_APP_IMAGE = |
|
95 |
new WindowsBundlerParam<>( |
|
96 |
I18N.getString("param.app-dir.name"), |
|
97 |
I18N.getString("param.app-dir.description"), |
|
98 |
"win.app.image", |
|
99 |
File.class, |
|
100 |
null, |
|
101 |
(s, p) -> null); |
|
102 |
||
103 |
public static final StandardBundlerParam<Boolean> MSI_SYSTEM_WIDE = |
|
104 |
new StandardBundlerParam<>( |
|
105 |
I18N.getString("param.system-wide.name"), |
|
106 |
I18N.getString("param.system-wide.description"), |
|
107 |
Arguments.CLIOptions.WIN_PER_USER_INSTALLATION.getId(), |
|
108 |
Boolean.class, |
|
109 |
params -> true, // MSIs default to system wide |
|
110 |
// valueOf(null) is false, |
|
111 |
// and we actually do want null |
|
112 |
(s, p) -> (s == null || "null".equalsIgnoreCase(s))? null |
|
113 |
: Boolean.valueOf(s) |
|
114 |
); |
|
115 |
||
116 |
||
117 |
public static final StandardBundlerParam<String> PRODUCT_VERSION = |
|
118 |
new StandardBundlerParam<>( |
|
119 |
I18N.getString("param.product-version.name"), |
|
120 |
I18N.getString("param.product-version.description"), |
|
121 |
"win.msi.productVersion", |
|
122 |
String.class, |
|
123 |
VERSION::fetchFrom, |
|
124 |
(s, p) -> s |
|
125 |
); |
|
126 |
||
127 |
public static final BundlerParamInfo<UUID> UPGRADE_UUID = |
|
128 |
new WindowsBundlerParam<>( |
|
129 |
I18N.getString("param.upgrade-uuid.name"), |
|
130 |
I18N.getString("param.upgrade-uuid.description"), |
|
57062
044e7a644ee3
8214575: Exe installers will install application to already existing instalation
herrick
parents:
57059
diff
changeset
|
131 |
Arguments.CLIOptions.WIN_UPGRADE_UUID.getId(), |
57038 | 132 |
UUID.class, |
57062
044e7a644ee3
8214575: Exe installers will install application to already existing instalation
herrick
parents:
57059
diff
changeset
|
133 |
params -> UUID.randomUUID(), |
57038 | 134 |
(s, p) -> UUID.fromString(s)); |
135 |
||
136 |
private static final String TOOL_CANDLE = "candle.exe"; |
|
137 |
private static final String TOOL_LIGHT = "light.exe"; |
|
138 |
// autodetect just v3.7, v3.8, 3.9, 3.10 and 3.11 |
|
139 |
private static final String AUTODETECT_DIRS = |
|
140 |
";C:\\Program Files (x86)\\WiX Toolset v3.11\\bin;" |
|
141 |
+ "C:\\Program Files\\WiX Toolset v3.11\\bin;" |
|
142 |
+ "C:\\Program Files (x86)\\WiX Toolset v3.10\\bin;" |
|
143 |
+ "C:\\Program Files\\WiX Toolset v3.10\\bin;" |
|
144 |
+ "C:\\Program Files (x86)\\WiX Toolset v3.9\\bin;" |
|
145 |
+ "C:\\Program Files\\WiX Toolset v3.9\\bin;" |
|
146 |
+ "C:\\Program Files (x86)\\WiX Toolset v3.8\\bin;" |
|
147 |
+ "C:\\Program Files\\WiX Toolset v3.8\\bin;" |
|
148 |
+ "C:\\Program Files (x86)\\WiX Toolset v3.7\\bin;" |
|
149 |
+ "C:\\Program Files\\WiX Toolset v3.7\\bin"; |
|
150 |
||
151 |
public static final BundlerParamInfo<String> TOOL_CANDLE_EXECUTABLE = |
|
152 |
new WindowsBundlerParam<>( |
|
153 |
I18N.getString("param.candle-path.name"), |
|
154 |
I18N.getString("param.candle-path.description"), |
|
155 |
"win.msi.candle.exe", |
|
156 |
String.class, |
|
157 |
params -> { |
|
158 |
for (String dirString : (System.getenv("PATH") + |
|
159 |
AUTODETECT_DIRS).split(";")) { |
|
160 |
File f = new File(dirString.replace("\"", ""), TOOL_CANDLE); |
|
161 |
if (f.isFile()) { |
|
162 |
return f.toString(); |
|
163 |
} |
|
164 |
} |
|
165 |
return null; |
|
166 |
}, |
|
167 |
null); |
|
168 |
||
169 |
public static final BundlerParamInfo<String> TOOL_LIGHT_EXECUTABLE = |
|
170 |
new WindowsBundlerParam<>( |
|
171 |
I18N.getString("param.light-path.name"), |
|
172 |
I18N.getString("param.light-path.description"), |
|
173 |
"win.msi.light.exe", |
|
174 |
String.class, |
|
175 |
params -> { |
|
176 |
for (String dirString : (System.getenv("PATH") + |
|
177 |
AUTODETECT_DIRS).split(";")) { |
|
178 |
File f = new File(dirString.replace("\"", ""), TOOL_LIGHT); |
|
179 |
if (f.isFile()) { |
|
180 |
return f.toString(); |
|
181 |
} |
|
182 |
} |
|
183 |
return null; |
|
184 |
}, |
|
185 |
null); |
|
186 |
||
187 |
public static final StandardBundlerParam<Boolean> MENU_HINT = |
|
188 |
new WindowsBundlerParam<>( |
|
189 |
I18N.getString("param.menu-shortcut-hint.name"), |
|
190 |
I18N.getString("param.menu-shortcut-hint.description"), |
|
191 |
Arguments.CLIOptions.WIN_MENU_HINT.getId(), |
|
192 |
Boolean.class, |
|
193 |
params -> false, |
|
194 |
// valueOf(null) is false, |
|
195 |
// and we actually do want null in some cases |
|
196 |
(s, p) -> (s == null || |
|
197 |
"null".equalsIgnoreCase(s))? true : Boolean.valueOf(s) |
|
198 |
); |
|
199 |
||
200 |
public static final StandardBundlerParam<Boolean> SHORTCUT_HINT = |
|
201 |
new WindowsBundlerParam<>( |
|
202 |
I18N.getString("param.desktop-shortcut-hint.name"), |
|
203 |
I18N.getString("param.desktop-shortcut-hint.description"), |
|
204 |
Arguments.CLIOptions.WIN_SHORTCUT_HINT.getId(), |
|
205 |
Boolean.class, |
|
206 |
params -> false, |
|
207 |
// valueOf(null) is false, |
|
208 |
// and we actually do want null in some cases |
|
209 |
(s, p) -> (s == null || |
|
210 |
"null".equalsIgnoreCase(s))? false : Boolean.valueOf(s) |
|
211 |
); |
|
212 |
||
213 |
public WinMsiBundler() { |
|
214 |
super(); |
|
215 |
baseResourceLoader = WinResources.class; |
|
216 |
} |
|
217 |
||
218 |
||
219 |
@Override |
|
220 |
public String getName() { |
|
57059 | 221 |
return I18N.getString("msi.bundler.name"); |
57038 | 222 |
} |
223 |
||
224 |
@Override |
|
225 |
public String getDescription() { |
|
57059 | 226 |
return I18N.getString("msi.bundler.description"); |
57038 | 227 |
} |
228 |
||
229 |
@Override |
|
230 |
public String getID() { |
|
231 |
return "msi"; |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public String getBundleType() { |
|
236 |
return "INSTALLER"; |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public Collection<BundlerParamInfo<?>> getBundleParameters() { |
|
241 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); |
|
242 |
results.addAll(WinAppBundler.getAppBundleParameters()); |
|
243 |
results.addAll(getMsiBundleParameters()); |
|
244 |
return results; |
|
245 |
} |
|
246 |
||
247 |
public static Collection<BundlerParamInfo<?>> getMsiBundleParameters() { |
|
248 |
return Arrays.asList( |
|
249 |
DESCRIPTION, |
|
250 |
MENU_GROUP, |
|
251 |
MENU_HINT, |
|
252 |
PRODUCT_VERSION, |
|
253 |
SHORTCUT_HINT, |
|
254 |
MSI_SYSTEM_WIDE, |
|
255 |
VENDOR, |
|
256 |
LICENSE_FILE, |
|
257 |
INSTALLDIR_CHOOSER |
|
258 |
); |
|
259 |
} |
|
260 |
||
261 |
@Override |
|
262 |
public File execute( |
|
263 |
Map<String, ? super Object> params, File outputParentDir) { |
|
264 |
return bundle(params, outputParentDir); |
|
265 |
} |
|
266 |
||
267 |
@Override |
|
268 |
public boolean supported() { |
|
269 |
return (Platform.getPlatform() == Platform.WINDOWS); |
|
270 |
} |
|
271 |
||
272 |
static class VersionExtractor extends PrintStream { |
|
273 |
double version = 0f; |
|
274 |
||
275 |
public VersionExtractor() { |
|
276 |
super(new ByteArrayOutputStream()); |
|
277 |
} |
|
278 |
||
279 |
double getVersion() { |
|
280 |
if (version == 0f) { |
|
281 |
String content = |
|
282 |
new String(((ByteArrayOutputStream) out).toByteArray()); |
|
283 |
Pattern pattern = Pattern.compile("version (\\d+.\\d+)"); |
|
284 |
Matcher matcher = pattern.matcher(content); |
|
285 |
if (matcher.find()) { |
|
286 |
String v = matcher.group(1); |
|
287 |
version = Double.parseDouble(v); |
|
288 |
} |
|
289 |
} |
|
290 |
return version; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
private static double findToolVersion(String toolName) { |
|
295 |
try { |
|
296 |
if (toolName == null || "".equals(toolName)) return 0f; |
|
297 |
||
298 |
ProcessBuilder pb = new ProcessBuilder( |
|
299 |
toolName, |
|
300 |
"/?"); |
|
301 |
VersionExtractor ve = new VersionExtractor(); |
|
302 |
// not interested in the output |
|
303 |
IOUtils.exec(pb, Log.isDebug(), true, ve); |
|
304 |
double version = ve.getVersion(); |
|
305 |
Log.verbose(MessageFormat.format( |
|
306 |
I18N.getString("message.tool-version"), |
|
307 |
toolName, version)); |
|
308 |
return version; |
|
309 |
} catch (Exception e) { |
|
310 |
if (Log.isDebug()) { |
|
311 |
Log.verbose(e); |
|
312 |
} |
|
313 |
return 0f; |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
@Override |
|
318 |
public boolean validate(Map<String, ? super Object> p) |
|
319 |
throws UnsupportedPlatformException, ConfigException { |
|
320 |
try { |
|
321 |
if (p == null) throw new ConfigException( |
|
322 |
I18N.getString("error.parameters-null"), |
|
323 |
I18N.getString("error.parameters-null.advice")); |
|
324 |
||
325 |
// run basic validation to ensure requirements are met |
|
326 |
// we are not interested in return code, only possible exception |
|
327 |
APP_BUNDLER.fetchFrom(p).doValidate(p); |
|
328 |
||
329 |
double candleVersion = |
|
330 |
findToolVersion(TOOL_CANDLE_EXECUTABLE.fetchFrom(p)); |
|
331 |
double lightVersion = |
|
332 |
findToolVersion(TOOL_LIGHT_EXECUTABLE.fetchFrom(p)); |
|
333 |
||
334 |
// WiX 3.0+ is required |
|
335 |
double minVersion = 3.0f; |
|
336 |
boolean bad = false; |
|
337 |
||
338 |
if (candleVersion < minVersion) { |
|
339 |
Log.verbose(MessageFormat.format( |
|
340 |
I18N.getString("message.wrong-tool-version"), |
|
341 |
TOOL_CANDLE, candleVersion, minVersion)); |
|
342 |
bad = true; |
|
343 |
} |
|
344 |
if (lightVersion < minVersion) { |
|
345 |
Log.verbose(MessageFormat.format( |
|
346 |
I18N.getString("message.wrong-tool-version"), |
|
347 |
TOOL_LIGHT, lightVersion, minVersion)); |
|
348 |
bad = true; |
|
349 |
} |
|
350 |
||
351 |
if (bad){ |
|
352 |
throw new ConfigException( |
|
353 |
I18N.getString("error.no-wix-tools"), |
|
354 |
I18N.getString("error.no-wix-tools.advice")); |
|
355 |
} |
|
356 |
||
357 |
if (lightVersion >= 3.6f) { |
|
358 |
Log.verbose(I18N.getString("message.use-wix36-features")); |
|
359 |
p.put(CAN_USE_WIX36.getID(), Boolean.TRUE); |
|
360 |
} |
|
361 |
||
362 |
/********* validate bundle parameters *************/ |
|
363 |
||
364 |
String version = PRODUCT_VERSION.fetchFrom(p); |
|
365 |
if (!isVersionStringValid(version)) { |
|
366 |
throw new ConfigException( |
|
367 |
MessageFormat.format(I18N.getString( |
|
368 |
"error.version-string-wrong-format"), version), |
|
369 |
MessageFormat.format(I18N.getString( |
|
370 |
"error.version-string-wrong-format.advice"), |
|
371 |
PRODUCT_VERSION.getID())); |
|
372 |
} |
|
373 |
||
374 |
// only one mime type per association, at least one file extension |
|
375 |
List<Map<String, ? super Object>> associations = |
|
376 |
FILE_ASSOCIATIONS.fetchFrom(p); |
|
377 |
if (associations != null) { |
|
378 |
for (int i = 0; i < associations.size(); i++) { |
|
379 |
Map<String, ? super Object> assoc = associations.get(i); |
|
380 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
381 |
if (mimes.size() > 1) { |
|
382 |
throw new ConfigException(MessageFormat.format( |
|
383 |
I18N.getString("error.too-many-content-" |
|
384 |
+ "types-for-file-association"), i), |
|
385 |
I18N.getString("error.too-many-content-" |
|
386 |
+ "types-for-file-association.advice")); |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
// validate license file, if used, exists in the proper place |
|
392 |
if (p.containsKey(LICENSE_FILE.getID())) { |
|
393 |
List<RelativeFileSet> appResourcesList = |
|
394 |
APP_RESOURCES_LIST.fetchFrom(p); |
|
395 |
for (String license : LICENSE_FILE.fetchFrom(p)) { |
|
396 |
boolean found = false; |
|
397 |
for (RelativeFileSet appResources : appResourcesList) { |
|
398 |
found = found || appResources.contains(license); |
|
399 |
} |
|
400 |
if (!found) { |
|
401 |
throw new ConfigException( |
|
402 |
MessageFormat.format(I18N.getString( |
|
403 |
"error.license-missing"), license), |
|
404 |
MessageFormat.format(I18N.getString( |
|
405 |
"error.license-missing.advice"), license)); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
return true; |
|
411 |
} catch (RuntimeException re) { |
|
412 |
if (re.getCause() instanceof ConfigException) { |
|
413 |
throw (ConfigException) re.getCause(); |
|
414 |
} else { |
|
415 |
throw new ConfigException(re); |
|
416 |
} |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
// http://msdn.microsoft.com/en-us/library/aa370859%28v=VS.85%29.aspx |
|
421 |
// The format of the string is as follows: |
|
422 |
// major.minor.build |
|
423 |
// The first field is the major version and has a maximum value of 255. |
|
424 |
// The second field is the minor version and has a maximum value of 255. |
|
425 |
// The third field is called the build version or the update version and |
|
426 |
// has a maximum value of 65,535. |
|
427 |
static boolean isVersionStringValid(String v) { |
|
428 |
if (v == null) { |
|
429 |
return true; |
|
430 |
} |
|
431 |
||
432 |
String p[] = v.split("\\."); |
|
433 |
if (p.length > 3) { |
|
434 |
Log.verbose(I18N.getString( |
|
435 |
"message.version-string-too-many-components")); |
|
436 |
return false; |
|
437 |
} |
|
438 |
||
439 |
try { |
|
440 |
int val = Integer.parseInt(p[0]); |
|
441 |
if (val < 0 || val > 255) { |
|
442 |
Log.verbose(I18N.getString( |
|
443 |
"error.version-string-major-out-of-range")); |
|
444 |
return false; |
|
445 |
} |
|
446 |
if (p.length > 1) { |
|
447 |
val = Integer.parseInt(p[1]); |
|
448 |
if (val < 0 || val > 255) { |
|
449 |
Log.verbose(I18N.getString( |
|
450 |
"error.version-string-minor-out-of-range")); |
|
451 |
return false; |
|
452 |
} |
|
453 |
} |
|
454 |
if (p.length > 2) { |
|
455 |
val = Integer.parseInt(p[2]); |
|
456 |
if (val < 0 || val > 65535) { |
|
457 |
Log.verbose(I18N.getString( |
|
458 |
"error.version-string-build-out-of-range")); |
|
459 |
return false; |
|
460 |
} |
|
461 |
} |
|
462 |
} catch (NumberFormatException ne) { |
|
463 |
Log.verbose(I18N.getString("error.version-string-part-not-number")); |
|
464 |
Log.verbose(ne); |
|
465 |
return false; |
|
466 |
} |
|
467 |
||
468 |
return true; |
|
469 |
} |
|
470 |
||
471 |
private boolean prepareProto(Map<String, ? super Object> p) |
|
472 |
throws IOException { |
|
473 |
File appImage = StandardBundlerParam.getPredefinedAppImage(p); |
|
474 |
File appDir = null; |
|
475 |
||
476 |
// we either have an application image or need to build one |
|
477 |
if (appImage != null) { |
|
478 |
appDir = new File( |
|
479 |
MSI_IMAGE_DIR.fetchFrom(p), APP_NAME.fetchFrom(p)); |
|
480 |
// copy everything from appImage dir into appDir/name |
|
481 |
IOUtils.copyRecursive(appImage.toPath(), appDir.toPath()); |
|
482 |
} else { |
|
483 |
appDir = APP_BUNDLER.fetchFrom(p).doBundle(p, |
|
484 |
MSI_IMAGE_DIR.fetchFrom(p), true); |
|
485 |
} |
|
486 |
||
487 |
p.put(WIN_APP_IMAGE.getID(), appDir); |
|
488 |
||
489 |
List<String> licenseFiles = LICENSE_FILE.fetchFrom(p); |
|
490 |
if (licenseFiles != null) { |
|
491 |
// need to copy license file to the root of win.app.image |
|
492 |
outerLoop: |
|
493 |
for (RelativeFileSet rfs : APP_RESOURCES_LIST.fetchFrom(p)) { |
|
494 |
for (String s : licenseFiles) { |
|
495 |
if (rfs.contains(s)) { |
|
496 |
File lfile = new File(rfs.getBaseDirectory(), s); |
|
497 |
File destFile = new File(appDir, lfile.getName()); |
|
498 |
IOUtils.copyFile(lfile, destFile); |
|
499 |
ensureByMutationFileIsRTF(destFile); |
|
500 |
break outerLoop; |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
// copy file association icons |
|
507 |
List<Map<String, ? super Object>> fileAssociations = |
|
508 |
FILE_ASSOCIATIONS.fetchFrom(p); |
|
509 |
for (Map<String, ? super Object> fa : fileAssociations) { |
|
510 |
File icon = FA_ICON.fetchFrom(fa); // TODO FA_ICON_ICO |
|
511 |
if (icon == null) { |
|
512 |
continue; |
|
513 |
} |
|
514 |
||
515 |
File faIconFile = new File(appDir, icon.getName()); |
|
516 |
||
517 |
if (icon.exists()) { |
|
518 |
try { |
|
519 |
IOUtils.copyFile(icon, faIconFile); |
|
520 |
} catch (IOException e) { |
|
521 |
e.printStackTrace(); |
|
522 |
} |
|
523 |
} |
|
524 |
} |
|
525 |
||
526 |
return appDir != null; |
|
527 |
} |
|
528 |
||
529 |
public File bundle(Map<String, ? super Object> p, File outdir) { |
|
530 |
if (!outdir.isDirectory() && !outdir.mkdirs()) { |
|
531 |
throw new RuntimeException(MessageFormat.format( |
|
532 |
I18N.getString("error.cannot-create-output-dir"), |
|
533 |
outdir.getAbsolutePath())); |
|
534 |
} |
|
535 |
if (!outdir.canWrite()) { |
|
536 |
throw new RuntimeException(MessageFormat.format( |
|
537 |
I18N.getString("error.cannot-write-to-output-dir"), |
|
538 |
outdir.getAbsolutePath())); |
|
539 |
} |
|
540 |
||
541 |
// validate we have valid tools before continuing |
|
542 |
String light = TOOL_LIGHT_EXECUTABLE.fetchFrom(p); |
|
543 |
String candle = TOOL_CANDLE_EXECUTABLE.fetchFrom(p); |
|
544 |
if (light == null || !new File(light).isFile() || |
|
545 |
candle == null || !new File(candle).isFile()) { |
|
546 |
Log.error(I18N.getString("error.no-wix-tools")); |
|
547 |
Log.verbose(MessageFormat.format( |
|
548 |
I18N.getString("message.light-file-string"), light)); |
|
549 |
Log.verbose(MessageFormat.format( |
|
550 |
I18N.getString("message.candle-file-string"), candle)); |
|
551 |
return null; |
|
552 |
} |
|
553 |
||
554 |
File imageDir = MSI_IMAGE_DIR.fetchFrom(p); |
|
555 |
try { |
|
556 |
imageDir.mkdirs(); |
|
557 |
||
558 |
boolean menuShortcut = MENU_HINT.fetchFrom(p); |
|
559 |
boolean desktopShortcut = SHORTCUT_HINT.fetchFrom(p); |
|
560 |
if (!menuShortcut && !desktopShortcut) { |
|
561 |
// both can not be false - user will not find the app |
|
562 |
Log.verbose(I18N.getString("message.one-shortcut-required")); |
|
563 |
p.put(MENU_HINT.getID(), true); |
|
564 |
} |
|
565 |
||
566 |
if (prepareProto(p) && prepareWiXConfig(p) |
|
567 |
&& prepareBasicProjectConfig(p)) { |
|
568 |
File configScriptSrc = getConfig_Script(p); |
|
569 |
if (configScriptSrc.exists()) { |
|
570 |
// we need to be running post script in the image folder |
|
571 |
||
572 |
// NOTE: Would it be better to generate it to the image |
|
573 |
// folder and save only if "verbose" is requested? |
|
574 |
||
575 |
// for now we replicate it |
|
576 |
File configScript = |
|
577 |
new File(imageDir, configScriptSrc.getName()); |
|
578 |
IOUtils.copyFile(configScriptSrc, configScript); |
|
579 |
Log.verbose(MessageFormat.format( |
|
580 |
I18N.getString("message.running-wsh-script"), |
|
581 |
configScript.getAbsolutePath())); |
|
582 |
IOUtils.run("wscript", |
|
583 |
configScript, false); |
|
584 |
} |
|
585 |
return buildMSI(p, outdir); |
|
586 |
} |
|
587 |
return null; |
|
588 |
} catch (IOException ex) { |
|
589 |
Log.verbose(ex); |
|
590 |
return null; |
|
591 |
} finally { |
|
592 |
try { |
|
593 |
if (imageDir != null && |
|
594 |
PREDEFINED_APP_IMAGE.fetchFrom(p) == null && |
|
595 |
(PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null || |
|
596 |
!Arguments.CREATE_JRE_INSTALLER.fetchFrom(p)) && |
|
597 |
!Log.isDebug() && |
|
598 |
!Log.isVerbose()) { |
|
599 |
IOUtils.deleteRecursive(imageDir); |
|
600 |
} else if (imageDir != null) { |
|
601 |
Log.verbose(MessageFormat.format( |
|
602 |
I18N.getString("message.debug-working-directory"), |
|
603 |
imageDir.getAbsolutePath())); |
|
604 |
} |
|
605 |
||
606 |
cleanupConfigFiles(p); |
|
607 |
} catch (IOException ex) { |
|
608 |
// noinspection ReturnInsideFinallyBlock |
|
609 |
Log.debug(ex.getMessage()); |
|
610 |
return null; |
|
611 |
} |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
protected void cleanupConfigFiles(Map<String, ? super Object> params) { |
|
616 |
if (Log.isDebug() || Log.isVerbose()) { |
|
617 |
return; |
|
618 |
} |
|
619 |
||
620 |
if (getConfig_ProjectFile(params) != null) { |
|
621 |
getConfig_ProjectFile(params).delete(); |
|
622 |
} |
|
623 |
if (getConfig_Script(params) != null) { |
|
624 |
getConfig_Script(params).delete(); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
// name of post-image script |
|
629 |
private File getConfig_Script(Map<String, ? super Object> params) { |
|
630 |
return new File(CONFIG_ROOT.fetchFrom(params), |
|
631 |
APP_FS_NAME.fetchFrom(params) + "-post-image.wsf"); |
|
632 |
} |
|
633 |
||
634 |
private boolean prepareBasicProjectConfig( |
|
635 |
Map<String, ? super Object> params) throws IOException { |
|
636 |
fetchResource(WinAppBundler.WIN_BUNDLER_PREFIX + |
|
637 |
getConfig_Script(params).getName(), |
|
638 |
I18N.getString("resource.post-install-script"), |
|
639 |
(String) null, |
|
640 |
getConfig_Script(params), |
|
641 |
VERBOSE.fetchFrom(params), |
|
642 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
643 |
return true; |
|
644 |
} |
|
645 |
||
646 |
private String relativePath(File basedir, File file) { |
|
647 |
return file.getAbsolutePath().substring( |
|
648 |
basedir.getAbsolutePath().length() + 1); |
|
649 |
} |
|
650 |
||
651 |
boolean prepareMainProjectFile( |
|
652 |
Map<String, ? super Object> params) throws IOException { |
|
653 |
Map<String, String> data = new HashMap<>(); |
|
654 |
||
655 |
UUID productGUID = UUID.randomUUID(); |
|
656 |
||
657 |
Log.verbose(MessageFormat.format( |
|
658 |
I18N.getString("message.generated-product-guid"), |
|
659 |
productGUID.toString())); |
|
660 |
||
661 |
// we use random GUID for product itself but |
|
662 |
// user provided for upgrade guid |
|
663 |
// Upgrade guid is important to decide whether it is an upgrade of |
|
664 |
// installed app. I.e. we need it to be the same for |
|
665 |
// 2 different versions of app if possible |
|
666 |
data.put("PRODUCT_GUID", productGUID.toString()); |
|
667 |
data.put("PRODUCT_UPGRADE_GUID", |
|
668 |
UPGRADE_UUID.fetchFrom(params).toString()); |
|
669 |
||
670 |
data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params)); |
|
671 |
data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params)); |
|
672 |
data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params)); |
|
673 |
data.put("APPLICATION_VERSION", PRODUCT_VERSION.fetchFrom(params)); |
|
674 |
||
675 |
// WinAppBundler will add application folder again => step out |
|
676 |
File imageRootDir = WIN_APP_IMAGE.fetchFrom(params); |
|
677 |
File launcher = new File(imageRootDir, |
|
678 |
WinAppBundler.getLauncherName(params)); |
|
679 |
||
680 |
String launcherPath = relativePath(imageRootDir, launcher); |
|
681 |
data.put("APPLICATION_LAUNCHER", launcherPath); |
|
682 |
||
683 |
String iconPath = launcherPath.replace(".exe", ".ico"); |
|
684 |
||
685 |
data.put("APPLICATION_ICON", iconPath); |
|
686 |
||
687 |
data.put("REGISTRY_ROOT", getRegistryRoot(params)); |
|
688 |
||
689 |
boolean canUseWix36Features = CAN_USE_WIX36.fetchFrom(params); |
|
690 |
data.put("WIX36_ONLY_START", |
|
691 |
canUseWix36Features ? "" : "<!--"); |
|
692 |
data.put("WIX36_ONLY_END", |
|
693 |
canUseWix36Features ? "" : "-->"); |
|
694 |
||
695 |
if (MSI_SYSTEM_WIDE.fetchFrom(params)) { |
|
696 |
data.put("INSTALL_SCOPE", "perMachine"); |
|
697 |
} else { |
|
698 |
data.put("INSTALL_SCOPE", "perUser"); |
|
699 |
} |
|
700 |
||
701 |
if (BIT_ARCH_64.fetchFrom(params)) { |
|
702 |
data.put("PLATFORM", "x64"); |
|
703 |
data.put("WIN64", "yes"); |
|
704 |
} else { |
|
705 |
data.put("PLATFORM", "x86"); |
|
706 |
data.put("WIN64", "no"); |
|
707 |
} |
|
708 |
||
709 |
data.put("UI_BLOCK", getUIBlock(params)); |
|
710 |
||
711 |
List<Map<String, ? super Object>> secondaryLaunchers = |
|
712 |
SECONDARY_LAUNCHERS.fetchFrom(params); |
|
713 |
||
714 |
StringBuilder secondaryLauncherIcons = new StringBuilder(); |
|
715 |
for (int i = 0; i < secondaryLaunchers.size(); i++) { |
|
716 |
Map<String, ? super Object> sl = secondaryLaunchers.get(i); |
|
717 |
// <Icon Id="DesktopIcon.exe" SourceFile="APPLICATION_ICON" /> |
|
718 |
if (SHORTCUT_HINT.fetchFrom(sl) || MENU_HINT.fetchFrom(sl)) { |
|
719 |
File secondaryLauncher = new File(imageRootDir, |
|
720 |
WinAppBundler.getLauncherName(sl)); |
|
721 |
String secondaryLauncherPath = |
|
722 |
relativePath(imageRootDir, secondaryLauncher); |
|
723 |
String secondaryLauncherIconPath = |
|
724 |
secondaryLauncherPath.replace(".exe", ".ico"); |
|
725 |
||
726 |
secondaryLauncherIcons.append(" <Icon Id=\"Launcher"); |
|
727 |
secondaryLauncherIcons.append(i); |
|
728 |
secondaryLauncherIcons.append(".exe\" SourceFile=\""); |
|
729 |
secondaryLauncherIcons.append(secondaryLauncherIconPath); |
|
730 |
secondaryLauncherIcons.append("\" />\r\n"); |
|
731 |
} |
|
732 |
} |
|
733 |
data.put("SECONDARY_LAUNCHER_ICONS", secondaryLauncherIcons.toString()); |
|
734 |
||
735 |
String wxs = Arguments.CREATE_JRE_INSTALLER.fetchFrom(params) ? |
|
736 |
MSI_PROJECT_TEMPLATE_SERVER_JRE : MSI_PROJECT_TEMPLATE; |
|
737 |
||
738 |
Writer w = new BufferedWriter( |
|
739 |
new FileWriter(getConfig_ProjectFile(params))); |
|
740 |
||
741 |
String content = preprocessTextResource( |
|
742 |
WinAppBundler.WIN_BUNDLER_PREFIX + |
|
743 |
getConfig_ProjectFile(params).getName(), |
|
744 |
I18N.getString("resource.wix-config-file"), |
|
745 |
wxs, data, VERBOSE.fetchFrom(params), |
|
746 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
747 |
w.write(content); |
|
748 |
w.close(); |
|
749 |
return true; |
|
750 |
} |
|
751 |
private int id; |
|
752 |
private int compId; |
|
753 |
private final static String LAUNCHER_ID = "LauncherId"; |
|
754 |
private final static String LAUNCHER_SVC_ID = "LauncherSvcId"; |
|
755 |
||
756 |
/** |
|
757 |
* Overrides the dialog sequence in built-in dialog set "WixUI_InstallDir" |
|
758 |
* to exclude license dialog |
|
759 |
*/ |
|
760 |
private static final String TWEAK_FOR_EXCLUDING_LICENSE = |
|
761 |
" <Publish Dialog=\"WelcomeDlg\" Control=\"Next\"" |
|
762 |
+ " Event=\"NewDialog\" Value=\"InstallDirDlg\"" |
|
763 |
+ " Order=\"2\"> 1" |
|
764 |
+ " </Publish>\n" |
|
765 |
+ " <Publish Dialog=\"InstallDirDlg\" Control=\"Back\"" |
|
766 |
+ " Event=\"NewDialog\" Value=\"WelcomeDlg\"" |
|
767 |
+ " Order=\"2\"> 1" |
|
768 |
+ " </Publish>\n"; |
|
769 |
||
770 |
/** |
|
771 |
* Creates UI element using WiX built-in dialog sets |
|
772 |
* - WixUI_InstallDir/WixUI_Minimal. |
|
773 |
* The dialog sets are the closest to what we want to implement. |
|
774 |
* |
|
775 |
* WixUI_Minimal for license dialog only |
|
776 |
* WixUI_InstallDir for installdir dialog only or for both |
|
777 |
* installdir/license dialogs |
|
778 |
*/ |
|
779 |
private String getUIBlock(Map<String, ? super Object> params) { |
|
780 |
String uiBlock = " <UI/>\n"; // UI-less element |
|
781 |
||
782 |
if (INSTALLDIR_CHOOSER.fetchFrom(params)) { |
|
783 |
boolean enableTweakForExcludingLicense = |
|
784 |
(getLicenseFile(params) == null); |
|
785 |
uiBlock = " <UI>\n" |
|
786 |
+ " <Property Id=\"WIXUI_INSTALLDIR\"" |
|
787 |
+ " Value=\"APPLICATIONFOLDER\" />\n" |
|
788 |
+ " <UIRef Id=\"WixUI_InstallDir\" />\n" |
|
789 |
+ (enableTweakForExcludingLicense ? |
|
790 |
TWEAK_FOR_EXCLUDING_LICENSE : "") |
|
791 |
+" </UI>\n"; |
|
792 |
} else if (getLicenseFile(params) != null) { |
|
793 |
uiBlock = " <UI>\n" |
|
794 |
+ " <UIRef Id=\"WixUI_Minimal\" />\n" |
|
795 |
+ " </UI>\n"; |
|
796 |
} |
|
797 |
||
798 |
return uiBlock; |
|
799 |
} |
|
800 |
||
801 |
private void walkFileTree(Map<String, ? super Object> params, |
|
802 |
File root, PrintStream out, String prefix) { |
|
803 |
List<File> dirs = new ArrayList<>(); |
|
804 |
List<File> files = new ArrayList<>(); |
|
805 |
||
806 |
if (!root.isDirectory()) { |
|
807 |
throw new RuntimeException( |
|
808 |
MessageFormat.format( |
|
809 |
I18N.getString("error.cannot-walk-directory"), |
|
810 |
root.getAbsolutePath())); |
|
811 |
} |
|
812 |
||
813 |
// sort to files and dirs |
|
814 |
File[] children = root.listFiles(); |
|
815 |
if (children != null) { |
|
816 |
for (File f : children) { |
|
817 |
if (f.isDirectory()) { |
|
818 |
dirs.add(f); |
|
819 |
} else { |
|
820 |
files.add(f); |
|
821 |
} |
|
822 |
} |
|
823 |
} |
|
824 |
||
825 |
// have files => need to output component |
|
826 |
out.println(prefix + " <Component Id=\"comp" + (compId++) |
|
827 |
+ "\" DiskId=\"1\"" |
|
828 |
+ " Guid=\"" + UUID.randomUUID().toString() + "\"" |
|
829 |
+ (BIT_ARCH_64.fetchFrom(params) ? " Win64=\"yes\"" : "") |
|
830 |
+ ">"); |
|
831 |
out.println(prefix + " <CreateFolder/>"); |
|
832 |
out.println(prefix + " <RemoveFolder Id=\"RemoveDir" |
|
833 |
+ (id++) + "\" On=\"uninstall\" />"); |
|
834 |
||
835 |
boolean needRegistryKey = !MSI_SYSTEM_WIDE.fetchFrom(params); |
|
836 |
File imageRootDir = WIN_APP_IMAGE.fetchFrom(params); |
|
837 |
File launcherFile = |
|
838 |
new File(imageRootDir, WinAppBundler.getLauncherName(params)); |
|
839 |
||
840 |
// Find out if we need to use registry. We need it if |
|
841 |
// - we doing user level install as file can not serve as KeyPath |
|
842 |
// - if we adding shortcut in this component |
|
843 |
||
844 |
for (File f: files) { |
|
845 |
boolean isLauncher = f.equals(launcherFile); |
|
846 |
if (isLauncher) { |
|
847 |
needRegistryKey = true; |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
if (needRegistryKey) { |
|
852 |
// has to be under HKCU to make WiX happy |
|
853 |
out.println(prefix + " <RegistryKey Root=\"HKCU\" " |
|
854 |
+ " Key=\"Software\\" + VENDOR.fetchFrom(params) + "\\" |
|
855 |
+ APP_NAME.fetchFrom(params) + "\"" |
|
856 |
+ (CAN_USE_WIX36.fetchFrom(params) ? |
|
857 |
">" : " Action=\"createAndRemoveOnUninstall\">")); |
|
858 |
out.println(prefix |
|
859 |
+ " <RegistryValue Name=\"Version\" Value=\"" |
|
860 |
+ VERSION.fetchFrom(params) |
|
861 |
+ "\" Type=\"string\" KeyPath=\"yes\"/>"); |
|
862 |
out.println(prefix + " </RegistryKey>"); |
|
863 |
} |
|
864 |
||
865 |
boolean menuShortcut = MENU_HINT.fetchFrom(params); |
|
866 |
boolean desktopShortcut = SHORTCUT_HINT.fetchFrom(params); |
|
867 |
||
868 |
Map<String, String> idToFileMap = new TreeMap<>(); |
|
869 |
boolean launcherSet = false; |
|
870 |
||
871 |
for (File f : files) { |
|
872 |
boolean isLauncher = f.equals(launcherFile); |
|
873 |
||
874 |
launcherSet = launcherSet || isLauncher; |
|
875 |
||
876 |
boolean doShortcuts = |
|
877 |
isLauncher && (menuShortcut || desktopShortcut); |
|
878 |
||
879 |
String thisFileId = isLauncher ? LAUNCHER_ID : ("FileId" + (id++)); |
|
880 |
idToFileMap.put(f.getName(), thisFileId); |
|
881 |
||
882 |
out.println(prefix + " <File Id=\"" + |
|
883 |
thisFileId + "\"" |
|
884 |
+ " Name=\"" + f.getName() + "\" " |
|
885 |
+ " Source=\"" + relativePath(imageRootDir, f) + "\"" |
|
886 |
+ (BIT_ARCH_64.fetchFrom(params) ? |
|
887 |
" ProcessorArchitecture=\"x64\"" : "") + ">"); |
|
888 |
if (doShortcuts && desktopShortcut) { |
|
889 |
out.println(prefix |
|
890 |
+ " <Shortcut Id=\"desktopShortcut\" Directory=" |
|
891 |
+ "\"DesktopFolder\"" |
|
892 |
+ " Name=\"" + APP_NAME.fetchFrom(params) |
|
893 |
+ "\" WorkingDirectory=\"INSTALLDIR\"" |
|
894 |
+ " Advertise=\"no\" Icon=\"DesktopIcon.exe\"" |
|
895 |
+ " IconIndex=\"0\" />"); |
|
896 |
} |
|
897 |
if (doShortcuts && menuShortcut) { |
|
898 |
out.println(prefix |
|
899 |
+ " <Shortcut Id=\"ExeShortcut\" Directory=" |
|
900 |
+ "\"ProgramMenuDir\"" |
|
901 |
+ " Name=\"" + APP_NAME.fetchFrom(params) |
|
902 |
+ "\" Advertise=\"no\" Icon=\"StartMenuIcon.exe\"" |
|
903 |
+ " IconIndex=\"0\" />"); |
|
904 |
} |
|
905 |
||
906 |
List<Map<String, ? super Object>> secondaryLaunchers = |
|
907 |
SECONDARY_LAUNCHERS.fetchFrom(params); |
|
908 |
for (int i = 0; i < secondaryLaunchers.size(); i++) { |
|
909 |
Map<String, ? super Object> sl = secondaryLaunchers.get(i); |
|
910 |
File secondaryLauncherFile = new File(imageRootDir, |
|
911 |
WinAppBundler.getLauncherName(sl)); |
|
912 |
if (f.equals(secondaryLauncherFile)) { |
|
913 |
if (SHORTCUT_HINT.fetchFrom(sl)) { |
|
914 |
out.println(prefix |
|
915 |
+ " <Shortcut Id=\"desktopShortcut" |
|
916 |
+ i + "\" Directory=\"DesktopFolder\"" |
|
917 |
+ " Name=\"" + APP_NAME.fetchFrom(sl) |
|
918 |
+ "\" WorkingDirectory=\"INSTALLDIR\"" |
|
919 |
+ " Advertise=\"no\" Icon=\"Launcher" |
|
920 |
+ i + ".exe\" IconIndex=\"0\" />"); |
|
921 |
} |
|
922 |
if (MENU_HINT.fetchFrom(sl)) { |
|
923 |
out.println(prefix |
|
924 |
+ " <Shortcut Id=\"ExeShortcut" |
|
925 |
+ i + "\" Directory=\"ProgramMenuDir\"" |
|
926 |
+ " Name=\"" + APP_NAME.fetchFrom(sl) |
|
927 |
+ "\" Advertise=\"no\" Icon=\"Launcher" |
|
928 |
+ i + ".exe\" IconIndex=\"0\" />"); |
|
929 |
// Should we allow different menu groups? Not for now. |
|
930 |
} |
|
931 |
} |
|
932 |
} |
|
933 |
out.println(prefix + " </File>"); |
|
934 |
} |
|
935 |
||
936 |
if (launcherSet) { |
|
937 |
List<Map<String, ? super Object>> fileAssociations = |
|
938 |
FILE_ASSOCIATIONS.fetchFrom(params); |
|
939 |
String regName = APP_REGISTRY_NAME.fetchFrom(params); |
|
940 |
Set<String> defaultedMimes = new TreeSet<>(); |
|
941 |
int count = 0; |
|
942 |
for (Map<String, ? super Object> fa : fileAssociations) { |
|
943 |
String description = FA_DESCRIPTION.fetchFrom(fa); |
|
944 |
List<String> extensions = FA_EXTENSIONS.fetchFrom(fa); |
|
945 |
List<String> mimeTypes = FA_CONTENT_TYPE.fetchFrom(fa); |
|
946 |
File icon = FA_ICON.fetchFrom(fa); // TODO FA_ICON_ICO |
|
947 |
||
948 |
String mime = (mimeTypes == null || |
|
949 |
mimeTypes.isEmpty()) ? null : mimeTypes.get(0); |
|
950 |
||
951 |
if (extensions == null) { |
|
952 |
Log.verbose(I18N.getString( |
|
953 |
"message.creating-association-with-null-extension")); |
|
954 |
||
955 |
String entryName = regName + "File"; |
|
956 |
if (count > 0) { |
|
957 |
entryName += "." + count; |
|
958 |
} |
|
959 |
count++; |
|
960 |
out.print(prefix + " <ProgId Id='" + entryName |
|
961 |
+ "' Description='" + description + "'"); |
|
962 |
if (icon != null && icon.exists()) { |
|
963 |
out.print(" Icon='" + idToFileMap.get(icon.getName()) |
|
964 |
+ "' IconIndex='0'"); |
|
965 |
} |
|
966 |
out.println(" />"); |
|
967 |
} else { |
|
968 |
for (String ext : extensions) { |
|
969 |
String entryName = regName + "File"; |
|
970 |
if (count > 0) { |
|
971 |
entryName += "." + count; |
|
972 |
} |
|
973 |
count++; |
|
974 |
||
975 |
out.print(prefix + " <ProgId Id='" + entryName |
|
976 |
+ "' Description='" + description + "'"); |
|
977 |
if (icon != null && icon.exists()) { |
|
978 |
out.print(" Icon='" |
|
979 |
+ idToFileMap.get(icon.getName()) |
|
980 |
+ "' IconIndex='0'"); |
|
981 |
} |
|
982 |
out.println(">"); |
|
983 |
||
984 |
if (extensions == null) { |
|
985 |
Log.verbose(I18N.getString( |
|
986 |
"message.creating-association-with-null-extension")); |
|
987 |
} else { |
|
988 |
out.print(prefix + " <Extension Id='" |
|
989 |
+ ext + "' Advertise='no'"); |
|
990 |
if (mime == null) { |
|
991 |
out.println(">"); |
|
992 |
} else { |
|
993 |
out.println(" ContentType='" + mime + "'>"); |
|
994 |
if (!defaultedMimes.contains(mime)) { |
|
995 |
out.println(prefix |
|
996 |
+ " <MIME ContentType='" |
|
997 |
+ mime + "' Default='yes' />"); |
|
998 |
defaultedMimes.add(mime); |
|
999 |
} |
|
1000 |
} |
|
1001 |
out.println(prefix |
|
1002 |
+ " <Verb Id='open' Command='Open' " |
|
1003 |
+ "TargetFile='" + LAUNCHER_ID |
|
1004 |
+ "' Argument='\"%1\"' />"); |
|
1005 |
out.println(prefix + " </Extension>"); |
|
1006 |
} |
|
1007 |
out.println(prefix + " </ProgId>"); |
|
1008 |
} |
|
1009 |
} |
|
1010 |
} |
|
1011 |
} |
|
1012 |
||
1013 |
out.println(prefix + " </Component>"); |
|
1014 |
||
1015 |
for (File d : dirs) { |
|
1016 |
out.println(prefix + " <Directory Id=\"dirid" + (id++) |
|
1017 |
+ "\" Name=\"" + d.getName() + "\">"); |
|
1018 |
walkFileTree(params, d, out, prefix + " "); |
|
1019 |
out.println(prefix + " </Directory>"); |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
1023 |
String getRegistryRoot(Map<String, ? super Object> params) { |
|
1024 |
if (MSI_SYSTEM_WIDE.fetchFrom(params)) { |
|
1025 |
return "HKLM"; |
|
1026 |
} else { |
|
1027 |
return "HKCU"; |
|
1028 |
} |
|
1029 |
} |
|
1030 |
||
1031 |
boolean prepareContentList(Map<String, ? super Object> params) |
|
1032 |
throws FileNotFoundException { |
|
1033 |
File f = new File( |
|
1034 |
CONFIG_ROOT.fetchFrom(params), MSI_PROJECT_CONTENT_FILE); |
|
1035 |
PrintStream out = new PrintStream(f); |
|
1036 |
||
1037 |
// opening |
|
1038 |
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); |
|
1039 |
out.println("<Include>"); |
|
1040 |
||
1041 |
out.println(" <Directory Id=\"TARGETDIR\" Name=\"SourceDir\">"); |
|
1042 |
if (MSI_SYSTEM_WIDE.fetchFrom(params)) { |
|
1043 |
// install to programfiles |
|
1044 |
if (BIT_ARCH_64.fetchFrom(params)) { |
|
1045 |
out.println(" <Directory Id=\"ProgramFiles64Folder\" " |
|
1046 |
+ "Name=\"PFiles\">"); |
|
1047 |
} else { |
|
1048 |
out.println(" <Directory Id=\"ProgramFilesFolder\" " |
|
1049 |
+ "Name=\"PFiles\">"); |
|
1050 |
} |
|
1051 |
} else { |
|
1052 |
// install to user folder |
|
1053 |
out.println( |
|
1054 |
" <Directory Name=\"AppData\" Id=\"LocalAppDataFolder\">"); |
|
1055 |
} |
|
1056 |
out.println(" <Directory Id=\"APPLICATIONFOLDER\" Name=\"" |
|
1057 |
+ APP_NAME.fetchFrom(params) + "\">"); |
|
1058 |
||
1059 |
// dynamic part |
|
1060 |
id = 0; |
|
1061 |
compId = 0; // reset counters |
|
1062 |
walkFileTree(params, WIN_APP_IMAGE.fetchFrom(params), out, " "); |
|
1063 |
||
1064 |
// closing |
|
1065 |
out.println(" </Directory>"); |
|
1066 |
out.println(" </Directory>"); |
|
1067 |
||
1068 |
// for shortcuts |
|
1069 |
if (SHORTCUT_HINT.fetchFrom(params)) { |
|
1070 |
out.println(" <Directory Id=\"DesktopFolder\" />"); |
|
1071 |
} |
|
1072 |
if (MENU_HINT.fetchFrom(params)) { |
|
1073 |
out.println(" <Directory Id=\"ProgramMenuFolder\">"); |
|
1074 |
out.println(" <Directory Id=\"ProgramMenuDir\" Name=\"" |
|
1075 |
+ MENU_GROUP.fetchFrom(params) + "\">"); |
|
1076 |
out.println(" <Component Id=\"comp" + (compId++) + "\"" |
|
1077 |
+ " Guid=\"" + UUID.randomUUID().toString() + "\"" |
|
1078 |
+ (BIT_ARCH_64.fetchFrom(params) ? " Win64=\"yes\"" : "") |
|
1079 |
+ ">"); |
|
1080 |
out.println(" <RemoveFolder Id=\"ProgramMenuDir\" " |
|
1081 |
+ "On=\"uninstall\" />"); |
|
1082 |
// This has to be under HKCU to make WiX happy. |
|
1083 |
// There are numberous discussions on this amoung WiX users |
|
1084 |
// (if user A installs and user B uninstalls key is left behind) |
|
1085 |
// there are suggested workarounds but none of them are appealing. |
|
1086 |
// Leave it for now |
|
1087 |
out.println( |
|
1088 |
" <RegistryValue Root=\"HKCU\" Key=\"Software\\" |
|
1089 |
+ VENDOR.fetchFrom(params) + "\\" |
|
1090 |
+ APP_NAME.fetchFrom(params) |
|
1091 |
+ "\" Type=\"string\" Value=\"\" />"); |
|
1092 |
out.println(" </Component>"); |
|
1093 |
out.println(" </Directory>"); |
|
1094 |
out.println(" </Directory>"); |
|
1095 |
} |
|
1096 |
||
1097 |
out.println(" </Directory>"); |
|
1098 |
||
1099 |
out.println(" <Feature Id=\"DefaultFeature\" " |
|
1100 |
+ "Title=\"Main Feature\" Level=\"1\">"); |
|
1101 |
for (int j = 0; j < compId; j++) { |
|
1102 |
out.println(" <ComponentRef Id=\"comp" + j + "\" />"); |
|
1103 |
} |
|
1104 |
// component is defined in the template.wsx |
|
1105 |
out.println(" <ComponentRef Id=\"CleanupMainApplicationFolder\" />"); |
|
1106 |
out.println(" </Feature>"); |
|
1107 |
out.println("</Include>"); |
|
1108 |
||
1109 |
out.close(); |
|
1110 |
return true; |
|
1111 |
} |
|
1112 |
||
1113 |
private File getConfig_ProjectFile(Map<String, ? super Object> params) { |
|
1114 |
return new File(CONFIG_ROOT.fetchFrom(params), |
|
1115 |
APP_NAME.fetchFrom(params) + ".wxs"); |
|
1116 |
} |
|
1117 |
||
1118 |
private String getLicenseFile(Map<String, ? super Object> params) { |
|
1119 |
List<String> licenseFiles = LICENSE_FILE.fetchFrom(params); |
|
1120 |
if (licenseFiles == null || licenseFiles.isEmpty()) { |
|
1121 |
return null; |
|
1122 |
} else { |
|
1123 |
return licenseFiles.get(0); |
|
1124 |
} |
|
1125 |
} |
|
1126 |
||
1127 |
private boolean prepareWiXConfig( |
|
1128 |
Map<String, ? super Object> params) throws IOException { |
|
1129 |
return prepareMainProjectFile(params) && prepareContentList(params); |
|
1130 |
||
1131 |
} |
|
1132 |
private final static String MSI_PROJECT_TEMPLATE = "template.wxs"; |
|
1133 |
private final static String MSI_PROJECT_TEMPLATE_SERVER_JRE = |
|
1134 |
"template.jre.wxs"; |
|
1135 |
private final static String MSI_PROJECT_CONTENT_FILE = "bundle.wxi"; |
|
1136 |
||
1137 |
private File buildMSI(Map<String, ? super Object> params, File outdir) |
|
1138 |
throws IOException { |
|
1139 |
File tmpDir = new File(BUILD_ROOT.fetchFrom(params), "tmp"); |
|
1140 |
File candleOut = new File( |
|
1141 |
tmpDir, APP_NAME.fetchFrom(params) +".wixobj"); |
|
1142 |
File msiOut = new File( |
|
1143 |
outdir, INSTALLER_FILE_NAME.fetchFrom(params) + ".msi"); |
|
1144 |
||
1145 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
1146 |
"message.preparing-msi-config"), msiOut.getAbsolutePath())); |
|
1147 |
||
1148 |
msiOut.getParentFile().mkdirs(); |
|
1149 |
||
1150 |
// run candle |
|
1151 |
ProcessBuilder pb = new ProcessBuilder( |
|
1152 |
TOOL_CANDLE_EXECUTABLE.fetchFrom(params), |
|
1153 |
"-nologo", |
|
1154 |
getConfig_ProjectFile(params).getAbsolutePath(), |
|
1155 |
"-ext", "WixUtilExtension", |
|
1156 |
"-out", candleOut.getAbsolutePath()); |
|
1157 |
pb = pb.directory(WIN_APP_IMAGE.fetchFrom(params)); |
|
1158 |
IOUtils.exec(pb, false); |
|
1159 |
||
1160 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
1161 |
"message.generating-msi"), msiOut.getAbsolutePath())); |
|
1162 |
||
1163 |
boolean enableLicenseUI = (getLicenseFile(params) != null); |
|
1164 |
boolean enableInstalldirUI = INSTALLDIR_CHOOSER.fetchFrom(params); |
|
1165 |
||
1166 |
List<String> commandLine = new ArrayList<>(); |
|
1167 |
||
1168 |
commandLine.add(TOOL_LIGHT_EXECUTABLE.fetchFrom(params)); |
|
1169 |
if (enableLicenseUI) { |
|
1170 |
commandLine.add("-dWixUILicenseRtf="+getLicenseFile(params)); |
|
1171 |
} |
|
1172 |
commandLine.add("-nologo"); |
|
1173 |
commandLine.add("-spdb"); |
|
1174 |
commandLine.add("-sice:60"); |
|
1175 |
// ignore warnings due to "missing launcguage info" (ICE60) |
|
1176 |
commandLine.add(candleOut.getAbsolutePath()); |
|
1177 |
commandLine.add("-ext"); |
|
1178 |
commandLine.add("WixUtilExtension"); |
|
1179 |
if (enableLicenseUI || enableInstalldirUI) { |
|
1180 |
commandLine.add("-ext"); |
|
1181 |
commandLine.add("WixUIExtension.dll"); |
|
1182 |
} |
|
1183 |
commandLine.add("-out"); |
|
1184 |
commandLine.add(msiOut.getAbsolutePath()); |
|
1185 |
||
1186 |
// create .msi |
|
1187 |
pb = new ProcessBuilder(commandLine); |
|
1188 |
||
1189 |
pb = pb.directory(WIN_APP_IMAGE.fetchFrom(params)); |
|
1190 |
IOUtils.exec(pb, false); |
|
1191 |
||
1192 |
candleOut.delete(); |
|
1193 |
IOUtils.deleteRecursive(tmpDir); |
|
1194 |
||
1195 |
return msiOut; |
|
1196 |
} |
|
1197 |
||
1198 |
public static void ensureByMutationFileIsRTF(File f) { |
|
1199 |
if (f == null || !f.isFile()) return; |
|
1200 |
||
1201 |
try { |
|
1202 |
boolean existingLicenseIsRTF = false; |
|
1203 |
||
1204 |
try (FileInputStream fin = new FileInputStream(f)) { |
|
1205 |
byte[] firstBits = new byte[7]; |
|
1206 |
||
1207 |
if (fin.read(firstBits) == firstBits.length) { |
|
1208 |
String header = new String(firstBits); |
|
1209 |
existingLicenseIsRTF = "{\\rtf1\\".equals(header); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
||
1213 |
if (!existingLicenseIsRTF) { |
|
1214 |
List<String> oldLicense = Files.readAllLines(f.toPath()); |
|
1215 |
try (Writer w = Files.newBufferedWriter( |
|
1216 |
f.toPath(), Charset.forName("Windows-1252"))) { |
|
1217 |
w.write("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033" |
|
1218 |
+ "{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\n" |
|
1219 |
+ "\\viewkind4\\uc1\\pard\\sa200\\sl276" |
|
1220 |
+ "\\slmult1\\lang9\\fs20 "); |
|
1221 |
oldLicense.forEach(l -> { |
|
1222 |
try { |
|
1223 |
for (char c : l.toCharArray()) { |
|
1224 |
// 0x00 <= ch < 0x20 Escaped (\'hh) |
|
1225 |
// 0x20 <= ch < 0x80 Raw(non - escaped) char |
|
1226 |
// 0x80 <= ch <= 0xFF Escaped(\ 'hh) |
|
1227 |
// 0x5C, 0x7B, 0x7D (special RTF characters |
|
1228 |
// \,{,})Escaped(\'hh) |
|
1229 |
// ch > 0xff Escaped (\\ud###?) |
|
1230 |
if (c < 0x10) { |
|
1231 |
w.write("\\'0"); |
|
1232 |
w.write(Integer.toHexString(c)); |
|
1233 |
} else if (c > 0xff) { |
|
1234 |
w.write("\\ud"); |
|
1235 |
w.write(Integer.toString(c)); |
|
1236 |
// \\uc1 is in the header and in effect |
|
1237 |
// so we trail with a replacement char if |
|
1238 |
// the font lacks that character - '?' |
|
1239 |
w.write("?"); |
|
1240 |
} else if ((c < 0x20) || (c >= 0x80) || |
|
1241 |
(c == 0x5C) || (c == 0x7B) || |
|
1242 |
(c == 0x7D)) { |
|
1243 |
w.write("\\'"); |
|
1244 |
w.write(Integer.toHexString(c)); |
|
1245 |
} else { |
|
1246 |
w.write(c); |
|
1247 |
} |
|
1248 |
} |
|
1249 |
// blank lines are interpreted as paragraph breaks |
|
1250 |
if (l.length() < 1) { |
|
1251 |
w.write("\\par"); |
|
1252 |
} else { |
|
1253 |
w.write(" "); |
|
1254 |
} |
|
1255 |
w.write("\r\n"); |
|
1256 |
} catch (IOException e) { |
|
1257 |
Log.verbose(e); |
|
1258 |
} |
|
1259 |
}); |
|
1260 |
w.write("}\r\n"); |
|
1261 |
} |
|
1262 |
} |
|
1263 |
} catch (IOException e) { |
|
1264 |
Log.verbose(e); |
|
1265 |
} |
|
1266 |
||
1267 |
} |
|
1268 |
} |