aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/WixAssignCulture.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2018-07-19 00:58:00 -0700
committerRob Mensching <rob@firegiant.com>2018-07-21 07:36:59 -0700
commit2724cfee4c163f3297ee25edfd2372767cfd4945 (patch)
tree8cdda34c83bea014a586a491e3b4b187ad8f16da /src/WixToolset.BuildTasks/WixAssignCulture.cs
parent4d40bef9cf51b8cff7e1f6a73fdf68b9722eb8a0 (diff)
downloadwix-2724cfee4c163f3297ee25edfd2372767cfd4945.tar.gz
wix-2724cfee4c163f3297ee25edfd2372767cfd4945.tar.bz2
wix-2724cfee4c163f3297ee25edfd2372767cfd4945.zip
Move tool projects to Tools repo
Diffstat (limited to 'src/WixToolset.BuildTasks/WixAssignCulture.cs')
-rw-r--r--src/WixToolset.BuildTasks/WixAssignCulture.cs229
1 files changed, 0 insertions, 229 deletions
diff --git a/src/WixToolset.BuildTasks/WixAssignCulture.cs b/src/WixToolset.BuildTasks/WixAssignCulture.cs
deleted file mode 100644
index a8baa62f..00000000
--- a/src/WixToolset.BuildTasks/WixAssignCulture.cs
+++ /dev/null
@@ -1,229 +0,0 @@
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.BuildTasks
4{
5 using System;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.IO;
9 using System.Xml;
10 using Microsoft.Build.Framework;
11 using Microsoft.Build.Utilities;
12
13 /// <summary>
14 /// This task assigns Culture metadata to files based on the value of the Culture attribute on the
15 /// WixLocalization element inside the file.
16 /// </summary>
17 public class WixAssignCulture : Task
18 {
19 private const string CultureAttributeName = "Culture";
20 private const string OutputFolderMetadataName = "OutputFolder";
21 private const string InvariantCultureIdentifier = "neutral";
22 private const string NullCultureIdentifier = "null";
23
24 /// <summary>
25 /// The list of cultures to build. Cultures are specified in the following form:
26 /// primary culture,first fallback culture, second fallback culture;...
27 /// Culture groups are seperated by semi-colons
28 /// Culture precedence within a culture group is evaluated from left to right where fallback cultures are
29 /// separated with commas.
30 /// The first (primary) culture in a culture group will be used as the output sub-folder.
31 /// </summary>
32 public string Cultures { get; set; }
33
34 /// <summary>
35 /// The list of files to apply culture information to.
36 /// </summary>
37 [Required]
38 public ITaskItem[] Files
39 {
40 get;
41 set;
42 }
43
44 /// <summary>
45 /// The files that had culture information applied
46 /// </summary>
47 [Output]
48 public ITaskItem[] CultureGroups
49 {
50 get;
51 private set;
52 }
53
54 /// <summary>
55 /// Applies culture information to the files specified by the Files property.
56 /// This task intentionally does not validate that strings are valid Cultures so that we can support
57 /// psuedo-loc.
58 /// </summary>
59 /// <returns>True upon completion of the task execution.</returns>
60 public override bool Execute()
61 {
62 // First, process the culture group list the user specified in the cultures property
63 List<CultureGroup> cultureGroups = new List<CultureGroup>();
64
65 if (!String.IsNullOrEmpty(this.Cultures))
66 {
67 // Get rid of extra quotes
68 this.Cultures = this.Cultures.Trim('\"');
69
70 foreach (string cultureGroupString in this.Cultures.Split(';'))
71 {
72 if (0 == cultureGroupString.Length)
73 {
74 // MSBuild v2.0.50727 cannnot handle "" items
75 // for the invariant culture we require the neutral keyword
76 continue;
77 }
78 CultureGroup cultureGroup = new CultureGroup(cultureGroupString);
79 cultureGroups.Add(cultureGroup);
80 }
81 }
82 else
83 {
84 // Only process the EmbeddedResource items if cultures was unspecified
85 foreach (ITaskItem file in this.Files)
86 {
87 // Ignore non-wxls
88 if (!String.Equals(file.GetMetadata("Extension"), ".wxl", StringComparison.OrdinalIgnoreCase))
89 {
90 Log.LogError("Unable to retrieve the culture for EmbeddedResource {0}. The file type is not supported.", file.ItemSpec);
91 return false;
92 }
93 XmlDocument wxlFile = new XmlDocument();
94
95 try
96 {
97 wxlFile.Load(file.ItemSpec);
98 }
99 catch (FileNotFoundException)
100 {
101 Log.LogError("Unable to retrieve the culture for EmbeddedResource {0}. The file was not found.", file.ItemSpec);
102 return false;
103 }
104 catch (Exception e)
105 {
106 Log.LogError("Unable to retrieve the culture for EmbeddedResource {0}: {1}", file.ItemSpec, e.Message);
107 return false;
108 }
109
110 // Take the culture value and try using it to create a culture.
111 XmlAttribute cultureAttr = wxlFile.DocumentElement.Attributes[WixAssignCulture.CultureAttributeName];
112 string wxlCulture = null == cultureAttr ? String.Empty : cultureAttr.Value;
113 if (0 == wxlCulture.Length)
114 {
115 // We use a keyword for the invariant culture because MSBuild v2.0.50727 cannnot handle "" items
116 wxlCulture = InvariantCultureIdentifier;
117 }
118
119 // We found the culture for the WXL, we now need to determine if it maps to a culture group specified
120 // in the Cultures property or if we need to create a new one.
121 Log.LogMessage(MessageImportance.Low, "Culture \"{0}\" from EmbeddedResource {1}.", wxlCulture, file.ItemSpec);
122
123 bool cultureGroupExists = false;
124 foreach (CultureGroup cultureGroup in cultureGroups)
125 {
126 foreach (string culture in cultureGroup.Cultures)
127 {
128 if (String.Equals(wxlCulture, culture, StringComparison.OrdinalIgnoreCase))
129 {
130 cultureGroupExists = true;
131 break;
132 }
133 }
134 }
135
136 // The WXL didn't match a culture group we already have so create a new one.
137 if (!cultureGroupExists)
138 {
139 cultureGroups.Add(new CultureGroup(wxlCulture));
140 }
141 }
142 }
143
144 // If we didn't create any culture groups the culture was unspecificed and no WXLs were included
145 // Build an unlocalized target in the output folder
146 if (cultureGroups.Count == 0)
147 {
148 cultureGroups.Add(new CultureGroup());
149 }
150
151 List<TaskItem> cultureGroupItems = new List<TaskItem>();
152
153 if (1 == cultureGroups.Count && 0 == this.Files.Length)
154 {
155 // Maintain old behavior, if only one culturegroup is specified and no WXL, output to the default folder
156 TaskItem cultureGroupItem = new TaskItem(cultureGroups[0].ToString());
157 cultureGroupItem.SetMetadata(OutputFolderMetadataName, CultureGroup.DefaultFolder);
158 cultureGroupItems.Add(cultureGroupItem);
159 }
160 else
161 {
162 foreach (CultureGroup cultureGroup in cultureGroups)
163 {
164 TaskItem cultureGroupItem = new TaskItem(cultureGroup.ToString());
165 cultureGroupItem.SetMetadata(OutputFolderMetadataName, cultureGroup.OutputFolder);
166 cultureGroupItems.Add(cultureGroupItem);
167 Log.LogMessage("Culture: {0}", cultureGroup.ToString());
168 }
169 }
170
171 this.CultureGroups = cultureGroupItems.ToArray();
172 return true;
173 }
174
175 private class CultureGroup
176 {
177 /// <summary>
178 /// TargetPath already has a '\', do not double it!
179 /// </summary>
180 public const string DefaultFolder = "";
181
182 /// <summary>
183 /// Initialize a null culture group
184 /// </summary>
185 public CultureGroup()
186 {
187 }
188
189 public CultureGroup(string cultureGroupString)
190 {
191 Debug.Assert(!String.IsNullOrEmpty(cultureGroupString));
192 foreach (string cultureString in cultureGroupString.Split(','))
193 {
194 this.Cultures.Add(cultureString);
195 }
196 }
197
198 public List<string> Cultures { get; } = new List<string>();
199
200 public string OutputFolder
201 {
202 get
203 {
204 string result = DefaultFolder;
205 if (this.Cultures.Count > 0 &&
206 !this.Cultures[0].Equals(InvariantCultureIdentifier, StringComparison.OrdinalIgnoreCase))
207 {
208 result = this.Cultures[0] + "\\";
209 }
210
211 return result;
212 }
213 }
214
215 public override string ToString()
216 {
217 if (this.Cultures.Count > 0)
218 {
219 return String.Join(";", this.Cultures);
220 }
221
222 // We use a keyword for a null culture because MSBuild cannnot handle "" items
223 // Null is different from neutral. For neutral we still want to do WXL
224 // filtering in Light.
225 return NullCultureIdentifier;
226 }
227 }
228 }
229}