Skip to content

Commit

Permalink
Added support for setting the target framework for projects with mult…
Browse files Browse the repository at this point in the history
…iple target frameworks, fixes #76
  • Loading branch information
maca88 committed Dec 30, 2017
1 parent 1dd41da commit 5b1cf67
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public interface IFluentProjectConfiguration
/// </summary>
IFluentProjectConfiguration ApplyChanges(bool value);

/// <summary>
/// Set the target framework that will be used to build the project. Use only when the project has multiple target frameworks.
/// </summary>
IFluentProjectConfiguration TargetFramework(string value);

IFluentProjectConfiguration ConfigureParsing(Action<IFluentProjectParseConfiguration> action);

IFluentProjectConfiguration ConfigureAnalyzation(Action<IFluentProjectAnalyzeConfiguration> action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface IFluentSolutionConfiguration
/// </summary>
IFluentSolutionConfiguration ApplyChanges(bool value);

/// <summary>
/// Set the target framework that will be used to build the solution. Use only when a project inside the solution has multiple target frameworks.
/// </summary>
IFluentSolutionConfiguration TargetFramework(string value);

/// <summary>
/// Add one or more regex patterns to suppress failures that occur while opening the solution. (e.g. failure for opening a not supported project inside a solution)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public interface IProjectConfiguration
IProjectCompileConfiguration CompileConfiguration { get; }

bool ConcurrentRun { get; }

string TargetFramework { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public interface ISolutionConfiguration
bool ConcurrentRun { get; }

bool ApplyChanges { get; }

string TargetFramework { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@
<xs:complexType name="Project">
<xs:all>
<xs:element name="ConcurrentRun" type="xs:boolean" nillable="true" minOccurs="0"/>
<xs:element name="TargetFramework" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="ApplyChanges" type="xs:boolean" nillable="true" minOccurs="0" />
<xs:element name="Analyzation" type="Analyzation" minOccurs="0"/>
<xs:element name="Transformation" type="Transformation" minOccurs="0"/>
Expand All @@ -416,8 +417,9 @@
<xs:complexType name="Solution">
<xs:all>
<xs:element name="ConcurrentRun" type="xs:boolean" nillable="true" minOccurs="0"/>
<xs:element name="TargetFramework" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="ApplyChanges" type="xs:boolean" nillable="true" minOccurs="0" />
<xs:element name="SuppressDiagnosticFailures">
<xs:element name="SuppressDiagnosticFailures" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Suppress" maxOccurs="unbounded" minOccurs="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public string FilePath
public bool? ConcurrentRun { get; set; }
[XmlElement(IsNullable = true)]
public bool? ApplyChanges { get; set; }
[XmlElement(IsNullable = true)]
public string TargetFramework { get; set; }
[XmlArrayItem(IsNullable = false)]
public List<Project> Projects { get; set; }
[XmlArrayItem("Suppress", IsNullable = false)]
Expand Down Expand Up @@ -86,6 +88,8 @@ public string FilePath
[XmlElement(IsNullable = true)]
public bool? ConcurrentRun { get; set; }
[XmlElement(IsNullable = true)]
public string TargetFramework { get; set; }
[XmlElement(IsNullable = true)]
public bool? ApplyChanges { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
Expand Down
11 changes: 10 additions & 1 deletion Source/AsyncGenerator.Core/FileConfiguration/FileConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public virtual void Configure(AsyncGenerator configuration, Solution solution, I
{
solutionConfiguration.ConcurrentRun(solution.ConcurrentRun.Value);
}
if (!string.IsNullOrEmpty(solution.TargetFramework))
{
solutionConfiguration.TargetFramework(solution.TargetFramework);
}

foreach (var item in solution.SuppressDiagnosticFailures)
{
Expand All @@ -48,6 +52,10 @@ public virtual void Configure(AsyncGenerator configuration, Project project, IFl
{
projectConfiguration.ConcurrentRun(project.ConcurrentRun.Value);
}
if (!string.IsNullOrEmpty(project.TargetFramework))
{
projectConfiguration.TargetFramework(project.TargetFramework);
}

foreach (var item in project.SuppressDiagnosticFailures)
{
Expand All @@ -64,7 +72,8 @@ public virtual void Configure(AsyncGenerator configuration, Project project, IFl
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(o => !o.IsDynamic)
.Distinct()
.ToDictionary(o => o.GetName().Name);
.GroupBy(o => o.GetName().Name) // there may be multiple versions of the same assembly
.ToDictionary(o => o.Key, o => o.First());

foreach (var plugin in project.RegisterPlugin)
{
Expand Down
25 changes: 25 additions & 0 deletions Source/AsyncGenerator.TestProjects/MultiTargetFrameworks.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiTargetFrameworks", "MultiTargetFrameworks\MultiTargetFrameworks.csproj", "{794C5BC3-7A73-4C7E-885F-085275A02B5B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{794C5BC3-7A73-4C7E-885F-085275A02B5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{794C5BC3-7A73-4C7E-885F-085275A02B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{794C5BC3-7A73-4C7E-885F-085275A02B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{794C5BC3-7A73-4C7E-885F-085275A02B5B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F5C3A8F-1BF9-4D5A-BA6B-47E6EA3D546E}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.7.1" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Source/AsyncGenerator.Tests/AsyncGenerator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
<Compile Include="SimpleClassInheritance\Input\TestCase.cs" />
<Compile Include="SimpleReference\Input\TestCase.cs" />
<Compile Include="SynhronizedMethod\Input\TestCase.cs" />
<Compile Include="TargetFramework\Fixture.cs" />
<Compile Include="TaskConflict\Fixture.cs" />
<Compile Include="TaskConflict\Input\Task.cs" />
<Compile Include="TaskConflict\Input\TestCase.cs" />
Expand Down
14 changes: 12 additions & 2 deletions Source/AsyncGenerator.Tests/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public static string GetBaseDirectory()
return AppDomain.CurrentDomain.BaseDirectory;
}

public static string GetTestProjectPath(string name)
{
return Path.GetFullPath(Path.Combine(GetBaseDirectory(), "..", "..", "..", "AsyncGenerator.TestProjects", name, $"{name}.csproj"));
}

public static string GetTestSolutionPath(string name)
{
return Path.GetFullPath(Path.Combine(GetBaseDirectory(), "..", "..", "..", "AsyncGenerator.TestProjects", $"{name}.sln"));
}

public async Task ReadonlyTest(Action<IFluentProjectConfiguration> action = null)
{
var configuration = Configure(action).ProjectConfigurations.Single();
Expand Down Expand Up @@ -223,14 +233,14 @@ protected string GetMethodName(LambdaExpression expression)
private static Microsoft.CodeAnalysis.Project OpenProject()
{
var filePath = Path.GetFullPath(Path.Combine(GetBaseDirectory(), "..", "..", "AsyncGenerator.Tests.csproj"));
var workspace = AsyncCodeGenerator.CreateWorkspace();
var workspace = AsyncCodeGenerator.CreateWorkspace(null);
return AsyncCodeGenerator.OpenProject(workspace, filePath, ImmutableArray<Predicate<string>>.Empty, Logger).GetAwaiter().GetResult();
}

private static Microsoft.CodeAnalysis.Solution OpenSolution()
{
var filePath = Path.GetFullPath(Path.Combine(GetBaseDirectory(), "..", "..", "..", "AsyncGenerator.sln"));
var workspace = AsyncCodeGenerator.CreateWorkspace();
var workspace = AsyncCodeGenerator.CreateWorkspace(null);
return AsyncCodeGenerator.OpenSolution(workspace, filePath, ImmutableArray<Predicate<string>>.Empty, Logger).GetAwaiter().GetResult();
}

Expand Down
135 changes: 135 additions & 0 deletions Source/AsyncGenerator.Tests/TargetFramework/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AsyncGenerator.Analyzation;
using AsyncGenerator.Configuration;
using AsyncGenerator.Core;
using AsyncGenerator.Core.Plugins;
using NUnit.Framework;

namespace AsyncGenerator.Tests.TargetFramework
{
[TestFixture]
public class Fixture : BaseFixture
{
[OneTimeSetUp]
public void Restore()
{
var process = new Process();
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = $"/C dotnet restore {GetTestSolutionPath("MultiTargetFrameworks")}"
};
process.StartInfo = startInfo;
process.Start();
}

[Test]
public Task TestSolution()
{
var config = AsyncCodeConfiguration.Create()
.ConfigureSolution(GetTestSolutionPath("MultiTargetFrameworks"), s => s
.TargetFramework("net461")
.ApplyChanges(false)
.ConfigureProject("MultiTargetFrameworks", p => p
.RegisterPlugin<NUnitAsyncCounterpartsFinder>()));

return AsyncCodeGenerator.GenerateAsync(config);
}

[Test]
public Task TestProject()
{
var config = AsyncCodeConfiguration.Create()
.ConfigureProject(GetTestProjectPath("MultiTargetFrameworks"), p => p
.TargetFramework("net461")
.ApplyChanges(false)
.RegisterPlugin<NUnitAsyncCounterpartsFinder>());

return AsyncCodeGenerator.GenerateAsync(config);
}

[Test]
public Task TestYamlSolution()
{
var config = ConfigureByYaml(
$@"
solution:
filePath: {GetTestSolutionPath("MultiTargetFrameworks")}
targetFramework: net461
applyChanges: false
projects:
- name: MultiTargetFrameworks
registerPlugin:
- type: AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder
assemblyName: AsyncGenerator.Core
");

return AsyncCodeGenerator.GenerateAsync(config);
}

[Test]
public Task TestYamlProject()
{
var config = ConfigureByYaml(
$@"
projects:
- filePath: {GetTestProjectPath("MultiTargetFrameworks")}
targetFramework: net461
applyChanges: false
registerPlugin:
- type: AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder
assemblyName: AsyncGenerator.Core
");

return AsyncCodeGenerator.GenerateAsync(config);
}

[Test]
public Task TestXmlSolution()
{
var config = ConfigureByXml(
$@"
<AsyncGenerator xmlns=""https://github.com/maca88/AsyncGenerator"">
<Solution filePath=""{GetTestSolutionPath("MultiTargetFrameworks")}"">
<ApplyChanges>false</ApplyChanges>
<TargetFramework>net461</TargetFramework>
<Projects>
<Project name=""MultiTargetFrameworks"">
<RegisterPlugin>
<Plugin type=""AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder"" assemblyName=""AsyncGenerator.Core"" />
</RegisterPlugin>
</Project>
</Projects>
</Solution>
</AsyncGenerator>
");

return AsyncCodeGenerator.GenerateAsync(config);
}

[Test]
public Task TestXmlProject()
{
var config = ConfigureByXml(
$@"
<AsyncGenerator xmlns=""https://github.com/maca88/AsyncGenerator"">
<Projects>
<Project filePath=""{GetTestProjectPath("MultiTargetFrameworks")}"">
<ApplyChanges>false</ApplyChanges>
<TargetFramework>net461</TargetFramework>
<RegisterPlugin>
<Plugin type=""AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder"" assemblyName=""AsyncGenerator.Core"" />
</RegisterPlugin>
</Project>
</Projects>
</AsyncGenerator>
");

return AsyncCodeGenerator.GenerateAsync(config);
}
}
}
10 changes: 7 additions & 3 deletions Source/AsyncGenerator/AsyncCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class AsyncCodeGenerator

foreach (var config in configuration.SolutionConfigurations)
{
var workspace = CreateWorkspace();
var workspace = CreateWorkspace(config.TargetFramework);
logger.Info($"Opening solution '{config.Path}' started");
var solution = await OpenSolution(workspace, config.Path,
config.SuppressDiagnosticFailuresPrediactes, logger, cancellationToken).ConfigureAwait(false);
Expand All @@ -65,7 +65,7 @@ public static class AsyncCodeGenerator

foreach (var config in configuration.ProjectConfigurations)
{
var workspace = CreateWorkspace();
var workspace = CreateWorkspace(config.TargetFramework);
logger.Info($"Opening project '{config.Path}' started");
var project = await OpenProject(workspace, config.Path,
config.SuppressDiagnosticFailuresPrediactes, logger, cancellationToken).ConfigureAwait(false);
Expand All @@ -87,12 +87,16 @@ public static class AsyncCodeGenerator
logger.Info("Generating async code completed");
}

internal static MSBuildWorkspace CreateWorkspace()
internal static MSBuildWorkspace CreateWorkspace(string targetFramework)
{
var props = new Dictionary<string, string>
{
["CheckForSystemRuntimeDependency"] = "true" // needed in order that project references are loaded
};
if (!string.IsNullOrEmpty(targetFramework))
{
props["TargetFramework"] = targetFramework;
}
return MSBuildWorkspace.Create(props);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private ProjectConfiguration()

public bool ConcurrentRun => _concurrentRun ?? (_solutionConfiguration?.ConcurrentRun ?? false);

public string TargetFramework { get; private set; }

public string Path { get; }

public string Name { get; }
Expand Down Expand Up @@ -76,6 +78,12 @@ IFluentProjectConfiguration IFluentProjectConfiguration.ApplyChanges(bool value)
return this;
}

IFluentProjectConfiguration IFluentProjectConfiguration.TargetFramework(string value)
{
TargetFramework = value ?? throw new ArgumentNullException(nameof(value));
return this;
}

IFluentProjectConfiguration IFluentProjectConfiguration.ConfigureParsing(Action<IFluentProjectParseConfiguration> action)
{
if (action == null)
Expand Down
Loading

0 comments on commit 5b1cf67

Please sign in to comment.