-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompiler.cpp
172 lines (136 loc) · 4.37 KB
/
Compiler.cpp
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
/*
* File: Compiler.cpp
* Author: Dreamszhu
*
* Created on September 16, 2014, 11:12 AM
*/
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include "parser.h"
#include "Compiler.h"
Compiler::Compiler() {
}
Compiler::Compiler(const path& app_path, const path& run_path) {
this->app_path = app_path;
this->run_path = run_path;
}
Compiler::~Compiler() {
}
bool Compiler::init(const std::string& ns) {
this->ext_namespace = ns;
this->run_path /= boost::to_lower_copy(this->ext_namespace);
if (!exists(this->run_path)) {
if (!create_directory(this->run_path)) {
std::cerr << "Failed to create directory " << this->run_path << ". Please check your folder permissions" << std::endl;
return false;
} else {
std::cout << "[OK]Success to create directory " << this->run_path << std::endl;
}
} else {
std::cout << "Already exists directory " << this->run_path << std::endl;
}
path ext_namespace_path = this->run_path / boost::to_lower_copy(this->ext_namespace);
if (!exists(ext_namespace_path)) {
if (!create_directory(ext_namespace_path)) {
std::cerr << "Failed to create directory " << ext_namespace_path << ". Please check your folder permissions" << std::endl;
return false;
} else {
std::cout << "[OK]Success to create directory " << ext_namespace_path << std::endl;
}
} else {
std::cout << "Already exists directory " << ext_namespace_path << std::endl;
}
path app_run_path = this->app_path / "ext";
if (!exists(app_run_path)) {
std::cerr << "Does not exist or is not a directory " << app_run_path << std::endl;
return false;
}
path ext_run_path = this->run_path / "ext";
if (!exists(ext_run_path)) {
if (!this->recursiveProcess(app_run_path, ext_run_path)) {
return false;
}
std::cout << "[OK]Success to create directory " << ext_run_path << std::endl;
} else {
std::cout << "Already exists directory " << ext_run_path << std::endl;
}
Json::Value config;
config["name"] = this->ext_namespace;
config["namespace"] = this->ext_namespace;
config["description"] = "";
config["author"] = "";
config["version"] = "0.0.1";
Json::FastWriter writer;
std::string config_file = writer.write(config);
path ext_config_path = this->run_path / "config.json";
std::ofstream ofs;
ofs.open(ext_config_path.generic_string());
ofs << config_file;
return true;
}
bool Compiler::generate() {
path ext_config_path = this->run_path / "config.json";
if (!exists(ext_config_path)) {
std::cerr << "Does not exist " << ext_config_path << std::endl;
return false;
}
std::ifstream ifs;
ifs.open(ext_config_path.generic_string());
Json::Reader reader;
if (!reader.parse(ifs, this->config, false)) {
std::cerr << "config.json is not valid or there is no Zephir extension initialized in this directory" << std::endl;
return false;
}
std::cout << this->config << std::endl;
return true;
}
Json::Value Compiler::parse( const std::string& filename) {
path full_path = this->run_path / filename;
if (!exists(full_path)) {
std::cerr << "Does not exist " << full_path << std::endl;
return Json::nullValue;
}
std::ifstream ifs;
ifs.open(full_path.generic_string());
std::ostringstream ost;
ost << ifs.rdbuf();
std::string program = ost.str();
return XX__parse_program((char *)program.c_str(), program.length(), (char *)filename.c_str());
}
bool Compiler::recursiveProcess(const path& source, const path& dest) {
try {
if (!exists(source) || !is_directory(source)) {
std::cerr << "Source directory " << source.string() << " does not exist or is not a directory." << std::endl;
return false;
}
if (exists(dest)) {
std::cerr << "Destination directory " << dest.string() << " already exists." << std::endl;
return false;
}
if (!create_directory(dest)) {
std::cerr << "Unable to create directory" << dest.string() << std::endl;
return false;
}
} catch (filesystem_error const & e) {
std::cerr << e.what() << std::endl;
return false;
}
for (directory_iterator file(source); file != directory_iterator(); ++file) {
try {
path current(file->path());
if (is_directory(current)) {
if (!this->recursiveProcess(current, dest / current.filename())) {
return false;
}
} else {
copy_file(current, dest / current.filename());
}
} catch (filesystem_error const & e) {
std::cerr << e.what() << std::endl;
return false;
}
}
return true;
}