Merge pull request #5108 from devlead/feature/gh-5107 · devlead/cake@989bfec · GitHub
Skip to content

Commit 989bfec

Browse files
authored
Merge pull request cake-build#5108 from devlead/feature/cake-buildgh-5107
GH5107: Add DotNetProjectConvert aliases
2 parents 0dbd633 + d2ace31 commit 989bfec

6 files changed

Lines changed: 376 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 Cake.Common.Tests.Fixtures.Tools.DotNet.Project.Convert;
6+
using Cake.Common.Tools.DotNet;
7+
using Cake.Testing;
8+
9+
namespace Cake.Common.Tests.Unit.Tools.DotNet.Project.Convert;
10+
11+
public sealed class DotNetProjectConverterTests
12+
{
13+
public sealed class TheConvertMethod
14+
{
15+
[Fact]
16+
public void Should_Throw_If_Process_Was_Not_Started()
17+
{
18+
// Given
19+
var fixture = new DotNetProjectConverterFixture();
20+
fixture.GivenProcessCannotStart();
21+
22+
// When
23+
var result = Record.Exception(() => fixture.Run());
24+
25+
// Then
26+
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
27+
}
28+
29+
[Fact]
30+
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
31+
{
32+
// Given
33+
var fixture = new DotNetProjectConverterFixture();
34+
fixture.GivenProcessExitsWithCode(1);
35+
36+
// When
37+
var result = Record.Exception(() => fixture.Run());
38+
39+
// Then
40+
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
41+
}
42+
43+
[Fact]
44+
public void Should_Throw_If_File_Is_Null()
45+
{
46+
// Given
47+
var fixture = new DotNetProjectConverterFixture();
48+
fixture.File = null;
49+
50+
// When
51+
var result = Record.Exception(() => fixture.Run());
52+
53+
// Then
54+
AssertEx.IsArgumentNullException(result, "file");
55+
}
56+
57+
[Fact]
58+
public void Should_Throw_If_Settings_Are_Null()
59+
{
60+
// Given
61+
var fixture = new DotNetProjectConverterFixture();
62+
fixture.Settings = null;
63+
fixture.GivenDefaultToolDoNotExist();
64+
65+
// When
66+
var result = Record.Exception(() => fixture.Run());
67+
68+
// Then
69+
AssertEx.IsArgumentNullException(result, "settings");
70+
}
71+
72+
[Fact]
73+
public void Should_Add_File_Argument()
74+
{
75+
// Given
76+
var fixture = new DotNetProjectConverterFixture();
77+
fixture.File = "cake.cs";
78+
79+
// When
80+
var result = fixture.Run();
81+
82+
// Then
83+
Assert.Equal("project convert \"/Working/cake.cs\"", result.Args);
84+
}
85+
86+
[Fact]
87+
public void Should_Add_Additional_Arguments()
88+
{
89+
// Given
90+
var fixture = new DotNetProjectConverterFixture();
91+
fixture.File = "cake.cs";
92+
fixture.Settings.DeleteSource = true;
93+
fixture.Settings.DryRun = true;
94+
fixture.Settings.Force = true;
95+
fixture.Settings.Interactive = true;
96+
fixture.Settings.Output = "./cake";
97+
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;
98+
99+
// When
100+
var result = fixture.Run();
101+
102+
// Then
103+
Assert.Equal("project convert \"/Working/cake.cs\" --delete-source --dry-run --force --interactive --output \"/Working/cake\" --verbosity diagnostic", result.Args);
104+
}
105+
}
106+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;
6+
using Cake.Common.Tools.DotNet.Project.Convert;
7+
using Cake.Core;
8+
using Cake.Core.Annotations;
9+
using Cake.Core.IO;
10+
11+
namespace Cake.Common.Tools.DotNet;
12+
13+
/// <summary>
14+
/// <para>Contains functionality related to <see href="https://github.com/dotnet/cli">.NET CLI</see>.</para>
15+
/// <para>
16+
/// In order to use the commands for this alias, the .NET CLI tools will need to be installed on the machine where
17+
/// the Cake script is being executed. See this <see href="https://www.microsoft.com/net/core">page</see> for information
18+
/// on how to install.
19+
/// </para>
20+
/// </summary>
21+
public static partial class DotNetAliases
22+
{
23+
/// <summary>
24+
/// Converts a file-based program to a project-based program.
25+
/// </summary>
26+
/// <param name="context">The context.</param>
27+
/// <param name="file">The file-based program to convert.</param>
28+
/// <example>
29+
/// <code>
30+
/// // cake.cs:
31+
/// // #:sdk Cake.Sdk
32+
/// //
33+
/// // Information("Hello world!");
34+
///
35+
/// DotNetProjectConvert("cake.cs");
36+
/// </code>
37+
/// </example>
38+
[CakeMethodAlias]
39+
[CakeAliasCategory("Project")]
40+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Project.Convert")]
41+
public static void DotNetProjectConvert(this ICakeContext context, FilePath file)
42+
{
43+
context.DotNetProjectConvert(file, null);
44+
}
45+
46+
/// <summary>
47+
/// Converts a file-based program to a project-based program.
48+
/// </summary>
49+
/// <param name="context">The context.</param>
50+
/// <param name="file">The file-based program to convert.</param>
51+
/// <param name="settings">The settings.</param>
52+
/// <example>
53+
/// <code>
54+
/// // cake.cs:
55+
/// // #:sdk Cake.Sdk
56+
/// //
57+
/// // Information("Hello world!");
58+
///
59+
/// DotNetProjectConvert("cake.cs", new DotNetProjectConvertSettings {
60+
/// Output = "./cake"
61+
/// });
62+
/// </code>
63+
/// </example>
64+
[CakeMethodAlias]
65+
[CakeAliasCategory("Project")]
66+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Project.Convert")]
67+
public static void DotNetProjectConvert(this ICakeContext context, FilePath file, DotNetProjectConvertSettings settings)
68+
{
69+
ArgumentNullException.ThrowIfNull(context);
70+
71+
settings ??= new DotNetProjectConvertSettings();
72+
73+
var converter = new DotNetProjectConverter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
74+
converter.Convert(file, settings);
75+
}
76+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 Cake.Core.IO;
6+
7+
namespace Cake.Common.Tools.DotNet.Project.Convert;
8+
9+
/// <summary>
10+
/// Contains settings used by <see cref="DotNetProjectConverter" />.
11+
/// </summary>
12+
public sealed class DotNetProjectConvertSettings : DotNetSettings
13+
{
14+
/// <summary>
15+
/// Gets or sets a value indicating whether to delete the original file-based program after conversion.
16+
/// </summary>
17+
public bool DeleteSource { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets a value indicating whether to show what would happen without writing files.
21+
/// </summary>
22+
public bool DryRun { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets a value indicating whether to overwrite existing output.
26+
/// </summary>
27+
public bool Force { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets a value indicating whether to allow the command to stop and wait for user input or action.
31+
/// For example, to complete authentication.
32+
/// </summary>
33+
public bool Interactive { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the output directory for the converted project.
37+
/// </summary>
38+
public DirectoryPath Output { get; set; }
39+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
6+
using Cake.Core;
7+
using Cake.Core.IO;
8+
using Cake.Core.Tooling;
9+
10+
namespace Cake.Common.Tools.DotNet.Project.Convert;
11+
12+
/// <summary>
13+
/// .NET project converter.
14+
/// </summary>
15+
public sealed class DotNetProjectConverter : DotNetTool<DotNetProjectConvertSettings>
16+
{
17+
private readonly ICakeEnvironment _environment;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="DotNetProjectConverter" /> class.
21+
/// </summary>
22+
/// <param name="fileSystem">The file system.</param>
23+
/// <param name="environment">The environment.</param>
24+
/// <param name="processRunner">The process runner.</param>
25+
/// <param name="tools">The tool locator.</param>
26+
public DotNetProjectConverter(
27+
IFileSystem fileSystem,
28+
ICakeEnvironment environment,
29+
IProcessRunner processRunner,
30+
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
31+
{
32+
_environment = environment;
33+
}
34+
35+
/// <summary>
36+
/// Converts a file-based program to a project-based program.
37+
/// </summary>
38+
/// <param name="file">The file-based program to convert.</param>
39+
/// <param name="settings">The settings.</param>
40+
public void Convert(FilePath file, DotNetProjectConvertSettings settings)
41+
{
42+
ArgumentNullException.ThrowIfNull(file);
43+
ArgumentNullException.ThrowIfNull(settings);
44+
45+
RunCommand(settings, GetArguments(file, settings));
46+
}
47+
48+
private ProcessArgumentBuilder GetArguments(FilePath file, DotNetProjectConvertSettings settings)
49+
{
50+
var builder = CreateArgumentBuilder(settings);
51+
52+
builder.Append("project");
53+
builder.Append("convert");
54+
builder.AppendQuoted(file.MakeAbsolute(_environment).FullPath);
55+
56+
if (settings.DeleteSource)
57+
{
58+
builder.Append("--delete-source");
59+
}
60+
61+
if (settings.DryRun)
62+
{
63+
builder.Append("--dry-run");
64+
}
65+
66+
if (settings.Force)
67+
{
68+
builder.Append("--force");
69+
}
70+
71+
if (settings.Interactive)
72+
{
73+
builder.Append("--interactive");
74+
}
75+
76+
if (settings.Output != null)
77+
{
78+
builder.AppendSwitchQuoted("--output", settings.Output.MakeAbsolute(_environment).FullPath);
79+
}
80+
81+
return builder;
82+
}
83+
}

‎tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake‎

Lines changed: 53 additions & 0 deletions

0 commit comments

Comments
 (0)