Line data Source code
1 : /*! 2 : * \file esys/repo/manifest/detect_manifest.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/manifest/detect.h" 20 : #include "esys/repo/grepo/folder.h" 21 : 22 : #include <boost/filesystem.hpp> 23 : 24 : namespace esys::repo::manifest 25 : { 26 : 27 10 : Detect::Detect(const std::string &folder_path) 28 10 : : m_folder_path(folder_path) 29 : { 30 10 : } 31 : 32 29 : Detect::~Detect() = default; 33 : 34 10 : void Detect::set_folder_path(const std::string &folder_path) 35 : { 36 10 : m_folder_path = folder_path; 37 10 : } 38 : 39 31 : const std::string &Detect::get_folder_path() const 40 : { 41 31 : return m_folder_path; 42 : } 43 : 44 10 : Result Detect::detect(std::shared_ptr<Config> config) 45 : { 46 10 : if (get_folder_path().empty()) return ESYSREPO_RESULT(ResultCode::EMPTY_PATH); 47 : 48 10 : if (config != nullptr) set_config(config); 49 20 : config = get_config_or_new(); 50 : 51 10 : boost::filesystem::path file_path = get_folder_path(); 52 10 : file_path /= ".esysrepo"; 53 : 54 10 : if (boost::filesystem::exists(file_path)) 55 : { 56 9 : std::shared_ptr<ConfigFolder> config_folder = std::make_shared<ConfigFolder>(); 57 : 58 18 : config_folder->set_config(config); 59 9 : Result result = config_folder->open(get_folder_path()); 60 27 : if (result.success()) set_config_folder(config_folder); 61 18 : return ESYSREPO_RESULT(result); 62 18 : } 63 : 64 1 : file_path = get_folder_path(); 65 1 : file_path /= ".repo"; 66 : 67 1 : if (boost::filesystem::exists(file_path)) 68 : { 69 1 : std::shared_ptr<grepo::Folder> folder = std::make_shared<grepo::Folder>(); 70 : 71 2 : folder->set_config(config); 72 1 : Result result = folder->open(get_folder_path()); 73 2 : return ESYSREPO_RESULT(result); 74 2 : } 75 : 76 0 : file_path = get_folder_path(); 77 0 : file_path /= ".esysrepo.manifest"; 78 : 79 0 : if (boost::filesystem::exists(file_path)) 80 : { 81 0 : config->set_manifest_type(Type::ESYSREPO_MANIFEST); 82 0 : config->set_manifest_kind(Kind::EMBEDDED); 83 0 : config->set_manifest_path(file_path.string()); 84 0 : config->set_manifest_format(Format::UNKNOWN); 85 0 : return ESYSREPO_RESULT(ResultCode::OK); 86 : } 87 : 88 0 : file_path = get_folder_path(); 89 0 : file_path /= ".esysrepo.manifest.xml"; 90 : 91 0 : if (boost::filesystem::exists(file_path)) 92 : { 93 0 : config->set_manifest_type(Type::ESYSREPO_MANIFEST); 94 0 : config->set_manifest_kind(Kind::EMBEDDED); 95 0 : config->set_manifest_path(file_path.string()); 96 0 : config->set_manifest_format(Format::XML); 97 0 : return ESYSREPO_RESULT(ResultCode::OK); 98 : } 99 : 100 0 : file_path = get_folder_path(); 101 0 : file_path /= ".esysrepo.manifest.json"; 102 : 103 0 : if (boost::filesystem::exists(file_path)) 104 : { 105 0 : config->set_manifest_type(Type::ESYSREPO_MANIFEST); 106 0 : config->set_manifest_kind(Kind::EMBEDDED); 107 0 : config->set_manifest_path(file_path.string()); 108 0 : config->set_manifest_format(Format::JSON); 109 0 : return ESYSREPO_RESULT(ResultCode::OK); 110 : } 111 : 112 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FAILED_TO_DETECT); 113 10 : } 114 : 115 0 : void Detect::set_config(std::shared_ptr<Config> config) 116 : { 117 0 : m_config = config; 118 0 : } 119 : 120 2 : std::shared_ptr<Config> Detect::get_config() 121 : { 122 2 : return m_config; 123 : } 124 : 125 10 : std::shared_ptr<Config> Detect::get_config_or_new() 126 : { 127 10 : if (m_config == nullptr) m_config = std::make_shared<Config>(); 128 10 : return m_config; 129 : } 130 : 131 9 : void Detect::set_config_folder(std::shared_ptr<ConfigFolder> config_folder) 132 : { 133 9 : m_config_folder = config_folder; 134 9 : } 135 : 136 19 : std::shared_ptr<ConfigFolder> Detect::get_config_folder() 137 : { 138 19 : return m_config_folder; 139 : } 140 : 141 : } // namespace esys::repo::manifest