👷 (oops) move initcpio from test

This commit is contained in:
Vladislav Nepogodin 2022-08-11 04:07:16 +04:00
parent bcfe5d0dbd
commit 6c6d098ff9
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
5 changed files with 95 additions and 67 deletions

View File

@ -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

65
src/initcpio.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "initcpio.hpp"
#include "utils.hpp"
#include <fstream> // for ofstream
#include <fmt/format.h>
#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 <range/v3/algorithm/find.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/getlines.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/split.hpp>
#include <range/v3/view/transform.hpp>
#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<size_t>(ranges::distance(rng)));
if (line.starts_with("MODULES")) {
auto&& formatted_modules = modules | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("MODULES=({})", formatted_modules);
} else if (line.starts_with("FILES")) {
auto&& formatted_files = files | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("FILES=({})", formatted_files);
} else if (line.starts_with("HOOKS")) {
auto&& formatted_hooks = hooks | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("HOOKS=({})", formatted_hooks);
}
return std::string{line.data(), line.size()};
})
| ranges::views::join('\n')
| ranges::to<std::string>();
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

26
src/initcpio.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef INITCPIO_HPP
#define INITCPIO_HPP
#include <string_view> // for string_view
#include <string> // for string
#include <vector> // 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<std::string> modules{};
std::vector<std::string> files{};
std::vector<std::string> hooks{};
private:
std::string_view m_file_path{};
};
} // namespace detail
#endif // INITCPIO_HPP

View File

@ -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

View File

@ -1,4 +1,5 @@
#include "utils.hpp"
#include "initcpio.hpp"
#include <cassert>
#include <fstream>
@ -9,29 +10,6 @@
#include <fmt/core.h>
#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 <range/v3/algorithm/find.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/getlines.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/split.hpp>
#include <range/v3/view/transform.hpp>
#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<size_t>(ranges::distance(rng)));
if (line.starts_with("MODULES")) {
auto&& formatted_modules = modules | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("MODULES=({})", formatted_modules);
} else if (line.starts_with("FILES")) {
auto&& formatted_files = files | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("FILES=({})", formatted_files);
} else if (line.starts_with("HOOKS")) {
auto&& formatted_hooks = hooks | ranges::views::join(' ')
| ranges::to<std::string>();
return fmt::format("HOOKS=({})", formatted_hooks);
}
return std::string{line.data(), line.size()};
})
| ranges::views::join('\n')
| ranges::to<std::string>();
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<std::string> modules{};
std::vector<std::string> files{};
std::vector<std::string> 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"});