Skip to content

Commit 3771d64

Browse files
authored
Merge pull request #3413 from Jack251970/infrusture_helper
New API Function ValidateDirectory and ValidateDataDirectory & Cleanup Helper.cs in Flow.Launcher.Infrastructure project
2 parents 3b43d45 + 44ba60c commit 3771d64

File tree

9 files changed

+70
-68
lines changed

9 files changed

+70
-68
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Flow.Launcher.Plugin;
1818
using System.Text.Json.Serialization;
1919
using System.Threading;
20+
using System.Text.Json;
2021

2122
namespace Flow.Launcher.Core
2223
{
@@ -51,7 +52,7 @@ public async Task UpdateAppAsync(bool silentUpdate = true)
5152
var newReleaseVersion = Version.Parse(newUpdateInfo.FutureReleaseEntry.Version.ToString());
5253
var currentVersion = Version.Parse(Constant.Version);
5354

54-
Log.Info($"|Updater.UpdateApp|Future Release <{newUpdateInfo.FutureReleaseEntry.Formatted()}>");
55+
Log.Info($"|Updater.UpdateApp|Future Release <{Formatted(newUpdateInfo.FutureReleaseEntry)}>");
5556

5657
if (newReleaseVersion <= currentVersion)
5758
{
@@ -151,5 +152,15 @@ public string NewVersionTips(string version)
151152

152153
return tips;
153154
}
155+
156+
private static string Formatted<T>(T t)
157+
{
158+
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
159+
{
160+
WriteIndented = true
161+
});
162+
163+
return formatted;
164+
}
154165
}
155166
}
Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#nullable enable
22

33
using System;
4-
using System.IO;
5-
using System.Text.Json;
6-
using System.Text.Json.Serialization;
74

85
namespace Flow.Launcher.Infrastructure
96
{
107
public static class Helper
118
{
12-
static Helper()
13-
{
14-
jsonFormattedSerializerOptions.Converters.Add(new JsonStringEnumConverter());
15-
}
16-
179
/// <summary>
1810
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
1911
/// </summary>
@@ -36,55 +28,5 @@ public static void RequireNonNull<T>(this T obj)
3628
throw new NullReferenceException();
3729
}
3830
}
39-
40-
public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
41-
{
42-
if (!Directory.Exists(dataDirectory))
43-
{
44-
Directory.CreateDirectory(dataDirectory);
45-
}
46-
47-
foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
48-
{
49-
var data = Path.GetFileName(bundledDataPath);
50-
var dataPath = Path.Combine(dataDirectory, data.NonNull());
51-
if (!File.Exists(dataPath))
52-
{
53-
File.Copy(bundledDataPath, dataPath);
54-
}
55-
else
56-
{
57-
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
58-
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
59-
if (time1 != time2)
60-
{
61-
File.Copy(bundledDataPath, dataPath, true);
62-
}
63-
}
64-
}
65-
}
66-
67-
public static void ValidateDirectory(string path)
68-
{
69-
if (!Directory.Exists(path))
70-
{
71-
Directory.CreateDirectory(path);
72-
}
73-
}
74-
75-
private static readonly JsonSerializerOptions jsonFormattedSerializerOptions = new JsonSerializerOptions
76-
{
77-
WriteIndented = true
78-
};
79-
80-
public static string Formatted<T>(this T t)
81-
{
82-
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
83-
{
84-
WriteIndented = true
85-
});
86-
87-
return formatted;
88-
}
8931
}
9032
}

Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using Flow.Launcher.Infrastructure.Logger;
44
using Flow.Launcher.Infrastructure.UserSettings;
5+
using Flow.Launcher.Plugin.SharedCommands;
56
using MemoryPack;
67

78
namespace Flow.Launcher.Infrastructure.Storage
@@ -21,7 +22,7 @@ public class BinaryStorage<T>
2122
public BinaryStorage(string filename, string directoryPath = null)
2223
{
2324
directoryPath ??= DataLocation.CacheDirectory;
24-
Helper.ValidateDirectory(directoryPath);
25+
FilesFolders.ValidateDirectory(directoryPath);
2526

2627
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
2728
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using Flow.Launcher.Infrastructure.UserSettings;
3+
using Flow.Launcher.Plugin.SharedCommands;
34

45
namespace Flow.Launcher.Infrastructure.Storage
56
{
@@ -8,10 +9,10 @@ namespace Flow.Launcher.Infrastructure.Storage
89
public FlowLauncherJsonStorage()
910
{
1011
var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
11-
Helper.ValidateDirectory(directoryPath);
12+
FilesFolders.ValidateDirectory(directoryPath);
1213

1314
var filename = typeof(T).Name;
1415
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
1516
}
1617
}
17-
}
18+
}

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.Json;
66
using System.Threading.Tasks;
77
using Flow.Launcher.Infrastructure.Logger;
8+
using Flow.Launcher.Plugin.SharedCommands;
89

