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

Commit e2d9d6d

Browse files
committed
finish persistant using sqlite3
1 parent c4738df commit e2d9d6d

22 files changed

+1823
-112
lines changed

build/Program.cs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text.RegularExpressions;
57
using System.Threading.Tasks;
68
using Cake.Common;
79
using Cake.Common.Diagnostics;
@@ -37,6 +39,7 @@ public class BuildContext : FrostingContext
3739
public const string DeployFramework = "net7.0-windows";
3840
public string PublishDir = ".dist";
3941
public string PublishVersion = "";
42+
public string BuildFor = "win-x64"; // win-x64 win-x86
4043

4144
public BuildContext(ICakeContext context)
4245
: base(context)
@@ -89,7 +92,7 @@ public class PublishTask : FrostingTask<BuildContext>
8992
public override void Run(BuildContext context)
9093
{
9194
var project = context.DefaultSln.Value.Projects.First(p => p.Name.EndsWith("ClipboardR"));
92-
var srcDir = project.Path.GetDirectory().Combine(new DirectoryPath($"bin/publish"));
95+
var srcDir = project.Path.GetDirectory().Combine(new DirectoryPath("bin/Publish"));
9396
var dstDir =
9497
$"{srcDir.GetParent().GetParent().GetParent().GetParent().FullPath}/{context.PublishDir}";
9598
context.DotNetPublish(
@@ -103,16 +106,51 @@ public override void Run(BuildContext context)
103106
}
104107
);
105108
context.CreateDirectory(dstDir);
106-
var files = context.GetFiles(
107-
@$"{srcDir}/**/(*(c|C)lipboard*.(png|json|dll)|*.png|plugin.json|(*simulator).dll)"
108-
);
109+
110+
// var builder = context.DefaultSln.Value.Projects.First(p => p.Name.EndsWith("Build"));
111+
// var midDir = builder.Path.GetDirectory().Combine(new DirectoryPath("bin/Publish"));
112+
// if (context.DirectoryExists(midDir))
113+
// context.DeleteDirectory(midDir, new DeleteDirectorySettings { Recursive = true, Force = true });
114+
// context.CreateDirectory(midDir);
115+
116+
// context.CopyDirectory(srcDir, midDir);
117+
118+
var ptn =
119+
@"Clipboar.+\.dll|" +
120+
@".+\.png|" +
121+
@"plugin\.json|H\.InputSimulator\.dll|" +
122+
@"SQLitePCLRaw.+\.dll|Microsoft.+(S|s)qlite\.dll";
123+
var files = context.GetFiles($"{srcDir}/**/*");
109124
FilePath? versionFile = null;
110125
foreach (var f in files)
111126
{
112-
context.Information($"Adding: {f}");
127+
if (f == null || f.ToString().EndsWith("e_sqlite3.dll"))
128+
{
129+
files.Remove(f);
130+
continue;
131+
}
113132
if (f.ToString().EndsWith("plugin.json"))
114133
versionFile = f;
134+
if (!Regex.IsMatch(f.GetFilename().ToString(), ptn))
135+
{
136+
context.DeleteFile(f);
137+
files.Remove(f);
138+
}
139+
else
140+
context.Information($"Added: {f}");
115141
}
142+
143+
var eSqlite3Path = srcDir
144+
.Combine($"runtimes")
145+
.Combine($"{context.BuildFor}")
146+
.Combine("native")
147+
.CombineWithFilePath(new FilePath("e_sqlite3.dll"));
148+
context.CopyFile(
149+
eSqlite3Path,
150+
srcDir.CombineWithFilePath("e_sqlite3.dll"));
151+
files.Add(srcDir.CombineWithFilePath("e_sqlite3.dll"));
152+
context.DeleteDirectory(srcDir.Combine("runtimes"), new DeleteDirectorySettings() { Recursive = true });
153+
116154
if (versionFile != null)
117155
{
118156
VersionInfo? versionInfoObj = JsonConvert.DeserializeObject<VersionInfo>(
@@ -123,6 +161,7 @@ public override void Run(BuildContext context)
123161
else
124162
Console.WriteLine("Get version info from plugin.json failed!");
125163
}
164+
126165
context.ZipCompress(
127166
rootPath: srcDir,
128167
outputPath: $"{dstDir}/ClipboardR-v{context.PublishVersion}.zip",
@@ -151,7 +190,32 @@ public override void Run(BuildContext context)
151190
[IsDependentOn(typeof(CleanTask))]
152191
[IsDependentOn(typeof(BuildTask))]
153192
[IsDependentOn(typeof(PublishTask))]
154-
public class DefaultTask : FrostingTask { }
193+
public class DefaultTask : FrostingTask
194+
{
195+
}
196+
197+
[TaskName("Deploy")]
198+
public class DeployTask : FrostingTask<BuildContext>
199+
{
200+
public override void Run(BuildContext context)
201+
{
202+
var builder = context.DefaultSln.Value.Projects.First(p => p.Name.EndsWith("Build"));
203+
var distDir = builder.Path.GetDirectory().GetParent().Combine(new DirectoryPath(context.PublishDir));
204+
var files = context.GetFiles($"{distDir}/ClipboardR*.zip");
205+
206+
DateTime t = DateTime.FromFileTime(0);
207+
FilePath mostRecentFile = files.First();
208+
foreach (var f in files)
209+
{
210+
if (File.GetCreationTime(f.FullPath) <= t) continue;
211+
t = File.GetCreationTime(f.FullPath);
212+
mostRecentFile = f;
213+
}
214+
215+
context.Unzip(mostRecentFile,
216+
new DirectoryPath(@"%APPDATA%/FlowLauncher/Plugins" + mostRecentFile.GetFilenameWithoutExtension()));
217+
}
218+
}
155219

156220
public class VersionInfo
157221
{
@@ -165,4 +229,4 @@ public class VersionInfo
165229
public string Website { get; set; }
166230
public string IcoPath { get; set; }
167231
public string ExecuteFileName { get; set; }
168-
}
232+
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
15-
<PackageReference Include="xunit" Version="2.4.2" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
15+
<PackageReference Include="xunit" Version="2.4.2"/>
1616
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
<PrivateAssets>all</PrivateAssets>
@@ -24,7 +24,16 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27-
<ProjectReference Include="..\ClipboardR.Core\ClipboardR.Core.csproj" />
27+
<ProjectReference Include="..\ClipboardR.Core\ClipboardR.Core.csproj"/>
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Content Include="Images\clipboard.png">
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
33+
</Content>
34+
<Content Include="Images\clipboard_b64.txt">
35+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
36+
</Content>
2837
</ItemGroup>
2938

3039
</Project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Windows.Media.Imaging;
2+
using Xunit.Abstractions;
3+
using System.Drawing;
4+
using ClipboardR.Core;
5+
6+
namespace ClipboardR.Core.Test;
7+
8+
public class DbHelperTest
9+
{
10+
private static string _defaultIconPath = "Images/clipboard.png";
11+
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+
};
28+
29+
private readonly ITestOutputHelper _testOutputHelper;
30+
31+
public DbHelperTest(ITestOutputHelper testOutputHelper)
32+
{
33+
_testOutputHelper = testOutputHelper;
34+
}
35+
36+
[Fact]
37+
public void TestCreateDb()
38+
{
39+
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
40+
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");
44+
}
45+
46+
[Fact]
47+
public void TestInsertRecord()
48+
{
49+
// test text
50+
var exampleTextRecord = TestRecord;
51+
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
52+
helper.CreateDb();
53+
helper.AddOneRecord(exampleTextRecord);
54+
var c = helper.GetAllRecord().First();
55+
helper.Close();
56+
Assert.True(c.Equals(exampleTextRecord));
57+
}
58+
59+
[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)
64+
{
65+
var exampleTextRecord = TestRecord;
66+
exampleTextRecord.CreateTime = DateTime.Parse(creatTime);
67+
var helper = new DbHelper("Data Source=InMemoryTestDb;Mode=Memory;Cache=Shared");
68+
helper.CreateDb();
69+
var spans = Enumerable.Range(0, 5000).Skip(12).Select(i => TimeSpan.FromHours(i));
70+
var rng = new Random();
71+
foreach (var s in spans)
72+
{
73+
var tmpRecord = exampleTextRecord;
74+
tmpRecord.Type = (CbMonitor.ContentTypes)rng.Next(0, 4);
75+
helper.AddOneRecord(tmpRecord);
76+
}
77+
78+
helper.DeleteRecordByKeepTime(type, keepTime);
79+
80+
var recordsAfterDelete = helper.GetAllRecord();
81+
foreach (var record in recordsAfterDelete)
82+
{
83+
Assert.True(record.CreateTime + TimeSpan.FromDays(keepTime / 24) >= DateTime.Now);
84+
}
85+
helper.Close();
86+
}
87+
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Windows.Media.Imaging;
22
using Xunit.Abstractions;
3+
using System.Drawing;
34

45
namespace ClipboardR.Core.Test;
5-
using System.Drawing;
6-
using Image = System.Drawing.Image;
6+
77
using ClipboardR.Core;
8+
89
public class ExtensionTest
910
{
1011
private readonly ITestOutputHelper _testOutputHelper;
@@ -14,14 +15,35 @@ public ExtensionTest(ITestOutputHelper testOutputHelper)
1415
_testOutputHelper = testOutputHelper;
1516
}
1617

17-
[Fact]
18-
public void Test1()
18+
[Theory]
19+
[InlineData(@"Images\clipboard.png")]
20+
public void TestToBitmapImage(string filename)
1921
{
20-
var filename = @"F:\cSharp\Flow.Launcher.Plugin.Clipboard\src\ClipboardR\Images\clipboard.png";
2122
var img = new Bitmap(filename);
2223
_testOutputHelper.WriteLine(img.RawFormat.ToString());
2324
var im = img.ToBitmapImage();
2425
BitmapImage bm = new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute));
26+
}
2527

