aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/AppCommon.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Core/AppCommon.cs')
-rw-r--r--src/WixToolset.Core/AppCommon.cs145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/WixToolset.Core/AppCommon.cs b/src/WixToolset.Core/AppCommon.cs
new file mode 100644
index 00000000..30ec7771
--- /dev/null
+++ b/src/WixToolset.Core/AppCommon.cs
@@ -0,0 +1,145 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolset
4{
5 using System;
6 using System.Collections.Specialized;
7 using System.Configuration;
8 using System.Globalization;
9 using System.IO;
10 using System.Reflection;
11 using System.Text;
12 using System.Threading;
13 using WixToolset.Data;
14
15 /// <summary>
16 /// Common utilities for Wix applications.
17 /// </summary>
18 public static class AppCommon
19 {
20 /// <summary>
21 /// Read the configuration file (*.exe.config).
22 /// </summary>
23 /// <param name="extensions">Extensions to load.</param>
24 public static void ReadConfiguration(StringCollection extensions)
25 {
26 if (null == extensions)
27 {
28 throw new ArgumentNullException("extensions");
29 }
30
31#if REDO_IN_NETCORE
32 // Don't use the default AppSettings reader because
33 // the tool may be called from within another process.
34 // Instead, read the .exe.config file from the tool location.
35 string toolPath = (new System.Uri(Assembly.GetCallingAssembly().CodeBase)).LocalPath;
36 Configuration config = ConfigurationManager.OpenExeConfiguration(toolPath);
37 if (config.HasFile)
38 {
39 KeyValueConfigurationElement configVal = config.AppSettings.Settings["extensions"];
40 if (configVal != null)
41 {
42 string extensionTypes = configVal.Value;
43 foreach (string extensionType in extensionTypes.Split(";".ToCharArray()))
44 {
45 extensions.Add(extensionType);
46 }
47 }
48 }
49#endif
50 }
51
52 /// <summary>
53 /// Gets a unique temporary location or uses the provided temporary location.
54 /// </summary>
55 /// <param name="tempLocation">Optional temporary location to use.</param>
56 /// <returns>Temporary location.</returns>
57 public static string GetTempLocation(string tempLocation = null)
58 {
59 if (String.IsNullOrEmpty(tempLocation))
60 {
61 tempLocation = Environment.GetEnvironmentVariable("WIX_TEMP") ?? Path.GetTempPath();
62
63 do
64 {
65 tempLocation = Path.Combine(tempLocation, DateTime.Now.ToString("wixyyMMddTHHmmssffff"));
66 } while (Directory.Exists(tempLocation));
67 }
68
69 return tempLocation;
70 }
71
72 /// <summary>
73 /// Delete a directory with retries and best-effort cleanup.
74 /// </summary>
75 /// <param name="path">The directory to delete.</param>
76 /// <param name="messageHandler">The message handler.</param>
77 /// <returns>True if all files were deleted, false otherwise.</returns>
78 public static bool DeleteDirectory(string path, IMessageHandler messageHandler)
79 {
80 return Common.DeleteTempFiles(path, messageHandler);
81 }
82
83 /// <summary>
84 /// Prepares the console for localization.
85 /// </summary>
86 public static void PrepareConsoleForLocalization()
87 {
88 Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture();
89 if ((Console.OutputEncoding.CodePage != Encoding.UTF8.CodePage) &&
90 (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage) &&
91 (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage))
92 {
93 Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
94 }
95 }
96
97 /// <summary>
98 /// Handler for display message events.
99 /// </summary>
100 /// <param name="sender">Sender of message.</param>
101 /// <param name="e">Event arguments containing message to display.</param>
102 public static void ConsoleDisplayMessage(object sender, DisplayEventArgs e)
103 {
104 switch (e.Level)
105 {
106 case MessageLevel.Warning:
107 case MessageLevel.Error:
108 Console.Error.WriteLine(e.Message);
109 break;
110 default:
111 Console.WriteLine(e.Message);
112 break;
113 }
114 }
115
116 /// <summary>
117 /// Creates and returns the string for CreatingApplication field (MSI Summary Information Stream).
118 /// </summary>
119 /// <remarks>It reads the AssemblyProductAttribute and AssemblyVersionAttribute of executing assembly
120 /// and builds the CreatingApplication string of the form "[ProductName] ([ProductVersion])".</remarks>
121 /// <returns>Returns value for PID_APPNAME."</returns>
122 public static string GetCreatingApplicationString()
123 {
124 Assembly assembly = Assembly.GetExecutingAssembly();
125 return WixDistribution.ReplacePlaceholders("[AssemblyProduct] ([FileVersion])", assembly);
126 }
127
128 /// <summary>
129 /// Displays help message header on Console for caller tool.
130 /// </summary>
131 public static void DisplayToolHeader()
132 {
133 Assembly assembly = Assembly.GetCallingAssembly();
134 Console.WriteLine(WixDistribution.ReplacePlaceholders(WixDistributionSpecificStrings.ToolsetHelpHeader, assembly));
135 }
136
137 /// <summary>
138 /// Displays help message header on Console for caller tool.
139 /// </summary>
140 public static void DisplayToolFooter()
141 {
142 Console.Write(WixDistribution.ReplacePlaceholders(WixDistributionSpecificStrings.ToolsetHelpFooter, null));
143 }
144 }
145}