Skip to content

Commit 44ba60c

Browse files
committed
Move ValidateDirectory functions to FileFolders for plugin usage
1 parent b62c19e commit 44ba60c

File tree

8 files changed

+58
-45
lines changed

8 files changed

+58
-45
lines changed
Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#nullable enable
22

33
using System;
4-
using System.IO;
54

65
namespace Flow.Launcher.Infrastructure
76
{
@@ -29,40 +28,5 @@ public static void RequireNonNull<T>(this T obj)
2928
throw new NullReferenceException();
3029
}
3130
}
32-
33-
public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
34-
{
35-
if (!Directory.Exists(dataDirectory))
36-
{
37-
Directory.CreateDirectory(dataDirectory);
38-
}
39-
40-
foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
41-
{
42-
var data = Path.GetFileName(bundledDataPath);
43-
var dataPath = Path.Combine(dataDirectory, data.NonNull());
44-
if (!File.Exists(dataPath))
45-
{
46-
File.Copy(bundledDataPath, dataPath);
47-
}
48-
else
49-
{
50-
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
51-
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
52-
if (time1 != time2)
53-
{
54-
File.Copy(bundledDataPath, dataPath, true);
55-
}
56-
}
57-
}
58-
}
59-
60-
public static void ValidateDirectory(string path)
61-
{
62-
if (!Directory.Exists(path))
63-
{
64-
Directory.CreateDirectory(path);
65-
}
66-
}
6731
}
6832
}

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)