@@ -9,6 +9,7 @@ std::unordered_map<std::string, size_t> Compiler::get_compiler_info()
9
9
{
10
10
return {{" version" , VERSION},
11
11
{" reversion" , REVISION},
12
+ {" patch" , PATCH},
12
13
{" compiledAt" , static_cast <size_t >(std::time (nullptr ))}};
13
14
}
14
15
@@ -62,35 +63,89 @@ nlohmann::json Compiler::output(const ScriptProcessor &processor)
62
63
return json_data;
63
64
}
64
65
65
- // compile the file
66
+ // compile the file/files
66
67
void Compiler::compile (const std::filesystem::path &path)
67
68
{
68
- compile (path, path. root_directory ());
69
+ compile (path, is_directory ( path) ? path : path. parent_path ());
69
70
}
70
71
71
72
void Compiler::compile (const std::filesystem::path &path, const std::filesystem::path &out_dir)
72
73
{
73
- if (!is_directory (path))
74
- {
75
- save (load_as_json (path), out_dir);
76
- } else
74
+ if (is_directory (path))
77
75
{
78
76
for (const auto &entry: std::filesystem::directory_iterator (path))
79
77
{
80
78
if (entry.path ().extension () == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
81
79
{
82
- compile (entry.path ());
80
+ compile_script (entry.path (), out_dir);
81
+ } else if (entry.is_directory ())
82
+ {
83
+ compile (entry.path (), out_dir / entry.path ().filename ());
84
+ }
85
+ }
86
+ } else if (path.extension () == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
87
+ {
88
+ compile_script (path, out_dir);
89
+ }
90
+ }
91
+
92
+ // compile a script file and save it to given output dir
93
+ void Compiler::compile_script (const std::filesystem::path &path, const std::filesystem::path &out_dir)
94
+ {
95
+ save (load_as_json (path), out_dir);
96
+ }
97
+
98
+ // compile the file/files using multithreading
99
+ void Compiler::parallel_compile (const std::filesystem::path &path)
100
+ {
101
+ parallel_compile (path, is_directory (path) ? path : path.parent_path ());
102
+ }
103
+
104
+ void Compiler::parallel_compile (const std::filesystem::path &path, const std::filesystem::path &out_dir)
105
+ {
106
+ std::vector<std::thread> tasks;
107
+ add_tasks (path, out_dir, tasks);
108
+ for (std::thread &task: tasks)
109
+ task.join ();
110
+ }
111
+
112
+ // create thread(s) that start compiling script(s)
113
+ void Compiler::add_tasks (
114
+ const std::filesystem::path &path, const std::filesystem::path &out_dir, std::vector<std::thread> &tasks)
115
+ {
116
+ if (is_directory (path))
117
+ {
118
+ for (const auto &entry: std::filesystem::directory_iterator (path))
119
+ {
120
+ const std::filesystem::path &entryPath = entry.path ();
121
+ if (entryPath.extension () == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
122
+ {
123
+ tasks.emplace_back ([entryPath, out_dir]() {
124
+ Compiler::compile_script (entryPath, out_dir);
125
+ });
126
+ } else if (entry.is_directory ())
127
+ {
128
+ add_tasks (entryPath, out_dir / entryPath.filename (), tasks);
83
129
}
84
130
}
131
+ } else if (path.extension () == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
132
+ {
133
+ tasks.emplace_back ([path, out_dir]() {
134
+ Compiler::compile_script (path, out_dir);
135
+ });
85
136
}
86
137
}
87
138
88
139
// save the json data into json
89
140
void Compiler::save (const nlohmann::json &json_data, const std::filesystem::path &dir_path)
90
141
{
91
142
std::stringstream file_name;
92
- std::string id = json_data.at (" id" );
93
- std::string lang = json_data.at (" language" );
143
+ const std::string id = json_data.at (" id" );
144
+ const std::string lang = json_data.at (" language" );
94
145
file_name << " chapter" << id << " _dialogs_" << lang << " .json" ;
146
+ // check output dir if it does not exist
147
+ if (!exists (dir_path))
148
+ create_directories (dir_path);
149
+ // save data
95
150
save_json (dir_path / file_name.str (), json_data);
96
151
}
0 commit comments