|
1 | 1 | using Photon.Framework.Agent; |
2 | 2 | using Photon.Framework.Tasks; |
3 | 3 | using Photon.Framework.Tools; |
4 | | -using Photon.MSBuild; |
| 4 | +using Photon.MSBuildPlugin; |
| 5 | +using Photon.NuGet.CorePlugin; |
5 | 6 | using Photon.NuGetPlugin; |
| 7 | +using System; |
6 | 8 | using System.IO; |
| 9 | +using System.Linq; |
7 | 10 | using System.Threading; |
8 | 11 | using System.Threading.Tasks; |
9 | 12 |
|
10 | 13 | namespace Jenkins.NET.Publishing |
11 | 14 | { |
12 | 15 | public class Publish_Windows : IBuildTask |
13 | 16 | { |
| 17 | + private MSBuildCommand msbuild; |
| 18 | + private NuGetCore nugetCore; |
| 19 | + private NuGetCommand nugetCmd; |
| 20 | + |
14 | 21 | public IAgentBuildContext Context {get; set;} |
15 | 22 |
|
16 | 23 |
|
17 | 24 | public async Task RunAsync(CancellationToken token) |
18 | 25 | { |
| 26 | + msbuild = new MSBuildCommand(Context) { |
| 27 | + Exe = Context.AgentVariables["global"]["msbuild_exe"], |
| 28 | + WorkingDirectory = Context.ContentDirectory, |
| 29 | + }; |
| 30 | + |
| 31 | + nugetCore = new NuGetCore(Context) { |
| 32 | + ApiKey = Context.ServerVariables["global"]["nuget/apiKey"], |
| 33 | + }; |
| 34 | + nugetCore.Initialize(); |
| 35 | + |
| 36 | + nugetCmd = new NuGetCommand(Context) { |
| 37 | + Exe = Path.Combine(Context.ContentDirectory, "bin", "NuGet.exe"), //Context.AgentVariables["global"]["nuget_exe"]; |
| 38 | + WorkingDirectory = Context.ContentDirectory, |
| 39 | + }; |
| 40 | + |
19 | 41 | await BuildSolution(token); |
20 | 42 |
|
21 | 43 | await PublishPackage(token); |
22 | 44 | } |
23 | 45 |
|
24 | 46 | private async Task BuildSolution(CancellationToken token) |
25 | 47 | { |
26 | | - var msbuild = new MSBuildCommand(Context) { |
27 | | - Exe = Context.AgentVariables["global"]["msbuild_exe"], |
28 | | - WorkingDirectory = Context.ContentDirectory, |
29 | | - }; |
30 | | - |
31 | | - var buildArgs = new MSBuildArguments { |
| 48 | + await msbuild.RunAsync(new MSBuildArguments { |
32 | 49 | ProjectFile = "Jenkins.NET.sln", |
33 | 50 | Properties = { |
34 | 51 | ["Configuration"] = "Release", |
35 | 52 | ["Platform"] = "Any CPU", |
36 | 53 | }, |
37 | | - Verbosity = MSBuildVerbosityLevel.Minimal, |
| 54 | + Verbosity = MSBuildVerbosityLevels.Minimal, |
38 | 55 | MaxCpuCount = 0, |
39 | | - }; |
40 | | - |
41 | | - await msbuild.RunAsync(buildArgs, token); |
| 56 | + }, token); |
42 | 57 | } |
43 | 58 |
|
44 | 59 | private async Task PublishPackage(CancellationToken token) |
45 | 60 | { |
| 61 | + const string packageId = "jenkinsnet"; |
46 | 62 | var assemblyFilename = Path.Combine(Context.ContentDirectory, "Jenkins.NET", "bin", "Release", "Jenkins.NET.dll"); |
47 | | - var version = AssemblyTools.GetVersion(assemblyFilename); |
| 63 | + var packageDefinitionFilename = Path.Combine(Context.ContentDirectory, "Jenkins.NET", "Jenkins.NET.csproj"); |
| 64 | + var nugetPackageDir = Path.Combine(Context.WorkDirectory, "Packages"); |
| 65 | + var assemblyVersion = AssemblyTools.GetVersion(assemblyFilename); |
48 | 66 |
|
49 | | - var apiKey = Context.ServerVariables["global"]["nuget.apiKey"]; |
50 | 67 |
|
51 | | - var nugetTool = new NuGetPackagePublisher(Context) { |
52 | | - Mode = NugetModes.Hybrid, |
53 | | - Client = new NuGetCore { |
54 | | - EnableV3 = true, |
55 | | - Output = Context.Output, |
56 | | - ApiKey = apiKey, |
57 | | - }, |
58 | | - CL = new NuGetCommandLine { |
59 | | - ExeFilename = Path.Combine(Context.ContentDirectory, "bin", "NuGet.exe"), |
60 | | - Output = Context.Output, |
61 | | - }, |
62 | | - PackageId = "jenkinsnet", |
63 | | - PackageDefinition = Path.Combine(Context.ContentDirectory, "Jenkins.NET", "Jenkins.NET.csproj"), |
64 | | - PackageDirectory = Path.Combine(Context.WorkDirectory, "Packages"), |
65 | | - //Configuration = "Release", |
66 | | - //Platform = "AnyCPU", |
67 | | - Version = version, |
68 | | - PackProperties = { |
69 | | - ["configuration"] = "Release", |
70 | | - ["platform"] = "AnyCPU", |
| 68 | + var versionList = await nugetCore.GetAllPackageVersions(packageId, token); |
| 69 | + var packageVersion = versionList.Any() ? versionList.Max() : null; |
| 70 | + |
| 71 | + if (!VersionTools.HasUpdates(packageVersion, assemblyVersion)) { |
| 72 | + Context.Output.WriteLine($"Package '{packageId}' is up-to-date. Version {packageVersion}", ConsoleColor.DarkCyan); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + await nugetCmd.RunAsync(new NuGetPackArguments { |
| 77 | + Filename = packageDefinitionFilename, |
| 78 | + Version = assemblyVersion, |
| 79 | + OutputDirectory = nugetPackageDir, |
| 80 | + Properties = { |
| 81 | + ["Configuration"] = "Release", |
| 82 | + ["Platform"] = "AnyCPU", |
| 83 | + ["Version"] = assemblyVersion, |
71 | 84 | }, |
72 | | - }; |
| 85 | + }, token); |
| 86 | + |
| 87 | + var packageFilename = Directory |
| 88 | + .GetFiles(nugetPackageDir, $"{packageId}.*.nupkg") |
| 89 | + .FirstOrDefault(); |
73 | 90 |
|
74 | | - nugetTool.Client.Initialize(); |
| 91 | + if (string.IsNullOrEmpty(packageFilename)) |
| 92 | + throw new ApplicationException($"No package found matching package ID '{packageId}'!"); |
75 | 93 |
|
76 | | - await nugetTool.PublishAsync(token); |
| 94 | + await nugetCore.PushAsync(packageFilename, token); |
77 | 95 | } |
78 | 96 | } |
79 | 97 | } |
0 commit comments