#include #include #include #include #include #include "config.hpp" #define JSON_INLINE_ALL #include namespace dropshell { typedef enum template_source_type { TEMPLATE_SOURCE_TYPE_LOCAL, TEMPLATE_SOURCE_TYPE_REGISTRY, TEMPLATE_SOURCE_NOT_SET } template_source_type; class template_info { public: template_info() : mIsSet(false) {} template_info(const std::string& template_name, const std::string& location_id, const std::filesystem::path& local_template_path); virtual ~template_info() {} bool is_set() const { return mIsSet; } std::string name() const { return mTemplateName; } std::string locationID() const { return mLocationID; } std::filesystem::path local_template_path() const { return mTemplateLocalPath; } bool template_valid() const { return mTemplateValid; } uint64_t hash() const { return mHash; } private: std::string mTemplateName; std::string mLocationID; std::filesystem::path mTemplateLocalPath; // source or cache. bool mTemplateValid; bool mIsSet; uint64_t mHash; }; class template_source_interface { public: virtual ~template_source_interface() {} virtual std::set get_template_list() = 0; virtual bool has_template(const std::string& template_name) = 0; virtual template_info get_template_info(const std::string& template_name) = 0; virtual bool template_command_exists(const std::string& template_name,const std::string& command) = 0; virtual std::string get_description() = 0; }; class template_source_registry : public template_source_interface { public: template_source_registry(tRegistryEntry registry) : mRegistry(registry) {} ~template_source_registry() {} std::set get_template_list(); bool has_template(const std::string& template_name); template_info get_template_info(const std::string& template_name); bool template_command_exists(const std::string& template_name,const std::string& command); std::string get_description() { return "Registry: " + mRegistry.name + " (" + mRegistry.url + ")"; } private: std::filesystem::path get_cache_dir(); private: tRegistryEntry mRegistry; std::vector mTemplates; // cached list. }; class template_source_local : public template_source_interface { public: template_source_local(std::string local_path) : mLocalPath(local_path) {} ~template_source_local() {} std::set get_template_list(); bool has_template(const std::string& template_name); template_info get_template_info(const std::string& template_name); bool template_command_exists(const std::string& template_name,const std::string& command); std::string get_description() { return "Local: " + mLocalPath.string(); } private: std::filesystem::path mLocalPath; }; class template_manager { public: template_manager() : mLoaded(false) {} ~template_manager() {} std::set get_template_list() const; bool has_template(const std::string& template_name) const; template_info get_template_info(const std::string& template_name) const; bool template_command_exists(const std::string& template_name,const std::string& command) const; bool create_template(const std::string& template_name) const; static bool test_template(const std::string& template_path); void list_templates() const; void load_sources(); void print_sources() const; bool is_loaded() const { return mLoaded; } int get_source_count() const { return mSources.size(); } private: static bool required_file(std::string path, std::string template_name); template_source_interface* get_source(const std::string& template_name) const; private: bool mLoaded; mutable std::vector> mSources; }; template_manager & gTemplateManager(); } // namespace dropshell