Skip to content

Commit

Permalink
👷 gucc: refactor initcpio
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Aug 9, 2024
1 parent 8867f96 commit 701e610
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions gucc/src/initcpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,46 @@

using namespace std::string_view_literals;

namespace gucc::detail {
namespace {

bool Initcpio::write() const noexcept {
auto&& file_content = file_utils::read_whole_file(m_file_path);
if (file_content.empty()) {
spdlog::error("[INITCPIO] '{}' error occurred!", m_file_path);
return false;
constexpr auto modify_initcpio_line(std::string_view line, std::string_view modules, std::string_view files, std::string_view hooks) noexcept -> std::string {
if (line.starts_with("MODULES"sv)) {
return fmt::format(FMT_COMPILE("MODULES=({})"), modules);
} else if (line.starts_with("FILES"sv)) {
return fmt::format(FMT_COMPILE("FILES=({})"), files);
} else if (line.starts_with("HOOKS"sv)) {
return fmt::format(FMT_COMPILE("HOOKS=({})"), hooks);
}
std::string&& result = file_content | std::ranges::views::split('\n')
return std::string{line.data(), line.size()};
}

constexpr auto modify_initcpio_fields(std::string_view file_content, std::string_view modules, std::string_view files, std::string_view hooks) noexcept -> std::string {

Check failure on line 26 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/src/initcpio.cpp:26:16 [clang-diagnostic-error]

no return statement in constexpr function
return file_content | std::ranges::views::split('\n')
| std::ranges::views::transform([&](auto&& rng) {
/* clang-format off */
auto&& line = std::string_view(&*rng.begin(), static_cast<size_t>(std::ranges::distance(rng)));
if (line.starts_with("MODULES"sv)) {
auto&& formatted_modules = modules | std::ranges::views::join_with(' ')
| std::ranges::to<std::string>();
return fmt::format(FMT_COMPILE("MODULES=({})"), std::move(formatted_modules));
} else if (line.starts_with("FILES"sv)) {
auto&& formatted_files = files | std::ranges::views::join_with(' ')
| std::ranges::to<std::string>();
return fmt::format(FMT_COMPILE("FILES=({})"), std::move(formatted_files));
} else if (line.starts_with("HOOKS"sv)) {
auto&& formatted_hooks = hooks | std::ranges::views::join_with(' ')
| std::ranges::to<std::string>();
return fmt::format(FMT_COMPILE("HOOKS=({})"), std::move(formatted_hooks));
}
/* clang-format on */
return std::string{line.data(), line.size()};
return modify_initcpio_line(line, modules, files, hooks);
})
| std::ranges::views::join_with('\n')

Check failure on line 33 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/src/initcpio.cpp:33:31 [clang-diagnostic-error]

no member named 'join_with' in namespace 'std::ranges::views'
| std::ranges::to<std::string>();

Check failure on line 34 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/src/initcpio.cpp:34:24 [clang-diagnostic-error]

no member named 'to' in namespace 'std::ranges'

Check failure on line 34 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/src/initcpio.cpp:34:38 [clang-diagnostic-error]

expected '(' for function-style cast or type construction

Check failure on line 34 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/src/initcpio.cpp:34:40 [clang-diagnostic-error]

expected expression
}

std::ofstream mhinitcpio_file{m_file_path.data()};
if (!mhinitcpio_file.is_open()) {
spdlog::error("[INITCPIO] '{}' open failed: {}", m_file_path, std::strerror(errno));
} // namespace

namespace gucc::detail {

bool Initcpio::write() const noexcept {
auto&& file_content = file_utils::read_whole_file(m_file_path);
if (file_content.empty()) {
spdlog::error("[INITCPIO] '{}' error occurred!", m_file_path);
return false;
}
mhinitcpio_file << result;
return true;
const auto& formatted_modules = utils::join(modules, ' ');
const auto& formatted_files = utils::join(files, ' ');
const auto& formatted_hooks = utils::join(hooks, ' ');

auto result = modify_initcpio_fields(file_content, formatted_modules, formatted_files, formatted_hooks);
return file_utils::create_file_for_overwrite(m_file_path, result);
}

bool Initcpio::parse_file() noexcept {
Expand Down

0 comments on commit 701e610

Please sign in to comment.