-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
50 lines (40 loc) · 1.76 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <thread>
#include <memory>
#include <string.h>
#include "include/watcher.hpp"
#include "vendor/CLI11.hpp"
void run(std::shared_ptr<std::thread> worker, std::string runner) {
std::shared_ptr<std::thread> swapper = std::make_shared<std::thread>([runner]() {
system(runner.c_str());
});
std::swap(*worker, *swapper);
swapper->join();
}
int main(int argc, char** argv) {
CLI::App app{"Hot reload anything by @kubgus."};
std::string runner = "echo \"No runner command! (use -r tag)\"";
app.add_option("-r,--run,--runner", runner, "Set command to run on refresh. (ex:\"g++ main.cpp; ./a.out\")");
std::string extensions = "c;cpp;h;hpp";
app.add_option("-e,--extension,--extensions", extensions, "Choose which file extensions are monitored. (ex:\"cpp;h;js;py\")");
std::string directory = "./";
app.add_option("-d,--dir,--directory", directory, "Choose which directory is monitored. (ex:\"./src/\")");
CLI11_PARSE(app, argc, argv);
std::cout << "Hotpot CLI by @kubgus.\n"
<< "Website: https://gustafik.com/\n\n"
<< "Launching and listening for file changes!\n\n";
std::shared_ptr<std::thread> worker = std::make_shared<std::thread>([runner]() {
system(runner.c_str());
});
HotWatch watcher{ directory };
watcher.start_loop([worker,runner,extensions](std::string file) {
for (char* tok = strtok(strdup(extensions.c_str()), ";"); tok != NULL; tok = strtok(NULL, ";"))
if (file.substr(file.find_last_of(".") + 1) == tok) {
std::cout << '\n' << "Detected change in " << file << "!\n"
<< "Refreshing...\n\n";
run(worker, runner);
}
});
return 0;
}