diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 67eed1c..8845215 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -19,12 +19,9 @@ #include "machine.h" #include "util/file.h" -std::vector Dotfile::s_excludePaths; -std::vector Dotfile::s_systemDirectories; -std::filesystem::path Dotfile::s_workingDirectory; -size_t Dotfile::s_workingDirectorySize { 0 }; - -Dotfile::Dotfile() +Dotfile::Dotfile(s) + : m_workingDirectory(std::filesystem::current_path()) + , m_workingDirectorySize(m_workingDirectory.string().size()) { } @@ -84,18 +81,18 @@ void Dotfile::add(const std::vector& targets) void Dotfile::list(const std::vector& targets) { - if (s_workingDirectory.empty()) { + if (m_workingDirectory.empty()) { fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); return; } - if (!std::filesystem::is_directory(s_workingDirectory)) { + if (!std::filesystem::is_directory(m_workingDirectory)) { fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is not a directory\n"); return; } - forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) { - printf("%s\n", path.path().c_str() + s_workingDirectorySize + 1); + forEachDotfile(targets, [this](std::filesystem::directory_entry path, size_t) { + printf("%s\n", path.path().c_str() + m_workingDirectorySize + 1); }); } @@ -131,33 +128,33 @@ void Dotfile::pullOrPush(SyncType type, const std::vector& targets) if (type == SyncType::Pull) { sync( type, dotfiles, homeIndices, systemIndices, - [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [this](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home// -> /home//dotfiles/ - paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize); + paths[0] = homeDirectory + homeFile.substr(m_workingDirectorySize); paths[1] = homeFile; }, - [](std::string* paths, const std::string& systemFile) { + [this](std::string* paths, const std::string& systemFile) { // systemFile = /home//dotfiles/ // copy: -> /home//dotfiles/ - paths[0] = systemFile.substr(s_workingDirectorySize); + paths[0] = systemFile.substr(m_workingDirectorySize); paths[1] = systemFile; }); } else { sync( type, dotfiles, homeIndices, systemIndices, - [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [this](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home//dotfiles/ -> /home// paths[0] = homeFile; - paths[1] = homeDirectory + homeFile.substr(s_workingDirectorySize); + paths[1] = homeDirectory + homeFile.substr(m_workingDirectorySize); }, - [](std::string* paths, const std::string& systemFile) { + [this](std::string* paths, const std::string& systemFile) { // systemFile = /home//dotfiles/ // copy: /home//dotfiles/ -> paths[0] = systemFile; - paths[1] = systemFile.substr(s_workingDirectorySize); + paths[1] = systemFile.substr(m_workingDirectorySize); }); } } @@ -171,7 +168,7 @@ void Dotfile::sync(SyncType type, if (!systemIndices.empty() && !root) { for (size_t i : systemIndices) { fprintf(stderr, "\033[31;1mDotfile:\033[0m need root privileges to copy system file '%s'\n", - paths.at(i).c_str() + s_workingDirectorySize); + paths.at(i).c_str() + m_workingDirectorySize); } return; } @@ -340,7 +337,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path) void Dotfile::forEachDotfile(const std::vector& targets, const std::function& callback) { size_t index = 0; - for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) { + for (const auto& path : std::filesystem::recursive_directory_iterator { m_workingDirectory }) { if (path.is_directory() || filter(path)) { continue; } @@ -353,14 +350,14 @@ void Dotfile::forEachDotfile(const std::vector& targets, const std: bool Dotfile::filter(const std::filesystem::path& path) { - for (auto& excludePath : s_excludePaths) { + for (auto& excludePath : m_excludePaths) { if (excludePath.type == ExcludeType::File) { - if (path.string() == s_workingDirectory / excludePath.path) { + if (path.string() == m_workingDirectory / excludePath.path) { return true; } } else if (excludePath.type == ExcludeType::Directory) { - if (path.string().find(s_workingDirectory / excludePath.path) == 0) { + if (path.string().find(m_workingDirectory / excludePath.path) == 0) { return true; } } @@ -377,7 +374,7 @@ bool Dotfile::filter(const std::filesystem::path& path) bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets) { for (const auto& target : targets) { - if (path.string().find(s_workingDirectory / target) == 0) { + if (path.string().find(m_workingDirectory / target) == 0) { return true; } } @@ -387,8 +384,8 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector #include -class Dotfile { +#include "util/singleton.h" + +class Dotfile : public Util::Singleton { public: - Dotfile(); + Dotfile(s); virtual ~Dotfile(); enum class SyncType { @@ -39,13 +41,13 @@ public: void pull(const std::vector& targets = {}); void push(const std::vector& targets = {}); - static void setWorkingDirectory(std::filesystem::path directory) + void setWorkingDirectory(std::filesystem::path directory) { - s_workingDirectory = directory; - s_workingDirectorySize = directory.string().size(); + m_workingDirectory = directory; + m_workingDirectorySize = directory.string().size(); } - static void setSystemDirectories(const std::vector& systemDirectories) { s_systemDirectories = systemDirectories; } - static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } + void setSystemDirectories(const std::vector& systemDirectories) { m_systemDirectories = systemDirectories; } + void setExcludePaths(const std::vector& excludePaths) { m_excludePaths = excludePaths; } private: void pullOrPush(SyncType type, const std::vector& targets = {}); @@ -60,10 +62,10 @@ private: bool include(const std::filesystem::path& path, const std::vector& targets); bool isSystemTarget(const std::string& target); - static std::vector s_excludePaths; - static std::vector s_systemDirectories; - static std::filesystem::path s_workingDirectory; - static size_t s_workingDirectorySize; + std::vector m_excludePaths; + std::vector m_systemDirectories; + std::filesystem::path m_workingDirectory; + size_t m_workingDirectorySize { 0 }; }; #endif // DOTFILE_H diff --git a/src/main.cpp b/src/main.cpp index e807e5d..2c78e15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,9 +63,7 @@ int main(int argc, const char* argv[]) Util::Timer t; if (fileOperation) { - Dotfile dotfile; - - Dotfile::setExcludePaths({ + Dotfile::the().setExcludePaths({ { Dotfile::ExcludeType::File, "dotfiles.sh" }, { Dotfile::ExcludeType::File, "packages" }, { Dotfile::ExcludeType::EndsWith, ".md" }, @@ -78,20 +76,19 @@ int main(int argc, const char* argv[]) { Dotfile::ExcludeType::Directory, "cppcheck-cppcheck-build-dir" }, }); - Dotfile::setSystemDirectories({ "/boot", "/etc", "/usr/share" }); - Dotfile::setWorkingDirectory(std::filesystem::current_path()); + Dotfile::the().setSystemDirectories({ "/boot", "/etc", "/usr/share" }); if (addOrAur) { - dotfile.add(targets); + Dotfile::the().add(targets); } if (pull) { - dotfile.pull(targets); + Dotfile::the().pull(targets); } if (pushOrStore) { - dotfile.push(targets); + Dotfile::the().push(targets); } if (!addOrAur && !pull && !pushOrStore) { - dotfile.list(targets); + Dotfile::the().list(targets); } } else if (packageOperation) {