forked from PlayFab/SDKGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
259 lines (214 loc) · 9.05 KB
/
generate.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
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
var fs = require("fs");
var path = require("path");
function stringStartsWith(value, prefix) {
return value.slice(0, prefix.length) === prefix;
}
function getTargetsList() {
var targetList = [];
var targetsDir = path.resolve(__dirname, "targets");
var targets = fs.readdirSync(targetsDir);
for (var i in targets) {
var target = targets[i];
if (target[0] === ".")
continue;
var targetSourceDir = path.resolve(targetsDir, target);
var targetMain = path.resolve(targetSourceDir, "make.js"); // search for make.js in each subdirectory within "targets"
if (fs.existsSync(targetMain))
targetList.push(target);
}
return targetList;
}
function generate(args) {
var targetList = getTargetsList();
var syntax = "Synatax: node generate.js <apiSpecLocation> <targetName>=<targetOutputLocation> [-beta]\n" +
"\t<apiSpecLocation> : Directory where the *.api.json files are\n" +
"\tYou must list one or more target=outputLocation arguments. Warning, there can be no spaces in the target-specification";
if (args.length < 3) { // If missing mandatory arguments, display syntax and exit
console.log(syntax);
console.log("Possible targetNames:");
console.log("\t" + targetList.join(", "));
process.exit();
}
var isBeta = false;
var targetOutputLocationList = []; // A list of objects that describe an sdk target
for (var i = 3; i < args.length; i++) {
if (stringStartsWith(args[i], "-t")) {
console.log("The -t option in SdkGenerator has been removed. The new Jenkins testing is more thorough and replaces this old functionality.");
console.log(syntax);
process.exit();
} else if (args[i] === "-beta") {
isBeta = true;
} else if (args[i].indexOf("=") !== -1) { // any parameter with an "=" is assumed to be a target specification
var argPair = args[i].split("=", 2);
var targetOutput = {};
targetOutput.name = argPair[0];
targetOutput.dest = path.normalize(argPair[1]);
if (fs.existsSync(targetOutput.dest) && !fs.lstatSync(targetOutput.dest).isDirectory()) {
console.log("Invalid target output path: " + targetOutput.dest);
process.exit();
}
targetOutputLocationList.push(targetOutput);
} else {
console.log("Cannot parse parameter: " + args[i]);
console.log(syntax);
process.exit();
}
}
var specLocation = path.normalize(args[2]);
var clientApi = GetApiDefinition(specLocation, "Client.api.json", isBeta);
var serverApis = [
GetApiDefinition(specLocation, "Admin.api.json", isBeta),
GetApiDefinition(specLocation, "Matchmaker.api.json", isBeta),
GetApiDefinition(specLocation, "Server.api.json", isBeta)
];
var allApis = serverApis.concat(clientApi);
console.log("Generating PlayFab APIs from specs at " + specLocation);
var targetsDir = path.resolve(__dirname, "targets");
for (var t in targetOutputLocationList) {
var target = targetOutputLocationList[t];
var sdkOutputDir = target.dest;
var targetSourceDir = path.resolve(targetsDir, target.name);
var targetMain = path.resolve(targetSourceDir, "make.js");
console.log("Making target " + target.name + " to location " + sdkOutputDir);
var targetMaker = require(targetMain);
// It would probably be better to pass these into the functions, but I don't want to change all the make___Api parameters for all projects today.
// For now, just change the global variables in each with the data loaded from SdkManualNotes.json
targetMaker.apiNotes = require(path.resolve(specLocation, "SdkManualNotes.json"));
targetMaker.sdkVersion = targetMaker.apiNotes.sdkVersion[target.name];
if (targetMaker.sdkVersion == null)
throw "SdkManualNotes does not contain sdkVersion for " + target.name; // The point of this error is to force you to add a line to sdkManualNotes.json, to describe the version and date when this sdk/collection is built
var apiOutputDir = "";
if (targetMaker.makeClientAPI) {
apiOutputDir = targetMaker.putInRoot ? sdkOutputDir : path.resolve(sdkOutputDir, "PlayFabClientSDK");
console.log(" + Generating Client to " + apiOutputDir);
if (!fs.existsSync(apiOutputDir))
mkdirParentsSync(apiOutputDir);
targetMaker.makeClientAPI(clientApi, targetSourceDir, apiOutputDir);
}
if (targetMaker.makeServerAPI) {
apiOutputDir = targetMaker.putInRoot ? sdkOutputDir : path.resolve(sdkOutputDir, "PlayFabServerSDK");
console.log(" + Generating Server to " + apiOutputDir);
if (!fs.existsSync(apiOutputDir))
mkdirParentsSync(apiOutputDir);
targetMaker.makeServerAPI(serverApis, targetSourceDir, apiOutputDir);
}
if (targetMaker.makeCombinedAPI) {
apiOutputDir = targetMaker.putInRoot ? sdkOutputDir : path.resolve(sdkOutputDir, "PlayFabSDK");
console.log(" + Generating Combined to " + apiOutputDir);
if (!fs.existsSync(apiOutputDir))
mkdirParentsSync(apiOutputDir);
targetMaker.makeCombinedAPI(allApis, targetSourceDir, apiOutputDir);
}
}
console.log("\n\nDONE!\n");
}
var GetApiDefinition = function (specLocation, apiFileName, isBeta) {
var api = require(path.resolve(specLocation, apiFileName));
if (isBeta)
return api; // Return the full API un-filtered
// Otherwise, filter all beta calls out of the API before returning it
var filteredCalls = [];
for (var a in api.calls)
if (!api.calls[a].hasOwnProperty("beta") || api.calls[a].beta !== true)
filteredCalls.push(api.calls[a]);
api.calls = filteredCalls;
return api;
}
GLOBAL.copyTree = function (source, dest) {
if (!fs.existsSync(source)) {
console.error("Copy tree source doesn't exist: " + source);
return;
}
if (fs.lstatSync(source).isDirectory()) {
if (!fs.existsSync(dest)) {
mkdirParentsSync(dest);
}
else if (!fs.lstatSync(dest).isDirectory()) {
console.error("Can't copy a directory onto a file: " + source + " " + dest);
return;
}
var filesInDir = fs.readdirSync(source);
for (var i in filesInDir) {
var filename = filesInDir[i];
var file = source + "/" + filename;
if (fs.lstatSync(file).isDirectory()) {
copyTree(file, dest + "/" + filename);
}
else {
copyFile(file, dest);
}
}
}
else {
copyFile(source, dest);
}
}
GLOBAL.copyFile = function (source, dest) {
if (!source || !dest) {
console.error("Invalid copy file parameters: " + source + " " + dest);
return;
}
if (!fs.existsSync(source)) {
console.error("copyFile source doesn't exist: " + source);
return;
}
var sourceStat = fs.lstatSync(source);
if (sourceStat.isDirectory()) {
console.error("copyFile source is a directory: " + source);
return;
}
var filename = path.basename(source);
if (fs.existsSync(dest)) {
if (fs.lstatSync(dest).isDirectory()) {
dest += "/" + filename;
}
}
else {
if (dest[dest.length - 1] === "/" || dest[dest.length - 1] === "\\") {
mkdirParentsSync(dest);
dest += filename;
}
else {
var dirname = path.dirname(dest);
mkdirParentsSync(dirname);
}
}
if (fs.existsSync(dest)) {
// TODO: Make this an optional flag
//if(fs.lstatSync(dest).mtime.getTime() >= sourceStat.mtime.getTime())
//{
// return;
//}
}
var bufLength = 64 * 1024;
var buff = new Buffer(bufLength);
var fdr = fs.openSync(source, "r");
var fdw = fs.openSync(dest, "w");
var bytesRead = 1;
var pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, buff, 0, bufLength, pos);
fs.writeSync(fdw, buff, 0, bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
fs.closeSync(fdw);
}
GLOBAL.mkdirParentsSync = function (dirname) {
if (fs.existsSync(dirname))
return;
var parentName = path.dirname(dirname);
mkdirParentsSync(parentName);
fs.mkdirSync(dirname);
}
GLOBAL.readFile = function (filename) {
return fs.readFileSync(filename, "utf8");
}
GLOBAL.writeFile = function (filename, data) {
var dirname = path.dirname(filename);
if (!fs.existsSync(dirname))
mkdirParentsSync(dirname);
return fs.writeFileSync(filename, data);
}
GLOBAL.ejs = require("ejs");
generate(process.argv);