-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.ts
More file actions
215 lines (203 loc) · 6.61 KB
/
Copy pathindex.ts
File metadata and controls
215 lines (203 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import type Parser from "tree-sitter";
import {
CSharpProjectMapper,
type DotNetProject,
} from "../projectMapper/index.ts";
import {
CSharpNamespaceMapper,
type SymbolNode,
} from "../namespaceMapper/index.ts";
import {
CSharpUsingResolver,
type UsingDirective,
} from "../usingResolver/index.ts";
import { csharpParser } from "../../../helpers/treeSitter/parsers.ts";
import type { DependencyManifest } from "../../../manifest/dependencyManifest/types.ts";
/**
* Represents an extracted file containing a symbol.
*/
export interface ExtractedFile {
/** The subproject to which the file belongs */
subproject: DotNetProject;
/** The namespace of the symbol */
namespace: string;
/** The symbol node */
symbol: SymbolNode;
/** The using directives in the file */
imports: UsingDirective[];
/** The name of the file */
name: string;
}
export class CSharpExtractor {
private manifest: DependencyManifest;
public projectMapper: CSharpProjectMapper;
private nsMapper: CSharpNamespaceMapper;
public usingResolver: CSharpUsingResolver;
constructor(
files: Map<string, { path: string; content: string }>,
manifest: DependencyManifest,
) {
this.manifest = manifest;
const csprojFiles = new Map<string, { path: string; content: string }>();
const parsedFiles = new Map<
string,
{ path: string; rootNode: Parser.SyntaxNode }
>();
for (const [filePath, file] of files) {
if (filePath.endsWith(".csproj")) {
csprojFiles.set(filePath, file);
} else if (filePath.endsWith(".cs")) {
parsedFiles.set(filePath, {
path: filePath,
rootNode: csharpParser.parse(file.content).rootNode,
});
}
}
this.projectMapper = new CSharpProjectMapper(csprojFiles);
this.nsMapper = new CSharpNamespaceMapper(parsedFiles);
this.usingResolver = new CSharpUsingResolver(
this.nsMapper,
this.projectMapper,
);
for (const [filePath] of parsedFiles) {
this.usingResolver.resolveUsingDirectives(filePath);
}
}
/**
* Finds all dependencies of a given symbol.
* @param symbol - The symbol for which to find dependencies.
* @returns An array of symbols.
*/
private findDependencies(symbol: SymbolNode): SymbolNode[] {
const dependencies: SymbolNode[] = [];
const symbolfullname = symbol.namespace !== ""
? symbol.namespace + "." + symbol.name
: symbol.name;
const symbolDependencies = this.manifest[symbol.filepath]
?.symbols[symbolfullname].dependencies;
if (symbolDependencies) {
for (const dependency of Object.values(symbolDependencies)) {
for (const depsymbol of Object.values(dependency.symbols)) {
const depsymbolnode = this.nsMapper.findClassInTree(
this.nsMapper.nsTree,
depsymbol,
);
if (depsymbolnode) {
dependencies.push(depsymbolnode);
}
}
}
}
return dependencies;
}
/**
* Finds all dependencies of a given symbol, and the dependencies of those dependencies.
* @param symbol - The symbol for which to find dependencies.
* @returns An array of symbols.
*/
private findAllDependencies(
symbol: SymbolNode,
visited: Set<SymbolNode> = new Set<SymbolNode>(),
): SymbolNode[] {
const allDependencies: Set<SymbolNode> = new Set<SymbolNode>();
if (visited.has(symbol)) {
return Array.from(allDependencies);
}
visited.add(symbol);
const dependencies = this.findDependencies(symbol);
for (const dependency of dependencies) {
allDependencies.add(dependency);
for (const dep of this.findAllDependencies(dependency, visited)) {
allDependencies.add(dep);
}
}
return Array.from(allDependencies);
}
/**
* Saves the extracted file containing a symbol into the filesystem.
* @param file - The extracted file to save.
*/
public getContent(file: ExtractedFile): string {
const usingDirectives = file.imports
.map((directive) => directive.node.text)
.join("\n");
const namespaceDirective = file.namespace !== ""
? `namespace ${file.namespace};`
: "";
const content =
`${usingDirectives}\n${namespaceDirective}\n${file.symbol.node.text}\n`;
return content;
}
/**
* Extracts a given symbol and its dependencies and returns them as files.
* @param symbol - The symbol to extract.
* @returns An array of extracted files.
*/
public extractSymbol(symbol: SymbolNode): ExtractedFile[] {
const subproject = this.projectMapper.findSubprojectForFile(
symbol.filepath,
);
if (!subproject) {
throw new Error(`Subproject not found for file: ${symbol.filepath}`);
}
const extractedFiles: ExtractedFile[] = [];
const visitedSymbols = new Set<string>();
const addExtractedFile = (symbol: SymbolNode) => {
// If the symbol is nested, we export the parent.
while (symbol.parent) {
symbol = symbol.parent;
}
if (!visitedSymbols.has(symbol.name)) {
visitedSymbols.add(symbol.name);
const subproject = this.projectMapper.findSubprojectForFile(
symbol.filepath,
);
if (subproject) {
const extractedFile: ExtractedFile = {
subproject,
namespace: symbol.namespace,
symbol,
imports: this.usingResolver.parseUsingDirectives(symbol.filepath),
name: symbol.name,
};
extractedFiles.push(extractedFile);
}
}
};
addExtractedFile(symbol);
const dependencies = this.findAllDependencies(symbol);
for (const dependency of dependencies) {
addExtractedFile(dependency);
}
return extractedFiles;
}
/**
* Extracts a symbol from a file by its name.
* @param filePath - The path to file that contains the symbol to extract
* @param symbolName - The name of the symbol to extract
* @returns A list of extracted files or undefined if the symbol is not found
*/
public extractSymbolFromFile(
filePath: string,
symbolName: string,
): ExtractedFile[] | undefined {
const fileExports = this.nsMapper.getFileExports(filePath);
const symbol = fileExports.find(
(symbol) =>
symbol.name === symbolName ||
symbol.namespace + "." + symbol.name === symbolName,
);
if (symbol) {
return this.extractSymbol(symbol);
}
return undefined;
}
public generateGlobalUsings(subproject: DotNetProject): string {
let content = "";
const directives = subproject.globalUsings.directives;
for (const directive of directives) {
content += `${directive.node.text}\n`;
}
return content;
}
}