Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (142 loc) · 6.44 KB
/
Copy pathmain.cpp
File metadata and controls
152 lines (142 loc) · 6.44 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
#include <iostream>
#include <string>
#include <filesystem>
#include <fstream>
#include <locale.h>
#include "./modules/colors.h"
#include <vector>
#include <algorithm>
#include <cctype>
#ifndef VERSION
#define VERSION "v0.3.0"
void loopPath(const std::filesystem::path& path, std::string filter, bool& logs, std::vector <std::string>& filesDir) {
for (const auto &entry : std::filesystem::recursive_directory_iterator(path)) {
if (entry.is_directory() && entry.path().filename().string()[0] == '.') continue; // Ignorar arquivos ocultos ou de configs
std::filesystem::path ext = entry.path().extension();
if (std::filesystem::is_regular_file(entry) && (ext == filter || (filter == ".cpp" && ext == ".c++"))) {
if (logs) std::cout << Colors::CYAN << "- " << entry.path().string() << std::endl; // Formatação para Logs
filesDir.push_back(entry.path().string());
}
}
}
int help() {
std::cout << Colors::RED << "* " << Colors::DEFAULT << "Use example:\n\n"
<< Colors::BLUE << "-path " << Colors::DEFAULT << "= Project folder for your program\n"
<< Colors::BLUE << "-flags " << Colors::DEFAULT << "= Use other configuration flags\n"
<< Colors::BLUE << "-gcc " << Colors::DEFAULT << "or" << Colors::BLUE << " -g++" << Colors::DEFAULT << " = Build from C or C++ project application\n"
<< Colors::BLUE << "-logs " << Colors::DEFAULT << "= View the file search flow\n"
<< Colors::BLUE << "-name " << Colors::DEFAULT << "= Inside \"YourProgram\" write the name for your program.\n"
<< Colors::BLUE << "-run " << Colors::DEFAULT << "= Execute the program automatically after a successful build.\n"
<< Colors::BLUE << "-fast " << Colors::DEFAULT << "= Apply -O3 and -march=native flags for maximum performance.\n"
<< Colors::BLUE << "-version " << Colors::DEFAULT << "= Returns the current version.\n" << std::endl;
// << Colors::BLUE << "-cbignore " << Colors::DEFAULT << "= Create a file called "<< Colors::YELLOW << "\"cbignore\"" << Colors::DEFAULT << " which will contain everything that should not be read." << std::endl;
return 1;
}
int main(int argCout, char* argVetor[]) {
bool logsOpc = false;
std::filesystem::path current_path = std::filesystem::current_path();
bool runOpc = false;
std::string otherFlags = "";
std::string code = "gcc ";
std::string filter = ".c";
std::vector <std::string> filesDir;
#ifdef __linux__
// std::setlocale(LC_ALL, "pt_BR.UTF-8");
std::string nameOpc = "main";
std::string runCmd = "./";
#elif _WIN32
// std::setlocale(LC_ALL, "Portuguese_Brazil.1252");
system("@echo off");
std::string nameOpc = "main";
std::string runCmd = ".\\";
#else
#error "OS not supported!"
#endif
if (argCout < 2) help();
for (int c = 1; c < argCout; c++) { // Indice atual das flags
std::string args(argVetor[c]);
std::transform(args.begin(), args.end(), args.begin(), [](unsigned char ch) { return std::tolower(ch); });
if (args == "-version") {
std::cout << "Current version: " << Colors::YELLOW << VERSION << Colors::DEFAULT << std::endl;
return 0;
}
if (args == "-run") runOpc = true;
if (args == "-help") return help();
if (args == "-path") {
if (++c < argCout) {
try { current_path = std::string(argVetor[++c]); }
catch(const std::exception& err) {
// std::cout << Colors::RED << "Error: -path\n" << Colors::DEFAULT << err.what();
std::cout << Colors::RED << "Error: -path\n" << Colors::DEFAULT << std::endl;
return 1;
}
} else {
std::cout << Colors::RED << "Error: -path \"C:\\your\\path\"" << Colors::DEFAULT << std::endl;
return 1;
}
};
if (args == "-logs") logsOpc = true;
// if (args == "-gcc") {
// files = "gcc ";
// filter = ".c";
// }
if (args == "-g++") {
code = "g++ ";
filter = ".cpp";
}
if (args == "-fast") otherFlags += "-O3 -march=native ";
if (args == "-flags") {
if (argCout) {}
try { otherFlags += std::string(argVetor[++c])+" "; }
catch(const std::exception& err) {
// std::cerr << e.what() << '\n';
std::cout << Colors::RED << "Error: -flags \"-example -example2\"" << Colors::DEFAULT << std::endl;
return 1;
}
};
if (args == "-name") {
try { nameOpc = std::string(argVetor[++c]); }
catch(const std::exception& e) {
// std::cerr << e.what() << '\n';
std::cout << Colors::RED << "Error: -name \"YourProgram\"" << Colors::DEFAULT << std::endl;
return 1;
}
};
}
// Ordenando a formação do Running "code +="
if (filter == ".c" && code != "gcc " || filter == ".cpp" && code != "g++ ") {
std::cout << Colors::RED << "Error: Parameter to filter wrongly used \"C\" and \"C++\" files" << Colors::DEFAULT << std::endl;
return 1;
}
if (otherFlags.length() > 0) code += otherFlags;
if (logsOpc) std::cout << Colors::YELLOW << "Loading Files\n" << Colors::DEFAULT << std::endl;
std::filesystem::path path = current_path; // path atual
loopPath(path, filter, logsOpc, filesDir);
if (filesDir.empty()) {
std::cout << Colors::RED << "No \"" << Colors::GRAY << filter << Colors::RED << "\" files found" << Colors::DEFAULT << std::endl;
return 1;
}
for (const auto& i: filesDir) code += "\"" + i + "\" ";
code += "-o \"" + nameOpc + "\"";
if (logsOpc) std::cout << Colors::PURPLE << "\nRunning: " << Colors::GREEN << code << std::endl;
// std::cout << argCout << " " << argVetor << Colors::RED << std::endl; // Local memory
// std::cout << "Index 0" << filesDir[0] << Colors::DEFAULT << std::endl;
std::filesystem::current_path(current_path);
try {
int err = system(code.c_str());
if (err == 0) {
std::cout << Colors::GREEN << "Build finished." << Colors::DEFAULT << std::endl;
if (runOpc) {
std::cout << Colors::YELLOW << "Executing " << nameOpc << "...\n" << Colors::DEFAULT << std::endl;
system((runCmd + nameOpc).c_str());
}
}
else std::cout << Colors::RED << "\nError: Check the compilation" << Colors::DEFAULT << std::endl;
} catch(const std::exception& err) {
// std::cout << Colors::DEFAULT << "Error: Check the compilation\n" << Colors::DEFAULT << err.what() << std::endl;
std::cout << Colors::DEFAULT << "Error: Check the compilation\n" << Colors::DEFAULT << std::endl;
return 1;
}
return 0;
}
#endif