Skip to content

Commit f7b18fa

Browse files
committed
Add data model for DotNetListPackage result
1 parent 21a1e97 commit f7b18fa

14 files changed

+338
-45
lines changed

src/Cake.Common.Tests/Fixtures/Tools/DotNet/Package/List/DotNetPackageListerFixture.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,41 @@ namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.List
99
internal sealed class DotNetPackageListerFixture : DotNetFixture<DotNetPackageListSettings>
1010
{
1111
public string Project { get; set; }
12+
public DotNetPackageList Result { get; set; }
13+
14+
public void GivenPackgeListResult()
15+
{
16+
ProcessRunner.Process.SetStandardOutput(new string[]
17+
{
18+
"{",
19+
" \"version\": 1,",
20+
" \"parameters\": \"\",",
21+
" \"projects\": [",
22+
" {",
23+
" \"path\": \"src/lib/MyProject.csproj\",",
24+
" \"frameworks\": [",
25+
" {",
26+
" \"framework\": \"netstandard2.0\",",
27+
" \"topLevelPackages\": [",
28+
" {",
29+
" \"id\": \"NETStandard.Library\",",
30+
" \"requestedVersion\": \"[2.0.3, )\",",
31+
" \"resolvedVersion\": \"2.0.3\",",
32+
" \"autoReferenced\": \"true\"",
33+
" }",
34+
" ]",
35+
" }",
36+
" ]",
37+
" }",
38+
" ]",
39+
"}"
40+
});
41+
}
1242

1343
protected override void RunTool()
1444
{
1545
var tool = new DotNetPackageLister(FileSystem, Environment, ProcessRunner, Tools);
16-
tool.List(Project, Settings);
46+
Result = tool.List(Project, Settings);
1747
}
1848
}
1949
}

src/Cake.Common.Tests/Unit/Tools/DotNet/Package/List/DotNetPackageListerTests.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using Cake.Common.Tests.Fixtures.Tools.DotNet.Package.List;
66
using Cake.Common.Tools.DotNet;
7-
using Cake.Common.Tools.DotNet.Package.List;
87
using Cake.Testing;
98
using Xunit;
109

@@ -88,8 +87,6 @@ public void Should_Add_Additional_Arguments()
8887
fixture.Settings.Source.Add("http://www.nuget.org/api/v2/package");
8988
fixture.Settings.Source.Add("http://www.symbolserver.org/");
9089
fixture.Settings.Vulnerable = true;
91-
fixture.Settings.Format = DotNetPackageListFormat.Json;
92-
fixture.Settings.OutputVersion = 1;
9390
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;
9491

9592
// When
@@ -100,6 +97,21 @@ public void Should_Add_Additional_Arguments()
10097
expected += "--source \"http://www.nuget.org/api/v2/package\" --source \"http://www.symbolserver.org/\" --vulnerable --format Json --output-version 1 --verbosity diagnostic";
10198
Assert.Equal(expected, result.Args);
10299
}
100+
101+
[Fact]
102+
public void Should_Return_Correct_Result()
103+
{
104+
// Given
105+
var fixture = new DotNetPackageListerFixture();
106+
fixture.GivenPackgeListResult();
107+
108+
// When
109+
var result = fixture.Run();
110+
111+
// Then
112+
Assert.Equal(1, fixture.Result.Version);
113+
Assert.Contains(fixture.Result.Projects, item => item.Path == "src/lib/MyProject.csproj");
114+
}
103115
}
104116
}
105117
}

src/Cake.Common/Tools/DotNet/DotNetAliases.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -2391,35 +2391,37 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
23912391
/// Lists the package references for a project or solution.
23922392
/// </summary>
23932393
/// <param name="context">The context.</param>
2394+
/// <returns>The the package references.</returns>
23942395
/// <example>
23952396
/// <code>
2396-
/// DotNetListPackage();
2397+
/// DotNetPackageList output = DotNetListPackage();
23972398
/// </code>
23982399
/// </example>
23992400
[CakeMethodAlias]
24002401
[CakeAliasCategory("Package")]
24012402
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
2402-
public static void DotNetListPackage(this ICakeContext context)
2403+
public static DotNetPackageList DotNetListPackage(this ICakeContext context)
24032404
{
2404-
context.DotNetListPackage(null);
2405+
return context.DotNetListPackage(null);
24052406
}
24062407

