diff --git a/src/configuration.h b/src/configuration.h index 5bbc916b..8a594571 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -28,22 +28,19 @@ inline auto find_configuration(const std::filesystem::path &path) return sourcemeta::blaze::Configuration::find(path); } -inline auto read_configuration( +inline auto load_configuration( const sourcemeta::core::Options &options, - const std::optional &configuration_path, - const std::optional &schema_path = std::nullopt) + const std::optional &configuration_path) -> const std::optional & { using CacheKey = std::optional; static std::map> configuration_cache; - // Check if configuration is already cached for this path auto iterator{configuration_cache.find(configuration_path)}; if (iterator != configuration_cache.end()) { return iterator->second; } - // Compute and cache the configuration std::optional result{std::nullopt}; if (configuration_path.has_value()) { LOG_DEBUG(options) << "Using configuration file: " @@ -95,16 +92,6 @@ inline auto read_configuration( } } } - - if (schema_path.has_value() && - !result.value().applies_to(schema_path.value())) { - LOG_DEBUG(options) - << "Ignoring configuration file given extensions mismatch: " - << sourcemeta::core::weakly_canonical(configuration_path.value()) - .string() - << "\n"; - result = std::nullopt; - } } auto [inserted_iterator, inserted] = @@ -112,6 +99,26 @@ inline auto read_configuration( return inserted_iterator->second; } +inline auto read_configuration( + const sourcemeta::core::Options &options, + const std::optional &configuration_path, + const std::optional &schema_path = std::nullopt) + -> const std::optional & { + const auto &configuration{load_configuration(options, configuration_path)}; + if (configuration.has_value() && schema_path.has_value() && + !configuration.value().applies_to(schema_path.value())) { + LOG_DEBUG(options) + << "Ignoring configuration file given extensions mismatch: " + << sourcemeta::core::weakly_canonical(configuration_path.value()) + .string() + << "\n"; + static const std::optional empty{ + std::nullopt}; + return empty; + } + return configuration; +} + } // namespace sourcemeta::jsonschema #endif diff --git a/src/input.h b/src/input.h index 6ff18636..c7c9bc93 100644 --- a/src/input.h +++ b/src/input.h @@ -125,17 +125,12 @@ inline auto merge_configuration_ignore(const std::filesystem::path &configuration_path, std::set &blacklist, const sourcemeta::core::Options &options) -> void { - try { - const auto configuration{sourcemeta::blaze::Configuration::read_json( - configuration_path, sourcemeta::core::read_file_to_string<>)}; - for (const auto &ignore_path : configuration.ignore) { - LOG_VERBOSE(options) << "Ignoring path from configuration: " - << ignore_path << "\n"; - blacklist.insert(ignore_path); - } - } catch (const sourcemeta::blaze::ConfigurationParseError &error) { - throw sourcemeta::core::FileError< - sourcemeta::blaze::ConfigurationParseError>(configuration_path, error); + const auto &configuration{load_configuration(options, configuration_path)}; + assert(configuration.has_value()); + for (const auto &ignore_path : configuration.value().ignore) { + LOG_VERBOSE(options) << "Ignoring path from configuration: " << ignore_path + << "\n"; + blacklist.insert(ignore_path); } } @@ -486,27 +481,18 @@ inline auto for_each_json(const std::vector &arguments, } for (const auto &entry : arguments) { - std::optional entry_configuration{ + std::optional entry_configuration_path{ std::nullopt}; if (entry != "-") { const auto entry_path{ sourcemeta::core::weakly_canonical(std::filesystem::path{entry})}; - const auto configuration_path{ + entry_configuration_path = find_configuration(std::filesystem::is_directory(entry_path) ? entry_path - : entry_path.parent_path())}; - if (configuration_path.has_value()) { - try { - entry_configuration = sourcemeta::blaze::Configuration::read_json( - configuration_path.value(), - sourcemeta::core::read_file_to_string<>); - } catch (const sourcemeta::blaze::ConfigurationParseError &error) { - throw sourcemeta::core::FileError< - sourcemeta::blaze::ConfigurationParseError>( - configuration_path.value(), error); - } - } + : entry_path.parent_path()); } + const auto &entry_configuration{ + load_configuration(options, entry_configuration_path)}; const auto &extensions{parse_extensions(options, entry_configuration)}; const auto before{result.size()}; handle_json_entry(entry, blacklist, extensions, result, options);