Browse Source

Manager: Fix system target detection

master
Riyyi 3 years ago
parent
commit
50c859fd34
  1. 27
      src/dotfile.cpp
  2. 7
      src/dotfile.h

27
src/dotfile.cpp

@ -21,6 +21,7 @@
std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths; std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths;
std::vector<std::filesystem::path> Dotfile::s_systemDirectories; std::vector<std::filesystem::path> Dotfile::s_systemDirectories;
std::filesystem::path Dotfile::s_workingDirectory; std::filesystem::path Dotfile::s_workingDirectory;
size_t Dotfile::s_workingDirectorySize { 0 };
Dotfile::Dotfile() Dotfile::Dotfile()
{ {
@ -92,9 +93,8 @@ void Dotfile::list(const std::vector<std::string>& targets)
return; return;
} }
size_t workingDirectory = s_workingDirectory.string().size(); forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) {
forEachDotfile(targets, [&workingDirectory](std::filesystem::directory_entry path, size_t) { printf("%s\n", path.path().c_str() + s_workingDirectorySize + 1);
printf("%s\n", path.path().c_str() + workingDirectory + 1);
}); });
} }
@ -115,19 +115,19 @@ void Dotfile::pull(const std::vector<std::string>& targets)
} }
}); });
size_t workingDirectory = s_workingDirectory.string().size();
sync( sync(
dotfiles, homeIndices, systemIndices, dotfiles, homeIndices, systemIndices,
[&workingDirectory](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { [](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(workingDirectory); paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize);
paths[1] = homeFile; paths[1] = homeFile;
}, },
[&workingDirectory](std::string* paths, const std::string& systemFile) { [](std::string* paths, const std::string& systemFile) {
printf("system file: %s", systemFile.c_str());
// 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(workingDirectory); paths[0] = systemFile.substr(s_workingDirectorySize);
paths[1] = systemFile; paths[1] = systemFile;
}); });
} }
@ -149,20 +149,19 @@ void Dotfile::push(const std::vector<std::string>& targets)
} }
}); });
size_t workingDirectory = s_workingDirectory.string().size();
sync( sync(
dotfiles, homeIndices, systemIndices, dotfiles, homeIndices, systemIndices,
[&workingDirectory](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { [](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(workingDirectory); paths[1] = homeDirectory + homeFile.substr(s_workingDirectorySize);
}, },
[&workingDirectory](std::string* paths, const std::string& systemFile) { [](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(workingDirectory); paths[1] = systemFile.substr(s_workingDirectorySize);
}); });
} }
@ -289,7 +288,7 @@ 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 : s_systemDirectories) {
if (target.find(systemDirectory) == 0) { if (target.find(systemDirectory) - s_workingDirectorySize == 0) {
return true; return true;
} }
} }

7
src/dotfile.h

@ -34,7 +34,11 @@ 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) { s_workingDirectory = directory; } static void setWorkingDirectory(std::filesystem::path directory)
{
s_workingDirectory = directory;
s_workingDirectorySize = directory.string().size();
}
static void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { s_systemDirectories = systemDirectories; } static void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { s_systemDirectories = systemDirectories; }
static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; } static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; }
@ -50,6 +54,7 @@ private:
static std::vector<ExcludePath> s_excludePaths; static std::vector<ExcludePath> s_excludePaths;
static std::vector<std::filesystem::path> s_systemDirectories; static std::vector<std::filesystem::path> s_systemDirectories;
static std::filesystem::path s_workingDirectory; static std::filesystem::path s_workingDirectory;
static size_t s_workingDirectorySize;
}; };
#endif // DOTFILE_H #endif // DOTFILE_H

Loading…
Cancel
Save