forked from dotnet/ResXResourceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cs
More file actions
208 lines (158 loc) · 7.06 KB
/
Copy pathHost.cs
File metadata and controls
208 lines (158 loc) · 7.06 KB
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
namespace ResXManager.Scripting
{
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using tomenglertde.ResXManager.Infrastructure;
using tomenglertde.ResXManager.Model;
using tomenglertde.ResXManager.Model.Properties;
using TomsToolbox.Desktop.Composition;
public sealed class Host : IDisposable
{
[NotNull]
private readonly ICompositionHost _compositionHost = new CompositionHost();
[NotNull]
private readonly SourceFilesProvider _sourceFilesProvider;
[NotNull]
private readonly ResourceManager _resourceManager;
[NotNull]
private readonly Configuration _configuration;
public Host()
{
var assembly = GetType().Assembly;
var folder = Path.GetDirectoryName(assembly.Location);
Contract.Assume(!string.IsNullOrEmpty(folder));
_compositionHost.AddCatalog(new DirectoryCatalog(folder, "*.dll"));
_sourceFilesProvider = _compositionHost.GetExportedValue<SourceFilesProvider>();
_resourceManager = _compositionHost.GetExportedValue<ResourceManager>();
_resourceManager.BeginEditing += ResourceManager_BeginEditing;
_configuration = _compositionHost.GetExportedValue<Configuration>();
}
[NotNull]
public ResourceManager ResourceManager => _resourceManager;
public void Load([CanBeNull] string folder, [CanBeNull] string exclusionFilter = @"Migrations\\\d{15}")
{
_sourceFilesProvider.Folder = folder;
_sourceFilesProvider.ExclusionFilter = exclusionFilter;
_resourceManager.Reload();
}
public void Save()
{
_resourceManager.Save();
}
public void ExportExcel([NotNull] string filePath)
{
Contract.Requires(filePath != null);
ExportExcel(filePath, null);
}
public void ExportExcel([NotNull] string filePath, [CanBeNull] object entries)
{
Contract.Requires(filePath != null);
ExportExcel(filePath, entries as IEnumerable<object>, null);
}
public void ExportExcel([NotNull] string filePath, [CanBeNull] object entries, [CanBeNull] object languages)
{
Contract.Requires(filePath != null);
ExportExcel(filePath, entries, languages, null);
}
public void ExportExcel([NotNull] string filePath, [CanBeNull] object entries, [CanBeNull] object languages, [CanBeNull] object comments)
{
Contract.Requires(filePath != null);
ExportExcel(filePath, entries, languages, comments, ExcelExportMode.SingleSheet);
}
public void ExportExcel([NotNull] string filePath, [CanBeNull] object entries, [CanBeNull] object languages, [CanBeNull] object comments, ExcelExportMode exportMode)
{
Contract.Requires(filePath != null);
var resourceScope = new ResourceScope(
entries ?? _resourceManager.TableEntries,
languages ?? _resourceManager.Cultures,
comments ?? new object[0]);
_resourceManager.ExportExcelFile(filePath, resourceScope, exportMode);
}
public void ImportExcel([NotNull] string filePath)
{
Contract.Requires(filePath != null);
var changes = _resourceManager.ImportExcelFile(filePath);
changes.Apply();
}
[NotNull]
public string CreateSnapshot()
{
Contract.Ensures(Contract.Result<string>() != null);
return _resourceManager.CreateSnapshot();
}
public void LoadSnapshot([CanBeNull] string value)
{
_resourceManager.LoadSnapshot(value);
}
public void Dispose()
{
_compositionHost.Dispose();
}
private void ResourceManager_BeginEditing([NotNull] object sender, [NotNull] ResourceBeginEditingEventArgs e)
{
if (!CanEdit(e.Entity, e.CultureKey))
{
e.Cancel = true;
}
}
private bool CanEdit([NotNull] ResourceEntity entity, [CanBeNull] CultureKey cultureKey)
{
Contract.Requires(entity != null);
if (cultureKey == null)
return true;
var rootFolder = _sourceFilesProvider.Folder;
if (string.IsNullOrEmpty(rootFolder))
return false;
var language = entity.Languages.FirstOrDefault(lang => cultureKey.Equals(lang.CultureKey));
if (language != null)
return true;
var culture = cultureKey.Culture;
if (culture == null)
return false; // no neutral culture => this should never happen.
var neutralLanguage = entity.Languages.FirstOrDefault();
if (neutralLanguage == null)
return false;
var languageFileName = neutralLanguage.ProjectFile.GetLanguageFileName(culture);
if (!File.Exists(languageFileName))
{
var directoryName = Path.GetDirectoryName(languageFileName);
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);
File.WriteAllText(languageFileName, Resources.EmptyResxTemplate);
}
entity.AddLanguage(new ProjectFile(languageFileName, rootFolder, entity.ProjectName, null));
return true;
}
[NotNull]
public Configuration Configuration => _configuration;
[ContractInvariantMethod]
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
[Conditional("CONTRACTS_FULL")]
private void ObjectInvariant()
{
Contract.Invariant(_compositionHost != null);
Contract.Invariant(_sourceFilesProvider != null);
Contract.Invariant(_resourceManager != null);
Contract.Invariant(_configuration != null);
}
}
[SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
[Export(typeof(IConfiguration))]
[Export(typeof(Configuration))]
public class Configuration : IConfiguration
{
public bool SaveFilesImmediatelyUponChange => false;
public CultureInfo NeutralResourcesLanguage { get; set; } = new CultureInfo("en-US");
public StringComparison? EffectiveResXSortingComparison { get; set; }
public DuplicateKeyHandling DuplicateKeyHandling { get; set; }
}
}