11const path = require ( "path" ) ;
22const promisify = require ( "promisify-node" ) ;
33const fse = promisify ( require ( "fs-extra" ) ) ;
4+ const os = require ( 'os' ) ;
45const exec = require ( '../../utils/execPromise' ) ;
56const utils = require ( "./utils" ) ;
67
@@ -24,27 +25,27 @@ module.exports = function generateNativeCode() {
2425 } ;
2526
2627 var partials = {
27- asyncFunction : utils . readFile ( "templates/partials/async_function.cc" ) ,
28- callbackHelpers : utils . readFile ( "templates/partials/callback_helpers.cc" ) ,
29- convertFromV8 : utils . readFile ( "templates/partials/convert_from_v8.cc" ) ,
30- convertToV8 : utils . readFile ( "templates/partials/convert_to_v8.cc" ) ,
31- doc : utils . readFile ( "templates/partials/doc.cc" ) ,
32- fields : utils . readFile ( "templates/partials/fields.cc" ) ,
33- guardArguments : utils . readFile ( "templates/partials/guard_arguments.cc" ) ,
34- syncFunction : utils . readFile ( "templates/partials/sync_function.cc" ) ,
35- fieldAccessors : utils . readFile ( "templates/partials/field_accessors.cc" ) ,
36- traits : utils . readFile ( "templates/partials/traits.h" )
28+ asyncFunction : utils . readLocalFile ( "templates/partials/async_function.cc" ) ,
29+ callbackHelpers : utils . readLocalFile ( "templates/partials/callback_helpers.cc" ) ,
30+ convertFromV8 : utils . readLocalFile ( "templates/partials/convert_from_v8.cc" ) ,
31+ convertToV8 : utils . readLocalFile ( "templates/partials/convert_to_v8.cc" ) ,
32+ doc : utils . readLocalFile ( "templates/partials/doc.cc" ) ,
33+ fields : utils . readLocalFile ( "templates/partials/fields.cc" ) ,
34+ guardArguments : utils . readLocalFile ( "templates/partials/guard_arguments.cc" ) ,
35+ syncFunction : utils . readLocalFile ( "templates/partials/sync_function.cc" ) ,
36+ fieldAccessors : utils . readLocalFile ( "templates/partials/field_accessors.cc" ) ,
37+ traits : utils . readLocalFile ( "templates/partials/traits.h" )
3738 } ;
3839
3940 var templates = {
40- class_content : utils . readFile ( "templates/templates/class_content.cc" ) ,
41- struct_content : utils . readFile ( "templates/templates/struct_content.cc" ) ,
42- class_header : utils . readFile ( "templates/templates/class_header.h" ) ,
43- struct_header : utils . readFile ( "templates/templates/struct_header.h" ) ,
44- binding : utils . readFile ( "templates/templates/binding.gyp" ) ,
45- nodegitCC : utils . readFile ( "templates/templates/nodegit.cc" ) ,
46- nodegitJS : utils . readFile ( "templates/templates/nodegit.js" ) ,
47- enums : utils . readFile ( "templates/templates/enums.js" )
41+ class_content : utils . readLocalFile ( "templates/templates/class_content.cc" ) ,
42+ struct_content : utils . readLocalFile ( "templates/templates/struct_content.cc" ) ,
43+ class_header : utils . readLocalFile ( "templates/templates/class_header.h" ) ,
44+ struct_header : utils . readLocalFile ( "templates/templates/struct_header.h" ) ,
45+ binding : utils . readLocalFile ( "templates/templates/binding.gyp" ) ,
46+ nodegitCC : utils . readLocalFile ( "templates/templates/nodegit.cc" ) ,
47+ nodegitJS : utils . readLocalFile ( "templates/templates/nodegit.js" ) ,
48+ enums : utils . readLocalFile ( "templates/templates/enums.js" )
4849 } ;
4950
5051 var filters = {
@@ -99,51 +100,63 @@ module.exports = function generateNativeCode() {
99100 return ! idef . ignore ;
100101 } ) ;
101102
103+ const tempDirPath = path . join ( os . tmpdir ( ) , 'nodegit_build' ) ;
104+ const tempSrcDirPath = path . join ( tempDirPath , "src" ) ;
105+ const tempIncludeDirPath = path . join ( tempDirPath , "include" ) ;
102106
103- fse . remove ( path . resolve ( __dirname , "../../src" ) ) . then ( function ( ) {
104- return fse . remove ( path . resolve ( __dirname , "../../include" ) ) ;
105- } ) . then ( function ( ) {
106- return fse . copy ( path . resolve ( __dirname , "../templates/manual/include" ) , path . resolve ( __dirname , "../../include" ) ) ;
107+ const finalSrcDirPath = path . join ( __dirname , '../../src' ) ;
108+ const finalIncludeDirPath = path . join ( __dirname , '../../include' ) ;
109+
110+ fse . remove ( tempDirPath ) . then ( function ( ) {
111+ return fse . copy ( path . resolve ( __dirname , "../templates/manual/include" ) , tempIncludeDirPath ) ;
107112 } ) . then ( function ( ) {
108- return fse . copy ( path . resolve ( __dirname , "../templates/manual/src" ) , path . resolve ( __dirname , "../../src" ) ) ;
113+ return fse . copy ( path . resolve ( __dirname , "../templates/manual/src" ) , tempSrcDirPath ) ;
109114 } ) . then ( function ( ) {
110115 // Write out single purpose templates.
111- utils . writeFile ( "../binding.gyp" , beautify ( templates . binding . render ( enabled ) ) , "binding.gyp" ) ;
112- utils . writeFile ( "../src/ nodegit.cc", templates . nodegitCC . render ( enabled ) , "nodegit.cc" ) ;
113- utils . writeFile ( "../lib/nodegit.js" , beautify ( templates . nodegitJS . render ( enabled ) ) , "nodegit.js" ) ;
116+ utils . writeLocalFile ( "../binding.gyp" , beautify ( templates . binding . render ( enabled ) ) , "binding.gyp" ) ;
117+ utils . writeFile ( path . join ( tempSrcDirPath , " nodegit.cc") , templates . nodegitCC . render ( enabled ) , "nodegit.cc" ) ;
118+ utils . writeLocalFile ( "../lib/nodegit.js" , beautify ( templates . nodegitJS . render ( enabled ) ) , "nodegit.js" ) ;
114119 // Write out all the classes.
115120 enabled . forEach ( function ( idef ) {
116121 if ( idef . type && idef . type != "enum" ) {
117122 utils . writeFile (
118- "../src/" + idef . filename + ".cc" ,
123+ path . join ( tempSrcDirPath , idef . filename + ".cc" ) ,
119124 templates [ idef . type + "_content" ] . render ( idef ) ,
120125 idef . type + "_content.cc"
121126 ) ;
127+
122128 utils . writeFile (
123- "../include/" + idef . filename + ".h" ,
129+ path . join ( tempIncludeDirPath , idef . filename + ".h" ) ,
124130 templates [ idef . type + "_header" ] . render ( idef ) ,
125131 idef . type + "_header.h"
126132 ) ;
127133 }
128134 } ) ;
129135
130- utils . writeFile ( "../lib/enums.js" , beautify ( templates . enums . render ( enabled ) ) , "enums.js" ) ;
136+ utils . writeLocalFile ( "../lib/enums.js" , beautify ( templates . enums . render ( enabled ) ) , "enums.js" ) ;
131137 } ) . then ( function ( ) {
132138 return exec ( "command -v astyle" ) . then ( function ( astyle ) {
133139 if ( astyle ) {
134140 return exec (
135141 "astyle --options=\".astylerc\" "
136- + path . resolve ( __dirname , "../../src" ) + "/*.cc "
137- + path . resolve ( __dirname , "../../include" ) + "/*.h"
142+ + tempSrcDirPath + "/*.cc "
143+ + tempIncludeDirPath + "/*.h"
138144 ) . then ( function ( ) {
139145 return exec (
140146 "rm "
141- + path . resolve ( __dirname , "../../src" ) + "/*.cc.orig "
142- + path . resolve ( __dirname , "../../include" ) + "/*.h.orig "
147+ + tempSrcDirPath + "/*.cc.orig "
148+ + tempIncludeDirPath + "/*.h.orig "
143149 ) ;
144150 } ) ;
145151 }
146152 } , function ( ) { } )
153+ } ) . then ( function ( ) {
154+ return Promise . all ( [
155+ utils . syncDirs ( tempSrcDirPath , finalSrcDirPath ) ,
156+ utils . syncDirs ( tempIncludeDirPath , finalIncludeDirPath ) ,
157+ ] ) ;
158+ } ) . then ( function ( ) {
159+ return fse . remove ( tempDirPath ) ;
147160 } ) . catch ( console . log ) ;
148161
149162} ;
0 commit comments