Skip to content

Commit

Permalink
Add dotnet sln and list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
James Connor authored and augustoproiete committed Oct 10, 2023
1 parent c3cd2af commit b3c6f92
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 0 deletions.
141 changes: 141 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Cake.Common.Tools.DotNet.Clean;
using Cake.Common.Tools.DotNet.Execute;
using Cake.Common.Tools.DotNet.Format;
using Cake.Common.Tools.DotNet.List;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Common.Tools.DotNet.NuGet.Delete;
using Cake.Common.Tools.DotNet.NuGet.Push;
Expand All @@ -20,6 +21,7 @@
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
using Cake.Common.Tools.DotNet.SDKCheck;
using Cake.Common.Tools.DotNet.Sln;
using Cake.Common.Tools.DotNet.Test;
using Cake.Common.Tools.DotNet.Tool;
using Cake.Common.Tools.DotNet.VSTest;
Expand Down Expand Up @@ -1300,6 +1302,145 @@ public static void DotNetMSBuild(this ICakeContext context, string projectOrDire
builder.Build(projectOrDirectory, settings);
}

/// <summary>
/// Adds a project to a solution
/// </summary>
/// <param name="context">The context.</param>
/// <param name="solution">Solution file to add to. Uses working directory if null.</param>
/// <param name="project">Project file to add.</param>
/// <param name="solutionFolder">Solution folder name to add the reference to. Created if it doesn't exist. Adds project to solution root if null.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// DotNetSlnAdd("MySolution.sln", "MyProject.csproj", null, settings);
///

Check failure on line 1316 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Check failure on line 1316 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Check failure on line 1316 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

/// DotNetSlnAdd("MySolution.sln", "MyProject.csproj", "MySolutionFolder", settings);
/// </code>
/// </example>
/// <remarks>
/// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file.
/// </remarks>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")]
public static void DotNetSlnAdd(this ICakeContext context, string solution, string project,
string solutionFolder, DotNetSlnSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetSlnSettings();
}

var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
executor.Add(solution, project, solutionFolder, settings);
}

/// <summary>
/// Removes a project to a solution
/// </summary>
/// <param name="context">The context.</param>
/// <param name="solution">Solution file to remove from. Searches working directory if null.</param>
/// <param name="project">Project file to remove.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// DotNetSlnRemove("MySolution.sln", "MyProject.csproj", settings);
/// </code>
/// </example>
/// <remarks>
/// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file.
/// </remarks>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")]
public static void DotNetSlnRemove(this ICakeContext context, string solution, string project,
DotNetSlnSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetSlnSettings();
}

var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
executor.Remove(solution, project, settings);
}

/// <summary>
/// Lists project references in a solution
/// </summary>
/// <param name="context">The context.</param>
/// <param name="solution">Solution file to add to. Uses working directory if null.</param>
/// <param name="settings">The settings.</param>
/// <returns>An array of absolute paths to any references found. These can be .csproj, .fsproj, .dwproj files etc.</returns>
/// <example>
/// <code>
/// DotNetSlnList("MySolution.sln", settings);
/// </code>
/// </example>
/// <remarks>
/// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file.
/// </remarks>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")]
public static string[] DotNetSlnList(this ICakeContext context, string solution, DotNetSlnSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetSlnSettings();
}

var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return executor.List(solution, settings);
}

Check failure on line 1411 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Check failure on line 1411 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Check failure on line 1411 in src/Cake.Common/Tools/DotNet/DotNetAliases.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)


/// <summary>
/// Given a project file, lists project references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project to inspect for project references.</param>
/// <param name="settings">The settings.</param>
/// <returns>An array of absolute paths to any project references found. These can be .csproj, .fsproj, .dwproj files etc.</returns>
/// <example>
/// <code>
/// DotNetListProjectReferences("MyProject.csproj", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("List")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.List")]
public static string[] DotNetListProjectReferences(ICakeContext context, string project, DotNetListSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetListSettings();
}

var executor = new DotNetList(context.FileSystem, context.Environment, context.ProcessRunner,
context.Tools);
return executor.ListProjectReferences(project, settings);
}
/// <summary>
/// Test one or more projects specified by a path or glob pattern using the VS Test host runner.
/// </summary>
Expand Down
86 changes: 86 additions & 0 deletions src/Cake.Common/Tools/DotNet/List/DotNetList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 System.IO;
using System.Linq;
using Cake.Common.Tools.DotNet.Format;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet.List
{
/// <summary>
/// 'dotnet list' commands

Check failure on line 16 in src/Cake.Common/Tools/DotNet/List/DotNetList.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Check failure on line 16 in src/Cake.Common/Tools/DotNet/List/DotNetList.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Check failure on line 16 in src/Cake.Common/Tools/DotNet/List/DotNetList.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

/// </summary>
public sealed class DotNetList : DotNetTool<DotNetListSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="DotNetList" /> 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 DotNetList(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
}

/// <summary>
/// Given a project, outputs all project references
/// </summary>
/// <param name="project">The target project path.</param>
/// <param name="settings">The settings.</param>
public string[] ListProjectReferences(string project, DotNetListSettings settings)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}

if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

var references = Array.Empty<string>();
Run(settings, GetListProjectReferencesArguments(project, settings),
new ProcessSettings { RedirectStandardOutput = true },
process =>
{
var projDir = new FileInfo(project).DirectoryName;
references =
process.GetStandardOutput().Skip(2)
.Select(p =>
System.IO.Path.GetFullPath(System.IO.Path.Join(projDir, p)).Replace('/', '\\'))
.ToArray();
});

return references;
}

private ProcessArgumentBuilder GetListProjectReferencesArguments(string project, DotNetListSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("list");

// Specific path?
if (project != null)
{
builder.AppendQuoted(project);
}

builder.Append("reference");

return builder;
}
}
}
13 changes: 13 additions & 0 deletions src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

namespace Cake.Common.Tools.DotNet.List
{
/// <summary>
/// DotNetListSettings class

Check failure on line 8 in src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Check failure on line 8 in src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Check failure on line 8 in src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

/// </summary>
public class DotNetListSettings : DotNetSettings
{
}
}
Loading

0 comments on commit b3c6f92

Please sign in to comment.