Skip to content

Commit 9a83d3e

Browse files
authored
Changes to copy from installed extension if CPPTOOLS_DEV has been set. (#1388)
We still allow overrides for different package locations but this will use the vscode extension folder as a fallback if you have the extension installed to get the extension location.
1 parent 11509be commit 9a83d3e

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

Extension/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,10 @@
10181018
}
10191019
},
10201020
"scripts": {
1021-
"vscode:prepublish": "tsc -p ./ && node ./out/src/Debugger/copyScript.js",
1021+
"vscode:prepublish": "npm install && tsc -p ./ && node ./out/src/Debugger/copyScript.js",
10221022
"pretest": "tsc -p ./",
10231023
"test": "mocha -u tdd ./out/test",
1024-
"compile": "tsc -watch -p ./",
1024+
"compile": "npm run vscode:prepublish && tsc -watch -p ./",
10251025
"postinstall": "node ./node_modules/vscode/bin/install"
10261026
},
10271027
"devDependencies": {

Extension/src/Debugger/copyScript.ts

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@
99
*/
1010

1111
import * as fs from 'fs';
12+
import * as os from 'os';
1213
import * as path from 'path';
1314
import * as child_process from 'child_process';
1415

16+
//Change this to true to force a dev workflow.
17+
const EnableDevWorkflow: Boolean = false;
18+
19+
const DebugAdapterPath = "./debugAdapters"
20+
const DebugAdapterBinPath = DebugAdapterPath + "/bin";
21+
22+
var CpptoolsExtensionRoot: string = null;
23+
var SearchCompleted: Boolean = false;
24+
1525
interface RootsHashtable {
1626
"miEngineRoot": string;
1727
"openDebugRoot": string;
@@ -25,16 +35,46 @@ const internalBinaryRoots: RootsHashtable = {
2535
};
2636

2737
const externalBinaryRoots: RootsHashtable = {
28-
"miEngineRoot": "./node_modules/msvscode.cpptools.miengine",
29-
"openDebugRoot": "./node_modules/msvscode.cpptools.opendebugad7",
30-
"monoDeps": "./node_modules/msvscode.cpptools.monodeps"
38+
"miEngineRoot": DebugAdapterBinPath,
39+
"openDebugRoot": DebugAdapterBinPath,
40+
"monoDeps": DebugAdapterPath
3141
};
3242

33-
//Change this to true to force a dev workflow.
34-
const EnableDevWorkflow: Boolean = false;
43+
function findCppToolsExtensionDebugAdapterFolder(): string {
44+
const vscodeFolderRegExp = new RegExp(/\.vscode-*[a-z]*$/);
45+
const cpptoolsFolderRegExp = new RegExp(/ms\-vscode\.cpptools\-.*$/);
46+
47+
var dirPath: string = os.homedir();
48+
if (fs.existsSync(dirPath)) {
49+
var files = fs.readdirSync(dirPath);
50+
for (var i = 0; i < files.length; i++) {
51+
// Check to see if it starts with '.vscode'
52+
if (vscodeFolderRegExp.test(files[i])) {
53+
var extPath: string = path.join(dirPath, files[i], "extensions");
54+
if (fs.existsSync(extPath)) {
55+
var extFiles = fs.readdirSync(extPath);
56+
for (var j = 0; j < extFiles.length; j++) {
57+
if (cpptoolsFolderRegExp.test(path.join(extFiles[j]))) {
58+
dirPath = path.join(extPath, extFiles[j]);
59+
break;
60+
}
61+
}
62+
}
63+
}
64+
}
3565

36-
const DebugAdapterPath = "./debugAdapters"
37-
const DebugAdapterBinPath = DebugAdapterPath + "/bin";
66+
if (dirPath == os.homedir()) {
67+
console.error("Could not find installed C/C++ extension.");
68+
return null;
69+
}
70+
71+
return dirPath;
72+
}
73+
else {
74+
console.error("Unable to determine C/C++ extension installation location.")
75+
return null;
76+
}
77+
}
3878

3979
function enableDevWorkflow(): Boolean {
4080
if (process.env.AGENT_ID) {
@@ -51,7 +91,22 @@ function copySourceDependencies(): void {
5191

5292
function getRoot(rootKey: string): string {
5393
const internal = internalBinaryRoots[rootKey];
54-
return internal ? internal : externalBinaryRoots[rootKey];
94+
if (internal) {
95+
return internal;
96+
}
97+
98+
// Only search for the extension root once.
99+
if (!CpptoolsExtensionRoot && !SearchCompleted) {
100+
CpptoolsExtensionRoot = findCppToolsExtensionDebugAdapterFolder();
101+
SearchCompleted = true;
102+
}
103+
104+
if (CpptoolsExtensionRoot) {
105+
return path.join(CpptoolsExtensionRoot, externalBinaryRoots[rootKey]);
106+
}
107+
108+
console.error("Unable to determine internal/external location to copy from for root %s.", rootKey);
109+
return null;
55110
}
56111

57112
function copyBinaryDependencies(): void {
@@ -84,14 +139,26 @@ function copyMonoDependencies(): void {
84139
}
85140

86141
function copy(root: string, target: string, file: string): void {
142+
if (!root) {
143+
console.error("Unknown root location. Copy Failed for %s.", file);
144+
return;
145+
}
146+
87147
var source = path.join(root, file);
88148
var destination: string = path.join(target, file);
89149

90-
console.log('Creating directory %s', target);
91-
makeDirectory(target);
150+
if (!fs.existsSync(target)) {
151+
console.log('Creating directory %s', target);
152+
makeDirectory(target);
153+
}
92154

93155
console.log('copying %s to %s', source, destination);
94-
fs.writeFileSync(destination, fs.readFileSync(source));
156+
if (fs.existsSync(source)) {
157+
fs.writeFileSync(destination, fs.readFileSync(source));
158+
}
159+
else {
160+
console.error('ERR: could not find file %s', source);
161+
}
95162
}
96163

97164
function copyFolder(root: string, target: string): void {

0 commit comments

Comments
 (0)