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 "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;
}
}

26
src/dotfile.h

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

15
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) {

Loading…
Cancel
Save