Skip to content

Commit d2980bf

Browse files
committed
build: simplify build scripts
1 parent 115ec84 commit d2980bf

File tree

3 files changed

+8
-109
lines changed

3 files changed

+8
-109
lines changed

build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[string]$SCRIPT = '.\build\build.cs'
22

33
# Install dotnet tool
4-
dotnet tool update --global dotnet-execute
4+
dotnet tool install --global dotnet-execute --prerelease
55

66
Write-Host "dotnet-exec $SCRIPT --args $ARGS" -ForegroundColor GREEN
77

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
SCRIPT='./build/build.cs'
33

44
# Install tool
5-
dotnet tool update --global dotnet-execute
5+
dotnet tool install --global dotnet-execute --prerelease
66
export PATH="$PATH:$HOME/.dotnet/tools"
77

88
echo "dotnet-exec $SCRIPT --args=$@"

build/build.cs

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,14 @@
1-
var target = CommandLineParser.Val("target", args, "Default");
2-
var apiKey = CommandLineParser.Val("apiKey", args);
3-
var stable = CommandLineParser.BooleanVal("stable", args);
4-
var noPush = CommandLineParser.BooleanVal("noPush", args);
5-
var branchName = EnvHelper.Val("BUILD_SOURCEBRANCHNAME", "local");
6-
71
var solutionPath = "./WeihanLi.EntityFramework.slnx";
82
string[] srcProjects = [
93
"./src/WeihanLi.EntityFramework/WeihanLi.EntityFramework.csproj"
104
];
115
string[] testProjects = [ "./test/WeihanLi.EntityFramework.Test/WeihanLi.EntityFramework.Test.csproj" ];
126

13-
await new BuildProcessBuilder()
14-
.WithSetup(() =>
15-
{
16-
// cleanup artifacts
17-
if (Directory.Exists("./artifacts/packages"))
18-
Directory.Delete("./artifacts/packages", true);
19-
20-
// args
21-
Console.WriteLine("Arguments");
22-
Console.WriteLine($" {args.StringJoin(" ")}");
23-
})
24-
.WithTaskExecuting(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executing ======"))
25-
.WithTaskExecuted(task => Console.WriteLine($@"===== Task {task.Name} {task.Description} executed ======"))
26-
.WithTask("hello", b => b.WithExecution(() => Console.WriteLine("Hello dotnet-exec build")))
27-
.WithTask("build", b =>
28-
{
29-
b.WithDescription("dotnet build")
30-
.WithExecution(() => ExecuteCommandAsync($"dotnet build {solutionPath}"))
31-
;
32-
})
33-
.WithTask("test", b =>
7+
await DotNetPackageBuildProcess
8+
.Create(options =>
349
{
35-
b.WithDescription("dotnet test")
36-
.WithDependency("build")
37-
.WithExecution(async () =>
38-
{
39-
foreach (var project in testProjects)
40-
{
41-
await ExecuteCommandAsync($"dotnet test --collect:\"XPlat Code Coverage;Format=cobertura,opencover;ExcludeByAttribute=ExcludeFromCodeCoverage,Obsolete,GeneratedCode,CompilerGeneratedAttribute\" {project}");
42-
}
43-
})
44-
;
10+
options.SolutionPath = solutionPath;
11+
options.SrcProjects = srcProjects;
12+
options.TestProjects = testProjects;
4513
})
46-
.WithTask("pack", b => b.WithDescription("dotnet pack")
47-
.WithDependency("build")
48-
.WithExecution(async () =>
49-
{
50-
if (stable || branchName == "master")
51-
{
52-
foreach (var project in srcProjects)
53-
{
54-
await ExecuteCommandAsync($"dotnet pack {project} -o ./artifacts/packages");
55-
}
56-
}
57-
else
58-
{
59-
var suffix = $"preview-{DateTime.UtcNow:yyyyMMdd-HHmmss}";
60-
foreach (var project in srcProjects)
61-
{
62-
await ExecuteCommandAsync($"dotnet pack {project} -o ./artifacts/packages --version-suffix {suffix}");
63-
}
64-
}
65-
66-
if (noPush)
67-
{
68-
Console.WriteLine("Skip push there's noPush specified");
69-
return;
70-
}
71-
72-
if (string.IsNullOrEmpty(apiKey))
73-
{
74-
// try to get apiKey from environment variable
75-
apiKey = Environment.GetEnvironmentVariable("NuGet__ApiKey");
76-
77-
if (string.IsNullOrEmpty(apiKey))
78-
{
79-
Console.WriteLine("Skip push since there's no apiKey found");
80-
return;
81-
}
82-
}
83-
84-
if (branchName != "master" && branchName != "preview")
85-
{
86-
Console.WriteLine($"Skip push since branch name {branchName} not support push packages");
87-
return;
88-
}
89-
90-
// push nuget packages
91-
foreach (var file in Directory.GetFiles("./artifacts/packages/", "*.nupkg"))
92-
{
93-
await ExecuteCommandAsync($"dotnet nuget push {file} -k {apiKey} --skip-duplicate", [new("$NuGet__ApiKey", apiKey)]);
94-
}
95-
}))
96-
.WithTask("Default", b => b.WithDependency("hello").WithDependency("pack"))
97-
.Build()
98-
.ExecuteAsync(target, ApplicationHelper.ExitToken);
99-
100-
async Task ExecuteCommandAsync(string commandText, KeyValuePair<string, string>[]? replacements = null)
101-
{
102-
var commandTextWithReplacements = commandText;
103-
if (replacements is { Length: > 0})
104-
{
105-
foreach (var item in replacements)
106-
{
107-
commandTextWithReplacements = commandTextWithReplacements.Replace(item.Value, item.Key);
108-
}
109-
}
110-
Console.WriteLine($"Executing command: \n {commandTextWithReplacements}");
111-
Console.WriteLine();
112-
var result = await CommandExecutor.ExecuteCommandAndOutputAsync(commandText);
113-
result.EnsureSuccessExitCode();
114-
Console.WriteLine();
115-
}
14+
.ExecuteAsync(args);

0 commit comments

Comments
 (0)