Skip to content

Commit

Permalink
Scripting - apply excel import, create missing languages on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Sep 17, 2016
1 parent f8b3131 commit c389170
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 41 deletions.
14 changes: 14 additions & 0 deletions ResXManager.Model/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ResXManager.Model/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,8 @@
<data name="SnapshotAnnotation" xml:space="preserve">
<value>Snapshot: "{0}"</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="EmptyResxTemplate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>EmptyResxTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>
1 change: 1 addition & 0 deletions ResXManager.Model/ResXManager.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Resource Include="Properties\EmptyResxTemplate.txt" />
<Content Include="Properties\Resources.Designer.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down
5 changes: 4 additions & 1 deletion ResXManager.Model/ResourceEntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ private static bool SetEntryData(this ResourceTableEntry entry, CultureInfo cult
{
Contract.Requires(entry != null);

if (!entry.CanEdit(culture))
return false;

switch (columnKind)
{
case ColumnKind.Text:
Expand Down Expand Up @@ -263,7 +266,7 @@ public static ICollection<EntryChange> ImportTable(this ResourceEntity entity, I
return changes;
}

private static IList<string> GetHeaderColumns(IList<IList<string>> table, ICollection<string> fixedColumnHeaders)
private static IList<string> GetHeaderColumns(ICollection<IList<string>> table, ICollection<string> fixedColumnHeaders)
{
Contract.Requires(table != null);
Contract.Requires(table.Count > 0);
Expand Down
68 changes: 60 additions & 8 deletions ResXManager.Scripting/Host.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
namespace ResXManager.Scripting
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Windows.Forms;
using System.Linq;

using tomenglertde.ResXManager.Infrastructure;
using tomenglertde.ResXManager.Model;
using tomenglertde.ResXManager.Model.Properties;

using TomsToolbox.Desktop;
using TomsToolbox.Desktop.Composition;

public sealed class Host : IDisposable
Expand All @@ -30,14 +30,15 @@ public Host()

_sourceFilesProvider = _compositionHost.GetExportedValue<SourceFilesProvider>();
_resourceManager = _compositionHost.GetExportedValue<ResourceManager>();
_resourceManager.BeginEditing += ResourceManager_BeginEditing;
}

public ResourceManager ResourceManager => _resourceManager;

public void Load(string folder)
{
_sourceFilesProvider.Folder = folder;

_resourceManager.Reload(DuplicateKeyHandling.Fail);
}

Expand Down Expand Up @@ -84,9 +85,9 @@ public void ExportExcel(string filePath, object entries, object languages, objec
Contract.Requires(filePath != null);

var resourceScope = new ResourceScope(
entries ?? _resourceManager.TableEntries,
languages ?? _resourceManager.Cultures,
comments ?? new object [0]);
entries ?? _resourceManager.TableEntries,
languages ?? _resourceManager.Cultures,
comments ?? new object[0]);

_resourceManager.ExportExcelFile(filePath, resourceScope, exportMode);
}
Expand All @@ -95,7 +96,9 @@ public void ImportExcel(string filePath)
{
Contract.Requires(filePath != null);

_resourceManager.ImportExcelFile(filePath);
var changes = _resourceManager.ImportExcelFile(filePath);

changes.Apply();
}

public string CreateSnapshot()
Expand All @@ -113,6 +116,55 @@ public void Dispose()
_compositionHost.Dispose();
}

private void ResourceManager_BeginEditing(object sender, ResourceBeginEditingEventArgs e)
{
if (!CanEdit(e.Entity, e.CultureKey))
{
e.Cancel = true;
}
}

private bool CanEdit(ResourceEntity entity, 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), DuplicateKeyHandling.Fail);

return true;
}

[ContractInvariantMethod]
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
private void ObjectInvariant()
Expand Down
14 changes: 7 additions & 7 deletions ResXManager.Scripting/Sample.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ $allCultures = $myhost.ResourceManager.Cultures
$allCultures

# To define a culture use any valid culture name; use $null or "" for the projects neutral culture
# Optionally prefix the culture name with '.' to simplify building file names using the same string, e.g. $fileName = $baseName $cultureName ".resx"
# Optionally prefix the culture name with '.' to simplify building file names using the same string, e.g. $fileName = $baseName + $cultureName + ".resx"
$neutralCulture = ""
$outputCulture = ".de"

