-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (127 loc) · 4.53 KB
/
index.js
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
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var val = require('validator');
var replace = require("replaceall");
//Generate
exports.generate = function(dingle, directory){
directory = path.join(process.cwd(), directory);
//Hostname
var hostnames = '';
if (dingle.config.https.listen != '' && dingle.config.https.hostname != ''){
hostnames += 'hostnames["https"] = "' + dingle.config.https.hostname + ':' + dingle.config.https.port + '"\n';
}
if (dingle.config.http.listen != '' && dingle.config.http.hostname != '' ){
hostnames += 'hostnames["http"] = "' + dingle.config.http.hostname + ':' + dingle.config.http.port + '"\n';
}
if (dingle.config.tcp.listen != '' && dingle.config.tcp.hostname != '' ){
hostnames += 'hostnames["tcp"] = "' + dingle.config.tcp.hostname + ':' + dingle.config.tcp.port + '"\n';
}
if (dingle.config.udp.listen != '' && dingle.config.udp.hostname != '' ){
hostnames += 'hostnames["udp"] = "' + dingle.config.udp.hostname + ':' + dingle.config.udp.port + '"\n';
}
//Generate
var functions = '';
for (func in dingle.functions){
functions += generate(dingle.functions[func]) + '';
}
//Make
var filename = path.join(directory, dingle.config.app.prefix + '.swift');
var contents = fs.readFileSync(path.join(__dirname + '/request.swift')).toString();
contents = replace("<class>", dingle.config.app.prefix, contents);
contents = replace("<hostnames>", hostnames, contents);
contents = replace("<functions>", functions, contents);
//Write file
mkdirp(path.dirname(filename), function (error) {
if (error){
throw error;
}else{
fs.writeFile(filename, contents, function(error){
if (error){
throw error;
}else{
return;
}
});
}
});
}
//Each Call
function generate(func){
//Parameters
var param_method = [];
var param_calling = [];
for(var key in func.params) {
var obj = func.params[key];
//Compare
var kind = 'String';
try{
if (obj.validator('true') === true){ kind = 'Bool'; }
}catch(error){ }
try{
if (obj.validator('123') === 123){ kind = 'Int'; }
}catch(error){ }
try{
if (obj.validator('123.123') === 123.123){ kind = 'Double'; }
}catch(error){ }
var file = { path: '/test/', size: 1 }
try{
if (obj.validator(file) === file){ kind = 'NSURL'; }
}catch(error){ }
//Calling
if (param_calling.length == 0){
param_calling.push(key);
}else{
param_calling.push(key + ': ' + key);
}
//Optional
if (!obj.required){
param_method.push(key + ': ' + kind + '?');
}else{
param_method.push(key + ': ' + kind);
}
}
//Join
if (param_method.length != 0){
param_method = param_method.join(', ') + ',';
}
if (param_calling.length != 0){
param_calling = param_calling.join(', ') + ', methods: ';
}
//Parameters
var method_values = [];
for(var key in func.methods) { method_values.push(func.methods[key]); }
//Generate
var str = ' /**' + func.description + '*/\n';
str += ' func ' + func.name + '(' + param_method + ' callback: (success: Bool, message: String, output: JSON?)->()){\n';
str += ' ' + func.name + '(' + param_calling + ' [ "' + method_values.join('", "') + '" ], callback: callback, uploading: nil, downloading: nil, stream: nil)\n';
str += ' }\n';
str += ' /**' + func.description + '*/\n';
str += ' func ' + func.name + '(' + param_method + ' methods: Array<String>, callback: (success: Bool, message: String, output: JSON?)->()){\n';
str += ' ' + func.name + '(' + param_calling + ' methods, callback: callback, uploading: nil, downloading: nil, stream: nil)\n';
str += ' }\n';
str += ' /**' + func.description + '*/\n';
str += ' func ' + func.name + '(' + param_method + '\n';
str += ' methods: Array<String>,\n';
str += ' callback: (success: Bool, message: String, output: JSON?)->(),\n';
str += ' uploading: ((size: Double, remaining: Double, percentage: Double)->Void)?,\n';
str += ' downloading: ((size: Double, remaining: Double, percentage: Double)->Void)?, stream: NSOutputStream?){\n';
str += '\n';
str += ' //Parameters\n';
str += ' var params = [String : AnyObject]()\n';
for(var key in func.params) {
var obj = func.params[key];
if (!obj.required){
str += ' if let value = ' + key + '{\n';
str += ' params["' + key + '"] = value\n';
str += ' }\n';
}else{
str += ' params["' + key + '"] = ' + key + '\n';
}
}
str += '\n';
str += ' //Execute\n';
str += ' sendRequest("' + func.name + '", methods: methods, params: params, callback: callback, uploading: uploading, downloading: downloading, stream: stream)\n';
str += ' }\n';
return str;
}