Skip to content

Commit c6e3b22

Browse files
committed
Make web files export configuration available on configuration tab
1 parent bb4e1e6 commit c6e3b22

File tree

5 files changed

+187
-18
lines changed

5 files changed

+187
-18
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace ResXManager.Model
2+
{
3+
using System.ComponentModel;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.IO;
6+
using System.Runtime.CompilerServices;
7+
8+
using Newtonsoft.Json;
9+
10+
using TomsToolbox.Essentials;
11+
12+
public sealed class WebFileExporterConfiguration : INotifyPropertyChanged
13+
{
14+
private const string WebExportConfigFileName = "resx-manager.webexport.config";
15+
16+
[JsonProperty("typeScriptFileDir")]
17+
public string? TypeScriptFileDir { get; set; }
18+
19+
[JsonProperty("jsonFileDir")]
20+
public string? JsonFileDir { get; set; }
21+
22+
[JsonProperty("exportNeutralJson")]
23+
public bool ExportNeutralJson { get; set; }
24+
25+
[JsonProperty("include")]
26+
public string? Include { get; set; }
27+
28+
public static bool Load(string? solutionFolder, [NotNullWhen(true)] out WebFileExporterConfiguration? config)
29+
{
30+
config = null;
31+
if (solutionFolder.IsNullOrEmpty())
32+
return false;
33+
34+
var configFilePath = Path.Combine(solutionFolder, WebExportConfigFileName);
35+
if (!File.Exists(configFilePath))
36+
return false;
37+
38+
config = JsonConvert.DeserializeObject<WebFileExporterConfiguration>(File.ReadAllText(configFilePath));
39+
40+
return config != null;
41+
}
42+
43+
public void Save(string solutionFolder)
44+
{
45+
var configFilePath = Path.Combine(solutionFolder, WebExportConfigFileName);
46+
47+
File.WriteAllText(configFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
48+
}
49+
50+
public event PropertyChangedEventHandler? PropertyChanged;
51+
52+
#pragma warning disable IDE0051 // Remove unused private members
53+
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
54+
{
55+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
56+
}
57+
}
58+
}

src/ResXManager.Model/WebFilesExporter.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text;
99
using System.Text.RegularExpressions;
1010

11-
using Newtonsoft.Json;
1211
using Newtonsoft.Json.Linq;
1312

1413
using ResXManager.Infrastructure;
@@ -44,14 +43,10 @@ private void Export()
4443
if (solutionFolder == null)
4544
return;
4645

47-
var configFilePath = Path.Combine(solutionFolder, "resx-manager.webexport.config");
48-
if (!File.Exists(configFilePath))
46+
if (!WebFileExporterConfiguration.Load(solutionFolder, out var config))
4947
return;
5048

51-
var config = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(configFilePath));
52-
5349
var typeScriptFileDir = config.TypeScriptFileDir;
54-
5550
if (typeScriptFileDir.IsNullOrEmpty())
5651
return;
5752

@@ -179,18 +174,6 @@ public static IEnumerable<string> ExtractPlaceholders(string text)
179174
return placeholders;
180175
}
181176