# Get the list of all resource entries
$allEntries = $myhost.ResourceManager.TableEntries

# Print a list of all entries
"All entires:"
$allEntries.Count
"All entires: " + $allEntries.Count
$allEntries | Select-Object Key,
@{ Name="Value"; Expression={$_.Values.GetValue($outputCulture)}},
@{ Name="Text"; Expression={$_.Values.GetValue($outputCulture)}},
@{ Name="Entity"; Expression={$_.Container}} |
Format-Table -GroupBy Entity

Expand All @@ -39,11 +38,12 @@ $myhost.LoadSnapshot($snapshot)
$changes = $allEntries |
Where-Object { $_.Values.GetValue($neutralCulture) -ne $_.SnapshotValues.GetValue($neutralCulture) }

$changes.Count
"Number of changes: " + $changes.Count
$changes | Select-Object Key,
@{ Name="Value"; Expression={$_.Values.GetValue($neutralCulture)}},
@{ Name="Text"; Expression={$_.Values.GetValue($neutralCulture)}},
@{ Name="Snapshot"; Expression={$_.SnapshotValues.GetValue($neutralCulture)}},
@{ Name="Entity"; Expression={$_.Container}}
@{ Name="Entity"; Expression={$_.Container}} |
Format-Table

# if there were changes in the neutral culture compared to the last snapshot, export the changed entries in neutral and output culuture
if ($changes -ne $null)
Expand Down
2 changes: 1 addition & 1 deletion ResXManager.VSIX/MyToolWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private bool AddLanguage(ResourceEntity entity, CultureInfo culture)
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);

File.WriteAllText(languageFileName, View.Properties.Resources.EmptyResxTemplate);
File.WriteAllText(languageFileName, Model.Properties.Resources.EmptyResxTemplate);
}

AddProjectItems(entity, neutralLanguage, languageFileName);
Expand Down
14 changes: 0 additions & 14 deletions ResXManager.View/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions ResXManager.View/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="AddEntryToWinFormsResourceWarning" xml:space="preserve">
<value>You are about to add new entries to a WinForms designer resource.
Additional entries will be lost if you edit the form.
Expand All @@ -137,7 +136,8 @@ Do you want to add a new entry anyway?</value>
</data>
<data name="CaseSensitive" xml:space="preserve">
<value>Case Sens.</value>
<comment>@Invariant</comment></data>
<comment>@Invariant</comment>
</data>
<data name="CellSelection" xml:space="preserve">
<value>Cell selection</value>
</data>
Expand Down Expand Up @@ -232,9 +232,6 @@ Do you want to add a new entry anyway?</value>
<data name="EditSolutionConfigurationNotification" xml:space="preserve">
<value>You are editing the configuration specific for the active solution. If you want to edit the default configuration, just close the solution.</value>
</data>
<data name="EmptyResxTemplate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>EmptyResxTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="Enabled" xml:space="preserve">
<value>Enabled</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions ResXManager.View/ResXManager.View.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,6 @@
<ItemGroup>
<Resource Include="Assets\btn_donate_SM.gif" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\EmptyResxTemplate.txt" />
</ItemGroup>
<ItemGroup>
<Resource Include="Assets\excel.png" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ResXManager/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private bool CanEdit(ResourceEntity entity, CultureKey cultureKey)
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);

File.WriteAllText(languageFileName, View.Properties.Resources.EmptyResxTemplate);
File.WriteAllText(languageFileName, Model.Properties.Resources.EmptyResxTemplate);
}

entity.AddLanguage(new ProjectFile(languageFileName, rootFolder, entity.ProjectName, null), _configuration.DuplicateKeyHandling);
Expand Down
2 changes: 1 addition & 1 deletion ResXManager/ResXManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
<ItemGroup>
<ZipFiles Include="$(PublishDir)*.*" />
</ItemGroup>
<MakeDir Directories="$(SolutionDir)Deploy"/>
<MakeDir Directories="$(SolutionDir)Deploy" />
<Exec Command="PowerShell -command Compress-Archive -Path '$(PublishDir)Application Files',@(ZipFiles, ',') -DestinationPath '$(SolutionDir)Deploy\Publish.zip' -Force" />
</Target>
</Project>

0 comments on commit c389170

Please sign in to comment.