99 */
1010
1111import * as fs from 'fs' ;
12+ import * as os from 'os' ;
1213import * as path from 'path' ;
1314import * 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+
1525interface RootsHashtable {
1626 "miEngineRoot" : string ;
1727 "openDebugRoot" : string ;
@@ -25,16 +35,46 @@ const internalBinaryRoots: RootsHashtable = {
2535} ;
2636
2737const 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 ( / \. v s c o d e - * [ a - z ] * $ / ) ;
45+ const cpptoolsFolderRegExp = new RegExp ( / m s \- v s c o d e \. c p p t o o l s \- .* $ / ) ;
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
3979function enableDevWorkflow ( ) : Boolean {
4080 if ( process . env . AGENT_ID ) {
@@ -51,7 +91,22 @@ function copySourceDependencies(): void {
5191
5292function 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
57112function copyBinaryDependencies ( ) : void {
@@ -84,14 +139,26 @@ function copyMonoDependencies(): void {
84139}
85140
86141function 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
97164function copyFolder ( root : string , target : string ) : void {
0 commit comments