diff options
| author | Rob Mensching <rob@firegiant.com> | 2017-12-31 02:17:12 -0800 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2017-12-31 02:17:12 -0800 |
| commit | 651ad904724f32f5e993fa73aa11a611d95a1a10 (patch) | |
| tree | c3f3541f36fe0dda04f2b756b59a0d7c535dbffa /src/WixToolset.Core/Resolver.cs | |
| parent | 84d1f05fee656c01938613bcc50bd8139006bbf6 (diff) | |
| download | wix-651ad904724f32f5e993fa73aa11a611d95a1a10.tar.gz wix-651ad904724f32f5e993fa73aa11a611d95a1a10.tar.bz2 wix-651ad904724f32f5e993fa73aa11a611d95a1a10.zip | |
Support filtering localizations by culture
Diffstat (limited to 'src/WixToolset.Core/Resolver.cs')
| -rw-r--r-- | src/WixToolset.Core/Resolver.cs | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/src/WixToolset.Core/Resolver.cs b/src/WixToolset.Core/Resolver.cs index c2d5cc87..03cf344b 100644 --- a/src/WixToolset.Core/Resolver.cs +++ b/src/WixToolset.Core/Resolver.cs | |||
| @@ -31,6 +31,8 @@ namespace WixToolset.Core | |||
| 31 | 31 | ||
| 32 | public IEnumerable<Localization> Localizations { get; set; } | 32 | public IEnumerable<Localization> Localizations { get; set; } |
| 33 | 33 | ||
| 34 | public IEnumerable<string> FilterCultures { get; set; } | ||
| 35 | |||
| 34 | public ResolveResult Execute() | 36 | public ResolveResult Execute() |
| 35 | { | 37 | { |
| 36 | var extensionManager = this.ServiceProvider.GetService<IExtensionManager>(); | 38 | var extensionManager = this.ServiceProvider.GetService<IExtensionManager>(); |
| @@ -40,6 +42,7 @@ namespace WixToolset.Core | |||
| 40 | context.BindPaths = this.BindPaths; | 42 | context.BindPaths = this.BindPaths; |
| 41 | context.Extensions = extensionManager.Create<IResolverExtension>(); | 43 | context.Extensions = extensionManager.Create<IResolverExtension>(); |
| 42 | context.ExtensionData = extensionManager.Create<IExtensionData>(); | 44 | context.ExtensionData = extensionManager.Create<IExtensionData>(); |
| 45 | context.FilterCultures = this.FilterCultures; | ||
| 43 | context.IntermediateFolder = this.IntermediateFolder; | 46 | context.IntermediateFolder = this.IntermediateFolder; |
| 44 | context.IntermediateRepresentation = this.IntermediateRepresentation; | 47 | context.IntermediateRepresentation = this.IntermediateRepresentation; |
| 45 | context.Localizations = this.Localizations; | 48 | context.Localizations = this.Localizations; |
| @@ -207,29 +210,76 @@ namespace WixToolset.Core | |||
| 207 | { | 210 | { |
| 208 | var creator = context.ServiceProvider.GetService<ITupleDefinitionCreator>(); | 211 | var creator = context.ServiceProvider.GetService<ITupleDefinitionCreator>(); |
| 209 | 212 | ||
| 213 | var localizations = FilterLocalizations(context); | ||
| 214 | |||
| 215 | foreach (var localization in localizations) | ||
| 216 | { | ||
| 217 | context.VariableResolver.AddLocalization(localization); | ||
| 218 | } | ||
| 219 | |||
| 220 | // Gather all the wix variables. | ||
| 221 | var wixVariableTuples = context.IntermediateRepresentation.Sections.SelectMany(s => s.Tuples).OfType<WixVariableTuple>(); | ||
| 222 | foreach (var tuple in wixVariableTuples) | ||
| 223 | { | ||
| 224 | context.VariableResolver.AddVariable(tuple.SourceLineNumbers, tuple.WixVariable, tuple.Value, tuple.Overridable); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | private static IEnumerable<Localization> FilterLocalizations(IResolveContext context) | ||
| 229 | { | ||
| 230 | var result = new List<Localization>(); | ||
| 231 | var filter = CalculateCultureFilter(context); | ||
| 232 | |||
| 210 | var localizations = context.Localizations.Concat(context.IntermediateRepresentation.Localizations).ToList(); | 233 | var localizations = context.Localizations.Concat(context.IntermediateRepresentation.Localizations).ToList(); |
| 211 | 234 | ||
| 212 | // Add localizations from the extensions with data. | 235 | // If there still is no filter, return all localizations. |
| 236 | AddFilteredLocalizations(result, filter, localizations); | ||
| 237 | |||
| 238 | // Filter localizations provided by extensions with data. | ||
| 239 | var creator = context.ServiceProvider.GetService<ITupleDefinitionCreator>(); | ||
| 240 | |||
| 213 | foreach (var data in context.ExtensionData) | 241 | foreach (var data in context.ExtensionData) |
| 214 | { | 242 | { |
| 215 | var library = data.GetLibrary(creator); | 243 | var library = data.GetLibrary(creator); |
| 216 | 244 | ||
| 217 | if (library?.Localizations != null) | 245 | if (library?.Localizations != null) |
| 218 | { | 246 | { |
| 219 | localizations.AddRange(library.Localizations); | 247 | var extensionFilter = (!filter.Any() && data.DefaultCulture != null) ? new[] { data.DefaultCulture } : filter; |
| 248 | |||
| 249 | AddFilteredLocalizations(result, extensionFilter, library.Localizations); | ||
| 220 | } | 250 | } |
| 221 | } | 251 | } |
| 222 | 252 | ||
| 223 | foreach (var localization in localizations) | 253 | return result; |
| 254 | } | ||
| 255 | |||
| 256 | private static IEnumerable<string> CalculateCultureFilter(IResolveContext context) | ||
| 257 | { | ||
| 258 | var filter = context.FilterCultures ?? Array.Empty<string>(); | ||
| 259 | |||
| 260 | // If no filter was specified, look for a language neutral localization file specified | ||
| 261 | // from the command-line (not embedded in the intermediate). If found, filter on language | ||
| 262 | // neutral. | ||
| 263 | if (!filter.Any() && context.Localizations.Any(l => String.IsNullOrEmpty(l.Culture))) | ||
| 224 | { | 264 | { |
| 225 | context.VariableResolver.AddLocalization(localization); | 265 | filter = new[] { String.Empty }; |
| 226 | } | 266 | } |
| 227 | 267 | ||
| 228 | // Gather all the wix variables. | 268 | return filter; |
| 229 | var wixVariableTuples = context.IntermediateRepresentation.Sections.SelectMany(s => s.Tuples).OfType<WixVariableTuple>(); | 269 | } |
| 230 | foreach (var tuple in wixVariableTuples) | 270 | |
| 271 | private static void AddFilteredLocalizations(List<Localization> result, IEnumerable<string> filter, IEnumerable<Localization> localizations) | ||
| 272 | { | ||
| 273 | if (!filter.Any()) | ||
| 231 | { | 274 | { |
| 232 | context.VariableResolver.AddVariable(tuple.SourceLineNumbers, tuple.WixVariable, tuple.Value, tuple.Overridable); | 275 | result.AddRange(localizations); |
| 276 | } | ||
| 277 | else // filter localizations in order specified by the filter | ||
| 278 | { | ||
| 279 | foreach (var culture in filter) | ||
| 280 | { | ||
| 281 | result.AddRange(localizations.Where(l => culture.Equals(l.Culture, StringComparison.OrdinalIgnoreCase))); | ||
| 282 | } | ||
| 233 | } | 283 | } |
| 234 | } | 284 | } |
| 235 | } | 285 | } |
