Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit af9ab2a

Browse files
committed
improve, restore settings, use dapper
1 parent 8b19105 commit af9ab2a

File tree

20 files changed

+746
-631
lines changed

20 files changed

+746
-631
lines changed

Flow.Launcher.Plugin.ClipboardR.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Build", "build\Build.csproj
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardR.Panels.Test", "src\ClipboardR.Panels.Test\ClipboardR.Panels.Test.csproj", "{B5DC21C9-618A-48D1-969F-E8D5BF7FBFD6}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{3C9C90EC-A670-441E-81C6-B857DEF8374E}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{B5DC21C9-618A-48D1-969F-E8D5BF7FBFD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{B5DC21C9-618A-48D1-969F-E8D5BF7FBFD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{B5DC21C9-618A-48D1-969F-E8D5BF7FBFD6}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{3C9C90EC-A670-441E-81C6-B857DEF8374E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{3C9C90EC-A670-441E-81C6-B857DEF8374E}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{3C9C90EC-A670-441E-81C6-B857DEF8374E}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{3C9C90EC-A670-441E-81C6-B857DEF8374E}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

build/Program.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
5-
using System.Runtime.InteropServices;
64
using System.Text.RegularExpressions;
7-
using System.Threading.Tasks;
85
using Cake.Common;
96
using Cake.Common.Diagnostics;
107
using Cake.Common.IO;
@@ -118,21 +115,24 @@ public override void Run(BuildContext context)
118115
var ptn =
119116
@"Clipboar.+\.dll|" +
120117
@".+\.png|" +
118+
@"Dapper\.dll|" +
121119
@"plugin\.json|H\.InputSimulator\.dll|" +
122120
@"SQLitePCLRaw.+\.dll|Microsoft.+(S|s)qlite\.dll";
123121
var files = context.GetFiles($"{srcDir}/**/*");
124122
FilePath? versionFile = null;
125123
foreach (var f in files)
126124
{
127-
var fstr = f.ToString();
128-
if (f == null || (fstr.EndsWith("e_sqlite3.dll") && ! fstr.EndsWith(".e_sqlite3.dll")))
125+
var fStr = f.ToString();
126+
var fName = f.GetFilename().ToString();
127+
if (fStr == null || fName == null) continue;
128+
if (fStr.EndsWith("e_sqlite3.dll") && ! fStr.EndsWith(".e_sqlite3.dll"))
129129
{
130130
files.Remove(f);
131131
continue;
132132
}
133-
if (fstr.EndsWith("plugin.json"))
133+
if (fStr.EndsWith("plugin.json"))
134134
versionFile = f;
135-
if (!Regex.IsMatch(f.GetFilename().ToString(), ptn))
135+
if (!Regex.IsMatch(fName, ptn))
136136
{
137137
context.DeleteFile(f);
138138
files.Remove(f);
@@ -158,7 +158,7 @@ public override void Run(BuildContext context)
158158
File.ReadAllText(versionFile.ToString()!)
159159
);
160160
if (versionInfoObj != null)
161-
context.PublishVersion = versionInfoObj.Version;
161+
context.PublishVersion = versionInfoObj.Version ?? "0.0.0";
162162
else
163163
Console.WriteLine("Get version info from plugin.json failed!");
164164
}
@@ -220,14 +220,14 @@ public override void Run(BuildContext context)
220220

221221
public class VersionInfo
222222
{
223-
public string ID { get; set; }
224-
public string ActionKeyword { get; set; }
225-
public string Name { get; set; }
226-
public string Description { get; set; }
227-
public string Author { get; set; }
228-
public string Version { get; set; }
229-
public string Language { get; set; }
230-
public string Website { get; set; }
231-
public string IcoPath { get; set; }
232-
public string ExecuteFileName { get; set; }
223+
public string? ID { get; set; }
224+
public string? ActionKeyword { get; set; }
225+
public string? Name { get; set; }
226+
public string? Description { get; set; }
227+
public string? Author { get; set; }
228+
public string? Version { get; set; }
229+
public string? Language { get; set; }
230+
public string? Website { get; set; }
231+
public string? IcoPath { get; set; }
232+
public string? ExecuteFileName { get; set; }
233233
}

src/ClipboardR.Core.Test/ClipboardR.Core.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Dapper" Version="2.0.151" />
1415
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
1516
<PackageReference Include="xunit" Version="2.4.2"/>
1617
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

src/ClipboardR.Core.Test/DbHelperTest.cs

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,37 @@
22
using Xunit.Abstractions;
33
using System.Drawing;
44
using ClipboardR.Core;
5+
using Dapper;
6+
using Microsoft.Data.Sqlite;
7+
using System;
8+
using System.Globalization;
59

610
namespace ClipboardR.Core.Test;
711

812
public class DbHelperTest
913
{
1014
private static string _defaultIconPath = "Images/clipboard.png";
1115

12-
private readonly ClipboardData TestRecord = new()
13-
{
14-
Text = "",
15-
Type = CbMonitor.ContentTypes.Image,
16-
Data = (object)"Test Text",
17-
SenderApp = "Source.exe",
18-
DisplayTitle = "Text Test",
19-
IconPath = _defaultIconPath,
20-
Icon = new BitmapImage(new Uri(_defaultIconPath, UriKind.RelativeOrAbsolute)),
21-
PreviewImagePath = _defaultIconPath,
22-
Score = 241,
23-
InitScore = 1,
24-
Time = DateTime.Now,
25-
Pined = false,
26-
CreateTime = DateTime.Now,
27-
};
16+
private readonly Image _defaultImage = new Bitmap(_defaultIconPath);
17+
18+
private readonly ClipboardData TestRecord =
19+
new()
20+
{
21+
HashId = Utils.GetGuid(),
22+
Text = "Text",
23+
Type = CbContentType.Text,
24+
Data = "Test Data",
25+
SenderApp = "Source.exe",
26+
DisplayTitle = "Test Display Title",
27+
IconPath = _defaultIconPath,
28+
Icon = new BitmapImage(new Uri(_defaultIconPath, UriKind.RelativeOrAbsolute)),
29+
PreviewImagePath = _defaultIconPath,
30+
Score = 241,
31+
InitScore = 1,
32+
Time = DateTime.Now,
33+
Pined = false,
34+
CreateTime = DateTime.Now,
35+
};
2836

2937
private readonly ITestOutputHelper _testOutputHelper;
3038

@@ -33,54 +41,90 @@ public DbHelperTest(ITestOutputHelper testOutputHelper)
3341
_testOutputHelper = testOutputHelper;
3442
}
3543

44+
public ClipboardData GetRandomClipboardData()
45+
{
46+
var rand = new Random();
47+
var data =
48+
new ClipboardData()
49+
{
50+
HashId = Utils.GetGuid(),
51+
Text = Utils.RandomString(10),
52+
Type = (CbContentType)rand.Next(3),
53+
Data = Utils.RandomString(10),
54+
SenderApp = Utils.RandomString(5)+".exe",
55+
DisplayTitle = Utils.RandomString(10),
56+
IconPath = _defaultIconPath,
57+
Icon = new BitmapImage(new Uri(_defaultIconPath, UriKind.RelativeOrAbsolute)),
58+
PreviewImagePath = _defaultIconPath,
59+
Score = rand.Next(1000),
60+
InitScore = rand.Next(1000),
61+
Time = DateTime.Now,
62+
Pined = false,
63+
CreateTime = DateTime.Now,
64+
};
65+
if (data.Type == CbContentType.Image)
66+
data.Data = _defaultImage;
67+
else if (data.Type == CbContentType.Files)
68+
data.Data = new string[] { Utils.RandomString(10), Utils.RandomString(10)};
69+
return data;
70+
}
71+
3672
[Fact]
3773
public void TestCreateDb()
3874
{
39-
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
75+
var helper = new DbHelper("TestDb", mode: SqliteOpenMode.Memory, cache: SqliteCacheMode.Shared);
76+
_testOutputHelper.WriteLine(helper.Connection.ConnectionString);
4077
helper.CreateDb();
41-
var command = helper.Connection.CreateCommand();
42-
command.CommandText = "SELECT name from sqlite_master WHERE name='record'";
43-
Assert.True(command.ExecuteScalar() is string name && name.ToString() == "record");
78+
var sql = @"SELECT name from sqlite_master WHERE name IN ('record', 'assets') ORDER BY name ASC;";
79+
var r = helper.Connection.Query(sql).AsList();
80+
Assert.True(r.Count == 2 && r[0].name == "assets" && r[1].name == "record");
81+
helper.Close();
4482
}
4583

4684
[Fact]
47-
public void TestInsertRecord()
85+
public async void TestInsertRecord()
4886
{
4987
// test text
50-
var exampleTextRecord = TestRecord;
51-
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
88+
var exampleTextRecord = GetRandomClipboardData();
89+
var helper = new DbHelper("TestDb", mode: SqliteOpenMode.Memory, cache: SqliteCacheMode.Shared);
5290
helper.CreateDb();
5391
helper.AddOneRecord(exampleTextRecord);
54-
var c = helper.GetAllRecord().First();
92+
var c = (await helper.GetAllRecord()).First();
5593
helper.Close();
56-
Assert.True(c.Equals(exampleTextRecord));
94+
Assert.True(c == exampleTextRecord);
5795
}
5896

5997
[Theory]
60-
[InlineData(0, "2023-06-01", 3)]
61-
[InlineData(1, "2023-06-01", 24)]
62-
[InlineData(2, "2023-06-01", 72)]
63-
public void TestDeleteRecordBefore(int type, string creatTime, uint keepTime)
98+
[InlineData(0, "2023-05-28 11:35:00.1+08:00", 3)]
99+
[InlineData(0, "2023-05-28 11:35:00.1+08:00", 3600)]
100+
[InlineData(1, "2023-05-28 11:35:00.1+08:00", 24)]
101+
[InlineData(1, "2023-05-28 11:35:00.1+08:00", 3600)]
102+
[InlineData(2, "2023-05-28 11:35:00.1+08:00", 72)]
103+
public async void TestDeleteRecordBefore(int type, string creatTime, int keepTime)
64104
{
65-
var exampleTextRecord = TestRecord;
66-
exampleTextRecord.CreateTime = DateTime.Parse(creatTime);
67-
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
105+
var helper = new DbHelper("TestDb", mode: SqliteOpenMode.Memory, cache: SqliteCacheMode.Shared);
68106
helper.CreateDb();
107+
var now = DateTime.Now;
108+
var ctime = DateTime.ParseExact(
109+
creatTime, "yyyy-MM-dd HH:mm:ss.fzzz", CultureInfo.CurrentCulture);
69110
var spans = Enumerable.Range(0, 5000).Skip(12).Select(i => TimeSpan.FromHours(i));
70-
var rng = new Random();
71111
foreach (var s in spans)
72112
{
73-
var tmpRecord = exampleTextRecord;
74-
tmpRecord.Type = (CbMonitor.ContentTypes)rng.Next(0, 4);
113+
var tmpRecord = GetRandomClipboardData();
114+
tmpRecord.CreateTime = ctime + s;
75115
helper.AddOneRecord(tmpRecord);
76116
}
117+
// helper.Connection.BackupDatabase(new SqliteConnection("Data Source=a.db"));
77118

78119
helper.DeleteRecordByKeepTime(type, keepTime);
79-
80-
var recordsAfterDelete = helper.GetAllRecord();
81-
foreach (var record in recordsAfterDelete)
120+
121+
var recordsAfterDelete = await helper.GetAllRecord();
122+
foreach (var record in recordsAfterDelete.Where(r=>r.Type==(CbContentType)type))
82123
{
83-
Assert.True(record.CreateTime + TimeSpan.FromDays(keepTime / 24) >= DateTime.Now);
124+
var expTime = record.CreateTime + TimeSpan.FromHours(keepTime);
125+
if (expTime < now)
126+
_testOutputHelper.WriteLine($"{record}");
127+
Assert.True(expTime >= now);
84128
}
85129
helper.Close();
86130
}

0 commit comments

Comments
 (0)