24072408
/// <summary>
24082409
/// Lists the package references for a project or solution.
24092410
/// </summary>
24102411
/// <param name="context">The context.</param>
24112412
/// <param name="project">The project or solution file to operate on. If not specified, the command searches the current directory for one. If more than one solution or project is found, an error is thrown.</param>
2413+
/// <returns>The the package references.</returns>
24122414
/// <example>
24132415
/// <code>
2414-
/// DotNetListPackage("./src/MyProject/MyProject.csproj");
2416+
/// DotNetPackageList output = DotNetListPackage("./src/MyProject/MyProject.csproj");
24152417
/// </code>
24162418
/// </example>
24172419
[CakeMethodAlias]
24182420
[CakeAliasCategory("Package")]
24192421
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
2420-
public static void DotNetListPackage(this ICakeContext context, string project)
2422+
public static DotNetPackageList DotNetListPackage(this ICakeContext context, string project)
24212423
{
2422-
context.DotNetListPackage(project, null);
2424+
return context.DotNetListPackage(project, null);
24232425
}
24242426

24252427
/// <summary>
@@ -2428,6 +2430,7 @@ public static void DotNetListPackage(this ICakeContext context, string project)
24282430
/// <param name="context">The context.</param>
24292431
/// <param name="project">The project or solution file to operate on. If not specified, the command searches the current directory for one. If more than one solution or project is found, an error is thrown.</param>
24302432
/// <param name="settings">The settings.</param>
2433+
/// <returns>The the package references.</returns>
24312434
/// <example>
24322435
/// <code>
24332436
/// var settings = new DotNetPackageListSettings
@@ -2436,13 +2439,13 @@ public static void DotNetListPackage(this ICakeContext context, string project)
24362439
/// Format = DotNetPackageListFormat.Json
24372440
/// };
24382441
///
2439-
/// DotNetListPackage("./src/MyProject/MyProject.csproj", settings);
2442+
/// DotNetPackageList output = DotNetListPackage("./src/MyProject/MyProject.csproj", settings);
24402443
/// </code>
24412444
/// </example>
24422445
[CakeMethodAlias]
24432446
[CakeAliasCategory("Package")]
24442447
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.List")]
2445-
public static void DotNetListPackage(this ICakeContext context, string project, DotNetPackageListSettings settings)
2448+
public static DotNetPackageList DotNetListPackage(this ICakeContext context, string project, DotNetPackageListSettings settings)
24462449
{
24472450
if (context is null)
24482451
{
@@ -2454,8 +2457,8 @@ public static void DotNetListPackage(this ICakeContext context, string project,
24542457
settings = new DotNetPackageListSettings();
24552458
}
24562459

2457-
var adder = new DotNetPackageLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
2458-
adder.List(project, settings);
2460+
var lister = new DotNetPackageLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
2461+
return lister.List(project, settings);
24592462
}
24602463
}
24612464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Cake.Common.Tools.DotNet.Package.List
9+
{
10+
/// <summary>
11+
/// An result as returned by <see cref="DotNetPackageLister"/>.
12+
/// </summary>
13+
public sealed class DotNetPackageList
14+
{
15+
/// <summary>
16+
/// Gets the output version.
17+
/// </summary>
18+
[JsonInclude]
19+
public int Version { get; private set; }
20+
21+
/// <summary>
22+
/// Gets the specified parameters.
23+
/// </summary>
24+
[JsonInclude]
25+
public string Parameters { get; private set; }
26+
27+
/// <summary>
28+
/// Gets the problems.
29+
/// </summary>
30+
[JsonInclude]
31+
public IEnumerable<DotNetPackageListProblemItem> Problems { get; private set; }
32+
33+
/// <summary>
34+
/// Gets the used sources.
35+
/// </summary>
36+
[JsonInclude]
37+
public IEnumerable<string> Sources { get; private set; }
38+
39+
/// <summary>
40+
/// Gets the projects.
41+
/// </summary>
42+
[JsonInclude]
43+
public IEnumerable<DotNetPackageListProjectItem> Projects { get; private set; }
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace Cake.Common.Tools.DotNet.Package.List
8+
{
9+
/// <summary>
10+
/// The alternative package information.
11+
/// </summary>
12+
public sealed class DotNetPackageListAlternativePackageItem
13+
{
14+
/// <summary>
15+
/// Gets the alternative package id.
16+
/// </summary>
17+
[JsonInclude]
18+
public string Id { get; private set; }
19+
20+
/// <summary>
21+
/// Gets the alternative package versions.
22+
/// </summary>
23+
[JsonInclude]
24+
public string VersionRange { get; private set; }
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Cake.Common.Tools.DotNet.Package.List
9+
{
10+
/// <summary>
11+
/// The framework information.
12+
/// </summary>
13+
public sealed class DotNetPackageListFrameworkItem
14+
{
15+
/// <summary>
16+
/// Gets the framework name.
17+
/// </summary>
18+
[JsonInclude]
19+
public string Framework { get; private set; }
20+
21+
/// <summary>
22+
/// Gets the top-level packages.
23+
/// </summary>
24+
[JsonInclude]
25+
public IEnumerable<DotNetPackageListPackageItem> TopLevelPackages { get; private set; }
26+
27+
/// <summary>
28+
/// Gets transitive packages.
29+
/// </summary>
30+
[JsonInclude]
31+
public IEnumerable<DotNetPackageListPackageItem> TransitivePackages { get; private set; }
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Cake.Common.Tools.DotNet.Package.List
9+
{
10+
/// <summary>
11+
/// The package information.
12+
/// </summary>
13+
public sealed class DotNetPackageListPackageItem
14+
{
15+
/// <summary>
16+
/// Gets the package id.
17+
/// </summary>
18+
[JsonInclude]
19+
public string Id { get; private set; }
20+
21+
/// <summary>
22+
/// Gets the requested version.
23+
/// </summary>
24+
[JsonInclude]
25+
public string RequestedVersion { get; private set; }
26+
27+
/// <summary>
28+
/// Gets the resolved version.
29+
/// </summary>
30+
[JsonInclude]
31+
public string ResolvedVersion { get; private set; }
32+
33+
/// <summary>
34+
/// Gets a value indicating whether the package is auto-referenced.
35+
/// </summary>
36+
[JsonInclude]
37+
public string AutoReferenced { get; private set; }
38+
39+
/// <summary>
40+
/// Gets the latest version.
41+
/// </summary>
42+
[JsonInclude]
43+
public string LatestVersion { get; private set; }
44+
45+
/// <summary>
46+
/// Gets the deprecation reasons.
47+
/// </summary>
48+
[JsonInclude]
49+
public IEnumerable<string> DeprecationReasons { get; private set; }
50+
51+
/// <summary>
52+
/// Gets the alternative package.
53+
/// </summary>
54+
[JsonInclude]
55+
public DotNetPackageListAlternativePackageItem AlternativePackage { get; private set; }
56+
57+
/// <summary>
58+
/// Gets the vulnerabilities list.
59+
/// </summary>
60+
[JsonInclude]
61+
public IEnumerable<DotNetPackageListVulnerabilityItem> Vulnerabilities { get; private set; }
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace Cake.Common.Tools.DotNet.Package.List
8+
{
9+
/// <summary>
10+
/// The problem information.
11+
/// </summary>
12+
public sealed class DotNetPackageListProblemItem
13+
{
14+
/// <summary>
15+
/// Gets the problem level.
16+
/// </summary>
17+
[JsonInclude]
18+
public DotNetPackageListProblemType? Level { get; private set; }
19+
20+
/// <summary>
21+
/// Gets the problem text.
22+
/// </summary>
23+
[JsonInclude]
24+
public string Text { get; private set; }
25+
26+
/// <summary>
27+
/// Gets the project path.
28+
/// </summary>
29+
[JsonInclude]
30+
public string Project { get; private set; }
31+
}
32+
}

src/Cake.Common/Tools/DotNet/Package/List/DotNetPackageListFormat.cs src/Cake.Common/Tools/DotNet/Package/List/DotNetPackageListProblemType.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
namespace Cake.Common.Tools.DotNet.Package.List
66
{
77
/// <summary>
8-
/// Formats of dotnet list package.
8+
/// The problem types.
99
/// </summary>
10-
public enum DotNetPackageListFormat
10+
public enum DotNetPackageListProblemType
1111
{
1212
/// <summary>
13-
/// Console.
13+
/// Warning.
1414
/// </summary>
15-
Console,
15+
Warning,
1616

1717
/// <summary>
18-
/// Json.
18+
/// Error.
1919
/// </summary>
20-
Json
20+
Error
2121
}
2222
}

0 commit comments

Comments
 (0)