This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtomAPI.hx
296 lines (255 loc) · 8.4 KB
/
AtomAPI.hx
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
#if macro
import haxe.Json;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import sys.FileSystem;
import sys.io.File;
using StringTools;
using haxe.macro.TypeTools;
using haxe.macro.ComplexTypeTools;
@:require(haxe_ver >= 4.0)
class AtomAPI {
static function generate( apiFile = 'atom-api.json', destination = 'src', clean = false, addDocumentation = true ) {
if( !FileSystem.exists( apiFile ) )
Context.fatalError( 'API description file [$apiFile] not found', Context.currentPos() );
var json = Json.parse( File.getContent( apiFile ) ).classes;
var classes = [for(f in Reflect.fields(json)) Reflect.field(json,f) ];
var types = new Gen( ['atom'] ).process( classes );
types.push( {
pack: [],
name: 'Atom',
kind: TDAlias(macro:atom.AtomEnvironment),
fields: [],
pos: null,
#if (haxe_ver>=4)
doc: 'Alias for atom.AtomEnvironment'
#end
} );
if( clean ) rmdir( destination );
var printer = new haxe.macro.Printer();
for( type in types ) {
var code = printer.printTypeDefinition( type );
var dir = destination+'/'+type.pack.join('/');
if( !FileSystem.exists( dir ) ) FileSystem.createDirectory( dir );
File.saveContent( '$dir/${type.name}.hx', code );
}
cpdir( 'extra/types', destination ); // Copy extra types
}
static function cpdir( src : String, dst : String ) {
for( f in FileSystem.readDirectory( src ) ) {
var s = '$src/$f';
var d = '$dst/$f' ;
if( FileSystem.isDirectory( s ) ) {
if( !FileSystem.exists( d ) ) FileSystem.createDirectory( d );
cpdir( s, d );
} else File.copy( s, d );
}
}
static function rmdir( path : String ) {
if( FileSystem.exists( path ) ) {
for( e in FileSystem.readDirectory( path ) ) {
var p = '$path/$e';
FileSystem.isDirectory( p ) ? rmdir( p ) : FileSystem.deleteFile( p );
}
FileSystem.deleteDirectory( path );
}
}
}
private class Gen {
static var KWDS = ['class','switch'];
var root : Array<String>;
var addDocumentation : Bool;
var classes : Array<Dynamic>;
public function new( ?root : Array<String>, addDocumentation = true ) {
this.root = (root != null) ? root : [];
this.addDocumentation = addDocumentation;
}
public function process( classes : Array<Dynamic> ) {
this.classes = classes;
var types = new Array<TypeDefinition>();
types.push( { pack: ['atom'], name: 'Marker', kind: TDAlias(macro:Dynamic), fields: [], pos: null } );
for( cl in classes ) {
// TODO pre patch
switch cl.name {
case 'File':
for( m in (cast (cl.instanceMethods,Array<Dynamic>)) ) {
switch m.name {
case 'constructor': m.arguments[1].isOptional = true;
case 'getBaseName': m.returnValues = [{ type: 'String', description: m.description }];
case 'getParent': m.returnValues = [{ type: 'Directory', description: m.description }];
}
}
}
var sup = null;
//TODO Model is not defined
if( cl.superClass != null && cl.superClass != "Model" ) {
sup = { pack: root, name: cl.superClass };
}
var fields = new Array<Field>();
var isStaticField = (cl.name == 'AtomEnvironment');
for( f in (cast (cl.classProperties,Array<Dynamic>)) ) fields.push( createVarField( f, true ) );
for( f in (cast (cl.classMethods,Array<Dynamic>)) ) fields.push( createMethodField( f, true ) );
for( f in (cast (cl.instanceProperties,Array<Dynamic>)) ) fields.push( createVarField( f, isStaticField ) );
for( f in (cast (cl.instanceMethods,Array<Dynamic>)) ) fields.push( createMethodField( f, isStaticField ) );
var meta = new Array<MetadataEntry>();
switch cl.name {
case 'AtomEnvironment': meta.push( { name: ':native', params: [macro "atom"], pos: null } );
case _: meta.push( { name: ':jsRequire', params: [macro "atom",macro $v{cl.name}], pos: null } );
}
var doc = getDoc( cl.description );
if( doc == null ) doc = '';
doc = doc+'\n\n@see '+cl.srcUrl;
types.push( {
pack: root,
name: cl.name,
isExtern: true,
kind: TDClass( sup ),
fields: fields,
meta: meta,
pos: null,
#if (haxe_ver>=4) doc: doc, #end
} );
}
return types;
}
function createVarField( v, isStatic = false ) : Field {
var name = v.name;
var access = [];
if( isStatic ) access.push( AStatic );
//HACK to have types for Atom properties
var type : ComplexType;
//TODO really #?!
var expr = ~/^.*\{([A-Z][A-Za-z]+)\}.*$/;
if( expr.match( v.summary ) ) {
var n = expr.matched(1);
for( c in classes ) {
if( c.name == n ) {
type = TPath( { name: n, pack: root.copy() } );
break;
}
}
}
if( type == null ) type = macro : Dynamic;
return createField( name, FVar( type ), access, v.description );
}
function createMethodField( m, isStatic = false ) : Field {
var name = m.name;
var access = [];
var ret : ComplexType = null;
switch name {
case 'constructor':
name = 'new';
ret = macro : Void;
default:
if( isStatic ) access.push( AStatic );
ret = (m.returnValues == null) ? macro : Void : getComplexType( m.returnValues );
}
var args = new Array<FunctionArg>();
if( m.arguments != null ) {
for( arg in cast(m.arguments,Array<Dynamic>) ) {
var name : String = arg.name;
var type = getComplexType( arg.type, arg.children );
if( name.startsWith( '...' ) ) {
name = name.substr( 3 );
type = macro : haxe.extern.Rest<$type>;
}
args.push({ name: name, type: type, opt: arg.isOptional });
}
}
//TODO
if( m.titledArguments != null ) {
args.push({ name: 'rest', type: macro : haxe.extern.Rest<Dynamic> });
}
return createField( name, FFun( { args: args, expr: null, ret: ret } ), access, m.description );
}
function createField( name : String, kind : FieldType, ?access : Array<Access>, ?doc : String ) : Field {
if( KWDS.indexOf( name ) != -1 ) {
//TODO
trace("INVALID FIELD NAME "+name );
name = name+'_';
}
return { access: access, name: name, kind: kind, doc: getDoc( doc ), pos: null };
}
function getComplexType( type, ?children : Array<Dynamic>, isOptional = false ) : ComplexType {
var t : ComplexType = switch type {
//case null: macro : Void;
case null: macro : Dynamic;
case 'Array':
// TODO parse array type from description (?)
macro : Array<Any>;
case 'Boolean': macro : Bool;
case 'Double','Float','Number': macro : Float;
case 'Function':
//TODO type parameters
//trace(children);
macro : haxe.Constraints.Function;
case 'HTMLElement': macro : js.html.HtmlElement;
case 'Object':
//if( children == null || children.length == 0 ) macro : Any else {
if( children == null || children.length == 0 ) macro : Dynamic else {
var fields = new Array<Field>();
for( c in children ) {
//var _type = getComplexType( c.type, null, c.isOptional );
var _type = getComplexType( c.type );
var _name = c.name;
var meta = new Array<MetadataEntry>();
if( c.isOptional ) meta.push( { name: ':optional', pos: null } );
if( KWDS.indexOf( _name ) != -1 ) {
meta.push( { name: ':native', params: [macro $v{_name}], pos: null } );
_name = _name+'_';
}
fields.push({
name: _name,
kind: FVar( _type ),
meta: meta,
doc: getDoc( c.description ),
pos: null
});
}
TAnonymous( fields );
}
case 'Promise':
macro : js.lib.Promise<Any>;
case 'ReadStream': macro : js.node.fs.ReadStream;
case 'RegExp': macro : EReg;
case 'String': macro : String;
case 'WriteStream': macro : js.node.fs.WriteStream;
case _ if( Std.is( type, Array ) ):
var types : Array<Dynamic> = cast type;
if( types.length == 1 ) getComplexType( types[0].type ) else macro : Dynamic;
default:
/*
trace(type);
var has = false;
for( c in classes ) {
if( c.name == type ) {
has = true;
break;
}
}
if( !has ) {
Context.warning( '[$type] not found', Context.currentPos() );
}
*/
TPath( { pack: ["atom"], name: type } );
}
if( isOptional ) {
//trace(type);
//t = TOptional(t);
}
return t;
}
function getDoc( str : String ) : String {
if( !addDocumentation || str == null || str.length == 0 )
return null;
//TODO
var ereg = ~/\{[A-Z]+[a-z0-9_]*(::[a-zA-z]+)?\}/g;
return ereg.map( str, function(r){
var s = r.matched( 0 ).replace( '::', '.' );
s = s.substr( 1, s.length - 2 );
return '`$s`';
});
}
}
#end