FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodegit.nodegit/generate/scripts/helpers.js at add-note-tests · javascript-forks/nodegit.nodegit · GitHub
javascript-forks
nodegit.nodegit
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodegit.nodegit
/
generate
/
scripts
/
helpers.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
379 lines (309 loc) · 11.3 KB
Breadcrumbs
nodegit.nodegit
/
generate
/
scripts
/
helpers.js
Copy path
File metadata and controls
379 lines (309 loc) · 11.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
var
callbackTypePattern
=
/
\s
*
_
c
b
/
;
var
utils
=
require
(
"./utils"
)
;
var
_
=
require
(
"lodash"
)
;
// TODO: When libgit2's docs include callbacks we should be able to remove this
var
version
=
require
(
"../../package.json"
)
.
libgit2
.
version
;
var
callbackDefs
=
require
(
"../input/callbacks.json"
)
;
var
descriptor
=
require
(
"../input/descriptor.json"
)
;
var
libgit2
=
require
(
"../input/v"
+
version
+
".json"
)
;
var
cTypes
=
libgit2
.
groups
.
map
(
function
(
group
)
{
return
group
[
0
]
;
}
)
;
var
cTypeMappings
=
{
"char"
:
"String"
,
"short"
:
"Number"
,
"int"
:
"Number"
,
"int16_t"
:
"Number"
,
"int32_t"
:
"Number"
,
"int64_t"
:
"Number"
,
"size_t"
:
"Number"
,
"uint16_t"
:
"Number"
,
"uint32_t"
:
"Number"
,
"uint64_t"
:
"Number"
}
var
collisionMappings
=
{
"new"
:
"create"
}
var
Helpers
=
{
normalizeCtype
:
function
(
cType
)
{
return
(
cType
||
""
)
.
toLowerCase
(
)
.
replace
(
"const "
,
""
)
.
replace
(
"unsigned "
,
""
)
.
replace
(
"struct"
,
""
)
.
replace
(
utils
.
doublePointerRegex
,
""
)
.
replace
(
utils
.
pointerRegex
,
""
)
.
trim
(
)
;
}
,
cTypeToCppName
:
function
(
cType
,
ownerType
)
{
var
normalizedType
=
Helpers
.
normalizeCtype
(
cType
)
;
if
(
ownerType
&&
normalizedType
!=
ownerType
)
{
normalizedType
=
normalizedType
.
replace
(
ownerType
,
""
)
;
}
return
cTypeMappings
[
normalizedType
]
||
utils
.
titleCase
(
normalizedType
)
;
}
,
cTypeToJsName
:
function
(
cType
,
ownerType
)
{
var
output
=
utils
.
camelCase
(
Helpers
.
cTypeToCppName
(
cType
,
ownerType
)
.
replace
(
/
^
G
i
t
/
,
""
)
)
;
var
mergedPrefixes
=
[
"from"
,
"by"
]
;
mergedPrefixes
.
forEach
(
function
(
prefix
)
{
var
reg
=
new
RegExp
(
"(^"
+
prefix
+
"|"
+
utils
.
titleCase
(
prefix
)
+
")([a-z]+)$"
)
;
output
=
output
.
replace
(
reg
,
function
(
all
,
prefixMatch
,
otherWord
)
{
return
prefixMatch
+
utils
.
titleCase
(
otherWord
)
;
}
)
;
}
)
;
output
=
output
.
replace
(
/
(
[
a
-
z
]
)
S
t
r
$
/
,
"$1String"
)
return
output
;
}
,
isConstructorFunction
:
function
(
cType
,
fnName
)
{
var
initFnName
=
cType
.
split
(
'_'
)
;
initFnName
.
splice
(
-
1
,
0
,
"init"
)
;
initFnName
=
initFnName
.
join
(
'_'
)
;
return
initFnName
===
fnName
;
}
,
hasConstructor
:
function
(
type
,
normalizedType
)
{
return
type
.
used
&&
type
.
used
.
needs
&&
type
.
used
.
needs
.
some
(
function
(
fnName
)
{
return
Helpers
.
isConstructorFunction
(
normalizedType
,
fnName
)
;
}
)
;
}
,
isCallbackFunction
:
function
(
cType
)
{
return
callbackTypePattern
.
test
(
cType
)
;
}
,
isPayloadFor
:
function
(
cbField
,
payloadName
)
{
return
payloadName
&&
~
payloadName
.
indexOf
(
"_payload"
)
&&
Helpers
.
isCallbackFunction
(
cbField
.
cType
)
&&
~
cbField
.
name
.
indexOf
(
payloadName
.
replace
(
"_payload"
,
""
)
)
;
}
,
getLibgitType
:
function
(
normalizedType
,
types
)
{
var
libgitType
;
types
.
some
(
function
(
type
)
{
if
(
type
[
0
]
===
normalizedType
)
{
libgitType
=
type
[
1
]
;
return
true
;
}
}
)
;
return
libgitType
;
}
,
processCallback
:
function
(
field
)
{
field
.
isCallbackFunction
=
true
;
if
(
callbackDefs
[
field
.
type
]
)
{
_
.
merge
(
field
,
callbackDefs
[
field
.
type
]
)
;
}
else
{
if
(
process
.
env
.
BUILD_ONLY
)
{
console
.
warn
(
"Couldn't find callback definition for "
+
field
.
type
)
;
}
}
}
,
processPayload
:
function
(
field
,
allFields
)
{
if
(
field
.
name
===
"payload"
)
{
field
.
payloadFor
=
"*"
;
field
.
globalPayload
=
true
;
field
.
isOptional
=
true
;
}
else
{
var
cbFieldName
;
allFields
.
some
(
function
(
cbField
)
{
if
(
Helpers
.
isPayloadFor
(
cbField
,
field
.
name
)
)
{
cbFieldName
=
cbField
.
name
;
return
true
;
}
}
)
;
if
(
cbFieldName
)
{
field
.
payloadFor
=
cbFieldName
;
field
.
isOptional
=
true
;
}
}
}
,
decorateLibgitType
:
function
(
type
,
types
,
enums
)
{
var
normalizedType
=
Helpers
.
normalizeCtype
(
type
.
cType
)
;
var
libgitType
=
Helpers
.
getLibgitType
(
normalizedType
,
types
)
;
if
(
libgitType
)
{
type
.
isLibgitType
=
true
;
type
.
isEnum
=
libgitType
.
type
===
"enum"
;
type
.
hasConstructor
=
Helpers
.
hasConstructor
(
type
,
normalizedType
)
;
// there are no enums at the struct level currently, but we still need to override function args
if
(
type
.
isEnum
)
{
type
.
cppClassName
=
"Number"
;
type
.
jsClassName
=
"Number"
;
if
(
enums
[
type
.
cType
]
)
{
type
.
isMask
=
enums
[
type
.
cType
]
.
isMask
||
false
}
}
// we don't want to overwrite the c type of the passed in type
_
.
merge
(
type
,
descriptor
.
types
[
normalizedType
.
replace
(
"git_"
,
""
)
]
||
{
}
,
{
cType
:
type
.
cType
}
)
;
}
}
,
decoratePrimaryType
:
function
(
typeDef
,
enums
)
{
var
typeDefOverrides
=
descriptor
.
types
[
typeDef
.
typeName
]
||
{
}
;
var
partialOverrides
=
_
.
omit
(
typeDefOverrides
,
[
"fields"
,
"functions"
]
)
;
typeDef
.
cType
=
typeDef
.
cType
||
null
;
typeDef
.
cppClassName
=
Helpers
.
cTypeToCppName
(
typeDef
.
cType
||
"git_"
+
typeDef
.
typeName
)
;
typeDef
.
jsClassName
=
utils
.
titleCase
(
Helpers
.
cTypeToJsName
(
typeDef
.
cType
||
"git_"
+
typeDef
.
typeName
)
)
;
typeDef
.
filename
=
typeDef
.
typeName
;
typeDef
.
isLibgitType
=
true
;
typeDef
.
dependencies
=
[
]
;
typeDef
.
fields
=
typeDef
.
fields
||
[
]
;
typeDef
.
fields
.
forEach
(
function
(
field
,
index
,
allFields
)
{
var
fieldOverrides
=
typeDefOverrides
.
fields
||
{
}
;
Helpers
.
decorateField
(
field
,
allFields
,
fieldOverrides
[
field
.
name
]
||
{
}
,
enums
)
;
}
)
;
typeDef
.
needsForwardDeclaration
=
typeDef
.
decl
===
typeDef
.
cType
;
var
normalizedType
=
Helpers
.
normalizeCtype
(
typeDef
.
cType
)
;
typeDef
.
hasConstructor
=
Helpers
.
hasConstructor
(
typeDef
,
normalizedType
)
;
typeDef
.
functions
=
(
typeDef
.
functions
)
.
map
(
function
(
fn
)
{
var
fnDef
=
libgit2
.
functions
[
fn
]
;
fnDef
.
cFunctionName
=
fn
;
return
fnDef
;
}
)
;
var
typeDefOverrides
=
descriptor
.
types
[
typeDef
.
typeName
]
||
{
}
;
var
functionOverrides
=
typeDefOverrides
.
functions
||
{
}
;
typeDef
.
functions
.
forEach
(
function
(
fnDef
)
{
Helpers
.
decorateFunction
(
fnDef
,
typeDef
,
functionOverrides
[
fnDef
.
cFunctionName
]
||
{
}
,
enums
)
;
}
)
;
_
.
merge
(
typeDef
,
partialOverrides
)
;
}
,
decorateField
:
function
(
field
,
allFields
,
fieldOverrides
,
enums
)
{
var
normalizeType
=
Helpers
.
normalizeCtype
(
field
.
type
)
;
field
.
cType
=
field
.
type
;
field
.
cppFunctionName
=
utils
.
titleCase
(
field
.
name
)
;
field
.
jsFunctionName
=
utils
.
camelCase
(
field
.
name
)
;
field
.
cppClassName
=
Helpers
.
cTypeToCppName
(
field
.
type
)
;
field
.
jsClassName
=
utils
.
titleCase
(
Helpers
.
cTypeToJsName
(
field
.
type
)
)
;
if
(
Helpers
.
isCallbackFunction
(
field
.
cType
)
)
{
Helpers
.
processCallback
(
field
)
;
var
argOverrides
=
fieldOverrides
.
args
||
{
}
;
field
.
args
=
field
.
args
||
[
]
;
field
.
args
.
forEach
(
function
(
arg
)
{
Helpers
.
decorateArg
(
arg
,
field
.
args
,
null
,
null
,
argOverrides
[
arg
.
name
]
||
{
}
,
enums
)
;
}
)
;
}
else
{
field
.
isCallbackFunction
=
false
;
Helpers
.
processPayload
(
field
,
allFields
)
;
if
(
field
.
payloadFor
)
{
return
;
}
}
Helpers
.
decorateLibgitType
(
field
,
libgit2
.
types
,
enums
)
;
_
.
merge
(
field
,
fieldOverrides
)
;
}
,
decorateArg
:
function
(
arg
,
allArgs
,
typeDef
,
fnDef
,
argOverrides
,
enums
)
{
var
type
=
arg
.
cType
||
arg
.
type
;
var
normalizedType
=
Helpers
.
normalizeCtype
(
type
)
;
arg
.
cType
=
type
;
arg
.
cppClassName
=
Helpers
.
cTypeToCppName
(
arg
.
cType
)
;
arg
.
jsClassName
=
utils
.
titleCase
(
Helpers
.
cTypeToJsName
(
arg
.
cType
)
)
;
Helpers
.
decorateLibgitType
(
arg
,
libgit2
.
types
,
enums
)
;
// Some arguments can be callbacks
if
(
Helpers
.
isCallbackFunction
(
type
)
)
{
Helpers
.
processCallback
(
arg
)
;
var
callBackArgOverrides
=
argOverrides
.
args
||
{
}
;
arg
.
args
=
arg
.
args
||
[
]
;
arg
.
args
.
forEach
(
function
(
argForCallback
)
{
Helpers
.
decorateArg
(
argForCallback
,
arg
.
args
,
null
,
null
,
callBackArgOverrides
[
argForCallback
.
name
]
||
{
}
,
enums
)
;
}
)
;
}
else
if
(
typeDef
&&
fnDef
)
{
Helpers
.
processPayload
(
arg
,
allArgs
)
;
if
(
arg
.
payloadFor
)
{
return
;
}
// Mark all of the args that are either returns or are the object
// itself and determine if this function goes on the prototype
// or is a constructor method.
arg
.
isReturn
=
arg
.
name
===
"out"
||
(
utils
.
isDoublePointer
(
arg
.
type
)
&&
normalizedType
==
typeDef
.
cType
)
;
arg
.
isSelf
=
utils
.
isPointer
(
arg
.
type
)
&&
normalizedType
==
typeDef
.
cType
;
if
(
arg
.
isReturn
&&
fnDef
.
return
&&
fnDef
.
return
.
type
===
"int"
)
{
fnDef
.
return
.
isErrorCode
=
true
;
fnDef
.
isAsync
=
true
;
}
if
(
arg
.
isReturn
&&
arg
.
isSelf
)
{
arg
.
isSelf
=
false
;
fnDef
.
isConstructorMethod
=
true
;
}
else
if
(
arg
.
isSelf
)
{
fnDef
.
isPrototypeMethod
=
true
;
}
}
_
.
merge
(
arg
,
argOverrides
)
;
}
,
decorateFunction
:
function
(
fnDef
,
typeDef
,
fnOverrides
,
enums
)
{
var
key
=
fnDef
.
cFunctionName
;
// if this is the free function for the class, make the ref on the class
// and then return since we don't want the free functions publicly
// available
if
(
key
==
typeDef
.
cType
+
"_free"
)
{
typeDef
.
freeFunctionName
=
key
;
//fnDef.ignore = true;
//return;
}
fnDef
.
cppFunctionName
=
Helpers
.
cTypeToCppName
(
key
,
"git_"
+
typeDef
.
typeName
)
;
fnDef
.
jsFunctionName
=
Helpers
.
cTypeToJsName
(
key
,
"git_"
+
typeDef
.
typeName
)
;
//fnDef.isAsync = false; // until proven otherwise
if
(
fnDef
.
cppFunctionName
==
typeDef
.
cppClassName
)
{
fnDef
.
cppFunctionName
=
fnDef
.
cppFunctionName
.
replace
(
"Git"
,
""
)
;
}
var
argOverrides
=
fnOverrides
.
args
||
{
}
;
fnDef
.
args
.
forEach
(
function
(
arg
)
{
Helpers
.
decorateArg
(
arg
,
fnDef
.
args
,
typeDef
,
fnDef
,
argOverrides
[
arg
.
name
]
||
{
}
,
enums
)
;
// if a function has any callbacks then it MUST be async
if
(
arg
.
isCallbackFunction
)
{
fnDef
.
isAsync
=
true
;
}
}
)
;
if
(
fnDef
.
return
)
{
Helpers
.
decorateArg
(
fnDef
.
return
,
fnDef
.
args
,
typeDef
,
fnDef
,
fnOverrides
.
return
||
{
}
,
enums
)
;
}
_
(
collisionMappings
)
.
forEach
(
function
(
newName
,
collidingName
)
{
if
(
fnDef
.
cppFunctionName
==
utils
.
titleCase
(
collidingName
)
)
{
fnDef
.
cppFunctionName
=
utils
.
titleCase
(
newName
)
;
}
if
(
fnDef
.
jsFunctionName
==
utils
.
camelCase
(
collidingName
)
)
{
fnDef
.
jsFunctionName
=
utils
.
camelCase
(
newName
)
;
}
}
)
.
value
(
)
;
if
(
"git_"
+
typeDef
.
typeName
==
fnDef
.
cFunctionName
)
{
fnDef
.
useAsOnRootProto
=
true
;
}
_
.
merge
(
fnDef
,
_
.
omit
(
fnOverrides
,
"args"
,
"return"
)
)
;
}
,
filterIgnored
:
function
(
arr
,
callback
)
{
if
(
!
arr
)
{
return
;
}
for
(
var
i
=
arr
.
length
-
1
;
i
>=
0
;
i
--
)
{
if
(
arr
[
i
]
.
ignore
)
{
arr
.
splice
(
i
,
1
)
;
}
else
if
(
callback
)
{
callback
(
arr
[
i
]
)
;
}
}
}
,
deleteProperties
:
function
(
obj
)
{
delete
obj
.
line
;
delete
obj
.
lineto
;
delete
obj
.
block
;
delete
obj
.
description
;
delete
obj
.
comments
;
delete
obj
.
tdef
;
delete
obj
.
decl
;
delete
obj
.
comments
;
delete
obj
.
argline
;
delete
obj
.
sig
;
}
,
filterDocumentation
:
function
(
idefs
)
{
Helpers
.
filterIgnored
(
idefs
,
function
(
idef
)
{
Helpers
.
deleteProperties
(
idef
)
;
Helpers
.
filterIgnored
(
idef
.
fields
,
Helpers
.
deleteProperties
)
;
Helpers
.
filterIgnored
(
idef
.
functions
,
function
(
fn
)
{
Helpers
.
deleteProperties
(
fn
)
;
Helpers
.
filterIgnored
(
fn
.
args
,
function
(
arg
)
{
Helpers
.
deleteProperties
(
arg
)
;
delete
arg
.
functions
;
}
)
;
}
)
;
}
)
;
}
}
;
module
.
exports
=
Helpers
;
Back
|
FazBrowse Home
|
New Git URL