Skip to content

Commit ef391f2

Browse files
authored
Merge pull request #9 from Crequency/dev=main
[Pull Request] `CancellactionToken` support, new `SignalTask` system
2 parents 3366c7b + 2ac280f commit ef391f2

11 files changed

Lines changed: 238 additions & 93 deletions

File tree

.github/workflows/build.yml

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build
1+
name: Build
22

33
on:
44
push:
@@ -17,67 +17,49 @@ on:
1717
workflow_dispatch:
1818

1919
jobs:
20-
build-on-windows:
21-
runs-on: windows-latest
20+
build:
21+
strategy:
22+
matrix:
23+
os: [windows-latest, ubuntu-latest, macos-latest]
24+
25+
runs-on: ${{ matrix.os }}
2226

2327
steps:
2428
- uses: actions/checkout@v3
2529

2630
- name: Setup .NET
2731
uses: actions/setup-dotnet@v2
2832
with:
29-
dotnet-version: "6.0.x"
33+
dotnet-version: |
34+
6.0.x
35+
7.0.x
3036
include-prerelease: false
3137

3238
- name: Build
3339
run: |
3440
cd Common.BasicHelper
35-
dotnet build -c Release
36-
37-
build-on-ubuntu:
38-
runs-on: ubuntu-latest
39-
40-
steps:
41-
- uses: actions/checkout@v3
4241
43-
- name: Setup .NET
44-
uses: actions/setup-dotnet@v2
45-
with:
46-
dotnet-version: "6.0.x"
47-
include-prerelease: false
42+
dotnet build -c Release
4843
49-
- name: Build
44+
- name: Test
5045
run: |
51-
cd Common.BasicHelper
52-
dotnet build -c Release
46+
cd Common.BasicHelper.Test
47+
48+
dotnet test -c Release
5349
5450
- name: Add to GitHub Repo
51+
if: ${{ matrix.os == 'ubuntu-latest' }}
5552
run: |
5653
nuget sources add -name github -Source https://nuget.pkg.github.com/Crequency/index.json -Username Crequency -Password ${{ secrets.GitHubToken }}
5754
5855
- name: Install NuGet
56+
if: ${{ matrix.os == 'ubuntu-latest' }}
5957
uses: nuget/setup-nuget@v1
6058
with:
6159
nuget-version: "6.x"
6260

6361
- name: Publish Package to GitHub and NuGet
62+
if: ${{ matrix.os == 'ubuntu-latest' }}
6463
run: |
6564
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbol
6665
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source github -SkipDuplicate
67-
68-
build-on-macos:
69-
runs-on: macos-latest
70-
71-
steps:
72-
- uses: actions/checkout@v3
73-
74-
- name: Setup .NET
75-
uses: actions/setup-dotnet@v2
76-
with:
77-
dotnet-version: "6.0.x"
78-
include-prerelease: false
79-
80-
- name: Build
81-
run: |
82-
cd Common.BasicHelper
83-
dotnet build -c Release

.github/workflows/codecov.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

.github/workflows/pr-merged.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "Pull Request Labeler"
2+
on:
3+
pull_request:
4+
types:
5+
- closed
6+
7+
jobs:
8+
pr-merged:
9+
if: github.event.pull_request.merged == true
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
steps:
14+
- uses: actions/github-script@v6
15+
with:
16+
script: |
17+
github.rest.issues.addLabels({
18+
issue_number: context.issue.number,
19+
owner: context.repo.owner,
20+
repo: context.repo.repo,
21+
labels: ["merged"]
22+
})

Common.BasicHelper.Test/Core/Shell/CommandsExecutor_Tests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ public class CommandsExecutor_Tests
66
[TestMethod]
77
public void Test_ExecuteAsCommand()
88
{
9-
Console.WriteLine("help".ExecuteAsCommand());
9+
if (OperatingSystem.IsWindows())
10+
Console.WriteLine("help".ExecuteAsCommand());
11+
12+
if (OperatingSystem.IsLinux())
13+
Console.WriteLine("apt".ExecuteAsCommand("-v"));
14+
15+
if (OperatingSystem.IsMacOS())
16+
Console.WriteLine("sw_vers".ExecuteAsCommand());
1017
}
1118

1219
[TestMethod]
1320
public async Task Test_ExecuteAsCommandAsync()
1421
{
15-
Console.WriteLine(await "help".ExecuteAsCommandAsync());
22+
if (OperatingSystem.IsWindows())
23+
Console.WriteLine(await "help".ExecuteAsCommandAsync());
24+
25+
if (OperatingSystem.IsLinux())
26+
Console.WriteLine(await "apt".ExecuteAsCommandAsync("-v"));
27+
28+
if (OperatingSystem.IsMacOS())
29+
Console.WriteLine(await "sw_vers".ExecuteAsCommandAsync());
1630
}
1731
}

Common.BasicHelper.Test/Network/NetUtils_Tests.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@ public class NetUtils_Tests
88
[TestMethod]
99
public void Test_IsWebConected()
1010
{
11-
Assert.IsTrue(NetUtils.IsWebConected("localhost", 3));
12-
Assert.IsFalse(NetUtils.IsWebConected("192.168.255.255", 3));
11+
if (OperatingSystem.IsWindows())
12+
{
13+
Assert.IsTrue(NetUtils.IsWebConected("localhost", 3));
14+
Assert.IsFalse(NetUtils.IsWebConected("192.168.255.255", 3));
15+
}
1316
}
1417

