@@ -20,39 +20,34 @@ public class SolutionConverter
2020 private readonly ILanguageConversion _languageConversion ;
2121 private readonly SolutionFileTextEditor _solutionFileTextEditor ;
2222 private readonly CancellationToken _cancellationToken ;
23-
24- public static IFileSystem FileSystem { get ; set ; } = new FileSystem ( ) ;
25-
26- public static SolutionConverter CreateFor < TLanguageConversion > ( IReadOnlyCollection < Project > projectsToConvert , string sourceSolutionContents )
27- where TLanguageConversion : ILanguageConversion , new ( )
28- {
29- return CreateFor < TLanguageConversion > ( projectsToConvert , solutionContents : sourceSolutionContents ) ;
30- }
23+ private readonly TextReplacementConverter _textReplacementConverter ;
3124
3225 public static SolutionConverter CreateFor < TLanguageConversion > ( IReadOnlyCollection < Project > projectsToConvert ,
3326 ConversionOptions conversionOptions = default ,
3427 IProgress < ConversionProgress > progress = null ,
3528 CancellationToken cancellationToken = default ,
29+ IFileSystem fileSystem = null ,
3630 string solutionContents = "" ) where TLanguageConversion : ILanguageConversion , new ( )
3731 {
3832 var conversion = new TLanguageConversion { ConversionOptions = conversionOptions } ;
39- return CreateFor ( conversion , projectsToConvert , progress , cancellationToken , solutionContents ) ;
33+ return CreateFor ( conversion , projectsToConvert , progress , cancellationToken , fileSystem , solutionContents ) ;
4034 }
4135
4236 public static SolutionConverter CreateFor ( ILanguageConversion languageConversion , IReadOnlyCollection < Project > projectsToConvert ,
43- IProgress < ConversionProgress > progress ,
44- CancellationToken cancellationToken , string solutionContents = "" )
37+ IProgress < ConversionProgress > progress , CancellationToken cancellationToken , IFileSystem fileSystem = null , string solutionContents = "" )
4538 {
4639 languageConversion . ConversionOptions ??= new ConversionOptions ( ) ;
40+ fileSystem ??= new FileSystem ( ) ;
41+
4742 var solutionFilePath = projectsToConvert . First ( ) . Solution . FilePath ;
48- var sourceSolutionContents = File . Exists ( solutionFilePath )
49- ? FileSystem . File . ReadAllText ( solutionFilePath )
43+ var sourceSolutionContents = fileSystem . File . Exists ( solutionFilePath )
44+ ? fileSystem . File . ReadAllText ( solutionFilePath )
5045 : solutionContents ;
5146
5247 var projTuples = projectsToConvert . Select ( proj =>
5348 {
5449 var relativeProjPath = PathConverter . GetRelativePath ( solutionFilePath , proj . FilePath ) ;
55- var projContents = FileSystem . File . ReadAllText ( proj . FilePath ) ;
50+ var projContents = fileSystem . File . ReadAllText ( proj . FilePath ) ;
5651
5752 return ( proj . Name , RelativeProjPath : relativeProjPath , ProjContents : projContents ) ;
5853 } ) ;
@@ -61,15 +56,15 @@ public static SolutionConverter CreateFor(ILanguageConversion languageConversion
6156 var projectReferenceReplacements = solutionFileTextEditor . GetProjectFileProjectReferenceReplacements ( projTuples , sourceSolutionContents ) ;
6257
6358 return new SolutionConverter ( solutionFilePath , sourceSolutionContents , projectsToConvert , projectReferenceReplacements ,
64- progress ?? new Progress < ConversionProgress > ( ) , cancellationToken , languageConversion , solutionFileTextEditor ) ;
59+ progress ?? new Progress < ConversionProgress > ( ) , cancellationToken , languageConversion , solutionFileTextEditor , fileSystem ) ;
6560 }
6661
6762 private SolutionConverter ( string solutionFilePath ,
6863 string sourceSolutionContents , IReadOnlyCollection < Project > projectsToConvert ,
6964 List < ( string Find , string Replace , bool FirstOnly ) > projectReferenceReplacements ,
7065 IProgress < ConversionProgress > showProgressMessage ,
7166 CancellationToken cancellationToken , ILanguageConversion languageConversion ,
72- SolutionFileTextEditor solutionFileTextEditor )
67+ SolutionFileTextEditor solutionFileTextEditor , IFileSystem fileSystem )
7368 {
7469 _solutionFilePath = solutionFilePath ;
7570 _sourceSolutionContents = sourceSolutionContents ;
@@ -79,6 +74,7 @@ private SolutionConverter(string solutionFilePath,
7974 _languageConversion = languageConversion ;
8075 _cancellationToken = cancellationToken ;
8176 _solutionFileTextEditor = solutionFileTextEditor ;
77+ _textReplacementConverter = new TextReplacementConverter ( fileSystem ) ;
8278 }
8379
8480 public async IAsyncEnumerable < ConversionResult > Convert ( )
@@ -103,17 +99,20 @@ private IAsyncEnumerable<ConversionResult> ConvertProject(Project project, IEnum
10399 {
104100 var replacements = _projectReferenceReplacements . ToArray ( ) ;
105101 _progress . Report ( new ConversionProgress ( $ "Converting { project . Name } ...") ) ;
106- return ProjectConversion . ConvertProject ( project , _languageConversion , _progress , assembliesBeingConverted , _cancellationToken , replacements ) ;
102+ return ProjectConversion . ConvertProject ( project , _languageConversion , _textReplacementConverter , _progress , assembliesBeingConverted , _cancellationToken , replacements ) ;
107103 }
108104
109105 private IEnumerable < ConversionResult > UpdateProjectReferences ( IEnumerable < Project > projectsToUpdateReferencesOnly )
110106 {
111107 var conversionResults = projectsToUpdateReferencesOnly
112108 . Where ( p => p . FilePath != null ) //Some project types like Websites don't have a project file
113109 . Select ( project => {
114- var withReferencesReplaced =
115- new FileInfo ( project . FilePath ) . ConversionResultFromReplacements ( _projectReferenceReplacements ) ;
110+ var fileInfo = new FileInfo ( project . FilePath ) ;
111+
112+ var withReferencesReplaced =
113+ _textReplacementConverter . ConversionResultFromReplacements ( fileInfo , _projectReferenceReplacements ) ;
116114 withReferencesReplaced . TargetPathOrNull = withReferencesReplaced . SourcePathOrNull ;
115+
117116 return withReferencesReplaced ;
118117 } ) ;
119118
@@ -129,7 +128,7 @@ public ConversionResult ConvertSolutionFile()
129128 var slnProjectReferenceReplacements = _solutionFileTextEditor . GetSolutionFileProjectReferenceReplacements ( relativeProjPaths ,
130129 _sourceSolutionContents , projectTypeGuidMappings ) ;
131130
132- var convertedSolutionContents = _sourceSolutionContents . Replace ( slnProjectReferenceReplacements ) ;
131+ var convertedSolutionContents = _textReplacementConverter . Replace ( _sourceSolutionContents , slnProjectReferenceReplacements ) ;
133132 return new ConversionResult ( convertedSolutionContents ) {
134133 SourcePathOrNull = _solutionFilePath ,
135134 TargetPathOrNull = _solutionFilePath
0 commit comments