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