1518
[TestMethod]
1619
public void Test_DownloadFile()
1720
{
18-
var path = Path.GetFullPath($"{Path.GetTempPath()}/test_downloadFile.txt");
21+
if (OperatingSystem.IsWindows())
22+
{
23+
var path = Path.GetFullPath($"{Path.GetTempPath()}/test_downloadFile.txt");
1924

20-
NetUtils.DownloadFile(testDownloadFilePath, path);
25+
NetUtils.DownloadFile(testDownloadFilePath, path);
2126

22-
File.Delete(path);
27+
File.Delete(path);
28+
}
2329
}
2430

2531
[TestMethod]
2632
public void Test_WebDownloadFile()
2733
{
28-
var path = Path.GetFullPath($"{Path.GetTempPath()}/test_webDownloadFile.txt");
34+
if (OperatingSystem.IsWindows())
35+
{
36+
var path = Path.GetFullPath($"{Path.GetTempPath()}/test_webDownloadFile.txt");
2937

30-
NetUtils.WebDownloadFile(testDownloadFilePath, path);
38+
NetUtils.WebDownloadFile(testDownloadFilePath, path);
3139

32-
File.Delete(path);
40+
File.Delete(path);
41+
}
3342
}
3443
}

Common.BasicHelper.Test/Utils/Extensions/Dumpper_Tests.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public void Test_Dump()
1414
.Dump()
1515
;
1616

17-
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
18-
foreach (var iface in interfaces)
19-
iface.Dump();
17+
if (OperatingSystem.IsWindows())
18+
{
19+
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
20+
foreach (var iface in interfaces)
21+
iface.Dump();
22+
}
2023
}
2124

2225
[TestMethod()]
@@ -28,9 +31,12 @@ public void Test_Dump2Lines()
2831
.Dump2Lines()
2932
;
3033

31-
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
32-
foreach (var iface in interfaces)
33-
iface.Dump2Lines();
34+
if (OperatingSystem.IsWindows())
35+
{
36+
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
37+
foreach (var iface in interfaces)
38+
iface.Dump2Lines();
39+
}
3440
}
3541

3642
[TestMethod()]

Common.BasicHelper.Test/Utils/Password_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Password_Tests
66
[TestMethod]
77
public void Test_GeneratePassword()
88
{
9-
foreach (var item in Enumerable.Range(0, 10))
9+
foreach (var _ in Enumerable.Range(0, 10))
1010
Console.WriteLine(Password.GeneratePassword(length: 12));
1111
}
1212
}

Common.BasicHelper/Core/Shell/CommandsExecutor.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static async Task<string> GetExecutionResultAsync
6767
string args,
6868
bool findInPath = false,
6969
Action<ProcessStartInfo>? action = null,
70-
CancellationToken? token = default
70+
CancellationToken? token = null
7171
)
7272
{
7373
if (findInPath)
@@ -90,19 +90,24 @@ public static async Task<string> GetExecutionResultAsync
9090
StartInfo = psi,
9191
};
9292

93-
//var sb = new StringBuilder();
94-
95-
//void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
96-
// => sb.AppendLine(outLine.Data);
97-
98-
//process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
99-
//process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
100-
10193
process.Start();
10294

10395
var output = process.StandardOutput.ReadToEnd();
10496

105-
await Task.Run(process.WaitForExit);
97+
await Task.Run(() =>
98+
{
99+
if (token is null) process.WaitForExit();
100+
101+
while (!process.HasExited)
102+
{
103+
if (token is not null && token.Value.IsCancellationRequested)
104+
{
105+
process.Kill();
106+
107+
break;
108+
}
109+
}
110+
});
106111

107112
return output;
108113
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Common.BasicHelper.Utils.Extensions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace Common.BasicHelper.Core.TaskSystem;
8+
9+
public class SignalTask
10+
{
11+
public Queue<Action> Actions { get; } = new();
12+
13+
public bool SignalReusable { get; set; } = false;
14+
15+
public bool ActionsReusable { get; set; } = false;
16+
17+
public SignalTask AddAction(Action action)
18+
{
19+
Actions.Enqueue(action);
20+
21+
return this;
22+
}
23+
24+
public SignalTask ReuseSignal(bool reusable = true)
25+
{
26+
SignalReusable = reusable;
27+
28+
return this;
29+
}
30+
31+
public SignalTask ReuseActions(bool reusable = true)
32+
{
33+
ActionsReusable = reusable;
34+
35+
return this;
36+
}
37+
38+
public SignalTask ExecuteAll(bool? reuseall = null)
39+
{
40+
Actions.ForEach(action => action(), reuseall ?? ActionsReusable);
41+
42+
return this;
43+
}
44+
45+
public async Task<SignalTask> ExecuteAllAsync(bool? reuseall = null, CancellationToken? token = null)
46+
{
47+
await Actions.ForEachAsync(
48+
action => action(),
49+
reuseall ?? ActionsReusable,
50+
token ?? default
51+
);
52+
53+
return this;
54+
}
55+
}
56+
57+
public static class SignalTaskExtensions
58+
{
59+
public static SignalTask AddTo(this Action action, SignalTask task) => task.AddAction(action);
60+
61+
public static SignalTask ExecuteAll(this SignalTask task, bool reuseAll = false)
62+
=> task.ExecuteAll(reuseAll);
63+
}

0 commit comments

Comments
 (0)