Repository navigation
Expand file tree
/
Copy pathEmitter.cpp
More file actions
119 lines (98 loc) · 2.79 KB
/
Copy pathEmitter.cpp
File metadata and controls
119 lines (98 loc) · 2.79 KB
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
#include "TSEmitter.h"
namespace metagen {
void TSLines::write(std::string value) {
std::string line;
for (int i = 0; i < indentLevel; i++) {
line += " ";
}
line += value;
lines.push_back(line);
}
void TSLines::newline() { lines.push_back(""); }
void TSLines::enter() { indentLevel++; }
void TSLines::exit() { indentLevel--; }
std::string TSFile::toString() {
std::string result;
result += "/// <reference types=\"@nativescript/objc-node-api\" />\n";
for (auto &import : imports) {
std::string line = "/// <reference path=\"";
line += import;
line += "\" />";
result += line + "\n";
}
result += "\n";
for (auto &line : code.lines) {
result += line + "\n";
}
return result;
}
void TSEmitter::resolveImports(TSFile &file) {
for (auto &name : file.classReferences) {
if (factory.classes.contains(name)) {
auto cls = factory.classes[name];
if (cls.framework != file.name) {
file.import("./" + cls.framework + ".d.ts");
}
}
}
}
void TSEmitter::ensureFile(std::string framework) {
if (!files.contains(framework)) {
files[framework] = TSFile(framework, &factory);
}
}
void TSEmitter::write() {
// Emit code
for (auto &var : factory.variables) {
ensureFile(var.second.framework);
files[var.second.framework].write(var.second);
}
for (auto &enm : factory.enums) {
ensureFile(enm.second.framework);
files[enm.second.framework].write(enm.second);
}
for (auto &strct : factory.structs) {
ensureFile(strct.second.framework);
files[strct.second.framework].write(strct.second);
}
for (auto &un : factory.unions) {
ensureFile(un.second.framework);
files[un.second.framework].write(un.second);
}
for (auto &func : factory.functions) {
ensureFile(func.framework);
files[func.framework].write(func);
}
for (auto &proto : factory.protocols) {
ensureFile(proto.second.framework);
files[proto.second.framework].write(proto.second);
}
for (auto &cls : factory.classes) {
if (cls.first.empty())
return;
ensureFile(cls.second.framework);
files[cls.second.framework].write(cls.second);
}
// Write files
for (auto &file : files) {
resolveImports(file.second);
auto name = outDir + "/" + file.second.name + ".d.ts";
auto f = std::fopen(name.c_str(), "w");
std::string code = file.second.toString();
std::fwrite(code.data(), 1, code.size(), f);
std::fclose(f);
}
// Write index.d.ts
{
std::string name = outDir + "/index.d.ts";
auto f = std::fopen(name.c_str(), "w");
for (auto file : files) {
std::string line = "/// <reference path=\"./";
line += file.second.name;
line += ".d.ts\" />";
std::fwrite(line.data(), 1, line.size(), f);
std::fwrite("\n", 1, 1, f);
}
}
}
} // namespace metagen