From f14985f783f2f96fc944e397c6c5072f8d3bc9b5 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 2 Jan 2020 16:09:11 +0100 Subject: [PATCH] [tools] Added cache tool A tool for adding resources to a cache database, important for adding fixtures to our render test cache database. --- bin/cache.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ next/bin/CMakeLists.txt | 14 ++++++ 2 files changed, 111 insertions(+) create mode 100644 bin/cache.cpp diff --git a/bin/cache.cpp b/bin/cache.cpp new file mode 100644 index 000000000..eee1d61b3 --- /dev/null +++ b/bin/cache.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + args::ArgumentParser p("Mapbox GL cache tool", ""); + args::HelpFlag helpFlag(p, "help", "Display this help menu", {'h'}); + + args::ValueFlag urlValue(p, "URL", "Resource URL (required)", {'u'}, args::Options::Required); + args::ValueFlag cacheValue( + p, "cache", "Path to the cache database (required)", {'c'}, args::Options::Required); + args::ValueFlag dataValue( + p, "data", "Path to the resource data (required)", {'d'}, args::Options::Required); + args::ValueFlag etagValue(p, "etag", "Cache eTag, none otherwise", {'t'}); + args::ValueFlag expiresValue(p, "expires", "Expires date, will use 'now' otherwise", {'e'}); + args::ValueFlag modifiedValue(p, "modified", "Modified date, will use 'now' otherwise", {'m'}); + + std::unordered_map typeMap{{"glyphs", mbgl::Resource::Glyphs}, + {"image", mbgl::Resource::Image}, + {"source", mbgl::Resource::Source}, + {"sprite-image", mbgl::Resource::SpriteImage}, + {"sprite-json", mbgl::Resource::SpriteJSON}, + {"style", mbgl::Resource::Style}, + {"tile", mbgl::Resource::Tile}}; + + std::string typeHelp("One of the following (required):"); + for (auto key : typeMap) { + typeHelp += " " + key.first; + } + + args::MapFlag typeFlag( + p, "type", typeHelp, {"type"}, typeMap, args::Options::Required); + + args::Group tileIdGroup(p, "Coordinates (required for 'tile')", args::Group::Validators::AllOrNone); + args::ValueFlag xValueFlag(tileIdGroup, "x", "Tile x coordinate", {'x'}); + args::ValueFlag yValueFlag(tileIdGroup, "y", "Tile y coordinate", {'y'}); + args::ValueFlag zValueFlag(tileIdGroup, "z", "The zoom level", {'z'}); + + try { + p.ParseCLI(argc, argv); + } catch (const args::Help&) { + std::cout << p; + exit(0); + } catch (const args::ParseError& e) { + std::cerr << e.what() << std::endl; + std::cerr << p; + exit(1); + } catch (const args::ValidationError& e) { + std::cerr << e.what() << std::endl; + std::cerr << p; + exit(2); + } + + auto file = mapbox::base::io::readFile(args::get(dataValue)); + if (!file) { + std::cerr << file.error() << std::endl; + exit(3); + } + + mbgl::Response response; + response.data = std::make_shared(*file); + response.etag = etagValue ? args::get(etagValue) : std::string(); + response.expires = expiresValue ? mbgl::Timestamp(mbgl::Seconds(args::get(expiresValue))) : mbgl::util::now(); + response.modified = modifiedValue ? mbgl::Timestamp(mbgl::Seconds(args::get(modifiedValue))) : mbgl::util::now(); + + mbgl::Resource resource(args::get(typeFlag), args::get(urlValue)); + + if (args::get(typeFlag) == mbgl::Resource::Tile) { + if (!xValueFlag || !yValueFlag || !zValueFlag) { + std::cerr << "Error: -x, -y and -z must be provided for tiles" << std::endl; + exit(1); + } + + resource.tileData = {{args::get(urlValue), + 1, + args::get(xValueFlag), + args::get(yValueFlag), + static_cast(args::get(zValueFlag))}}; + } + + mbgl::util::RunLoop loop; + mbgl::DefaultFileSource fileSource(args::get(cacheValue), "."); + + fileSource.put(resource, response); + + return 0; +} diff --git a/next/bin/CMakeLists.txt b/next/bin/CMakeLists.txt index daef945a4..b2414a6c8 100644 --- a/next/bin/CMakeLists.txt +++ b/next/bin/CMakeLists.txt @@ -1,3 +1,17 @@ +add_executable( + mbgl-cache + ${MBGL_ROOT}/bin/cache.cpp +) + +target_link_libraries( + mbgl-cache + PRIVATE + Mapbox::Base::Extras::args + Mapbox::Base::io + mbgl-compiler-options + mbgl-core +) + add_executable( mbgl-offline ${MBGL_ROOT}/bin/offline.cpp