182-
private class Configuration
183-
{
184-
[JsonProperty("typeScriptFileDir")]
185-
public string? TypeScriptFileDir { get; set; }
186-
[JsonProperty("jsonFileDir")]
187-
public string? JsonFileDir { get; set; }
188-
[JsonProperty("exportNeutralJson")]
189-
public bool ExportNeutralJson { get; set; }
190-
[JsonProperty("include")]
191-
public string? Include { get; set; }
192-
}
193-
194177
private static JObject GenerateJsonObjectWithComment()
195178
{
196179
var value = new JObject { { "_comment", JToken.FromObject("Auto-generated; do not modify!") } };
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<UserControl x:Class="ResXManager.View.Visuals.WebExportConfigurationView"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:ResXManager.View.Visuals"
7+
xmlns:styles="urn:TomsToolbox.Wpf.Styles"
8+
mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"
9+
d:DataContext="{d:DesignInstance local:WebExportConfigurationViewModel}">
10+
<StackPanel>
11+
<StackPanel.Resources>
12+
<Style TargetType="TextBlock">
13+
<Setter Property="Margin" Value="0,0,0,2" />
14+
</Style>
15+
</StackPanel.Resources>
16+
<StackPanel Orientation="Horizontal">
17+
<TextBlock Text="Solution folder:" />
18+
<Decorator Width="5" />
19+
<TextBlock Text="{Binding SolutionFolder}" />
20+
</StackPanel>
21+
<Decorator Height="5" />
22+
<TextBlock Text="Typescript files target folder:" />
23+
<TextBox Text="{Binding Configuration.TypeScriptFileDir, UpdateSourceTrigger=PropertyChanged}"
24+
IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=(local:WebExportConfigurationViewModel.IsConfigurationEnabled)}"
25+
Style="{DynamicResource {x:Type TextBox}}" />
26+
<Decorator Height="5" />
27+
<TextBlock Text="JSON files target folder:" />
28+
<TextBox Text="{Binding Configuration.JsonFileDir, UpdateSourceTrigger=PropertyChanged}"
29+
IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=(local:WebExportConfigurationViewModel.IsConfigurationEnabled)}"
30+
Style="{DynamicResource {x:Type TextBox}}" />
31+
<Decorator Height="5" />
32+
<TextBlock Text="Resource file filter (RegEx):" />
33+
<TextBox Text="{Binding Configuration.Include, UpdateSourceTrigger=PropertyChanged}"
34+
IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=(local:WebExportConfigurationViewModel.IsConfigurationEnabled)}"
35+
Style="{DynamicResource {x:Type TextBox}}" />
36+
<Decorator Height="10" />
37+
<CheckBox Content="Export neutral resources as JSON"
38+
IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=(local:WebExportConfigurationViewModel.IsConfigurationEnabled)}"
39+
IsChecked="{Binding Configuration.ExportNeutralJson}"
40+
Style="{DynamicResource {x:Type CheckBox}}" />
41+
<StackPanel.Style>
42+
<Style TargetType="StackPanel">
43+
<Style.Triggers>
44+
<DataTrigger Binding="{Binding SolutionFolder}" Value="">
45+
<Setter Property="local:WebExportConfigurationViewModel.IsConfigurationEnabled" Value="False"/>
46+
</DataTrigger>
47+
</Style.Triggers>
48+
</Style>
49+
</StackPanel.Style>
50+
</StackPanel>
51+
</UserControl>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace ResXManager.View.Visuals
2+
{
3+
using TomsToolbox.Wpf.Composition.AttributedModel;
4+
5+
/// <summary>
6+
/// Interaction logic for WebExportConfigurationView.xaml
7+
/// </summary>
8+
[DataTemplate(typeof(WebExportConfigurationViewModel))]
9+
public partial class WebExportConfigurationView
10+
{
11+
public WebExportConfigurationView()
12+
{
13+
InitializeComponent();
14+
}
15+
}
16+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace ResXManager.View.Visuals
2+
{
3+
using System.ComponentModel;
4+
using System.Windows;
5+
6+
using ResXManager.Infrastructure;
7+
using ResXManager.Model;
8+
9+
using Throttle;
10+
11+
using TomsToolbox.Essentials;
12+
using TomsToolbox.Wpf;
13+
using TomsToolbox.Wpf.Composition.AttributedModel;
14+
15+
[DisplayName("Web File Export")]
16+
[VisualCompositionExport(RegionId.Configuration)]
17+
internal class WebExportConfigurationViewModel
18+
{
19+
public WebExportConfigurationViewModel(ISourceFilesProvider sourceFilesProvider)
20+
{
21+
SolutionFolder = sourceFilesProvider.SolutionFolder ?? string.Empty;
22+
23+
if (!WebFileExporterConfiguration.Load(SolutionFolder, out var configuration))
24+
{
25+
configuration = new WebFileExporterConfiguration();
26+
}
27+
28+
Configuration = configuration;
29+
configuration.PropertyChanged += Configuration_PropertyChanged;
30+
}
31+
32+
public WebFileExporterConfiguration Configuration { get; }
33+
34+
public string? SolutionFolder { get; }
35+
36+
public static void SetIsConfigurationEnabled(DependencyObject element, bool value)
37+
{
38+
element.SetValue(IsConfigurationEnabledProperty, value);
39+
}
40+
public static bool GetIsConfigurationEnabled(DependencyObject element)
41+
{
42+
return (bool)element.GetValue(IsConfigurationEnabledProperty);
43+
}
44+
public static readonly DependencyProperty IsConfigurationEnabledProperty = DependencyProperty.RegisterAttached(
45+
"IsConfigurationEnabled", typeof(bool), typeof(WebExportConfigurationViewModel), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
46+
47+
private void Configuration_PropertyChanged(object sender, PropertyChangedEventArgs e)
48+
{
49+
SaveChanges();
50+
}
51+
52+
[Throttled(typeof(Throttle), 500)]
53+
private void SaveChanges()
54+
{
55+
if (!SolutionFolder.IsNullOrEmpty())
56+
{
57+
Configuration.Save(SolutionFolder);
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)