@@ -8,6 +8,15 @@ import {
88} from "../../languagePlugins/csharp/extractor/index.js" ;
99import { DependencyManifest } from "@nanoapi.io/shared" ;
1010import { DotNetProject } from "../../languagePlugins/csharp/projectMapper/index.js" ;
11+ import Parser from "tree-sitter" ;
12+ import { csharpParser } from "../../helpers/treeSitter/parsers.js" ;
13+ import { CSharpNamespaceMapper } from "../../languagePlugins/csharp/namespaceMapper/index.js" ;
14+ import { CSharpProjectMapper } from "../../languagePlugins/csharp/projectMapper/index.js" ;
15+ import {
16+ CSharpUsingResolver ,
17+ InternalSymbol ,
18+ ExternalSymbol ,
19+ } from "../../languagePlugins/csharp/usingResolver/index.js" ;
1120
1221/**
1322 * Extracts C# symbols from the given files.
@@ -27,6 +36,11 @@ export function extractCSharpSymbols(
2736 console . time ( `Extracted ${ symbolsToExtract . size } symbol(s)` ) ;
2837 const extractor = new CSharpExtractor ( files , dependencyManifest ) ;
2938 const extractedFiles : ExtractedFile [ ] = [ ] ;
39+ const parsedFiles = new Map <
40+ string ,
41+ { path : string ; rootNode : Parser . SyntaxNode }
42+ > ( ) ;
43+ const csprojFiles = new Map < string , { path : string ; content : string } > ( ) ;
3044 // Extract symbols from the files
3145 for ( const symbolSet of symbolsToExtract . values ( ) ) {
3246 for ( const symbol of symbolSet . symbols ) {
@@ -35,10 +49,65 @@ export function extractCSharpSymbols(
3549 symbol ,
3650 ) ;
3751 if ( extractedFile ) {
52+ // Add the extracted file to the list of extracted files
3853 extractedFiles . push ( ...extractedFile ) ;
54+ // Add the extracted file to the parsed files map
55+ // This is used to create a representation of the exported project
56+ // It will help us to find which namespaces cannot be used in using directives anymore
57+ for ( const file of extractedFile ) {
58+ const filePath = file . name ;
59+ if ( ! parsedFiles . has ( filePath ) ) {
60+ parsedFiles . set ( filePath , {
61+ path : filePath ,
62+ rootNode : csharpParser . parse ( extractor . getContent ( file ) ) . rootNode ,
63+ } ) ;
64+ }
65+ // Add the csproj file to the csproj files map
66+ const subproject = file . subproject ;
67+ if ( ! csprojFiles . has ( subproject . csprojPath ) ) {
68+ csprojFiles . set ( subproject . csprojPath , {
69+ path : subproject . csprojPath ,
70+ content : subproject . csprojContent ,
71+ } ) ;
72+ const globalUsingsPath = path . join (
73+ subproject . rootFolder ,
74+ "GlobalUsings.cs" ,
75+ ) ;
76+ const globalUsingsContent =
77+ extractor . generateGlobalUsings ( subproject ) ;
78+ if ( ! parsedFiles . has ( globalUsingsPath ) ) {
79+ parsedFiles . set ( globalUsingsPath , {
80+ path : globalUsingsPath ,
81+ rootNode : csharpParser . parse ( globalUsingsContent ) . rootNode ,
82+ } ) ;
83+ }
84+ }
85+ }
86+ }
87+ }
88+ }
89+ // For each extracted file, check if the using directives are still valid
90+ const nsMapper = new CSharpNamespaceMapper ( parsedFiles ) ;
91+ const pjMapper = new CSharpProjectMapper ( csprojFiles ) ;
92+ const usingResolver = new CSharpUsingResolver ( nsMapper , pjMapper ) ;
93+ for ( const extractedFile of extractedFiles ) {
94+ const imports = extractedFile . imports ;
95+ for ( const importDirective of imports ) {
96+ const resolvedInNewFile =
97+ usingResolver . resolveUsingDirective ( importDirective ) ;
98+ const resolvedInOldFile =
99+ extractor . usingResolver . resolveUsingDirective ( importDirective ) ;
100+ if (
101+ resolvedInNewFile instanceof ExternalSymbol &&
102+ resolvedInOldFile instanceof InternalSymbol
103+ ) {
104+ extractedFile . imports = extractedFile . imports . filter (
105+ ( imp ) => imp !== importDirective ,
106+ ) ;
39107 }
40108 }
41109 }
110+ // Actually extract the files
42111 const subprojects : DotNetProject [ ] = [ ] ;
43112 const extractedFilesMap : ExtractedFilesMap = new Map ( ) ;
44113 for ( const extractedFile of extractedFiles ) {
@@ -62,9 +131,11 @@ export function extractCSharpSymbols(
62131 `${ name } .cs` ,
63132 ) ;
64133 if ( ! extractedFilesMap . has ( key ) ) {
134+ const filecontent = extractor . getContent ( extractedFile ) ;
135+ // Add the extracted file to the output map
65136 extractedFilesMap . set ( key , {
66137 path : key ,
67- content : extractor . getContent ( extractedFile ) ,
138+ content : filecontent ,
68139 } ) ;
69140 }
70141 }
@@ -73,6 +144,7 @@ export function extractCSharpSymbols(
73144 const projectPath = path . join ( subproject . name , `${ subproject . name } .csproj` ) ;
74145 const globalUsingPath = path . join ( subproject . name , "GlobalUsings.cs" ) ;
75146 if ( ! extractedFilesMap . has ( projectPath ) ) {
147+ // Add the project files to the output map
76148 extractedFilesMap . set ( projectPath , {
77149 path : projectPath ,
78150 content : subproject . csprojContent ,
0 commit comments