910
namespace Flow.Launcher.Infrastructure.Storage
1011
{
@@ -37,7 +38,7 @@ public JsonStorage(string filePath)
3738
FilePath = filePath;
3839
DirectoryPath = Path.GetDirectoryName(filePath) ?? throw new ArgumentException("Invalid file path");
3940

40-
Helper.ValidateDirectory(DirectoryPath);
41+
FilesFolders.ValidateDirectory(DirectoryPath);
4142
}
4243

4344
public async Task<T> LoadAsync()

Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using Flow.Launcher.Infrastructure.UserSettings;
3+
using Flow.Launcher.Plugin.SharedCommands;
34

45
namespace Flow.Launcher.Infrastructure.Storage
56
{
@@ -14,7 +15,7 @@ public PluginJsonStorage()
1415
var dataType = typeof(T);
1516
AssemblyName = dataType.Assembly.GetName().Name;
1617
DirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, AssemblyName);
17-
Helper.ValidateDirectory(DirectoryPath);
18+
FilesFolders.ValidateDirectory(DirectoryPath);
1819

1920
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
2021
}

Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,51 @@ public static string EnsureTrailingSlash(this string path)
318318
{
319319
return path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
320320
}
321+
322+
/// <summary>
323+
/// Validates a directory, creating it if it doesn't exist
324+
/// </summary>
325+
/// <param name="path"></param>
326+
public static void ValidateDirectory(string path)
327+
{
328+
if (!Directory.Exists(path))
329+
{
330+
Directory.CreateDirectory(path);
331+
}
332+
}
333+
334+
/// <summary>
335+
/// Validates a data directory, synchronizing it by ensuring all files from a bundled source directory exist in it.
336+
/// If files are missing or outdated, they are copied from the bundled directory to the data directory.
337+
/// </summary>
338+
/// <param name="bundledDataDirectory"></param>
339+
/// <param name="dataDirectory"></param>
340+
public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
341+
{
342+
if (!Directory.Exists(dataDirectory))
343+
{
344+
Directory.CreateDirectory(dataDirectory);
345+
}
346+
347+
foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
348+
{
349+
var data = Path.GetFileName(bundledDataPath);
350+
if (data == null) continue;
351+
var dataPath = Path.Combine(dataDirectory, data);
352+
if (!File.Exists(dataPath))
353+
{
354+
File.Copy(bundledDataPath, dataPath);
355+
}
356+
else
357+
{
358+
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
359+
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
360+
if (time1 != time2)
361+
{
362+
File.Copy(bundledDataPath, dataPath, true);
363+
}
364+
}
365+
}
366+
}
321367
}
322368
}

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using System.Windows.Controls;
9-
using Flow.Launcher.Infrastructure;
109
using Flow.Launcher.Infrastructure.Logger;
1110
using Flow.Launcher.Infrastructure.Storage;
1211
using Flow.Launcher.Infrastructure.UserSettings;
1312
using Flow.Launcher.Plugin.Program.Programs;
1413
using Flow.Launcher.Plugin.Program.Views;
1514
using Flow.Launcher.Plugin.Program.Views.Models;
15+
using Flow.Launcher.Plugin.SharedCommands;
1616
using Microsoft.Extensions.Caching.Memory;
1717
using Path = System.IO.Path;
1818
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
@@ -191,7 +191,7 @@ public async Task InitAsync(PluginInitContext context)
191191

192192
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () =>
193193
{
194-
Helper.ValidateDirectory(Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
194+
FilesFolders.ValidateDirectory(Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
195195

196196
static void MoveFile(string sourcePath, string destinationPath)
197197
{

Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using System.Windows.Controls;
8-
using Flow.Launcher.Infrastructure;
98
using Flow.Launcher.Plugin.SharedCommands;
109

1110
namespace Flow.Launcher.Plugin.WebSearch
@@ -180,7 +179,7 @@ void Init()
180179

181180
// Default images directory is in the WebSearch's application folder
182181
DefaultImagesDirectory = Path.Combine(pluginDirectory, Images);
183-
Helper.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory);
182+
FilesFolders.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory);
184183

185184
// Custom images directory is in the WebSearch's data location folder
186185
CustomImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginSettingsDirectoryPath, "CustomIcons");

0 commit comments

Comments
 (0)