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
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageVersion Include="Microsoft.IO.Redist" Version="6.1.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="3.14.2075" />
<PackageVersion Include="Microsoft.VisualStudio.SolutionPersistence" Version="1.0.52" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
Expand All @@ -27,4 +28,4 @@
<Compile Include="..\Shared\GlobalSuppressions.cs" />
<AdditionalFiles Include="..\Shared\stylecop.json" />
</ItemGroup>
</Project>
</Project>
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ And the resulting project would look like this:
</Project>
```

# Visual Studio Solutions
You can create Visual Studio solutions with the `SolutionCreator` class.
This class is a wrapper around the [VS-SolutionPersistence] library which supports both `.sln` and `.slnx` solution file formats.

The following example creates a solution with two projects:
```C#
ProjectCreator project1 = ProjectCreator.Templates.SdkCsproj(path: Path.Combine(Environment.CurrentDirectory, "project1", "project1.csproj"));

SolutionCreator.Create(Path.Combine(Environment.CurrentDirectory, "solution1.sln"))
.Configuration("Debug")
.Configuration("Release")
.Platform("Any CPU")
.Project(project1)
.Save();
```

# Package Repositories and Feeds
NuGet and MSBuild are very tightly coupled and a lot of times you need packages available when building projects. This API offers two solutions:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Jeff Kluge. All rights reserved.
//
// Licensed under the MIT license.

using Microsoft.VisualStudio.SolutionPersistence.Model;
using Shouldly;
using System.IO;
using Xunit;

namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
{
public class SolutionTests : TestBase
{
[Fact]
public void BasicTest()
{
string solutionFileFullPath = Path.Combine(TestRootPath, "solution1.sln");

string project1Name = "project1";

string project1FullPath = Path.Combine(TestRootPath, project1Name, "project1.csproj");

ProjectCreator project1 = ProjectCreator.Templates.SdkCsproj(project1FullPath);

SolutionCreator solution = SolutionCreator.Create(solutionFileFullPath)
.TryProject(project1, projectInSolution: out SolutionProjectModel projectInSolution)
.Save();

File.ReadAllText(solutionFileFullPath).ShouldBe(
@$"Microsoft Visual Studio Solution File, Format Version 12.00
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{project1Name}"", ""{project1FullPath.Replace('/', '\\')}"", ""{{{projectInSolution.Id.ToString().ToUpperInvariant()}}}""
EndProject
Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
",
StringCompareShould.IgnoreLineEndings);
}

[Fact]
public void CanBuild()
{
ProjectCreator project1 = ProjectCreator.Templates.SdkCsproj(path: Path.Combine(TestRootPath, "project1", "project1.csproj"));

SolutionCreator.Create(Path.Combine(TestRootPath, "solution1.sln"))
.Configuration("Debug")
.Configuration("Release")
.Platform("Any CPU")
.Project(project1)
.TryBuild(out _);
}
}
}
209 changes: 209 additions & 0 deletions src/Microsoft.Build.Utilities.ProjectCreation/BuildHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Copyright (c) Jeff Kluge. All rights reserved.
//
// Licensed under the MIT license.

using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using System;
using System.Collections.Generic;

namespace Microsoft.Build.Utilities.ProjectCreation
{
internal static class BuildHost
{
private static readonly IDictionary<string, string> EmptyGlobalProperties = new Dictionary<string, string>(capacity: 0);

#if !NET8_0
private static readonly IDictionary<string, string?> EmptyGlobalPropertiesWithNull = new Dictionary<string, string?>(capacity: 0);
#endif

public static bool Restore(
string projectFullPath,
ProjectCollection projectCollection,
IDictionary<string, string>? globalProperties,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs)
{
Dictionary<string, string> restoreGlobalProperties = new(globalProperties ?? projectCollection.GlobalProperties); // IMPORTANT: Make a copy of the global properties here so as not to modify the ones passed in

restoreGlobalProperties["ExcludeRestorePackageImports"] = "true";
restoreGlobalProperties["MSBuildRestoreSessionId"] = Guid.NewGuid().ToString("D");

BuildRequestDataFlags buildRequestDataFlags = BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports;

return BuildProjectFromFullPath(projectFullPath, ["Restore"], restoreGlobalProperties, [.. projectCollection.Loggers, buildOutput], buildRequestDataFlags, out targetOutputs);
}

public static bool TryBuild(
string projectFullPath,
ProjectCollection projectCollection,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs,
bool restore = false,
string? target = null,
IDictionary<string, string>? globalProperties = null)
{
return Build(projectFullPath, restore, target is null ? Array.Empty<string>() : [target], projectCollection, globalProperties, buildOutput, out targetOutputs);
}

public static bool TryBuild(
string projectFullPath,
ProjectCollection projectCollection,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs,
bool restore = false,
string[]? targets = null,
IDictionary<string, string>? globalProperties = null)
{
return Build(projectFullPath, restore, targets ?? Array.Empty<string>(), projectCollection, globalProperties, buildOutput, out targetOutputs);
}

public static bool TryBuild(
ProjectInstance projectInstance,
ProjectCollection projectCollection,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs,
string? target = null,
IDictionary<string, string>? globalProperties = null)
{
return Build(projectInstance, target is null ? Array.Empty<string>() : [target], projectCollection, globalProperties, buildOutput, out targetOutputs);
}

public static bool TryBuild(
ProjectInstance projectInstance,
ProjectCollection projectCollection,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs,
string[]? targets = null,
IDictionary<string, string>? globalProperties = null)
{
return Build(projectInstance, targets ?? Array.Empty<string>(), projectCollection, globalProperties, buildOutput, out targetOutputs);
}

private static bool Build(
string projectFullPath,
bool restore,
string[] targets,
ProjectCollection projectCollection,
IDictionary<string, string>? globalProperties,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs)
{
targetOutputs = null;

if (restore)
{
if (!Restore(projectFullPath, projectCollection, globalProperties, buildOutput, out _))
{
return false;
}
}

return BuildProjectFromFullPath(projectFullPath, targets, globalProperties, [.. projectCollection.Loggers, buildOutput], BuildRequestDataFlags.None, out targetOutputs);
}

private static bool Build(
ProjectInstance projectInstance,
string[] targets,
ProjectCollection projectCollection,
IDictionary<string, string>? globalProperties,
BuildOutput buildOutput,
out IDictionary<string, TargetResult>? targetOutputs)
{
targetOutputs = null;

return BuildProjectFromProjectInstance(projectInstance, targets, globalProperties, [.. projectCollection.Loggers, buildOutput], BuildRequestDataFlags.None, out targetOutputs);
}

private static bool BuildProjectFromProjectInstance(
ProjectInstance projectInstance,
string[] targets,
IDictionary<string, string>? globalProperties,
IEnumerable<ILogger> loggers,
BuildRequestDataFlags buildRequestDataFlags,
out IDictionary<string, TargetResult>? targetOutputs)
{
targetOutputs = null;

BuildResult buildResult = BuildManagerHost.Build(
projectInstance,
targets,
globalProperties ?? EmptyGlobalProperties,
loggers,
buildRequestDataFlags);

if (buildResult.Exception != null)
{
throw buildResult.Exception;
}

targetOutputs = buildResult.ResultsByTarget;

return buildResult.OverallResult == BuildResultCode.Success;
}

private static bool BuildProjectFromFullPath(
string projectFullPath,
string[] targets,
IDictionary<string, string>? globalProperties,
IEnumerable<ILogger> loggers,
BuildRequestDataFlags buildRequestDataFlags,
out IDictionary<string, TargetResult>? targetOutputs)
{
targetOutputs = null;

BuildResult buildResult = BuildManagerHost.Build(
projectFullPath,
targets,
GetGlobalProperties(globalProperties),
loggers,
buildRequestDataFlags);

if (buildResult.Exception != null)
{
throw buildResult.Exception;
}

if (targetOutputs != null)
{
foreach (KeyValuePair<string, TargetResult> targetResult in buildResult.ResultsByTarget)
{
targetOutputs[targetResult.Key] = targetResult.Value;
}
}
else
{
targetOutputs = buildResult.ResultsByTarget;
}

return buildResult.OverallResult == BuildResultCode.Success;

#if NET8_0
IDictionary<string, string>
#else
IDictionary<string, string?>
#endif
GetGlobalProperties(IDictionary<string, string>? globalProperties)
{
#if NET8_0
return globalProperties ?? EmptyGlobalProperties;
#else
if (globalProperties is null)
{
return EmptyGlobalPropertiesWithNull;
}

Dictionary<string, string?> finalGlobalProperties = new(globalProperties.Count);

foreach (var kvp in globalProperties)
{
finalGlobalProperties[kvp.Key] = kvp.Value;
}

return finalGlobalProperties;
#endif
}
}
}
}
16 changes: 0 additions & 16 deletions src/Microsoft.Build.Utilities.ProjectCreation/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ public static IEnumerable<T> AsEnumerable<T>(this T? item)
return result;
}

/// <summary>
/// Gets the current object as an array of objects.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="item">The item to make into an array.</param>
/// <returns>An array of T objects.</returns>
[DebuggerStepThrough]
public static T[] ToArrayWithSingleElement<T>(this T item)
where T : class
{
return new[]
{
item,
};
}

/// <summary>
/// Creates an entry in the current <see cref="ZipArchive" /> based on the specified <see cref="Stream" />.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" PrivateAssets="All" Condition="'$(OfficialBuild)' != 'true'" />
<PackageReference Include="Microsoft.IO.Redist" Condition="'$(TargetFramework)' == 'net472'" />
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Condition="'$(TargetFramework)' == 'net472'" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.SolutionPersistence" />
<PackageReference Include="System.IO.Compression" Condition="'$(TargetFramework)' == 'net472'" />
<PackageReference Include="System.ValueTuple" VersionOverride="4.5.0" Condition="'$(TargetFramework)' == 'net472'" ExcludeAssets="Compile" />
</ItemGroup>
Expand All @@ -55,6 +56,6 @@

<ItemGroup>
<None Include="PublicAPI\**" />
<AdditionalFiles Include="PublicAPI\$(TargetFramework)\PublicAPI.*.txt" />
<AdditionalFiles Include="PublicAPI\$(TargetFramework)\PublicAPI.*.txt" />
</ItemGroup>
</Project>
Loading