Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNet.Project.Convert;
using Cake.Core.IO;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Project.Convert;

internal sealed class DotNetProjectConverterFixture : DotNetFixture<DotNetProjectConvertSettings>
{
public FilePath File { get; set; } = "cake.cs";

protected override void RunTool()
{
var tool = new DotNetProjectConverter(FileSystem, Environment, ProcessRunner, Tools);
tool.Convert(File, Settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotNet.Project.Convert;
using Cake.Common.Tools.DotNet;
using Cake.Testing;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Project.Convert;

public sealed class DotNetProjectConverterTests
{
public sealed class TheConvertMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.GivenProcessCannotStart();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
}

[Fact]
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.GivenProcessExitsWithCode(1);

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
}

[Fact]
public void Should_Throw_If_File_Is_Null()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.File = null;

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "file");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "settings");
}

[Fact]
public void Should_Add_File_Argument()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.File = "cake.cs";

// When
var result = fixture.Run();

// Then
Assert.Equal("project convert \"/Working/cake.cs\"", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetProjectConverterFixture();
fixture.File = "cake.cs";
fixture.Settings.DeleteSource = true;
fixture.Settings.DryRun = true;
fixture.Settings.Force = true;
fixture.Settings.Interactive = true;
fixture.Settings.Output = "./cake";
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

// When
var result = fixture.Run();

// Then
Assert.Equal("project convert \"/Working/cake.cs\" --delete-source --dry-run --force --interactive --output \"/Working/cake\" --verbosity diagnostic", result.Args);
}
}
}
76 changes: 76 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Cake.Common.Tools.DotNet.Project.Convert;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;

namespace Cake.Common.Tools.DotNet;

/// <summary>
/// <para>Contains functionality related to <see href="https://github.com/dotnet/cli">.NET CLI</see>.</para>
/// <para>
/// In order to use the commands for this alias, the .NET CLI tools will need to be installed on the machine where
/// the Cake script is being executed. See this <see href="https://www.microsoft.com/net/core">page</see> for information
/// on how to install.
/// </para>
/// </summary>
public static partial class DotNetAliases
{
/// <summary>
/// Converts a file-based program to a project-based program.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="file">The file-based program to convert.</param>
/// <example>
/// <code>
/// // cake.cs:
/// // #:sdk Cake.Sdk
/// //
/// // Information("Hello world!");
///
/// DotNetProjectConvert("cake.cs");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Project")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Project.Convert")]
public static void DotNetProjectConvert(this ICakeContext context, FilePath file)
{
context.DotNetProjectConvert(file, null);
}

/// <summary>
/// Converts a file-based program to a project-based program.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="file">The file-based program to convert.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// // cake.cs:
/// // #:sdk Cake.Sdk
/// //
/// // Information("Hello world!");
///
/// DotNetProjectConvert("cake.cs", new DotNetProjectConvertSettings {
/// Output = "./cake"
/// });
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Project")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Project.Convert")]
public static void DotNetProjectConvert(this ICakeContext context, FilePath file, DotNetProjectConvertSettings settings)
{
ArgumentNullException.ThrowIfNull(context);

settings ??= new DotNetProjectConvertSettings();

var converter = new DotNetProjectConverter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
converter.Convert(file, settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Core.IO;

namespace Cake.Common.Tools.DotNet.Project.Convert;

/// <summary>
/// Contains settings used by <see cref="DotNetProjectConverter" />.
/// </summary>
public sealed class DotNetProjectConvertSettings : DotNetSettings
{
/// <summary>
/// Gets or sets a value indicating whether to delete the original file-based program after conversion.
/// </summary>
public bool DeleteSource { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show what would happen without writing files.
/// </summary>
public bool DryRun { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to overwrite existing output.
/// </summary>
public bool Force { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to allow the command to stop and wait for user input or action.
/// For example, to complete authentication.
/// </summary>
public bool Interactive { get; set; }

/// <summary>
/// Gets or sets the output directory for the converted project.
/// </summary>
public DirectoryPath Output { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet.Project.Convert;

/// <summary>
/// .NET project converter.
/// </summary>
public sealed class DotNetProjectConverter : DotNetTool<DotNetProjectConvertSettings>
{
private readonly ICakeEnvironment _environment;

/// <summary>
/// Initializes a new instance of the <see cref="DotNetProjectConverter" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
public DotNetProjectConverter(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
_environment = environment;
}

/// <summary>
/// Converts a file-based program to a project-based program.
/// </summary>
/// <param name="file">The file-based program to convert.</param>
/// <param name="settings">The settings.</param>
public void Convert(FilePath file, DotNetProjectConvertSettings settings)
{
ArgumentNullException.ThrowIfNull(file);
ArgumentNullException.ThrowIfNull(settings);

RunCommand(settings, GetArguments(file, settings));
}

private ProcessArgumentBuilder GetArguments(FilePath file, DotNetProjectConvertSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("project");
builder.Append("convert");
builder.AppendQuoted(file.MakeAbsolute(_environment).FullPath);

if (settings.DeleteSource)
{
builder.Append("--delete-source");
}

if (settings.DryRun)
{
builder.Append("--dry-run");
}

if (settings.Force)
{
builder.Append("--force");
}

if (settings.Interactive)
{
builder.Append("--interactive");
}

if (settings.Output != null)
{
builder.AppendSwitchQuoted("--output", settings.Output.MakeAbsolute(_environment).FullPath);
}

return builder;
}
}
53 changes: 53 additions & 0 deletions tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,57 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnMigrate")
Assert.True(FileExists(path.CombineWithFilePath("hwapp.slnx")));
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// Given
var path = Paths.Temp.Combine("./Cake.Common/Tools/DotNet/DotNetProjectConvert");
var output = path.Combine("cake");
EnsureDirectoryExist(path);
var file = path.CombineWithFilePath("cake.cs");
System.IO.File.WriteAllText(
file.FullPath,
"""
#:sdk Cake.Sdk@6.3.0

Information("Hello world!");
""");

// When
DotNetProjectConvert(file, new DotNetProjectConvertSettings { Output = output });

// Then
var project = GetFiles($"{output.FullPath}/**/*.csproj").FirstOrDefault();
Assert.NotNull(project);
var sdk = XmlPeek(project.FullPath, "/Project/@Sdk");
Assert.StartsWith("Cake.Sdk", sdk);
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert.DryRun")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// Given
var path = Paths.Temp.Combine("./Cake.Common/Tools/DotNet/DotNetProjectConvertDryRun");
var output = path.Combine("cake");
EnsureDirectoryExist(path);
var file = path.CombineWithFilePath("cake.cs");
System.IO.File.WriteAllText(
file.FullPath,
"""
#:sdk Cake.Sdk@6.3.0

Information("Hello world!");
""");

// When
DotNetProjectConvert(file, new DotNetProjectConvertSettings { Output = output, DryRun = true });

// Then
Assert.Empty(GetFiles($"{output.FullPath}/**/*.csproj"));
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnRemove")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
Expand Down Expand Up @@ -789,6 +840,8 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnAdd")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnList")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnMigrate")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert.DryRun")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnRemove")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadRepair")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadRestore")
Expand Down
Loading