Skip to content

Commit

Permalink
HOT PROJECT RELOADING BITCHES
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Oct 31, 2023
1 parent 87a21ba commit 9893db9
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
23 changes: 22 additions & 1 deletion install_manifest.txt
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
/usr/local/bin/cmaker
/usr/local/bin/cmaker
/usr/local/lib/libcurl.so.4.8.0
/usr/local/lib/libcurl.so.4
/usr/local/lib/libcurl.so
/usr/local/bin/curl-config
/usr/local/lib/pkgconfig/libcurl.pc
/usr/local/include/curl/multi.h
/usr/local/include/curl/stdcheaders.h
/usr/local/include/curl/curlver.h
/usr/local/include/curl/urlapi.h
/usr/local/include/curl/typecheck-gcc.h
/usr/local/include/curl/options.h
/usr/local/include/curl/curl.h
/usr/local/include/curl/header.h
/usr/local/include/curl/websockets.h
/usr/local/include/curl/mprintf.h
/usr/local/include/curl/system.h
/usr/local/include/curl/easy.h
/usr/local/lib/cmake/CURL/CURLTargets.cmake
/usr/local/lib/cmake/CURL/CURLTargets-release.cmake
/usr/local/lib/cmake/CURL/CURLConfigVersion.cmake
/usr/local/lib/cmake/CURL/CURLConfig.cmake
1 change: 1 addition & 0 deletions src/Command/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ namespace Command {
bool addLib(std::shared_ptr<Context>, cxxopts::ParseResult &args);
bool update(std::shared_ptr<Context>, cxxopts::ParseResult &args);

bool dev(std::shared_ptr<Context> ctx);
bool remove(std::shared_ptr<Context>ctx, cxxopts::ParseResult &args);
bool removeDep(std::shared_ptr<Context> ctx, cxxopts::ParseResult &args);
}
91 changes: 91 additions & 0 deletions src/Command/CommandDev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <chrono>
#include <filesystem>
#include <functional>
#include <string>
#include <thread>
#include <unordered_map>
#include "Command.hpp"

enum class FileStatus { created, modified, erased };

class FileWatcher {
public:
std::string path_to_watch;
std::chrono::duration<int, std::milli> delay;
bool running_ = true;
std::unordered_map<std::string, std::filesystem::file_time_type> paths_;
// Keep a record of files from the base directory and their last modification
// time
FileWatcher(std::string path_to_watch, std::chrono::duration<int, std::milli> delay): path_to_watch{path_to_watch}, delay{delay} {
for (auto &file :
std::filesystem::recursive_directory_iterator(path_to_watch)) {
paths_[file.path().string()] = std::filesystem::last_write_time(file);
}
}
bool contains(const std::string &key) {
auto el = paths_.find(key);
return el != paths_.end();
}
void start(const std::function<void(std::string, FileStatus)> &action) {
while (running_) {
// Wait for "delay" milliseconds
std::this_thread::sleep_for(delay);

auto it = paths_.begin();
while (it != paths_.end()) {
if (!std::filesystem::exists(it->first)) {
action(it->first, FileStatus::erased);
it = paths_.erase(it);
} else {
it++;
}
}

// Check if a file was created or modified
for (auto &file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
auto current_file_last_write_time =
std::filesystem::last_write_time(file);

// File creation
if (!contains(file.path().string())) {
paths_[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::created);
// File modification
}
else {
if (paths_[file.path().string()] != current_file_last_write_time) {
paths_[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::modified);
}
}
}
}
}
};

namespace Command {
bool dev(std::shared_ptr<Command::Context> ctx) {
std::cout << "Starting dev mode" << std::endl;
std::cout << "Watching for changes in " << ctx->src_dir << std::endl;
FileWatcher fw{ctx->src_dir, std::chrono::milliseconds(500)};
fw.start([ctx](std::string path_to_watch, FileStatus status) -> void {
std::string command = "cmake . && make && ./" + ctx->build_dir + "/" + ctx->project_name;
switch (status) {
case FileStatus::created:
std::cout << "File created: " << path_to_watch << '\n';
break;
case FileStatus::modified:
std::cout << "File modified: " << path_to_watch << '\n';
break;
case FileStatus::erased:
std::cout << "File erased: " << path_to_watch << '\n';
break;
default:
std::cout << "Error! Unknown file status.\n";
}
system(command.c_str());
});
return true;
}

}
3 changes: 2 additions & 1 deletion src/Command/CommandHelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace Command {
"\t [-n | --name example-name] " << ENDL
"\t [-l | --language cpp/c]:" << ENDL
"\t initializes your project" << ENDL
"\t r | run: builds and runs your project" << ENDL
"\t run: builds and runs your project" << ENDL
"\t dev: watches builds and runs your project on changes" << ENDL
"\t add [subcommand dep lib, flags]: add library, dependency or flags to your project" << ENDL
"\t ftp: deletes the entire project" << ENDL
"\t flags: adds a flag to your project" << ENDL
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ int main(int argc, char **argv) {
Command::loadPackageToml(ctx);
Command::remove(ctx, options);
}
else if (command == "dev"){
Command::loadPackageToml(ctx);
Command::dev(ctx);
}
else if (command == "update"){

ParseResult options = Command::updateOptions(argc, argv);
Command::update(ctx, result);
}
Expand Down

0 comments on commit 9893db9

Please sign in to comment.