Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/script/setup/copySources.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'fs';
7
import * as path from 'path';
8
import { join } from 'path';
9
import * as ts from 'typescript';
10
const VS_ROOT = join(__dirname, '../../../vscode/src');
11
const TARGET = join(__dirname, '../../src/util/vs');
12
13
/**
14
* Returns the absolute file path where the given file should be placed.
15
*/
16
function determineTargetPath(absoluteVSCodeFilePath: string): string {
17
18
const vsRelative = path.relative(VS_ROOT, absoluteVSCodeFilePath);
19
20
const segements = vsRelative.split(path.sep);
21
22
if (segements[0] === 'typings' || segements[0] === 'vs') {
23
segements.shift();
24
}
25
26
return join(TARGET, segements.join(path.sep));
27
}
28
29
/**
30
* Returns the relative path of `importedFilePath` to `currentFilePath` in a format suitable for import statements.
31
*/
32
function createRelativeImportPath(currentFilePath: string, importedFilePath: string): string {
33
const relativePath = path.relative(path.dirname(currentFilePath), importedFilePath).replaceAll('\\', '/');
34
const result = relativePath.startsWith('.') ? relativePath : './' + relativePath;
35
return result.replace(/\.ts$/, '');
36
}
37
38
async function doIt(filepaths: string[]) {
39
try {
40
await fs.promises.access(VS_ROOT);
41
} catch {
42
console.error(`❌ VS Code root not found at ${VS_ROOT}`);
43
process.exit(1);
44
}
45
46
try {
47
await fs.promises.rm(join(TARGET), { recursive: true });
48
} catch {
49
// ignore
50
}
51
52
type Edit = ts.TextRange & { newText: string };
53
type File = { sourceFilePath: string; targetFilePath: string; contents: string };
54
type StackElement = { filepath: string; importTrajectory: string[] };
55
56
const seen = new Map<string, File>(); // indexed by sourceFilePath
57
const stack: StackElement[] = [...filepaths.map(p => ({ filepath: join(VS_ROOT, p), importTrajectory: [] }))];
58
59
while (stack.length > 0) {
60
const stackElement = stack.pop()!;
61
const importTrajectory = stackElement.importTrajectory.slice(0);
62
importTrajectory.push(stackElement.filepath);
63
64
let filepath = stackElement.filepath;
65
if (seen.has(filepath)) {
66
continue;
67
}
68
69
const edits: Edit[] = [];
70
let source: string = '';
71
try {
72
source = String(await fs.promises.readFile(filepath));
73
} catch (e) {
74
try {
75
// .ts doesn't exist, try, .d.ts
76
filepath = filepath.replace(/\.ts$/, '.d.ts');
77
source = String(await fs.promises.readFile(filepath));
78
} catch (e) {
79
console.error(`❌ Error reading file ${filepath}. Trajectory:\n${stackElement.importTrajectory.reverse().map(el => `- ${el}`).join('\n')}:`);
80
throw e;
81
}
82
}
83
84
const destinationFilePath = determineTargetPath(filepath);
85
const info = ts.preProcessFile(source, true, true);
86
for (const importedFile of info.importedFiles) {
87
88
let absolutePath: string | undefined;
89
if (importedFile.fileName.startsWith('.')) {
90
absolutePath = join(filepath, '..', importedFile.fileName.replace(/\.js$/, '.ts'));
91
} else if (importedFile.fileName.includes('/')) {
92
absolutePath = join(VS_ROOT, importedFile.fileName.replace(/\.js$/, '.ts'));
93
}
94
95
if (absolutePath) {
96
stack.push({ filepath: absolutePath, importTrajectory });
97
98
edits.push({
99
...importedFile,
100
newText: createRelativeImportPath(destinationFilePath, determineTargetPath(absolutePath)),
101
});
102
}
103
104
// console.log(`${filepath} <<<imports<<< ${absolutePath}`);
105
}
106
107
let newSource = source;
108
109
for (const edit of edits.sort((a, b) => b.pos - a.pos)) {
110
newSource = newSource.slice(0, edit.pos + 1) + edit.newText + newSource.slice(edit.end + 1);
111
}
112
113
newSource = '//!!! DO NOT modify, this file was COPIED from \'microsoft/vscode\'\n\n' + newSource;
114
115
seen.set(filepath, {
116
sourceFilePath: filepath,
117
targetFilePath: destinationFilePath,
118
contents: newSource
119
});
120
}
121
122
for (const [_, file] of seen) {
123
124
const targetFilepath = file.targetFilePath;
125
126
await fs.promises.mkdir(join(targetFilepath, '..'), { recursive: true });
127
await fs.promises.writeFile(targetFilepath, file.contents);
128
}
129
130
console.log(`✅ done, copied ${filepaths.length} files and ${seen.size - filepaths.length} dependencies`);
131
}
132
133
(async function () {
134
try {
135
await doIt([
136
// ********************************************
137
// add modules from `base` here and
138
// run `npx tsx script/setup/copySources.ts`
139
// ********************************************
140
'vs/base/common/arrays.ts',
141
'vs/base/common/async.ts',
142
'vs/base/common/cache.ts',
143
'vs/base/common/cancellation.ts',
144
'vs/base/common/charCode.ts',
145
'vs/base/common/date.ts',
146
'vs/base/common/errors.ts',
147
'vs/base/common/event.ts',
148
'vs/base/common/functional.ts',
149
'vs/base/common/glob.ts',
150
'vs/base/common/htmlContent.ts',
151
'vs/base/common/iconLabels.ts',
152
'vs/base/common/iterator.ts',
153
'vs/base/common/lifecycle.ts',
154
'vs/base/common/linkedList.ts',
155
'vs/base/common/map.ts',
156
'vs/base/common/numbers.ts',
157
'vs/base/common/objects.ts',
158
'vs/base/common/resources.ts',
159
'vs/base/common/strings.ts',
160
'vs/base/common/ternarySearchTree.ts',
161
'vs/base/common/themables.ts',
162
'vs/base/common/uri.ts',
163
'vs/base/common/uuid.ts',
164
'vs/base/common/yaml.ts',
165
'vs/editor/common/core/ranges/offsetRange.ts',
166
'vs/editor/common/core/wordHelper.ts',
167
'vs/editor/common/model/prefixSumComputer.ts',
168
169
'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.ts',
170
171
'vs/base/node/ports.ts',
172
173
'vs/platform/instantiation/common/instantiationService.ts',
174
'vs/editor/common/core/edits/lineEdit.ts',
175
'vs/editor/common/core/edits/lengthEdit.ts',
176
'vs/editor/common/core/edits/arrayEdit.ts',
177
'vs/editor/common/core/text/positionToOffset.ts',
178
'vs/editor/common/model/mirrorTextModel.ts',
179
180
'vs/workbench/api/common/extHostTypes/diagnostic.ts',
181
'vs/workbench/api/common/extHostTypes/location.ts',
182
'vs/workbench/api/common/extHostTypes/markdownString.ts',
183
'vs/workbench/api/common/extHostTypes/notebooks.ts',
184
'vs/workbench/api/common/extHostTypes/position.ts',
185
'vs/workbench/api/common/extHostTypes/range.ts',
186
'vs/workbench/api/common/extHostTypes/selection.ts',
187
'vs/workbench/api/common/extHostTypes/snippetString.ts',
188
'vs/workbench/api/common/extHostTypes/snippetTextEdit.ts',
189
'vs/workbench/api/common/extHostTypes/textEdit.ts',
190
'vs/workbench/api/common/extHostTypes/symbolInformation.ts',
191
'vs/workbench/api/common/extHostDocumentData.ts',
192
'vs/workbench/contrib/chat/common/promptSyntax/promptFileParser.ts',
193
194
'vs/base/common/sseParser.ts',
195
196
// SPECIAL IMPLICIT DEPENDENCIES
197
'typings/vscode-globals-nls.d.ts',
198
'typings/vscode-globals-product.d.ts',
199
'typings/base-common.d.ts',
200
'typings/crypto.d.ts',
201
]);
202
} catch (error) {
203
console.error(error);
204
}
205
})();
206
207