From 6c6d098ff9179d225fa1f4c3a8937db66687d963 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Thu, 11 Aug 2022 04:07:16 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20(oops)=20move=20initcpio=20from?= =?UTF-8?q?=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + src/initcpio.cpp | 65 ++++++++++++++++++++++++++++++++++++++ src/initcpio.hpp | 26 ++++++++++++++++ tests/CMakeLists.txt | 1 + tests/unit-initcpio.cpp | 69 ++--------------------------------------- 5 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 src/initcpio.cpp create mode 100644 src/initcpio.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c1ec1d..c88d108 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,7 @@ add_executable(${PROJECT_NAME} src/screen_service.hpp src/screen_service.cpp src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp + src/initcpio.cpp src/initcpio.hpp src/disk.cpp src/disk.hpp src/drivers.cpp src/drivers.hpp src/widgets.cpp src/widgets.hpp diff --git a/src/initcpio.cpp b/src/initcpio.cpp new file mode 100644 index 0000000..4c89173 --- /dev/null +++ b/src/initcpio.cpp @@ -0,0 +1,65 @@ +#include "initcpio.hpp" +#include "utils.hpp" + +#include // for ofstream + +#include + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" +#elif defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnull-dereference" +#pragma GCC diagnostic ignored "-Wuseless-cast" +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + +#include +#include +#include +#include +#include +#include + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + +namespace detail { + +bool Initcpio::write() const noexcept { + auto&& file_content = utils::read_whole_file(m_file_path); + std::string&& result = file_content | ranges::views::split('\n') + | ranges::views::transform([&](auto&& rng) { + auto&& line = std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); + if (line.starts_with("MODULES")) { + auto&& formatted_modules = modules | ranges::views::join(' ') + | ranges::to(); + return fmt::format("MODULES=({})", formatted_modules); + } else if (line.starts_with("FILES")) { + auto&& formatted_files = files | ranges::views::join(' ') + | ranges::to(); + return fmt::format("FILES=({})", formatted_files); + } else if (line.starts_with("HOOKS")) { + auto&& formatted_hooks = hooks | ranges::views::join(' ') + | ranges::to(); + return fmt::format("HOOKS=({})", formatted_hooks); + } + return std::string{line.data(), line.size()}; + }) + | ranges::views::join('\n') + | ranges::to(); + result += '\n'; + + /* clang-format off */ + std::ofstream mhinitcpio_file{m_file_path.data()}; + if (!mhinitcpio_file.is_open()) { return false; } + /* clang-format on */ + mhinitcpio_file << result; + return true; +} + +} // namespace detail diff --git a/src/initcpio.hpp b/src/initcpio.hpp new file mode 100644 index 0000000..848ae3c --- /dev/null +++ b/src/initcpio.hpp @@ -0,0 +1,26 @@ +#ifndef INITCPIO_HPP +#define INITCPIO_HPP + +#include // for string_view +#include // for string +#include // for vector + +namespace detail { + +class Initcpio { + public: + Initcpio(const std::string_view& file_path) noexcept : m_file_path(file_path) {} + + bool write() const noexcept; + + std::vector modules{}; + std::vector files{}; + std::vector hooks{}; + + private: + std::string_view m_file_path{}; +}; + +} // namespace detail + +#endif // INITCPIO_HPP diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3406314..5bc6bea 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ list(APPEND test_SOURCES ${CMAKE_SOURCE_DIR}/src/config.cpp ${CMAKE_SOURCE_DIR}/src/utils.cpp + ${CMAKE_SOURCE_DIR}/src/initcpio.cpp ${CMAKE_SOURCE_DIR}/src/disk.cpp ${CMAKE_SOURCE_DIR}/src/drivers.cpp ${CMAKE_SOURCE_DIR}/src/widgets.cpp diff --git a/tests/unit-initcpio.cpp b/tests/unit-initcpio.cpp index a022458..f00443d 100644 --- a/tests/unit-initcpio.cpp +++ b/tests/unit-initcpio.cpp @@ -1,4 +1,5 @@ #include "utils.hpp" +#include "initcpio.hpp" #include #include @@ -9,29 +10,6 @@ #include -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wold-style-cast" -#elif defined(__GNUC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wnull-dereference" -#pragma GCC diagnostic ignored "-Wuseless-cast" -#pragma GCC diagnostic ignored "-Wold-style-cast" -#endif - -#include -#include -#include -#include -#include -#include - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#pragma GCC diagnostic pop -#endif - namespace fs = std::filesystem; static constexpr auto MKINITCPIO_STR = R"( @@ -62,49 +40,6 @@ FILES=() HOOKS=(base usr lvm2 zfs) )"; -class Initcpio { - public: - Initcpio(const std::string_view& file_path) noexcept : m_file_path(file_path) {} - - bool write() const noexcept { - auto&& file_content = utils::read_whole_file(m_file_path); - std::string&& result = file_content | ranges::views::split('\n') - | ranges::views::transform([&](auto&& rng) { - auto&& line = std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); - if (line.starts_with("MODULES")) { - auto&& formatted_modules = modules | ranges::views::join(' ') - | ranges::to(); - return fmt::format("MODULES=({})", formatted_modules); - } else if (line.starts_with("FILES")) { - auto&& formatted_files = files | ranges::views::join(' ') - | ranges::to(); - return fmt::format("FILES=({})", formatted_files); - } else if (line.starts_with("HOOKS")) { - auto&& formatted_hooks = hooks | ranges::views::join(' ') - | ranges::to(); - return fmt::format("HOOKS=({})", formatted_hooks); - } - return std::string{line.data(), line.size()}; - }) - | ranges::views::join('\n') - | ranges::to(); - result += '\n'; - - /* clang-format off */ - std::ofstream mhinitcpio_file{m_file_path.data()}; - if (!mhinitcpio_file.is_open()) { return false; } - /* clang-format on */ - mhinitcpio_file << result; - return true; - } - - std::vector modules{}; - std::vector files{}; - std::vector hooks{}; - - private: - std::string_view m_file_path{}; -}; int main() { static constexpr std::string_view filename{"/tmp/mkinitcpio.conf"}; @@ -117,7 +52,7 @@ int main() { mhinitcpio_file << MKINITCPIO_STR; mhinitcpio_file.close(); - auto initcpio = Initcpio{filename}; + auto initcpio = detail::Initcpio{filename}; // Insert data. initcpio.modules.insert(initcpio.modules.end(), {"crc32c-intel"});