@@ -33,6 +33,7 @@ public static class SolutionCreator
3333
3434 /// <summary>
3535 /// Writes a solution file to the specified <paramref name="targetFile"/>.
36+ /// Also fixes intra-solution project references in the project files.
3637 /// </summary>
3738 /// <param name="targetFile">The full path of the file to write.</param>
3839 /// <param name="projects">The projects contained in this solution.</param>
@@ -164,30 +165,45 @@ static void FixProjectReferences(List<ProjectItem> projects)
164165 {
165166 XDocument projectDoc = XDocument . Load ( project . FilePath ) ;
166167
168+ if ( projectDoc . Root ? . Name . LocalName != "Project" )
169+ {
170+ throw new InvalidOperationException (
171+ $ "The file { project . FilePath } is not a valid project file, " +
172+ $ "no <Project> at the root; could not fix project references.") ;
173+ }
174+
175+ var sdkStyle = projectDoc . Root . Attribute ( "Sdk" ) != null ;
176+ var itemGroupTagName = sdkStyle ? "ItemGroup" : ProjectFileNamespace + "ItemGroup" ;
177+ var referenceTagName = sdkStyle ? "Reference" : ProjectFileNamespace + "Reference" ;
178+
167179 var referencesItemGroups = projectDoc . Root
168- . Elements ( ProjectFileNamespace + "ItemGroup" )
169- . Where ( e => e . Elements ( ProjectFileNamespace + "Reference" ) . Any ( ) ) ;
180+ . Elements ( itemGroupTagName )
181+ . Where ( e => e . Elements ( referenceTagName ) . Any ( ) )
182+ . ToList ( ) ;
170183
171184 foreach ( var itemGroup in referencesItemGroups )
172185 {
173- FixProjectReferences ( project . FilePath , itemGroup , projectsMap ) ;
186+ FixProjectReferences ( project . FilePath , itemGroup , projectsMap , sdkStyle ) ;
174187 }
175188
176189 projectDoc . Save ( project . FilePath ) ;
177190 }
178191 }
179192
180- private static void FixProjectReferences ( string projectFilePath , XElement itemGroup , IDictionary < string , ProjectItem > projects )
193+ static void FixProjectReferences ( string projectFilePath , XElement itemGroup ,
194+ Dictionary < string , ProjectItem > projects , bool sdkStyle )
181195 {
182- foreach ( var item in itemGroup . Elements ( ProjectFileNamespace + "Reference" ) . ToList ( ) )
196+ var referenceTagName = sdkStyle ? "Reference" : ProjectFileNamespace + "Reference" ;
197+
198+ foreach ( var item in itemGroup . Elements ( referenceTagName ) . ToList ( ) )
183199 {
184200 var assemblyName = item . Attribute ( "Include" ) ? . Value ;
185201 if ( assemblyName != null && projects . TryGetValue ( assemblyName , out var referencedProject ) )
186202 {
187203 item . Remove ( ) ;
188204
189205 var projectReference = new XElement ( ProjectFileNamespace + "ProjectReference" ,
190- new XElement ( ProjectFileNamespace + "Project" , referencedProject . Guid . ToString ( "B" ) . ToUpperInvariant ( ) ) ,
206+ new XElement ( ProjectFileNamespace + "Project" , referencedProject . Guid . ToString ( "B" ) . ToLowerInvariant ( ) ) ,
191207 new XElement ( ProjectFileNamespace + "Name" , referencedProject . ProjectName ) ) ;
192208 projectReference . SetAttributeValue ( "Include" , GetRelativePath ( projectFilePath , referencedProject . FilePath ) ) ;
193209
0 commit comments