Compare commits

...
4 Commits
3 changed files with 342 additions and 63 deletions
+14 -4
View File
@@ -254,12 +254,15 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
bool isFiltering = false;
std::string filter[3];
std::string commentCharacter;
std::string commentTerminationCharacter;
size_t positionInFile = 0;
auto commentOrUncommentLine = [&](std::string& line, bool addComment) {
size_t lineLength = line.size();
size_t whiteSpaceBeforeComment = line.find_first_of(commentCharacter);
size_t contentAfterComment = line.find_first_not_of(commentCharacter + " \t");
size_t whiteSpaceBeforeComment = line.find_first_not_of(" \t");
size_t contentAfterComment = line.find_first_not_of(" \t" + commentCharacter);
// NOTE: The +1 is needed to take the newline into account
size_t contentLength = lineLength + 1 - contentAfterComment - (lineLength - line.find_last_not_of(" \t" + commentTerminationCharacter));
// If there was no comment, grab whitespace correctly
if (whiteSpaceBeforeComment == std::string::npos) {
@@ -268,12 +271,13 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
if (!addComment) {
line = line.substr(0, whiteSpaceBeforeComment)
+ line.substr(contentAfterComment);
+ line.substr(contentAfterComment, contentLength);
}
else {
line = line.substr(0, whiteSpaceBeforeComment)
+ commentCharacter + ' '
+ line.substr(contentAfterComment);
+ line.substr(contentAfterComment, contentLength)
+ (!commentTerminationCharacter.empty() ? ' ' + commentTerminationCharacter : "");
}
dotfile.replace(positionInFile, lineLength, line);
@@ -296,6 +300,11 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
// Get the characters used for commenting in this file-type
commentCharacter = line.substr(0, line.find_first_of(">>>"));
for (size_t i = commentCharacter.size() - 1; i != std::string::npos; --i) {
// Support for /* C-style comments */
if (i > 0 && commentCharacter.at(i - 1) == '/' && commentCharacter.at(i) == '*') {
commentTerminationCharacter = "*/";
}
// NOTE: Modification of the string should be at the end of the iteration to prevent 'out of range' errors
if (commentCharacter.at(i) == ' ' || commentCharacter.at(i) == '\t') {
commentCharacter.erase(i, 1);
}
@@ -311,6 +320,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
filter[1] = "";
filter[2] = "";
commentCharacter.clear();
commentTerminationCharacter.clear();
continue;
}
+6 -8
View File
@@ -96,7 +96,7 @@ std::optional<std::string> Package::fetchAurHelper()
"trizen",
};
for(const auto& helper : helpers) {
for (const auto& helper : helpers) {
if (findDependency(helper)) {
return { helper };
}
@@ -112,18 +112,17 @@ void Package::installOrAurInstall(InstallType type)
std::optional<std::string> aurHelper;
if (type == InstallType::AurInstall) {
if (m_distro == Distro::Arch) {
if (m_distro != Distro::Arch) {
fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n");
return;
}
aurHelper = fetchAurHelper();
if (!aurHelper.has_value()) {
fprintf(stderr, "\033[31;1mPackage:\033[0m no supported AUR helper found\n");
return;
}
}
else {
fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n");
return;
}
}
std::string command = "";
@@ -207,7 +206,6 @@ bool Package::distroDependencies()
dependencies.push_back({ "dpkg-query", "dpkg" });
}
Util::System $;
for (const auto& dependency : dependencies) {
if (!findDependency(dependency.at(0))) {
fprintf(stderr, "\033[31;1mPackage:\033[0m required dependency '%s' is missing\n", dependency.at(1).c_str());
+309 -38
View File
@@ -4,8 +4,10 @@
* SPDX-License-Identifier: MIT
*/
#include <cstddef> // size_t
#include <cstdint> // uint32_t
#include <cstdio> // stderr
#include <filesystem>
#include <filesystem> // path
#include <string>
#include <unistd.h> // geteuid, setegid, seteuid
#include <vector>
@@ -226,15 +228,10 @@ TEST_CASE(PushDotfilesWithExcludePath)
TEST_CASE(PushDotfilesSelectivelyComment)
{
std::vector<std::string> fileNames = {
"__test-file-1",
"__test-file-2",
"__test-file-3",
"__test-file-4",
"__test-file-5",
"__test-file-6",
"__test-file-7",
};
std::vector<std::string> fileNames;
for (size_t i = 0; i < 36; ++i) {
fileNames.push_back( "__test-file-" + std::to_string(i + 1));
}
auto distro = Machine::the().distroId();
auto hostname = Machine::the().hostname();
@@ -242,65 +239,339 @@ TEST_CASE(PushDotfilesSelectivelyComment)
std::string placeholder = "@@@@";
std::vector<std::string> fileContents = {
// Untouched #
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
# this should be uncommented
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
// Comment #
"# >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
# this should remain commented
test data # comment
# <<<
)",
"# >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
# this should remain commented
" # >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
test data # comment
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
test data # comment
# <<<
)",
" # >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
test data # comment
# <<<
)",
// Uncomment #
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
# test data # uncomment
# <<<
)",
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
this should be commented
# <<<
)",
" # >>> distro=" + distro + R"(
# this should be uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
# test data # uncomment
# <<<
)",
" # >>> user=" + username + R"(
# this should be uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
# test data # uncomment
# <<<
)",
" # >>> hostname=" + hostname + R"(
this should remain uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
# test data # uncomment
# <<<
)",
// -----------------------------------------
// Untouched //
"// >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
// Comment //
"// >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
test data // comment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
test data // comment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
test data // comment
// <<<
)",
" // >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
test data // comment
// <<<
)",
// Uncomment //
"// >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
// test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
// test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
// test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
// test data // uncomment
// <<<
)",
// -----------------------------------------
// Untouched /**/
"/* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
// Comment /**/
"/* >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ comment
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + " */" + R"(
test data /**/ comment
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + " */" + R"(
test data /**/ comment
/* <<< */
)",
" /* >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ comment
/* <<< */
)",
// Uncomment /**/
"/* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ uncomment */
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ uncomment */
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ uncomment */
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ uncomment */
/* <<< */
)",
};
std::vector<std::string> pushedFileContents = {
// Untouched #
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
this should be uncommented
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # untouched
# <<<
)",
// Comment #
"# >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
# this should remain commented
# test data # comment
# <<<
)",
"# >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
# this should remain commented
" # >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
# test data # comment
# <<<
)",
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
# test data # comment
# <<<
)",
" # >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
# test data # comment
# <<<
)",
// Uncomment #
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # uncomment
# <<<
)",
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
# this should be commented
# <<<
)",
" # >>> distro=" + distro + R"(
this should be uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # uncomment
# <<<
)",
" # >>> user=" + username + R"(
this should be uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # uncomment
# <<<
)",
" # >>> hostname=" + hostname + R"(
this should remain uncommented
" # >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data # uncomment
# <<<
)",
// -----------------------------------------
// Untouched //
"// >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // untouched
// <<<
)",
// Comment //
"// >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
// test data // comment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
// test data // comment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
// test data // comment
// <<<
)",
" // >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
// test data // comment
// <<<
)",
// Uncomment //
"// >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // uncomment
// <<<
)",
" // >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
test data // uncomment
// <<<
)",
// -----------------------------------------
// Untouched /**/
"/* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ untouched
/* <<< */
)",
// Comment /**/
"/* >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ comment */
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + " */" + R"(
/* test data /**/ comment */
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + " */" + R"(
/* test data /**/ comment */
/* <<< */
)",
" /* >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " */" + R"(
/* test data /**/ comment */
/* <<< */
)",
// Uncomment /**/
"/* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ uncomment
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ uncomment
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ uncomment
/* <<< */
)",
" /* >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " */" + R"(
test data /**/ uncomment
/* <<< */
)",
};
createTestDotfiles(fileNames, fileContents);