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