Line data Source code
1 : /*! 2 : * \file esys/repo/grepo/folder_grepo.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2022 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/grepo/folder.h" 20 : #include "esys/repo/grepo/manifest.h" 21 : 22 : #include <boost/filesystem.hpp> 23 : 24 : namespace esys::repo::grepo 25 : { 26 : 27 2 : Folder::Folder(const std::string &folder_path) 28 2 : : m_folder_path(folder_path) 29 : { 30 2 : } 31 : 32 4 : Folder::~Folder() = default; 33 : 34 : 35 0 : bool Folder::is_folder(const std::string &path) 36 : { 37 0 : boost::filesystem::path folder_path = path; 38 : 39 0 : folder_path /= ".repo"; 40 0 : return boost::filesystem::exists(folder_path); 41 0 : } 42 : 43 2 : Result Folder::open(const std::string &folder_path) 44 : { 45 2 : if (!folder_path.empty()) set_folder_path(folder_path); 46 : 47 2 : if (get_folder_path().empty()) return ESYSREPO_RESULT(ResultCode::EMPTY_PATH); 48 : 49 2 : boost::filesystem::path path = get_folder_path(); 50 4 : if (path.filename() != ".repo") path /= ".repo"; 51 : 52 2 : if (!boost::filesystem::exists(path)) return ESYSREPO_RESULT(ResultCode::PATH_NOT_EXISTING, path.string()); 53 : 54 4 : boost::filesystem::path manifest_xml = path / "manifest.xml"; 55 2 : if (!boost::filesystem::exists(manifest_xml)) return ESYSREPO_RESULT(ResultCode::FILE_NOT_EXISTING, path.string()); 56 : 57 2 : auto manifest = std::make_shared<Manifest>(); 58 : 59 2 : Result result = manifest->read(manifest_xml.string()); 60 2 : if (result.error()) return ESYSREPO_RESULT(result); 61 : 62 2 : auto data = manifest->get_data(); 63 2 : if (data->get_includes().size() != 1) ESYSREPO_RESULT(ResultCode::GREPO_FOLDER_NO_INCLUDE, path.string()); 64 : 65 2 : auto include = data->get_includes()[0]; 66 2 : if (include == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 67 : 68 2 : auto config = get_or_new_config(); 69 2 : config->set_manifest_type(manifest::Type::RAW_GOOGLE_MANIFEST); 70 2 : config->set_manifest_kind(manifest::Kind::ISOLATED); 71 2 : config->set_manifest_format(manifest::Format::XML); 72 2 : config->set_manifest_path(include->get_name()); 73 4 : set_config(config); 74 2 : return ESYSREPO_RESULT(ResultCode::OK); 75 12 : } 76 : 77 2 : void Folder::set_folder_path(const std::string &folder_path) 78 : { 79 2 : m_folder_path = folder_path; 80 2 : } 81 : 82 4 : const std::string &Folder::get_folder_path() const 83 : { 84 4 : return m_folder_path; 85 : } 86 : 87 4 : std::shared_ptr<Config> Folder::get_config() const 88 : { 89 4 : return m_config; 90 : } 91 : 92 4 : void Folder::set_config(std::shared_ptr<Config> config) 93 : { 94 4 : m_config = config; 95 4 : } 96 : 97 2 : std::shared_ptr<Config> Folder::get_or_new_config() 98 : { 99 2 : auto cfg = get_config(); 100 : 101 3 : if (cfg != nullptr) return cfg; 102 : 103 1 : set_config(std::make_shared<Config>()); 104 : 105 1 : return get_config(); 106 2 : } 107 : 108 : } // namespace esys::repo::grepo