Line data Source code
1 : /*! 2 : * \file esys/repo/workspace.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2023 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/repository.h" 20 : #include "esys/repo/workspace.h" 21 : 22 : #include "esys/repo/configfolder.h" 23 : 24 : #include <boost/filesystem.hpp> 25 : 26 : namespace esys::repo 27 : { 28 : 29 1 : Workspace::Workspace() = default; 30 : 31 3 : Workspace::~Workspace() = default; 32 : 33 1 : void Workspace::set_path(const std::string &path) 34 : { 35 1 : m_path = path; 36 1 : } 37 : 38 8 : const std::string &Workspace::get_path() const 39 : { 40 8 : return m_path; 41 : } 42 : 43 1 : void Workspace::set_folder_path(const std::string &folder_path) 44 : { 45 1 : m_folder_path = folder_path; 46 1 : } 47 : 48 0 : const std::string &Workspace::get_folder_path() const 49 : { 50 0 : return m_folder_path; 51 : } 52 : 53 1 : void Workspace::set_kind(Kind kind) 54 : { 55 1 : m_kind = kind; 56 1 : } 57 : 58 0 : Workspace::Kind Workspace::get_kind() const 59 : { 60 0 : return m_kind; 61 : } 62 : 63 1 : Result Workspace::find(const std::string &path) 64 : { 65 1 : boost::filesystem::path cur_path; 66 : 67 1 : if (!path.empty()) 68 1 : cur_path = path; 69 : else 70 0 : cur_path = boost::filesystem::current_path(); 71 : 72 1 : set_folder_path(cur_path.string()); 73 : 74 1 : while (!cur_path.empty()) 75 : { 76 1 : if (is_esysrepo_folder(cur_path.string())) 77 : { 78 1 : set_path(cur_path.string()); 79 1 : set_kind(Kind::ESYSREPO); 80 1 : return ResultCode::OK; 81 : } 82 0 : if (is_grepo_folder(cur_path.string())) 83 : { 84 0 : set_path(cur_path.string()); 85 0 : set_kind(Kind::GREPO); 86 0 : return ResultCode::OK; 87 : } 88 0 : cur_path = cur_path.parent_path(); 89 : } 90 : 91 0 : return ResultCode::GENERIC_ERROR; 92 1 : } 93 : 94 1 : Result Workspace::load(const std::string &path) 95 : { 96 1 : auto result = find(path); 97 1 : if (result.error()) return ESYSREPO_RESULT(result); 98 : 99 2 : if (get_load_folder() == nullptr) set_load_folder(std::make_shared<LoadFolder>()); 100 : 101 1 : get_load_folder()->set_folder_path(get_path()); 102 1 : result = get_load_folder()->run(); 103 1 : if (result.ok()) 104 : { 105 2 : m_manifest = get_load_folder()->get_manifest(); 106 : } 107 2 : return ESYSREPO_RESULT(result); 108 1 : } 109 : 110 6 : std::string Workspace::get_full_path(std::shared_ptr<manifest::Repository> repo) const 111 : { 112 6 : boost::filesystem::path p = get_path(); 113 : 114 6 : p /= repo->get_path(); 115 : 116 6 : return p.string(); 117 6 : } 118 : 119 1 : void Workspace::set_load_folder(std::shared_ptr<LoadFolder> load_folder) 120 : { 121 1 : m_load_folder = load_folder; 122 1 : } 123 : 124 4 : std::shared_ptr<LoadFolder> Workspace::get_load_folder() const 125 : { 126 4 : return m_load_folder; 127 : } 128 : 129 0 : std::shared_ptr<ConfigFolder> Workspace::get_config_folder() const 130 : { 131 0 : if (get_load_folder() == nullptr) return nullptr; 132 0 : return get_load_folder()->get_config_folder(); 133 : } 134 : 135 4 : std::shared_ptr<Manifest> Workspace::get_manifest() const 136 : { 137 4 : return m_manifest; 138 : } 139 : 140 0 : bool Workspace::is_grepo_folder(const std::string &path) 141 : { 142 0 : boost::filesystem::path folder_path = path; 143 : 144 0 : folder_path /= ".repo"; 145 0 : return boost::filesystem::exists(folder_path); 146 0 : } 147 : 148 1 : bool Workspace::is_esysrepo_folder(const std::string &path) 149 : { 150 1 : boost::filesystem::path folder_path = path; 151 : 152 1 : folder_path /= ".esysrepo"; 153 1 : return boost::filesystem::exists(folder_path); 154 1 : } 155 : 156 : } // namespace esys::repo