-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
267 lines (241 loc) · 9.66 KB
/
project.cpp
File metadata and controls
267 lines (241 loc) · 9.66 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
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
260
261
262
263
264
265
266
267
#include "ccc/project.h"
#include "ccc/command.h"
#include "ccc/info.hpp"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace ccc;
namespace fs = filesystem;
static project ccc_project(
"CCC",
[](project* self, string, vector<string> args) {
// Set compiler flags
unordered_set<string> ccc_args(args.begin(), args.end());
if (ccc_args.find("release") != ccc_args.end()) {
self->add_compile_flags(
{"-O2", "-std=c++20", "-W", "-Wall", "-Wextra"});
} else {
self->add_compile_flags(
{"-g", "-std=c++20", "-W", "-Wall", "-Wextra"});
}
if (ccc_args.count("--noprint")) {
self->config.is_print = false;
}
/* Describe the executable program ccc. */
static execution ccc(
"ccc",
"The ccc does not rely on cccmain and cccunit, but its working "
"principle is to link project.cpp, cccmain, and cccunit in a "
"directory with project.cpp to generate and call project, and call "
"defautl_project in a directory without project.cpp.");
ccc.add_source_files({"./ccc/src/ccc.cpp"});
ccc.add_header_folder_paths({"./cccunit/inc", "./ccc/inc"});
self->add_task(&ccc);
/* Describe the library file cccunit. */
static library cccunit(
"cccunit", shared_library,
"The ccc project is the main component of the project, "
"defining how to describe the project when using ccc.");
cccunit.add_source_files({"./cccunit/src/"}, {".cpp"});
cccunit.add_header_folder_paths({"./cccunit/inc"});
self->add_task(&cccunit);
/* Describe the library file cccmain. */
static library cccmain("cccmain", static_library,
"The cccmain relies on cccunit and defines the "
"built-in commands of "
"ccc using the interfaces provided in cccunit.");
cccmain.add_source_files({"./cccmain/src/cccmain.cpp"});
cccmain.add_dependency(&cccunit, false);
self->add_task(&cccmain);
},
[](project*, string cmd, vector<string>) {
// Copy the cccunit/inc directory to the build/inc directory when
// running the build command.
if (cmd == "build" && fs::exists("./cccunit/inc") &&
fs::is_directory("./cccunit/inc")) {
// AI generated
function<void(const fs::path& source, const fs::path& destination)>
copy_directory = [&](const fs::path& source,
const fs::path& destination) -> void {
try {
// Check if the source path exists and is a directory
if (!fs::exists(source) || !fs::is_directory(source)) {
cerr << "Source directory does not exist or is not a "
"directory."
<< endl;
return;
}
// Create target directory
if (!fs::exists(destination)) {
fs::create_directories(destination);
}
// Traverse all entries in the source directory
for (const auto& entry : fs::directory_iterator(source)) {
const auto& path = entry.path();
const auto& filename = path.filename();
const auto dest_path = destination / filename;
if (fs::is_directory(path)) {
// If it is a directory, recursively copy
copy_directory(path, dest_path);
} else if (fs::is_regular_file(path)) {
// If it is a file, remove the existing file and
// copy the file
if (fs::exists(dest_path)) {
fs::remove(dest_path);
}
fs::copy_file(path, dest_path,
fs::copy_options::overwrite_existing);
}
}
} catch (const fs::filesystem_error& e) {
cerr << "Filesystem error: " << e.what() << endl;
} catch (const exception& e) {
cerr << "General error: " << e.what() << endl;
}
};
copy_directory("./cccunit/inc", "./build/inc");
}
// Clean the build/inc directory when running the clean command.
if (cmd == "clean") {
fs::remove_all("./build/inc");
}
},
"\n" + info::help_msg +
"Note: For the built-in commands provided above, you can "
"use them directly in any project.\n" +
"\nExtended commands:\n"
" debug Compile the ccc in debug mode.\n"
" release Compile the ccc in release mode.\n"
" help Print the help message about this "
"project(ccc).\n"
" line Print the number of lines of code for the "
"ccc project.\n"
"Extended arguments:\n"
" --noprint Don't generate any output when compile the "
"ccc.\n"
"Note: For the extended commands and arguments, you can use them in "
"the this project. But if you want to use them in other projects, you "
"need to achieve them by yourself.\n");
command debug_cmd(
"debug",
[](vector<string> args) {
cout << "Compile the ccc in debug mode." << endl;
std::string cmd = "ccc build debug";
for (size_t i = 0; i < args.size(); i++) {
cmd += " " + args[i];
}
#ifdef _WIN32
if (system(cmd.c_str()) != 0) {
exit(-1);
}
#endif
#ifdef __linux__
if (system(("bash -c '" + cmd + "'").c_str()) != 0) {
exit(-1);
}
#endif
},
"Compile the ccc in debug mode.");
command release_cmd(
"release",
[](vector<string> args) {
cout << "Compile the ccc in release mode." << endl;
std::string cmd = "ccc build release";
for (size_t i = 0; i < args.size(); i++) {
cmd += " " + args[i];
}
#ifdef _WIN32
if (system(cmd.c_str()) != 0) {
exit(-1);
}
#endif
#ifdef __linux__
if (system(("bash -c '" + cmd + "'").c_str()) != 0) {
exit(-1);
}
#endif
},
"Compile the ccc in release mode.");
command help_cmd(
"help",
[](vector<string> args) {
std::string cmd = "ccc desc CCC";
for (size_t i = 0; i < args.size(); i++) {
cmd += " " + args[i];
}
#ifdef _WIN32
system(cmd.c_str());
#endif
#ifdef __linux__
system(("bash -c '" + cmd + "'").c_str());
#endif
},
"Print the help message about this project(ccc).");
command line_cmd(
"line",
// AI generated
[](vector<string>) {
int total_lines = 0;
vector<string> directories = {"./ccc", "./cccmain", "./cccunit"};
unordered_set<string> valid_ext = {".cpp", ".h", ".hpp", ".c"};
auto count_lines = [&](const fs::path& path) {
ifstream file(path);
if (!file.is_open())
return 0;
int count = 0;
string line;
bool in_block_comment = false;
while (getline(file, line)) {
// Remove whitespace
line.erase(remove_if(line.begin(), line.end(), ::isspace),
line.end());
// Single-Line annotation
if (line.length() >= 2 && line[0] == '/' && line[1] == '/')
continue;
// Single-line block annotation
if (line.length() >= 4 && line[0] == '/' && line[1] == '*' &&
line[line.length() - 2] == '*' &&
line[line.length() - 1] == '/')
continue;
// Processing multi line block annotations
if (line.length() >= 2 && line[0] == '/' && line[1] == '*')
in_block_comment = true;
if (in_block_comment) {
if (line.length() >= 2 && line[line.length() - 2] == '*' &&
line[line.length() - 1] == '/')
in_block_comment = false;
continue;
}
if (!line.empty())
count++;
}
return count;
};
for (const auto& dir : directories) {
int dir_lines = 0;
cout << "Counting lines in: " << dir << endl;
for (const auto& entry : fs::recursive_directory_iterator(dir)) {
if (entry.is_regular_file() &&
valid_ext.count(entry.path().extension().string())) {
int lines = count_lines(entry.path());
dir_lines += lines;
cout << " " << entry.path().filename() << ": " << lines
<< endl;
}
}
cout << "Total for " << fs::path(dir).filename() << ": "
<< dir_lines << "\n\n";
total_lines += dir_lines;
}
cout << "================================\n";
cout << "Grand total lines of code: " << total_lines << endl;
},
"Print the number of lines of code for the ccc project.");