Browse Source

Manager: Deduplicate code between pulling and pushing functions

master
Riyyi 3 years ago
parent
commit
7be7440840
  1. 85
      src/dotfile.cpp
  2. 7
      src/dotfile.h

85
src/dotfile.cpp

@ -100,38 +100,17 @@ void Dotfile::list(const std::vector<std::string>& targets)
void Dotfile::pull(const std::vector<std::string>& targets) void Dotfile::pull(const std::vector<std::string>& targets)
{ {
std::vector<std::string> dotfiles; pullOrPush(SyncType::Pull, targets);
std::vector<size_t> homeIndices;
std::vector<size_t> systemIndices;
// Separate home and system targets
forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index) {
dotfiles.push_back(path.path().string());
if (isSystemTarget(path.path().string())) {
systemIndices.push_back(index);
}
else {
homeIndices.push_back(index);
}
});
sync(
dotfiles, homeIndices, systemIndices,
[](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[1] = homeFile;
},
[](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[1] = systemFile;
});
} }
void Dotfile::push(const std::vector<std::string>& targets) void Dotfile::push(const std::vector<std::string>& targets)
{
pullOrPush(SyncType::Push, targets);
}
// -----------------------------------------
void Dotfile::pullOrPush(SyncType type, const std::vector<std::string>& targets)
{ {
std::vector<std::string> dotfiles; std::vector<std::string> dotfiles;
std::vector<size_t> homeIndices; std::vector<size_t> homeIndices;
@ -148,24 +127,40 @@ void Dotfile::push(const std::vector<std::string>& targets)
} }
}); });
sync( if (type == SyncType::Pull) {
dotfiles, homeIndices, systemIndices, sync(
[](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { dotfiles, homeIndices, systemIndices,
// homeFile = /home/<user>/dotfiles/<file> [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) {
// copy: /home/<user>/dotfiles/<file> -> /home/<user>/<file> // homeFile = /home/<user>/dotfiles/<file>
paths[0] = homeFile; // copy: /home/<user>/<file> -> /home/<user>/dotfiles/<file>
paths[1] = homeDirectory + homeFile.substr(s_workingDirectorySize); paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize);
}, paths[1] = homeFile;
[](std::string* paths, const std::string& systemFile) { },
// systemFile = /home/<user>/dotfiles/<file> [](std::string* paths, const std::string& systemFile) {
// copy: /home/<user>/dotfiles/<file> -> <file> // systemFile = /home/<user>/dotfiles/<file>
paths[0] = systemFile; // copy: <file> -> /home/<user>/dotfiles/<file>
paths[1] = systemFile.substr(s_workingDirectorySize); paths[0] = systemFile.substr(s_workingDirectorySize);
}); paths[1] = systemFile;
});
}
else {
sync(
dotfiles, homeIndices, systemIndices,
[](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);
},
[](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);
});
}
} }
// -----------------------------------------
void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size_t>& homeIndices, const std::vector<size_t>& systemIndices, void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size_t>& homeIndices, const std::vector<size_t>& systemIndices,
const std::function<void(std::string*, const std::string&, const std::string&)>& generateHomePaths, const std::function<void(std::string*, const std::string&, const std::string&)>& generateHomePaths,
const std::function<void(std::string*, const std::string&)>& generateSystemPaths) const std::function<void(std::string*, const std::string&)>& generateSystemPaths)

7
src/dotfile.h

@ -18,6 +18,11 @@ public:
Dotfile(); Dotfile();
virtual ~Dotfile(); virtual ~Dotfile();
enum class SyncType {
Pull,
Push,
};
enum class ExcludeType { enum class ExcludeType {
File, File,
Directory, Directory,
@ -43,9 +48,11 @@ public:
static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; } static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; }
private: private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
void sync(const std::vector<std::string>& paths, const std::vector<size_t>& homeIndices, const std::vector<size_t>& systemIndices, void sync(const std::vector<std::string>& paths, const std::vector<size_t>& homeIndices, const std::vector<size_t>& systemIndices,
const std::function<void(std::string*, const std::string&, const std::string&)>& generateHomePaths, const std::function<void(std::string*, const std::string&, const std::string&)>& generateHomePaths,
const std::function<void(std::string*, const std::string&)>& generateSystemPaths); const std::function<void(std::string*, const std::string&)>& generateSystemPaths);
void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback); void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(const std::filesystem::directory_entry&, size_t)>& callback);
bool filter(const std::filesystem::path& path); bool filter(const std::filesystem::path& path);
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);

Loading…
Cancel
Save