Skip to content

Commit 7fee3d1

Browse files
maca88hazzik
authored andcommitted
fixed compilation errors on emit
1 parent b18327d commit 7fee3d1

File tree

5 files changed

+88
-59
lines changed

5 files changed

+88
-59
lines changed

src/NHibernate.AsyncGenerator/DocumentWriter.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.CodeAnalysis.FindSymbols;
1111
using NHibernate.AsyncGenerator.Extensions;
1212
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
13+
using static NHibernate.AsyncGenerator.Extensions.SyntaxHelper;
1314

1415
namespace NHibernate.AsyncGenerator
1516
{
@@ -634,6 +635,8 @@ private RewriteTypeResult RewriteType(TypeInfo rootTypeInfo, bool onlyMissingMem
634635
return result;
635636
}
636637

638+
639+
637640
public TransformDocumentResult Transform()
638641
{
639642
var result = new TransformDocumentResult();
@@ -720,27 +723,24 @@ public TransformDocumentResult Transform()
720723
var lockNamespace = Configuration.Async.Lock.Namespace;
721724
if (asyncLockUsed && rootNode.Usings.All(o => o.Name.ToString() != lockNamespace))
722725
{
723-
rootNode = rootNode.AddUsings(UsingDirective(IdentifierName(lockNamespace)));
726+
rootNode = rootNode.AddUsings(UsingDirective(NameSyntax(lockNamespace)));
724727
}
725728

726729
if (!taskConflict && rootNode.Usings.All(o => o.Name.ToString() != "System.Threading.Tasks"))
727730
{
728-
rootNode = rootNode.AddUsings(UsingDirective(IdentifierName("System.Threading.Tasks")));
731+
rootNode = rootNode.AddUsings(UsingDirective(NameSyntax("System.Threading.Tasks")));
729732
}
730733
if (tasksUsed && rootNode.Usings.All(o => o.Name.ToString() != "System"))
731734
{
732735
rootNode = rootNode.AddUsings( // using Exception = System.Exception
733-
UsingDirective(
734-
QualifiedName(
735-
IdentifierName("System"),
736-
IdentifierName("Exception")))
736+
UsingDirective(NameSyntax("System.Exception"))
737737
.WithAlias(
738738
NameEquals(
739739
IdentifierName("Exception"))));
740740
}
741741
if (tasksUsed && !string.IsNullOrEmpty(customTask.Namespace) && rootNode.Usings.All(o => o.Name.ToString() != customTask.Namespace))
742742
{
743-
rootNode = rootNode.AddUsings(UsingDirective(IdentifierName(customTask.Namespace)));
743+
rootNode = rootNode.AddUsings(UsingDirective(NameSyntax(customTask.Namespace)));
744744
}
745745

746746
if (!string.IsNullOrEmpty(Configuration.Directive))

src/NHibernate.AsyncGenerator/Extensions/SyntaxExtensions.Await.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.CodeAnalysis.Formatting;
1313
using Microsoft.CodeAnalysis.Simplification;
1414
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
15+
using static NHibernate.AsyncGenerator.Extensions.SyntaxHelper;
1516

1617
namespace NHibernate.AsyncGenerator.Extensions
1718
{
@@ -32,7 +33,21 @@ public static TypeDeclarationSyntax AddGeneratedCodeAttribute(this TypeDeclarati
3233
SeparatedList(
3334
new List<AttributeSyntax>
3435
{
35-
Attribute(IdentifierName("System.CodeDom.Compiler.GeneratedCode(\"AsyncGenerator\", \"1.0.0\")"))
36+
Attribute(NameSyntax("System.CodeDom.Compiler.GeneratedCode"))
37+
.WithArgumentList(
38+
AttributeArgumentList(
39+
SeparatedList<AttributeArgumentSyntax>(
40+
new SyntaxNodeOrToken[]{
41+
AttributeArgument(
42+
LiteralExpression(
43+
SyntaxKind.StringLiteralExpression,
44+
Literal("AsyncGenerator"))),
45+
Token(SyntaxKind.CommaToken),
46+
AttributeArgument(
47+
LiteralExpression(
48+
SyntaxKind.StringLiteralExpression,
49+
Literal("1.0.0")))
50+
})))
3651
}
3752
),
3853
Token(SyntaxKind.CloseBracketToken)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
8+
9+
namespace NHibernate.AsyncGenerator.Extensions
10+
{
11+
public static class SyntaxHelper
12+
{
13+
public static NameSyntax NameSyntax(string name)
14+
{
15+
var names = name.Split('.').ToList();
16+
if (names.Count < 2)
17+
{
18+
return IdentifierName(name);
19+
}
20+
var result = QualifiedName(IdentifierName(names[0]), IdentifierName(names[1]));
21+
for (var i = 2; i < names.Count; i++)
22+
{
23+
result = QualifiedName(result, IdentifierName(names[i]));
24+
}
25+
return result;
26+
}
27+
}
28+
}

src/NHibernate.AsyncGenerator/NHibernate.AsyncGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="Extensions\SyntaxExtensions.Async.cs" />
123123
<Compile Include="Extensions\SyntaxExtensions.cs" />
124124
<Compile Include="Extensions\SyntaxExtensions.Trivia.cs" />
125+
<Compile Include="Extensions\SyntaxHelper.cs" />
125126
<Compile Include="Extensions\SyntaxNodeExtensions.cs" />
126127
<Compile Include="MethodAnalyzeResult.cs" />
127128
<Compile Include="MethodInfo.cs" />

src/NHibernate.AsyncGenerator/Program.cs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.CodeAnalysis.CSharp.Syntax;
1414
using Microsoft.CodeAnalysis.FindSymbols;
1515
using Microsoft.CodeAnalysis.MSBuild;
16+
using Microsoft.CodeAnalysis.Text;
1617
using NHibernate.AsyncGenerator.Extensions;
1718
using Nito.AsyncEx;
1819

@@ -104,6 +105,7 @@ public async Task Generate(WriterConfiguration configuration)
104105
{
105106
continue;
106107
}
108+
107109
var config = projectConfigs[projectOrig.Name];
108110
var projectInfo = new ProjectInfo(this, projectOrig.Id, config);
109111
ProjectInfos.Add(projectInfo);
@@ -128,40 +130,23 @@ public async Task Generate(WriterConfiguration configuration)
128130
project = project.GetDocument(docInfo.Document.Id).WithSyntaxRoot(result.OriginalRootNode).Project;
129131
}
130132
var folders = new List<string> { projectInfo.Configuration.AsyncFolder }.Union(docInfo.Folders);
131-
project = project.AddDocument(docInfo.Name, result.NewRootNode, folders, $"{writer.DestinationFolder}\\{docInfo.Name}").Project;
133+
project = project.AddDocument(docInfo.Name, result.NewRootNode.GetText(Encoding.UTF8), folders, $"{writer.DestinationFolder}\\{docInfo.Name}").Project;
132134
Solution = project.Solution;
133135
}
134136

137+
//var origCompilation = await projectOrig.GetCompilationAsync();
138+
//var origEmit = origCompilation.Emit("output.exe", "output.pdb");
139+
//var origDiagnostincs = origEmit.Diagnostics;
140+
//var compilation = await project.GetCompilationAsync();
141+
//var emit = compilation.Emit("output2.exe", "output2.pdb");
142+
//var diagnostincs = emit.Diagnostics;
143+
////var diagnostincs = compilation.GetDeclarationDiagnostics();
144+
//foreach (var diagnostic in diagnostincs)
145+
//{
146+
//}
147+
135148
ProjectInfos.Clear();
136149
}
137-
138-
//foreach (var projectInfo in ProjectInfos)
139-
//{
140-
// var modifiedProject = projectInfo.RemoveGeneratedDocuments();
141-
// Solution = modifiedProject.Solution; // update solution as it is immutable
142-
// await projectInfo.Analyze().ConfigureAwait(false);
143-
// projectInfo.PostAnalyze();
144-
145-
// var project = projectInfo.Project;
146-
147-
// foreach (var pair in projectInfo.Where(o => o.Value.Any()))
148-
// {
149-
// var docInfo = pair.Value;
150-
// var writer = new DocumentWriter(docInfo, configuration);
151-
// var result = writer.Transform();
152-
// if (result == null)
153-
// {
154-
// continue;
155-
// }
156-
// if (result.OriginalRootNode != null)
157-
// {
158-
// project = project.GetDocument(docInfo.Document.Id).WithSyntaxRoot(result.OriginalRootNode).Project;
159-
// }
160-
// var folders = new List<string> { projectInfo.Configuration.AsyncFolder }.Union(docInfo.Folders);
161-
// project = project.AddDocument(docInfo.Name, result.NewRootNode, folders, $"{writer.DestinationFolder}\\{docInfo.Name}").Project;
162-
// Solution = project.Solution;
163-
// }
164-
//}
165150
Workspace.TryApplyChanges(Solution);
166151
}
167152
}
@@ -320,28 +305,28 @@ static void Main(string[] args)
320305
{
321306
ProjectConfigurations =
322307
{
323-
//new ProjectConfiguration("NHibernate")
324-
//{
325-
// ScanMethodsBody = true,
326-
// IgnoreExternalReferences = true,
327-
// MethodConversionFunc = symbol =>
328-
// {
329-
// if (symbol.GetAttributes().Any(a => a.AttributeClass.Name == "AsyncAttribute"))
330-
// {
331-
// return MethodAsyncConversion.ToAsync;
332-
// }
333-
// return MethodAsyncConversion.None;
334-
// },
335-
// FindAsyncCounterpart = findAsyncFn
336-
//},
337-
338-
//new ProjectConfiguration("NHibernate.DomainModel")
339-
//{
340-
// ScanMethodsBody = true,
341-
// IgnoreExternalReferences = true,
342-
// ScanForMissingAsyncMembers = true,
343-
// FindAsyncCounterpart = findAsyncFn
344-
//},
308+
new ProjectConfiguration("NHibernate")
309+
{
310+
ScanMethodsBody = true,
311+
IgnoreExternalReferences = true,
312+
MethodConversionFunc = symbol =>
313+
{
314+
if (symbol.GetAttributes().Any(a => a.AttributeClass.Name == "AsyncAttribute"))
315+
{
316+
return MethodAsyncConversion.ToAsync;
317+
}
318+
return MethodAsyncConversion.None;
319+
},
320+
FindAsyncCounterpart = findAsyncFn
321+
},
322+
323+
new ProjectConfiguration("NHibernate.DomainModel")
324+
{
325+
ScanMethodsBody = true,
326+
IgnoreExternalReferences = true,
327+
ScanForMissingAsyncMembers = true,
328+
FindAsyncCounterpart = findAsyncFn
329+
},
345330

346331
new ProjectConfiguration("NHibernate.Test")
347332
{

0 commit comments

Comments
 (0)