8#include "nlohmann/json.hpp"
17std::filesystem::path checksums_file(
const std::filesystem::path &project_root,
const std::string &tileset_name)
19 return project_root /
"porytiles" /
"tilesets" / tileset_name /
"tileset.cache.json";
26std::unordered_map<ArtifactKey, std::string>
29 std::unordered_map<ArtifactKey, std::string> checksums{};
31 for (
const auto &key : keys) {
34 const auto absolute_path = project_root_ / key.key();
35 if (!std::filesystem::exists(absolute_path)) {
36 panic(std::format(
"expected artifact '{}' does not exist", absolute_path.string()));
38 std::ifstream stream{absolute_path};
39 const auto key_digest = digest.digest(stream);
40 checksums.emplace(key, key_digest);
46std::unordered_map<ArtifactKey, std::string>
49 const auto artifact_checksum_path = checksums_file(project_root_, tileset_name);
52 if (!exists(artifact_checksum_path)) {
56 std::ifstream file{artifact_checksum_path};
57 nlohmann::json json_data;
60 std::unordered_map<ArtifactKey, std::string> checksums;
61 for (
const auto &[key, value] : json_data.items()) {
62 const auto full_path = std::filesystem::path{key};
63 checksums.emplace(
ArtifactKey{full_path}, value.get<std::string>());
70 const std::string &tileset_name,
const std::unordered_map<ArtifactKey, std::string> &checksums)
const
72 const auto artifact_checksum_file = checksums_file(project_root_, tileset_name);
73 std::filesystem::create_directories(artifact_checksum_file.parent_path());
74 std::ofstream file{artifact_checksum_file};
77 std::map<std::string, std::string> sorted_checksums;
78 for (
const auto &[artifact_key, checksum] : checksums) {
80 sorted_checksums[artifact_key.key()] = checksum;
92 nlohmann::json json_data;
93 for (
const auto &[path, checksum] : sorted_checksums) {
94 json_data[path] = checksum;
98 file << json_data.dump(2);
A type-safe wrapper for artifact keys.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
std::unordered_map< ArtifactKey, std::string > load_cached_tileset_checksums(const std::string &tileset_name) const override
Loads the cached checksums for the given Tileset.
ChainableResult< void > cache_tileset_checksums(const std::string &tileset_name, const std::unordered_map< ArtifactKey, std::string > &checksums) const override
Caches checksums for the given Tileset to persistent storage.
std::unordered_map< ArtifactKey, std::string > compute_tileset_artifact_checksums(const std::vector< ArtifactKey > &keys) const override
Computes checksums for the given Tileset artifacts.
Computes the MD5 digest of an input stream.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.