Skip to content

Commit 7c514fd

Browse files
committed
Add capability to configure multiple file extensions for code and resx
1 parent 5d747f6 commit 7c514fd

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

Program.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ internal static class Global
2727
{
2828
public static IConfigurationRoot Configuration;
2929

30-
public static string WatchedCodeExtension = "cs";
31-
public static string WatchedResXExtension = "resx";
30+
public static List<string> WatchedCodeExtension = new List<string>() { "cs" };
31+
public static List<string> WatchedResXExtension = new List<string>() { "resx" };
3232

3333
public static List<string> ExcludedExtensions = new List<string>() { "*~", "tmp" };
3434
public static List<string> IgnorePathsStartingWith = new List<string>();
@@ -114,8 +114,8 @@ private static void Main()
114114
Global.AsyncPath = fileConfig["AsyncPath"];
115115
Global.SyncPath = fileConfig["SyncPath"];
116116

117-
Global.WatchedCodeExtension = fileConfig["WatchedCodeExtension"];
118-
Global.WatchedResXExtension = fileConfig["WatchedResXExtension"];
117+
Global.WatchedCodeExtension = fileConfig.GetSection("WatchedCodeExtension").GetChildren().Select(c => c.Value.ToUpperInvariant()).ToList();
118+
Global.WatchedResXExtension = fileConfig.GetSection("WatchedResXExtension").GetChildren().Select(c => c.Value.ToUpperInvariant()).ToList();
119119

120120
//this would need Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Binder packages
121121
//Global.ExcludedExtensions = fileConfig.GetSection("ExcludedExtensions").Get<string[]>();
@@ -191,21 +191,19 @@ private static async Task MainTask()
191191

192192

193193
//1. Do initial synchronisation from sync to async folder //TODO: config for enabling and ordering of this operation
194-
foreach (var fileInfo in ProcessSubDirs(new DirectoryInfo(Global.SyncPath), "*." + Global.WatchedCodeExtension))
194+
foreach (var fileInfo in ProcessSubDirs(new DirectoryInfo(Global.SyncPath), "*.*")) //NB! use *.* in order to sync resx files also
195195
{
196-
{
197-
await ConsoleWatch.OnAddedAsync
198-
(
199-
new DummyFileSystemEvent(fileInfo),
200-
new CancellationToken()
201-
);
202-
}
196+
await ConsoleWatch.OnAddedAsync
197+
(
198+
new DummyFileSystemEvent(fileInfo),
199+
new CancellationToken()
200+
);
203201
}
204202

205203
if (Global.Bidirectional)
206204
{
207205
//2. Do initial synchronisation from async to sync folder //TODO: config for enabling and ordering of this operation
208-
foreach (var fileInfo in ProcessSubDirs(new DirectoryInfo(Global.AsyncPath), "*." + Global.WatchedCodeExtension))
206+
foreach (var fileInfo in ProcessSubDirs(new DirectoryInfo(Global.AsyncPath), "*.*")) //NB! use *.* in order to sync resx files also
209207
{
210208
await ConsoleWatch.OnAddedAsync
211209
(
@@ -535,9 +533,11 @@ public static async Task FileUpdated(string fullName, Context context)
535533
var otherFullName = GetOtherFullName(fullName);
536534
using (await Global.FileOperationLocks.LockAsync(fullName, otherFullName, context.Token))
537535
{
536+
var fullNameInvariant = fullName.ToUpperInvariant();
537+
538538
if (
539-
fullName.EndsWith("." + Global.WatchedCodeExtension)
540-
|| Global.WatchedCodeExtension == "*"
539+
Global.WatchedCodeExtension.Any(x => fullNameInvariant.EndsWith("." + x.ToUpperInvariant()))
540+
|| Global.WatchedCodeExtension.Contains("*")
541541
)
542542
{
543543
if (fullName.ToUpperInvariant().StartsWith(Global.AsyncPath.ToUpperInvariant()))
@@ -604,10 +604,10 @@ private static bool IsWatchedFile(string fullName)
604604

605605
if (
606606
(
607-
fullNameInvariant.EndsWith("." + Global.WatchedCodeExtension.ToUpperInvariant())
608-
|| fullNameInvariant.EndsWith("." + Global.WatchedResXExtension.ToUpperInvariant())
609-
|| Global.WatchedCodeExtension == "*"
610-
|| Global.WatchedResXExtension == "*"
607+
Global.WatchedCodeExtension.Any(x => fullNameInvariant.EndsWith("." + x.ToUpperInvariant()))
608+
|| Global.WatchedResXExtension.Any(x => fullNameInvariant.EndsWith("." + x.ToUpperInvariant()))
609+
|| Global.WatchedCodeExtension.Contains("*")
610+
|| Global.WatchedResXExtension.Contains("*")
611611
)
612612
&&
613613
Global.ExcludedExtensions.All(x =>
@@ -812,7 +812,7 @@ public static async Task SaveFileModifications(string fullName, string fileData,
812812
//NB! detect whether the file actually changed
813813
var otherFileData = File.Exists(otherFullName)
814814
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
815-
? await FileExtensions.ReadAllTextAsync(@"\\?\" + otherFullName, context.Token)
815+
? await FileExtensions.ReadAllTextAsync(@"\\?\" + otherFullName, context.Token) //TODO: optimisation: no need to read the bytes in case the file lenghts are different
816816
: null;
817817

818818
if (
@@ -861,7 +861,7 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
861861
//NB! detect whether the file actually changed
862862
var otherFileData = File.Exists(otherFullName)
863863
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
864-
? await FileExtensions.ReadAllBytesAsync(@"\\?\" + otherFullName, context.Token)
864+
? await FileExtensions.ReadAllBytesAsync(@"\\?\" + otherFullName, context.Token) //TODO: optimisation: no need to read the bytes in case the file lenghts are different
865865
: null;
866866

867867
if (

appsettings.example.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"AsyncPath": "C:\\yourpath\\yourproject\\",
99

1010

11-
"WatchedCodeExtension": "cs",
12-
"WatchedResXExtension": "resx",
11+
"WatchedCodeExtension": [ "cs" ],
12+
"WatchedResXExtension": [ "resx" ],
1313

1414
"ExcludedExtensions": [
1515
"*~",

0 commit comments

Comments
 (0)