diff --git a/MapTo.sln b/MapTo.sln
index 1c5d946..1476ab5 100644
--- a/MapTo.sln
+++ b/MapTo.sln
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapTo", "MapTo\MapTo.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapToTests", "MapToTests\MapToTests.csproj", "{797DA57B-AC7E-468B-8799-44C5A574C0E3}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleApp", "TestConsoleApp\TestConsoleApp.csproj", "{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
diff --git a/MapTo/MapToGenerator.cs b/MapTo/MapToGenerator.cs
index 9f14e14..366faf7 100644
--- a/MapTo/MapToGenerator.cs
+++ b/MapTo/MapToGenerator.cs
@@ -1,5 +1,4 @@
-using System.Collections.Generic;
-using System.Linq;
+using System.Linq;
using MapTo.Extensions;
using MapTo.Models;
using Microsoft.CodeAnalysis;
@@ -7,6 +6,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace MapTo
{
+ [Generator]
public class MapToGenerator : ISourceGenerator
{
///
diff --git a/MapTo/Mapto.csproj b/MapTo/Mapto.csproj
index ff7bb1f..11d8a8f 100644
--- a/MapTo/Mapto.csproj
+++ b/MapTo/Mapto.csproj
@@ -1,17 +1,37 @@
+ MapTo
+ Generates mapping code between two types using Roslyn code generator.
+ true
+ true
+ NU5128
+ MapTo
+ https://github.com/mrtaikandi/mapto
+ false
+ $(Version)
+ true
+ https://github.com/mrtaikandi/mapto
+ snupkg
+ MapTo
netstandard2.0
- 9
enable
+ preview
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
+
+
+
+
diff --git a/MapTo/SourceProvider.cs b/MapTo/SourceProvider.cs
index ec11207..62d73d0 100644
--- a/MapTo/SourceProvider.cs
+++ b/MapTo/SourceProvider.cs
@@ -37,7 +37,7 @@ namespace MapTo
}
";
- context.AddSource("MapFromAttribute", SourceText.From(source, Encoding.UTF8));
+ context.AddSource("MapFromAttribute.g.cs", source);
}
internal static (string source, string hintName) GenerateSource(MapModel model)
@@ -81,7 +81,7 @@ namespace MapTo
// End namespace declaration
.AppendClosingBracket();
- return (builder.ToString(), $"{model.ClassName}.cs");
+ return (builder.ToString(), $"{model.ClassName}.g.cs");
}
private static StringBuilder GenerateUsings(this StringBuilder builder, MapModel model)
diff --git a/MapToTests/CSharpGenerator.cs b/MapToTests/CSharpGenerator.cs
index 8ca9356..22c262a 100644
--- a/MapToTests/CSharpGenerator.cs
+++ b/MapToTests/CSharpGenerator.cs
@@ -24,10 +24,10 @@ namespace MapToTests
internal static void ShouldBeSuccessful(this ImmutableArray diagnostics)
{
- Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {diagnostics.FirstOrDefault()?.GetMessage()}");
+ Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", diagnostics.Select(c => c.GetMessage()))}");
}
- internal static (Compilation compilation, ImmutableArray diagnostics) GetOutputCompilation(string source)
+ internal static (Compilation compilation, ImmutableArray diagnostics) GetOutputCompilation(string source, bool assertCompilation = false)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var references = AppDomain.CurrentDomain.GetAssemblies()
@@ -37,9 +37,12 @@ namespace MapToTests
var compilation = CSharpCompilation.Create("foo", new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
- // NB: Uncomment this line if you want to fail tests when the injected program isn't valid _before_ running generators
- // var compileDiagnostics = compilation.GetDiagnostics();
- // Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), "Failed: " + compileDiagnostics.FirstOrDefault()?.GetMessage());
+ if (assertCompilation)
+ {
+ // NB: fail tests when the injected program isn't valid _before_ running generators
+ var compileDiagnostics = compilation.GetDiagnostics();
+ Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", compileDiagnostics.Select(c => c.GetMessage()))}");
+ }
ISourceGenerator generator = new MapToGenerator();
var driver = CSharpGeneratorDriver.Create(generator);
diff --git a/MapToTests/Tests.cs b/MapToTests/Tests.cs
index 4489335..d9d0a26 100644
--- a/MapToTests/Tests.cs
+++ b/MapToTests/Tests.cs
@@ -66,9 +66,11 @@ namespace MapTo
{
// Arrange
const string source = @"
+using MapTo;
+
namespace Test
{
- [MapFrom(typeof(Baz)]
+ [MapFrom(typeof(Baz))]
public partial class Foo
{
@@ -113,7 +115,7 @@ namespace Test
const string source = @"
namespace Test
{
- [MapTo.MapFrom(typeof(Baz)]
+ [MapTo.MapFrom(typeof(Baz))]
public partial class Foo
{
@@ -248,6 +250,7 @@ using Bazaar;
{
var builder = new StringBuilder();
builder.AppendLine($@"
+{(includeAttributeNamespace ? string.Empty : "using MapTo;")}
namespace Test
{{
{(sourceClassNamespace != "Test" && !includeAttributeNamespace ? $"using {sourceClassNamespace};": string.Empty)}
diff --git a/TestConsoleApp/Data/Models/User.cs b/TestConsoleApp/Data/Models/User.cs
new file mode 100644
index 0000000..f9cdc05
--- /dev/null
+++ b/TestConsoleApp/Data/Models/User.cs
@@ -0,0 +1,13 @@
+namespace TestConsoleApp.Data.Models
+{
+ public class User
+ {
+ public int Id { get; set; }
+
+ public string FirstName { get; set; }
+
+ public string LastName { get; set; }
+
+ public string FullName => $"{FirstName} {LastName}";
+ }
+}
\ No newline at end of file
diff --git a/TestConsoleApp/Program.cs b/TestConsoleApp/Program.cs
new file mode 100644
index 0000000..6b0b98a
--- /dev/null
+++ b/TestConsoleApp/Program.cs
@@ -0,0 +1,13 @@
+using System;
+using TestConsoleApp.ViewModels;
+
+namespace TestConsoleApp
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ var userViewModel = User.From(new Data.Models.User());
+ }
+ }
+}
\ No newline at end of file
diff --git a/TestConsoleApp/TestConsoleApp.csproj b/TestConsoleApp/TestConsoleApp.csproj
new file mode 100644
index 0000000..103211f
--- /dev/null
+++ b/TestConsoleApp/TestConsoleApp.csproj
@@ -0,0 +1,11 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
diff --git a/TestConsoleApp/ViewModels/User.cs b/TestConsoleApp/ViewModels/User.cs
new file mode 100644
index 0000000..8415924
--- /dev/null
+++ b/TestConsoleApp/ViewModels/User.cs
@@ -0,0 +1,11 @@
+
+// using MapTo;
+
+namespace TestConsoleApp.ViewModels
+{
+ [MapTo.MapFrom(typeof(Data.Models.User))]
+ public partial class User
+ {
+ public string FirstName { get; }
+ }
+}
\ No newline at end of file