|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using NuGet.Common; |
| 7 | +using NuGet.Protocol; |
| 8 | +using NuGet.Protocol.Core.Types; |
| 9 | +using NuGet.Versioning; |
| 10 | +using Nuke.Common; |
| 11 | +using Nuke.Common.IO; |
| 12 | +using Nuke.Common.Tools.DotNet; |
| 13 | +using Serilog; |
| 14 | +using static Nuke.Common.Tools.DotNet.DotNetTasks; |
| 15 | + |
| 16 | +[SuppressMessage("ReSharper", "AllUnderscoreLocalParameterName")] |
| 17 | +[SuppressMessage("ReSharper", "UnusedMember.Local")] |
| 18 | +[SuppressMessage("ReSharper", "CheckNamespace")] |
| 19 | +partial class Build |
| 20 | +{ |
| 21 | + [Parameter("Extension library name")] |
| 22 | + string ExtensionName = EnvironmentInfo.GetVariable<string>("EXTENSION_NAME") ?? string.Empty; |
| 23 | + |
| 24 | + [Parameter("Extension library version")] |
| 25 | + string ExtensionLibVersion = EnvironmentInfo.GetVariable<string>("EXTENSION_VERSION") ?? string.Empty; |
| 26 | + |
| 27 | + AbsolutePath ExtensionDirectory => OutputDirectory / ExtensionName; |
| 28 | + |
| 29 | + Target Extension_FindNextVersion => _ => _ |
| 30 | + .Requires(() => !string.IsNullOrWhiteSpace(ExtensionName)) |
| 31 | + .OnlyWhenDynamic(() => string.IsNullOrWhiteSpace(ExtensionLibVersion)) |
| 32 | + .Executes(async () => |
| 33 | + { |
| 34 | + var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); |
| 35 | + var resources = await repository.GetResourceAsync<FindPackageByIdResource>(); |
| 36 | + var metadata = await resources.GetAllVersionsAsync(ExtensionName, new SourceCacheContext(), NullLogger.Instance, CancellationToken.None); |
| 37 | + var currentVersion = metadata |
| 38 | + .OrderByDescending(x => x) |
| 39 | + .FirstOrDefault() ?? new NuGetVersion("0.1.0"); |
| 40 | + |
| 41 | + ExtensionLibVersion = currentVersion.Version.Major + "." + currentVersion.Version.Minor + "." + (currentVersion.Version.Build + 1); |
| 42 | + Log.Information("Extension library version: {SharedLibVersion}", ExtensionLibVersion); |
| 43 | + }); |
| 44 | + |
| 45 | + Target Extension_Build => _ => _ |
| 46 | + .Requires(() => !string.IsNullOrWhiteSpace(ExtensionName)) |
| 47 | + .DependsOn(Solution_Restore, Extension_FindNextVersion) |
| 48 | + .Executes(() => |
| 49 | + { |
| 50 | + var project = Solution |
| 51 | + .SolutionFolders |
| 52 | + .First(x => x.Name.Equals("Extensions", StringComparison.Ordinal)) |
| 53 | + .Projects |
| 54 | + .FirstOrDefault(x => x.Name.Equals(ExtensionName, StringComparison.Ordinal)); |
| 55 | + |
| 56 | + if (project == null) throw new FileNotFoundException($"Extension library project '{ExtensionName}' wasn't found."); |
| 57 | + |
| 58 | + DotNetBuild(x => x |
| 59 | + .SetProjectFile(project) |
| 60 | + .SetConfiguration(Configuration) |
| 61 | + .SetVersion(ExtensionLibVersion) |
| 62 | + .SetOutputDirectory(ExtensionDirectory)); |
| 63 | + }); |
| 64 | + |
| 65 | + Target Extension_Push => _ => _ |
| 66 | + .Requires(() => !string.IsNullOrWhiteSpace(ExtensionName)) |
| 67 | + .DependsOn(Extension_Build) |
| 68 | + .Executes(() => |
| 69 | + { |
| 70 | + var path = Directory.GetFiles(ExtensionDirectory, "*.nupkg", SearchOption.AllDirectories).FirstOrDefault(); |
| 71 | + if (string.IsNullOrWhiteSpace(path)) throw new FileNotFoundException("Extension library wasn't found."); |
| 72 | + |
| 73 | + DotNetNuGetPush(x => x |
| 74 | + .SetTargetPath(path) |
| 75 | + .SetSource("https://api.nuget.org/v3/index.json") |
| 76 | + .SetApiKey(PushApiKey)); |
| 77 | + }); |
| 78 | +} |
0 commit comments