Browse Source

Manager: Convert dotfile class to singleton

master
Riyyi 3 years ago
parent
commit
d7ff637fd0
  1. 49
      src/dotfile.cpp
  2. 26
      src/dotfile.h
  3. 15
      src/main.cpp

49
src/dotfile.cpp

@ -19,12 +19,9 @@
#include "machine.h" #include "machine.h"
#include "util/file.h" #include "util/file.h"
std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths; Dotfile::Dotfile(s)
std::vector<std::filesystem::path> Dotfile::s_systemDirectories; : m_workingDirectory(std::filesystem::current_path())
std::filesystem::path Dotfile::s_workingDirectory; , m_workingDirectorySize(m_workingDirectory.string().size())
size_t Dotfile::s_workingDirectorySize { 0 };
Dotfile::Dotfile()
{ {
} }
@ -84,18 +81,18 @@ void Dotfile::add(const std::vector<std::string>& targets)
void Dotfile::list(const std::vector<std::string>& targets) void Dotfile::list(const std::vector<std::string>& targets)
{ {
if (s_workingDirectory.empty()) { if (m_workingDirectory.empty()) {
fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n");
return; 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"); fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is not a directory\n");
return; return;
} }
forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) { forEachDotfile(targets, [this](std::filesystem::directory_entry path, size_t) {
printf("%s\n", path.path().c_str() + s_workingDirectorySize + 1); printf("%s\n", path.path().c_str() + m_workingDirectorySize + 1);
}); });
} }
@ -131,33 +128,33 @@ void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
if (type == SyncType::Pull) { if (type == SyncType::Pull) {
sync( sync(
type, dotfiles, homeIndices, systemIndices, 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/<user>/dotfiles/<file> // homeFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/<file> -> /home/<user>/dotfiles/<file> // copy: /home/<user>/<file> -> /home/<user>/dotfiles/<file>
paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize); paths[0] = homeDirectory + homeFile.substr(m_workingDirectorySize);
paths[1] = homeFile; paths[1] = homeFile;
}, },
[](std::string* paths, const std::string& systemFile) { [this](std::string* paths, const std::string& systemFile) {
// systemFile = /home/<user>/dotfiles/<file> // systemFile = /home/<user>/dotfiles/<file>
// copy: <file> -> /home/<user>/dotfiles/<file> // copy: <file> -> /home/<user>/dotfiles/<file>
paths[0] = systemFile.substr(s_workingDirectorySize); paths[0] = systemFile.substr(m_workingDirectorySize);
paths[1] = systemFile; paths[1] = systemFile;
}); });
} }
else { else {
sync( sync(
type, dotfiles, homeIndices, systemIndices, 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/<user>/dotfiles/<file> // homeFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file> // copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file>
paths[0] = homeFile; 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/<user>/dotfiles/<file> // systemFile = /home/<user>/dotfiles/<file>
// copy: /home/<user>/dotfiles/<file> -> <file> // copy: /home/<user>/dotfiles/<file> -> <file>
paths[0] = systemFile; 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) { if (!systemIndices.empty() && !root) {
for (size_t i : systemIndices) { for (size_t i : systemIndices) {
fprintf(stderr, "\033[31;1mDotfile:\033[0m need root privileges to copy system file '%s'\n", 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; return;
} }
@ -340,7 +337,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback) void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback)
{ {
size_t index = 0; 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)) { if (path.is_directory() || filter(path)) {
continue; continue;
} }
@ -353,14 +350,14 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
bool Dotfile::filter(const std::filesystem::path& path) bool Dotfile::filter(const std::filesystem::path& path)
{ {
for (auto& excludePath : s_excludePaths) { for (auto& excludePath : m_excludePaths) {
if (excludePath.type == ExcludeType::File) { if (excludePath.type == ExcludeType::File) {
if (path.string() == s_workingDirectory / excludePath.path) { if (path.string() == m_workingDirectory / excludePath.path) {
return true; return true;
} }
} }
else if (excludePath.type == ExcludeType::Directory) { 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; 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<std::string>& targets) bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::string>& targets)
{ {
for (const auto& target : targets) { for (const auto& target : targets) {
if (path.string().find(s_workingDirectory / target) == 0) { if (path.string().find(m_workingDirectory / target) == 0) {
return true; return true;
} }
} }
@ -387,8 +384,8 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::
bool Dotfile::isSystemTarget(const std::string& target) bool Dotfile::isSystemTarget(const std::string& target)
{ {
for (const auto& systemDirectory : s_systemDirectories) { for (const auto& systemDirectory : m_systemDirectories) {
if (target.find(systemDirectory) - s_workingDirectorySize == 0) { if (target.find(systemDirectory) - m_workingDirectorySize == 0) {
return true; return true;
} }
} }

26
src/dotfile.h

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2021 Riyyi * Copyright (C) 2021-2022 Riyyi
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
@ -13,9 +13,11 @@
#include <string> #include <string>
#include <vector> #include <vector>
class Dotfile { #include "util/singleton.h"
class Dotfile : public Util::Singleton<Dotfile> {
public: public:
Dotfile(); Dotfile(s);
virtual ~Dotfile(); virtual ~Dotfile();
enum class SyncType { enum class SyncType {
@ -39,13 +41,13 @@ public:
void pull(const std::vector<std::string>& targets = {}); void pull(const std::vector<std::string>& targets = {});
void push(const std::vector<std::string>& targets = {}); void push(const std::vector<std::string>& targets = {});
static void setWorkingDirectory(std::filesystem::path directory) void setWorkingDirectory(std::filesystem::path directory)
{ {
s_workingDirectory = directory; m_workingDirectory = directory;
s_workingDirectorySize = directory.string().size(); m_workingDirectorySize = directory.string().size();
} }
static void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { s_systemDirectories = systemDirectories; } void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_systemDirectories = systemDirectories; }
static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; } void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { m_excludePaths = excludePaths; }
private: private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {}); void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
@ -60,10 +62,10 @@ private:
bool include(const std::filesystem::path& path, const std::vector<std::string>& targets); bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);
bool isSystemTarget(const std::string& target); bool isSystemTarget(const std::string& target);
static std::vector<ExcludePath> s_excludePaths; std::vector<ExcludePath> m_excludePaths;
static std::vector<std::filesystem::path> s_systemDirectories; std::vector<std::filesystem::path> m_systemDirectories;
static std::filesystem::path s_workingDirectory; std::filesystem::path m_workingDirectory;
static size_t s_workingDirectorySize; size_t m_workingDirectorySize { 0 };
}; };
#endif // DOTFILE_H #endif // DOTFILE_H

15
src/main.cpp

@ -63,9 +63,7 @@ int main(int argc, const char* argv[])
Util::Timer t; Util::Timer t;
if (fileOperation) { if (fileOperation) {
Dotfile dotfile; Dotfile::the().setExcludePaths({
Dotfile::setExcludePaths({
{ Dotfile::ExcludeType::File, "dotfiles.sh" }, { Dotfile::ExcludeType::File, "dotfiles.sh" },
{ Dotfile::ExcludeType::File, "packages" }, { Dotfile::ExcludeType::File, "packages" },
{ Dotfile::ExcludeType::EndsWith, ".md" }, { Dotfile::ExcludeType::EndsWith, ".md" },
@ -78,20 +76,19 @@ int main(int argc, const char* argv[])
{ Dotfile::ExcludeType::Directory, "cppcheck-cppcheck-build-dir" }, { Dotfile::ExcludeType::Directory, "cppcheck-cppcheck-build-dir" },
}); });
Dotfile::setSystemDirectories({ "/boot", "/etc", "/usr/share" }); Dotfile::the().setSystemDirectories({ "/boot", "/etc", "/usr/share" });
Dotfile::setWorkingDirectory(std::filesystem::current_path());
if (addOrAur) { if (addOrAur) {
dotfile.add(targets); Dotfile::the().add(targets);
} }
if (pull) { if (pull) {
dotfile.pull(targets); Dotfile::the().pull(targets);
} }
if (pushOrStore) { if (pushOrStore) {
dotfile.push(targets); Dotfile::the().push(targets);
} }
if (!addOrAur && !pull && !pushOrStore) { if (!addOrAur && !pull && !pushOrStore) {
dotfile.list(targets); Dotfile::the().list(targets);
} }
} }
else if (packageOperation) { else if (packageOperation) {

Loading…
Cancel
Save