6
6
using System . Threading . Tasks ;
7
7
using ICSharpCode . CodeConverter . CSharp ;
8
8
using ICSharpCode . CodeConverter . Util ;
9
+ using ICSharpCode . CodeConverter . VB ;
9
10
using Microsoft . CodeAnalysis ;
11
+ using Microsoft . CodeAnalysis . CSharp ;
10
12
using Microsoft . CodeAnalysis . Formatting ;
11
13
using Microsoft . CodeAnalysis . Text ;
14
+ using Microsoft . CodeAnalysis . VisualBasic ;
12
15
13
16
namespace ICSharpCode . CodeConverter . Shared
14
17
{
@@ -24,18 +27,19 @@ public class ProjectConversion
24
27
private readonly ILanguageConversion _languageConversion ;
25
28
private readonly bool _handlePartialConversion ;
26
29
27
- private ProjectConversion ( Compilation sourceCompilation , string solutionDir , ILanguageConversion languageConversion )
28
- : this ( sourceCompilation , sourceCompilation . SyntaxTrees . Where ( t => t . FilePath . StartsWith ( solutionDir ) ) , languageConversion )
30
+ private ProjectConversion ( Compilation sourceCompilation , string solutionDir , ILanguageConversion languageConversion , Compilation convertedCompilation )
31
+ : this ( sourceCompilation , sourceCompilation . SyntaxTrees . Where ( t => t . FilePath . StartsWith ( solutionDir ) ) , languageConversion , convertedCompilation )
29
32
{
30
33
_solutionDir = solutionDir ;
31
34
}
32
35
33
- private ProjectConversion ( Compilation sourceCompilation , IEnumerable < SyntaxTree > syntaxTreesToConvert , ILanguageConversion languageConversion )
36
+ private ProjectConversion ( Compilation sourceCompilation , IEnumerable < SyntaxTree > syntaxTreesToConvert , ILanguageConversion languageConversion , Compilation convertedCompilation )
34
37
{
35
38
_languageConversion = languageConversion ;
36
39
this . _sourceCompilation = sourceCompilation ;
37
40
_syntaxTreesToConvert = syntaxTreesToConvert . ToList ( ) ;
38
41
_handlePartialConversion = _syntaxTreesToConvert . Count ( ) == 1 ;
42
+ languageConversion . Initialize ( convertedCompilation . RemoveAllSyntaxTrees ( ) ) ;
39
43
}
40
44
41
45
public static ConversionResult ConvertText < TLanguageConversion > ( string text , IReadOnlyCollection < MetadataReference > references ) where TLanguageConversion : ILanguageConversion , new ( )
@@ -54,7 +58,7 @@ public static async Task<ConversionResult> ConvertSingle(Compilation compilation
54
58
syntaxTree = annotatedSyntaxTree ;
55
59
}
56
60
57
- var conversion = new ProjectConversion ( compilation , new [ ] { syntaxTree } , languageConversion ) ;
61
+ var conversion = new ProjectConversion ( compilation , new [ ] { syntaxTree } , languageConversion , GetConvertedCompilation ( compilation , languageConversion ) ) ;
58
62
var conversionResults = ConvertProjectContents ( conversion ) . ToList ( ) ;
59
63
var codeResult = conversionResults . SingleOrDefault ( x => ! string . IsNullOrWhiteSpace ( x . ConvertedCode ) )
60
64
?? conversionResults . First ( ) ;
@@ -68,10 +72,28 @@ public static IEnumerable<ConversionResult> ConvertProjectContents(Project proje
68
72
var solutionFilePath = project . Solution . FilePath ;
69
73
var solutionDir = Path . GetDirectoryName ( solutionFilePath ) ;
70
74
var compilation = project . GetCompilationAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
71
- var projectConversion = new ProjectConversion ( compilation , solutionDir , languageConversion ) ;
75
+ var projectConversion = new ProjectConversion ( compilation , solutionDir , languageConversion , GetConvertedCompilation ( project . GetCompilationAsync ( ) . GetAwaiter ( ) . GetResult ( ) , languageConversion ) ) ;
72
76
foreach ( var conversionResult in ConvertProjectContents ( projectConversion ) ) yield return conversionResult ;
73
77
}
74
78
79
+ /// <summary>
80
+ /// If the source compilation has project references to a compilation of the same language, this will fail with an argument exception.
81
+ /// Use <see cref="GetConvertedCompilationWithProjectReferences"/> wherever this is possible.
82
+ /// </summary>
83
+ private static Compilation GetConvertedCompilation ( Compilation compilation , ILanguageConversion languageConversion )
84
+ {
85
+ return languageConversion is VBToCSConversion ? CSToVBConversion . CreateCSharpCompilation ( compilation . References ) : ( Compilation ) VBToCSConversion . CreateVisualBasicCompilation ( compilation . References ) ;
86
+ }
87
+
88
+ private static Compilation GetConvertedCompilationWithProjectReferences ( Project project , ILanguageConversion languageConversion )
89
+ {
90
+ return project . Solution . RemoveProject ( project . Id )
91
+ . AddProject ( project . Id , project . Name , project . AssemblyName , languageConversion . TargetLanguage )
92
+ . GetProject ( project . Id )
93
+ . WithProjectReferences ( project . AllProjectReferences ) . WithMetadataReferences ( project . MetadataReferences )
94
+ . GetCompilationAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
95
+ }
96
+
75
97
private static IEnumerable < ConversionResult > ConvertProjectContents ( ProjectConversion projectConversion )
76
98
{
77
99
foreach ( var pathNodePair in projectConversion . Convert ( ) )
0 commit comments