Browse Source

Everywhere: Change exclude path configuration formatting

master
Riyyi 3 years ago
parent
commit
b37a8087e9
  1. 32
      README.org
  2. 16
      doc/manafiles.json
  3. 8
      src/config.cpp
  4. 18
      src/config.h
  5. 3
      src/dotfile.cpp
  6. 98
      test/unit/testdotfile.cpp

32
README.org

@ -31,23 +31,29 @@ the config file is searched recursively. \\
**** Exclude paths **** Exclude paths
Everything in this list will get excluded when pushing config files from the working directory to the system. \\ Everything in this list will get excluded when pulling/pushing config files from the working directory. \\
Currently three types of file matching are supported: Currently two types of file matching are supported:
- ~file~ full name of the file matches - ~literal~ matches a file or directory literally
- ~directory~ full name of the directory matches - ~wildcard~ the asterisk matches zero or more characters
- ~endsWith~ end of the path matches
These behave similarly to a ~.gitignore~ pattern.
- If the pattern starts with a slash, it matches files and directories in the working directory root only.
- If the pattern doesn’t start with a slash, it matches files and directories in any directory or subdirectory.
- If the pattern ends with a slash, it matches only directories. When a directory is ignored, \\
all of its files and subdirectories are also ignored.
The excluded paths from the example config: The excluded paths from the example config:
#+BEGIN_SRC javascript #+BEGIN_SRC javascript
"excludePaths" : { "excludePaths" : [
".git": "directory", ".git/",
".md": "endsWith", "*.md",
"manafiles.json": "endsWith", "manafiles.json",
"packages": "file", "packages",
"README.org": "endsWith", "README.org",
"screenshot.png": "file" "screenshot.png"
} ]
#+END_SRC #+END_SRC
**** System config files **** System config files

16
doc/manafiles.json

