@@ -59,12 +59,6 @@ string GetSystemAssemblyPathByName(string assemblyName)
5959 return System . IO . Path . Combine ( root , assemblyName ) ;
6060 }
6161 }
62- // based on http://code.fitness/post/2017/02/using-csharpscript-with-netstandard.html
63- public static string GetSystemAssemblyPathByName ( string assemblyName )
64- {
65- var root = System . IO . Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
66- return System . IO . Path . Combine ( root , assemblyName ) ;
67- }
6862
6963 private static readonly ImmutableArray < MetadataReference > References ;
7064
@@ -73,40 +67,7 @@ public static string GetSystemAssemblyPathByName(string assemblyName)
7367 private static readonly string VisualBasicDefaultExt = "vb" ;
7468 private static readonly string TestProjectName = "TestProject" ;
7569
76- /// <summary>
77- /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it.
78- /// </summary>
79- /// <param name="sources">Classes in the form of strings</param>
80- /// <param name="language">The language the source code is in</param>
81- /// <returns>A Tuple containing the Documents produced from the sources and their TextSpans if relevant</returns>
82- public static Document [ ] GetDocuments ( string [ ] sources , string language )
83- {
84- if ( language != LanguageNames . CSharp && language != LanguageNames . VisualBasic )
85- {
86- throw new ArgumentException ( "Unsupported Language" ) ;
87- }
88-
89- var project = CreateProject ( sources , language ) ;
90- var documents = project . Documents . ToArray ( ) ;
91-
92- if ( sources . Length != documents . Length )
93- {
94- throw new SystemException ( "Amount of sources did not match amount of Documents created" ) ;
95- }
96-
97- return documents ;
98- }
99-
100- /// <summary>
101- /// Create a Document from a string through creating a project that contains it.
102- /// </summary>
103- /// <param name="source">Classes in the form of a string</param>
104- /// <param name="language">The language the source code is in</param>
105- /// <returns>A Document created from the source string</returns>
106- public static Document CreateDocument ( string source , string language = LanguageNames . CSharp )
107- {
108- return CreateProject ( new [ ] { source } , language ) . Documents . First ( ) ;
109- }
70+ public static Document CreateDocument ( CsProjectArguments arguments ) => CreateProject ( arguments ) . Documents . First ( ) ;
11071
11172 /// <summary>
11273 /// Create a project using the inputted strings as sources.
@@ -115,27 +76,40 @@ public static Document CreateDocument(string source, string language = LanguageN
11576 /// <param name="language">The language the source code is in</param>
11677 /// <returns>A Project created out of the Documents created from the source strings</returns>
11778 public static Project CreateProject ( string [ ] sources , string language = LanguageNames . CSharp )
79+ {
80+ var arguments = new CsProjectArguments
81+ {
82+ Language = language ,
83+ Sources = sources ,
84+ TargetFramework = TargetFramework . Net6_0 ,
85+ } ;
86+ return CreateProject ( arguments ) . AddMetadataReferences ( References ) ;
87+ }
88+
89+ public static Project CreateProject ( CsProjectArguments arguments )
11890 {
11991 string fileNamePrefix = DefaultFilePathPrefix ;
120- string fileExt = language == LanguageNames . CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt ;
92+ string fileExt = arguments . Language is LanguageNames . CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt ;
12193
12294 var projectId = ProjectId . CreateNewId ( debugName : TestProjectName ) ;
12395
12496 var solution = new AdhocWorkspace ( )
12597 . CurrentSolution
126- . AddProject ( projectId , TestProjectName , TestProjectName , language )
127- . AddMetadataReferences ( projectId , References ) ;
98+ . AddProject ( projectId , TestProjectName , TestProjectName , arguments . Language ) ;
99+ foreach ( var package in arguments . PackageReferences )
100+ {
101+ solution = solution . AddPackageReference ( projectId , package ) ;
102+ }
103+
104+ solution = solution . AddTargetFrameworkReference ( projectId , arguments . TargetFramework ) ;
128105
129- int count = 0 ;
130- foreach ( var source in sources )
106+ for ( int i = 0 ; i < arguments . Sources . Length ; i ++ )
131107 {
132- var newFileName = fileNamePrefix + count + "." + fileExt ;
108+ var newFileName = fileNamePrefix + i + "." + fileExt ;
133109 var documentId = DocumentId . CreateNewId ( projectId , debugName : newFileName ) ;
134- solution = solution . AddDocument ( documentId , newFileName , SourceText . From ( source ) ) ;
135- count ++ ;
110+ solution = solution . AddDocument ( documentId , newFileName , SourceText . From ( arguments . Sources [ i ] ) ) ;
136111 }
137112 return solution . GetProject ( projectId ) ;
138113 }
139114 }
140115}
141-
0 commit comments