Manager: Convert dotfile class to singleton
This commit is contained in:
+23
-26
@@ -19,12 +19,9 @@
|
||||
#include "machine.h"
|
||||
#include "util/file.h"
|
||||
|
||||
std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths;
|
||||
std::vector<std::filesystem::path> 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<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");
|
||||
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<std::string>& 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/<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;
|
||||
},
|
||||
[](std::string* paths, const std::string& systemFile) {
|
||||
[this](std::string* paths, const std::string& systemFile) {
|
||||
// systemFile = /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;
|
||||
});
|
||||
}
|
||||
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/<user>/dotfiles/<file>
|
||||
// copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file>
|
||||
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>
|
||||
// copy: /home/<user>/dotfiles/<file> -> <file>
|
||||
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<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& 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<std::string>& 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<std::string>& 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<std::
|
||||
|
||||
bool Dotfile::isSystemTarget(const std::string& target)
|
||||
{
|
||||
for (const auto& systemDirectory : s_systemDirectories) {
|
||||
if (target.find(systemDirectory) - s_workingDirectorySize == 0) {
|
||||
for (const auto& systemDirectory : m_systemDirectories) {
|
||||
if (target.find(systemDirectory) - m_workingDirectorySize == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user