@ -1,12 +1,12 @@
{ {
"excludePaths" : { "excludePaths" : [
".git": "directory", ".git/",
".md": "endsWith", "*.md",
"manafiles.json": "endsWith", "manafiles.json",
"packages": "file", "packages",
"README.org": "endsWith", "README.org",
"screenshot.png": "file" "screenshot.png"
}, ],
"systemDirectories": [ "systemDirectories": [
"/boot", "/boot",
"/etc", "/etc",

8
src/config.cpp

@ -89,12 +89,4 @@ void from_json(const nlohmann::json& object, Settings& settings)
if (object.find("systemDirectories") != object.end()) { if (object.find("systemDirectories") != object.end()) {
object.at("systemDirectories").get_to(settings.systemDirectories); object.at("systemDirectories").get_to(settings.systemDirectories);
} }
// Check for correct exclude type values
for (const auto& path : settings.excludePaths) {
if (path.second != "file" && path.second != "directory" && path.second != "endsWith") {
fprintf(stderr, "\033[31;1mConfig:\033[0m invalid exclude type '%s'\n", path.second.c_str());
raise(SIGABRT);
}
}
} }

18
src/config.h

@ -9,7 +9,6 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <filesystem> // path #include <filesystem> // path
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@ -18,12 +17,13 @@
#include "util/singleton.h" #include "util/singleton.h"
struct Settings { struct Settings {
std::map<std::string, std::string> excludePaths { std::vector<std::string> excludePaths {
{ ".git", "directory" }, ".git/",
{ ".md", "endsWith" }, "*.md",
{ "packages", "file" }, "manafiles.json",
{ "README.org", "endsWith" }, "packages",
{ "screenshot.png", "file" }, "README.org",
"screenshot.png",
}; };
std::vector<std::filesystem::path> systemDirectories { std::vector<std::filesystem::path> systemDirectories {
"/boot", "/boot",
@ -38,10 +38,10 @@ public:
virtual ~Config(); virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; } void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setExcludePaths(const std::map<std::string, std::string>& excludePaths) { m_settings.excludePaths = excludePaths; } void setExcludePaths(const std::vector<std::string>& excludePaths) { m_settings.excludePaths = excludePaths; }
void setVerbose(bool verbose) { m_verbose = verbose; } void setVerbose(bool verbose) { m_verbose = verbose; }
const std::map<std::string, std::string>& excludePaths() const { return m_settings.excludePaths; } const std::vector<std::string>& excludePaths() const { return m_settings.excludePaths; }
const std::vector<std::filesystem::path>& systemDirectories() const { return m_settings.systemDirectories; } const std::vector<std::filesystem::path>& systemDirectories() const { return m_settings.systemDirectories; }
const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } const std::filesystem::path& workingDirectory() const { return m_workingDirectory; }

3
src/dotfile.cpp

@ -115,8 +115,7 @@ bool Dotfile::filter(const std::filesystem::directory_entry& path)
size_t cutFrom = pathString.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0; size_t cutFrom = pathString.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0;
pathString = pathString.substr(cutFrom); pathString = pathString.substr(cutFrom);
for (const auto& excludePathMapEntry : Config::the().excludePaths()) { for (const auto& excludePath : Config::the().excludePaths()) {
const auto& excludePath = excludePathMapEntry.first;
if (pathString == excludePath) { if (pathString == excludePath) {
return true; return true;

98
test/unit/testdotfile.cpp

@ -82,7 +82,7 @@ void removeTestDotfiles(const std::vector<std::string>& files, bool deleteInHome
} }
void testDotfileFilters(const std::unordered_map<std::string, bool>& tests, void testDotfileFilters(const std::unordered_map<std::string, bool>& tests,
const std::unordered_map<std::string, std::string>& testFilters) const std::vector<std::string>& testFilters)
{ {
std::vector<std::string> fileNames; std::vector<std::string> fileNames;
std::vector<std::string> fileContents; std::vector<std::string> fileContents;
@ -117,8 +117,8 @@ TEST_CASE(DotfilesLiteralIgnoreAllFiles)
{ "error.log", false }, { "error.log", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "access.log", "" }, "access.log",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
@ -133,8 +133,8 @@ TEST_CASE(DotfilesLiteralIgnoreFileInRoot)
{ "error.log", false }, { "error.log", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "/access.log", "" }, "/access.log",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
@ -147,14 +147,15 @@ TEST_CASE(DotfilesLiteralIgnoreDirectories)
{ "doc/build", false }, { "doc/build", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "build/", "" }, "build/",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension) { TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "error.log", true }, { "error.log", true },
{ "logs/debug.log", true }, { "logs/debug.log", true },
@ -164,14 +165,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllFilesWithExtension) {
{ "log.txt", false }, { "log.txt", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "*.log", "" }, "*.log",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension) { TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "error.log", true }, { "error.log", true },
{ "logs/debug.log", false }, { "logs/debug.log", false },
@ -181,14 +183,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllFilesInRootWithExtension) {
{ "log.txt", false }, { "log.txt", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "/*.log", "" }, "/*.log",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions) { TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "README.md", true }, { "README.md", true },
{ "doc/README.org", true }, { "doc/README.org", true },
@ -198,14 +201,15 @@ TEST_CASE(DotfilesWildcardIgnoreFileWithAllExtensions) {
{ "Config.org", false }, { "Config.org", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "README.*", "" }, "README.*",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern) { TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "cmake/uninstall.cmake.in", true }, { "cmake/uninstall.cmake.in", true },
{ "cmake/templates/template.cmake.in", true }, { "cmake/templates/template.cmake.in", true },
@ -214,14 +218,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllWithStartingPattern) {
{ "CMakeLists.txt", false }, { "CMakeLists.txt", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "cmake*", "" }, "cmake*",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern) { TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "project-directory/README.org", true }, { "project-directory/README.org", true },
{ "project-directory/build/executable", true }, { "project-directory/build/executable", true },
@ -229,28 +234,30 @@ TEST_CASE(DotfilesWildcardIgnoreAllInRootWithStartingPattern) {
{ "doc/project-instructions.txt", false }, { "doc/project-instructions.txt", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "/project*", "" }, "/project*",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllInDirectory) { TEST_CASE(DotfilesWildcardIgnoreAllInDirectory)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "build/x32/executable", true }, { "build/x32/executable", true },
{ "build/x64/executable", true }, { "build/x64/executable", true },
{ "build/executable", true }, { "build/executable", true },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "build/*", "" }, "build/*",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory) { TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "build/x32/executable", true }, { "build/x32/executable", true },
{ "build/x64/executable", true }, { "build/x64/executable", true },
@ -259,14 +266,15 @@ TEST_CASE(DotfilesWildcardIgnoreFileInSubDirectory) {
{ "build/executable", false }, { "build/executable", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "build/*/executable", "" }, "build/*/executable",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory) { TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "build/x32/executable", true }, { "build/x32/executable", true },
{ "build/x64/executable", true }, { "build/x64/executable", true },
@ -275,14 +283,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllInSubDirectory) {
{ "build/executable", false }, { "build/executable", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "build/*/*", "" }, "build/*/*",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern) { TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "include/header.h", true }, { "include/header.h", true },
{ "include-dependency/header.h", true }, { "include-dependency/header.h", true },
@ -290,14 +299,15 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithStartingPattern) {
{ "src/include/header.h", true }, { "src/include/header.h", true },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "include*/", "" }, "include*/",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
} }
TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern) { TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern)
{
std::unordered_map<std::string, bool> tests = { std::unordered_map<std::string, bool> tests = {
{ "include/header.h", true }, { "include/header.h", true },
{ "include-dependency/header.h", true }, { "include-dependency/header.h", true },
@ -305,8 +315,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithStartingPattern) {
{ "src/include/header.h", false }, { "src/include/header.h", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "/include*/", "" }, "/include*/",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
@ -321,8 +331,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesWithEndingPattern)
{ "src/include/header.h", true }, { "src/include/header.h", true },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "*include/", "" }, "*include/",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
@ -337,8 +347,8 @@ TEST_CASE(DotfilesWildcardIgnoreAllDirectoriesInRootWithEndingPattern)
{ "src/include/header.h", false }, { "src/include/header.h", false },
}; };
std::unordered_map<std::string, std::string> testFilters = { std::vector<std::string> testFilters = {
{ "/*include/", "" }, "/*include/",
}; };
testDotfileFilters(tests, testFilters); testDotfileFilters(tests, testFilters);
@ -478,9 +488,9 @@ TEST_CASE(PushDotfilesWithExcludePath)
createTestDotfiles(fileNames, { "", "", "", "" }); createTestDotfiles(fileNames, { "", "", "", "" });
Config::the().setExcludePaths({ Config::the().setExcludePaths({
{ "__test-file-1", "file" }, "__test-file-1",
{ "__subdir/", "directory" }, "__subdir/",
{ "*.test", "endsWith" }, "*.test",
}); });
Dotfile::the().push(fileNames); Dotfile::the().push(fileNames);
Config::the().setExcludePaths({}); Config::the().setExcludePaths({});

Loading…
Cancel
Save