28+
[Fact]
29+
public void TestImageToBase64()
30+
{
31+
using var f = File.OpenText(@"Images\clipboard_b64.txt");
32+
var s = f.ReadToEnd();
33+
var img = new Bitmap(@"Images\clipboard.png");
34+
var s1 = img.ToBase64();
35+
Image img1 = s1.ToImage();
36+
37+
var imgBitmap = img1.ToBitmapImage();
38+
var sBitmap = imgBitmap.ToBase64();
39+
}
40+
41+
[Fact]
42+
public void TestBase64ToImage()
43+
{
44+
using var f = File.OpenText(@"Images\clipboard_b64.txt");
45+
var s = f.ReadToEnd();
46+
var img1 = s.ToImage();
47+
var imgBitmap = s.ToBitmapImage();
2648
}
2749
}
2.39 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAJQElEQVR4nO2de1BU1x3Hv8tTFlgRQYiviK+KVasggi9A5P1UnguLvFFhBXmKiqIEXMAHxqiNOhLNwzyMmMZRR1MfMdMZp9NJZ9qaZNqMzaRTp20eShOZTqdNfp2z7qUuLLgLmHvPzf3OfP/YuWfv+Z3f555z9t679x5AkSJF8pQXgAoAFwD8HsAdAFcBNACYKnZwPyapANQD+AYADeJ/A9gHwEnsYOUuewBvComfu2AJ5a6vp/rmo9TQ8iIV6hspICSc7OzsBDAfAHAVO2g5az9LtIvalTZUt9DBly5bdE3TCzTO01uAcl7soOWqhQD+6+DgSJXb9g+A0Nl1yexzY/tJclG7CVDWiB28HHWKJTc8JtUs8XkbG8hzvBe5qNUUFpVMB05e7NuWllv++NClaJQn8gcsuY3tXX0Jb9p3msZ7edOdO3eop6eHVoaGUWZ+Zd/2jmPvkKOjEwPyPQBvhcjoyAXAH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0tVL38oelINw3TL9R6aPG36gEUGpA4kzdK1LOajDXE0adoM2nL2E9GTa7DRz127T0vitBS7cuAbu9k5lwnIRkhQqy29yUHwwZoY0mjcaWlirvHxsKpXfjsqV3F3PgWzA6f44GWKzG8gb19fyopdZHGlIPbEsQlIDCQotlAWzZ7qaRGI8FLlfZWRFBc6n/ym+hqHMnd3N0lZo3GjKRMn0LLAn1Bd3nL68JXiQdvj7+dFKuA7KS/r+hGDcuWQdtBGyMW/PPJo/SoANyBh6VmQC2f70IPrtaInrfcpmbUtYI6vACQJEpYzgE9ZoOvi59u8DlUvB2ZtYm0zwbgGDrRAWMMwccUs+vzCJtGT2DtK/vMv9BS/fKZphTYVW2/LBxytZfgVC9xd7WR8QxBbXefT7nLR15i6Z6P/1F1mXBWBvZjN1eXRaqQq4I8AZoMzzQBwwdS1ZWHVowUzjwLwAMcKAtAB4NcA7pnWM+fJnwF4H8BWANPFTqYiRYoUKVKkSJEiRYoUKYK89T/r3qyCyntVwgAAAABJRU5ErkJggg==

src/ClipboardR.Core/CbHandle.Designer.cs

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)