|
| 1 | +var target = CommandLineParser.Val(args, "target", "Default"); |
| 2 | +var apiKey = CommandLineParser.Val(args, "apiKey"); |
| 3 | +var noPush = CommandLineParser.BooleanVal(args, "noPush"); |
| 4 | +var version = Environment.GetEnvironmentVariable("VERSION"); |
| 5 | +var stable = CommandLineParser.BooleanVal(args, "stable") || !string.IsNullOrEmpty(version); |
| 6 | +var runningOnGithubActions = Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true"; |
| 7 | + |
| 8 | +Console.WriteLine($$""" |
| 9 | +Arguments: |
| 10 | +
|
| 11 | +target: {{target}} |
| 12 | +stable: {{stable}} |
| 13 | +noPush: {{noPush}} |
| 14 | +args: |
| 15 | +{{args.StringJoin("\n")}} |
| 16 | +
|
| 17 | +"""); |
| 18 | + |
| 19 | +var solutionPath = "./GaussDB.slnx"; |
| 20 | +string[] srcProjects = [ |
| 21 | + "./src/GaussDB/GaussDB.csproj" |
| 22 | +]; |
| 23 | +string[] testProjects = [ |
| 24 | + "./test/GaussDB.Tests/GaussDB.Tests.csproj", |
| 25 | + "./test/GaussDB.GaussDB.DependencyInjection.Tests/GaussDB.GaussDB.DependencyInjection.Tests.csproj" |
| 26 | +]; |
| 27 | + |
| 28 | +await new BuildProcessBuilder() |
| 29 | + .WithSetup(() => |
| 30 | + { |
| 31 | + // cleanup previous artifacts |
| 32 | + if (Directory.Exists("./artifacts/packages")) |
| 33 | + Directory.Delete("./artifacts/packages", true); |
| 34 | + }) |
| 35 | + .WithTaskExecuting(task => Console.WriteLine($@"===== Task [{task.Name}] {task.Description} executing ======")) |
| 36 | + .WithTaskExecuted(task => Console.WriteLine($@"===== Task [{task.Name}] {task.Description} executed ======")) |
| 37 | + .WithTask("build", b => |
| 38 | + { |
| 39 | + b.WithDescription("build") |
| 40 | + .WithExecution(cancellationToken => ExecuteCommandAsync($"dotnet build {solutionPath}", cancellationToken)) |
| 41 | + ; |
| 42 | + }) |
| 43 | + .WithTask("test", b => |
| 44 | + { |
| 45 | + b.WithDescription("dotnet test") |
| 46 | + .WithDependency("build") |
| 47 | + .WithExecution(async cancellationToken => |
| 48 | + { |
| 49 | + foreach (var project in testProjects) |
| 50 | + { |
| 51 | + var loggerOptions = runningOnGithubActions |
| 52 | + ? "--logger GitHubActions" |
| 53 | + : "--logger \"console;verbosity=d\""; |
| 54 | + var command = $"dotnet test --blame --collect:\"XPlat Code Coverage;Format=cobertura,opencover;ExcludeByAttribute=ExcludeFromCodeCoverage,Obsolete,GeneratedCode,CompilerGenerated\" {loggerOptions} -v=d {project}"; |
| 55 | + await ExecuteCommandAsync(command, cancellationToken); |
| 56 | + } |
| 57 | + }) |
| 58 | + ; |
| 59 | + }) |
| 60 | + .WithTask("pack", b => b |
| 61 | + .WithDescription("dotnet pack") |
| 62 | + .WithDependency("build") |
| 63 | + .WithExecution(async cancellationToken => |
| 64 | + { |
| 65 | + var packOptions = " -o ./artifacts/packages"; |
| 66 | + if (stable) |
| 67 | + { |
| 68 | + if (!string.IsNullOrEmpty(version)) |
| 69 | + { |
| 70 | + packOptions += $" -p VersionPrefix={version}"; |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + var suffix = $"preview-{DateTime.UtcNow:yyyyMMdd-HHmmss}"; |
| 76 | + packOptions += $" --version-suffix {suffix}"; |
| 77 | + } |
| 78 | + |
| 79 | + foreach (var project in srcProjects) |
| 80 | + { |
| 81 | + await ExecuteCommandAsync($"dotnet pack {project} {packOptions}", cancellationToken); |
| 82 | + } |
| 83 | + |
| 84 | + if (noPush) |
| 85 | + { |
| 86 | + Console.WriteLine("Skip push there's noPush specified"); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + if (string.IsNullOrEmpty(apiKey)) |
| 91 | + { |
| 92 | + // try to get apiKey from environment variable |
| 93 | + apiKey = Environment.GetEnvironmentVariable("NUGET_API_KEY"); |
| 94 | + |
| 95 | + if (string.IsNullOrEmpty(apiKey)) |
| 96 | + { |
| 97 | + Console.WriteLine("Skip push since there's no apiKey found"); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // push nuget packages |
| 103 | + foreach (var file in Directory.GetFiles("./artifacts/packages/", "*.nupkg")) |
| 104 | + { |
| 105 | + await RetryHelper.TryInvokeAsync(() => ExecuteCommandAsync($"dotnet nuget push {file} -s https://api.nuget.org/v3/index.json -k {apiKey} --skip-duplicate", cancellationToken), cancellationToken: cancellationToken); |
| 106 | + } |
| 107 | + })) |
| 108 | + .WithTask("Default", b => b.WithDependency("pack")) |
| 109 | + .Build() |
| 110 | + .ExecuteAsync(target, ApplicationHelper.ExitToken); |
| 111 | + |
| 112 | +async Task ExecuteCommandAsync(string commandText, CancellationToken cancellationToken = default) |
| 113 | +{ |
| 114 | + Console.WriteLine($"Executing command: \n {commandText}"); |
| 115 | + Console.WriteLine(); |
| 116 | + |
| 117 | + var result = await CommandExecutor.ExecuteCommandAndOutputAsync(commandText, cancellationToken: cancellationToken); |
| 118 | + result.EnsureSuccessExitCode(); |
| 119 | + Console.WriteLine(); |
| 120 | +} |
0 commit comments