Line data Source code
1 : /*! 2 : * \file esys/repo/configfolder.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020 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/configfolder.h" 20 : #include "esys/repo/manifest/base.h" 21 : #include "esys/repo/grepo/manifest.h" 22 : 23 : #include <boost/filesystem.hpp> 24 : 25 : namespace esys::repo 26 : { 27 : 28 59 : ConfigFolder::ConfigFolder() 29 59 : : log::User() 30 : { 31 59 : } 32 : 33 212 : ConfigFolder::~ConfigFolder() = default; 34 : 35 80 : void ConfigFolder::set_workspace_path(const std::string &parent_path) 36 : { 37 80 : boost::filesystem::path path = parent_path; 38 : 39 160 : boost::filesystem::absolute(path).make_preferred(); 40 : 41 240 : m_parent_path = boost::filesystem::absolute(path).make_preferred().string(); 42 80 : } 43 : 44 669 : const std::string &ConfigFolder::get_workspace_path() const 45 : { 46 669 : return m_parent_path; 47 : } 48 : 49 80 : void ConfigFolder::populate_all_pathes() 50 : { 51 80 : boost::filesystem::path path = get_workspace_path(); 52 80 : boost::filesystem::path temp; 53 80 : boost::filesystem::path config_file; 54 : 55 80 : path /= ".esysrepo"; 56 80 : m_path = path.string(); 57 : 58 80 : temp = path; 59 80 : temp /= "temp"; 60 80 : m_temp_path = temp.string(); 61 : 62 80 : config_file = path; 63 80 : config_file /= "config.json"; 64 80 : m_config_file_path = config_file.string(); 65 : 66 80 : boost::filesystem::path pins_file = path; 67 80 : pins_file /= "pins.json"; 68 160 : m_pins_file_path = pins_file.string(); 69 80 : } 70 : 71 23 : Result ConfigFolder::create(const std::string &parent_path, bool ok_if_exists) 72 : { 73 23 : if (!parent_path.empty()) set_workspace_path(parent_path); 74 : 75 23 : boost::filesystem::path rel_parent_path = boost::filesystem::relative(get_workspace_path()); 76 : 77 23 : if (!boost::filesystem::exists(get_workspace_path())) 78 : { 79 2 : bool result = boost::filesystem::create_directories(get_workspace_path()); 80 2 : if (result) 81 : { 82 4 : info("Created folder : " + rel_parent_path.string()); 83 : } 84 : else 85 : { 86 0 : error("Couldn't create output folder : " + rel_parent_path.string()); 87 0 : return ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, get_workspace_path()); 88 : } 89 : } 90 : 91 23 : populate_all_pathes(); 92 : 93 23 : boost::filesystem::path rel_path = boost::filesystem::relative(get_path()); 94 : 95 23 : if (!boost::filesystem::exists(get_path())) 96 : { 97 23 : bool result = boost::filesystem::create_directory(get_path()); 98 23 : if (!result) 99 : { 100 0 : error("The following folder couldn't be created : " + rel_path.string()); 101 0 : return ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, get_path()); 102 : } 103 : else 104 46 : info("Created folder : " + rel_path.string()); 105 : } 106 : else 107 : { 108 0 : if (!ok_if_exists) 109 : { 110 0 : error("The following folder already exits : " + rel_path.string()); 111 0 : return ESYSREPO_RESULT(ResultCode::FOLDER_ALREADY_EXISTS, get_path()); 112 : } 113 : else 114 0 : warn("The following folder already exits : " + rel_path.string()); 115 : } 116 : 117 23 : boost::filesystem::path rel_temp_path = boost::filesystem::relative(get_temp_path()); 118 23 : if (!boost::filesystem::exists(get_temp_path())) 119 : { 120 23 : bool result = boost::filesystem::create_directory(get_temp_path()); 121 23 : if (!result) 122 : { 123 0 : error("The following folder couldn't be created : " + rel_temp_path.string()); 124 0 : return ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, get_temp_path()); 125 : } 126 : else 127 46 : info("Created folder : " + rel_temp_path.string()); 128 : } 129 : else 130 : { 131 0 : if (!ok_if_exists) 132 : { 133 0 : error("The following folder already exits : " + rel_temp_path.string()); 134 0 : return ESYSREPO_RESULT(ResultCode::FOLDER_ALREADY_EXISTS, get_temp_path()); 135 : } 136 : else 137 0 : warn("The following folder already exits : " + rel_temp_path.string()); 138 : } 139 23 : return ESYSREPO_RESULT(ResultCode::OK); 140 46 : } 141 : 142 57 : Result ConfigFolder::open(const std::string &parent_path, bool print_error) 143 : { 144 57 : if (!parent_path.empty()) set_workspace_path(parent_path); 145 : 146 57 : populate_all_pathes(); 147 : 148 57 : boost::filesystem::path rel_path = boost::filesystem::relative(get_path()); 149 57 : if (!boost::filesystem::exists(get_path())) 150 : { 151 21 : if (print_error) error("The following folder is not existing : " + rel_path.string()); 152 21 : return ESYSREPO_RESULT(ResultCode::PATH_NOT_EXISTING, get_path()); 153 : } 154 : 155 36 : boost::filesystem::path rel_config_file_path = boost::filesystem::relative(get_config_file_path()); 156 36 : if (!boost::filesystem::exists(get_config_file_path())) 157 : { 158 0 : if (print_error) error("The esysrepo config file can't be found : " + rel_config_file_path.string()); 159 0 : return ESYSREPO_RESULT(ResultCode::PATH_NOT_EXISTING, get_config_file_path()); 160 : } 161 : 162 36 : m_config_file = std::make_shared<ConfigFile>(); 163 36 : Result result = m_config_file->open(get_config_file_path()); 164 36 : if (result.error()) 165 : { 166 0 : if (print_error) error("The esysrepo config file couldn't opened : " + rel_config_file_path.string()); 167 0 : return ESYSREPO_RESULT(result); 168 : } 169 36 : set_config(m_config_file->get_config()); 170 : 171 36 : Result pins_result = load_pins_file(); 172 36 : if (pins_result.error()) return ESYSREPO_RESULT(pins_result); 173 : 174 36 : return ESYSREPO_RESULT(ResultCode::OK); 175 93 : } 176 : 177 0 : int ConfigFolder::write(const std::string &parent_path) 178 : { 179 0 : if (!parent_path.empty()) set_workspace_path(parent_path); 180 : 181 0 : return -1; 182 : } 183 : 184 281 : const std::string &ConfigFolder::get_path() const 185 : { 186 281 : return m_path; 187 : } 188 : 189 104 : const std::string &ConfigFolder::get_temp_path() const 190 : { 191 104 : return m_temp_path; 192 : } 193 : 194 152 : const std::string &ConfigFolder::get_config_file_path() const 195 : { 196 152 : return m_config_file_path; 197 : } 198 : 199 20 : std::string ConfigFolder::get_manifest_repo_path() const 200 : { 201 20 : boost::filesystem::path repo_path = get_path(); 202 40 : if (get_config() == nullptr) return ""; 203 : 204 20 : repo_path /= get_config()->get_manifest_path(); 205 60 : repo_path = repo_path.parent_path().lexically_normal().make_preferred(); 206 : 207 20 : return repo_path.string(); 208 20 : } 209 : 210 0 : std::string ConfigFolder::get_manifest_rel_file_name() const 211 : { 212 0 : assert(get_config() != nullptr); 213 : 214 0 : std::string folder_name; 215 : 216 0 : switch (get_config()->get_manifest_type()) 217 : { 218 0 : case manifest::Type::ESYSREPO_MANIFEST: folder_name = manifest::Base::get_folder_name(); break; 219 0 : case manifest::Type::GOOGLE_MANIFEST: folder_name = grepo::Manifest::get_folder_name(); break; 220 0 : default: return ""; 221 : } 222 : 223 0 : boost::filesystem::path manifest_path = get_config()->get_manifest_path(); 224 : 225 0 : auto rel_folder_path = boost::filesystem::relative(manifest_path, folder_name); 226 0 : return rel_folder_path.string(); 227 0 : } 228 : 229 309 : std::shared_ptr<Config> ConfigFolder::get_config() const 230 : { 231 309 : return m_config; 232 : } 233 : 234 60 : void ConfigFolder::set_config(std::shared_ptr<Config> config) 235 : { 236 60 : m_config = config; 237 60 : } 238 : 239 21 : std::shared_ptr<Config> ConfigFolder::get_or_new_config() 240 : { 241 21 : auto cfg = get_config(); 242 : 243 42 : if (cfg != nullptr) return cfg; 244 : 245 21 : set_config(std::make_shared<Config>()); 246 : 247 21 : return get_config(); 248 21 : } 249 : 250 22 : Result ConfigFolder::write_config_file() 251 : { 252 22 : m_config_file = std::make_shared<ConfigFile>(); 253 22 : m_config_file->set_config(get_config()); 254 22 : Result result = m_config_file->write(get_config_file_path()); 255 22 : boost::filesystem::path rel_config_file_path = boost::filesystem::relative(get_config_file_path()); 256 22 : if (result.error()) 257 0 : error("Couldn't write the esysrepo config file : " + rel_config_file_path.string()); 258 : else 259 44 : debug(0, "Wrote the esysrepo config file : " + rel_config_file_path.string()); 260 22 : return result; 261 22 : } 262 : 263 56 : const std::string &ConfigFolder::get_pins_file_path() const 264 : { 265 56 : return m_pins_file_path; 266 : } 267 : 268 0 : std::shared_ptr<RevisionPins> ConfigFolder::get_revision_pins() const 269 : { 270 0 : return m_revision_pins; 271 : } 272 : 273 56 : void ConfigFolder::set_revision_pins(std::shared_ptr<RevisionPins> pins) 274 : { 275 56 : m_revision_pins = pins; 276 56 : } 277 : 278 20 : std::shared_ptr<RevisionPins> ConfigFolder::get_or_new_revision_pins() 279 : { 280 20 : if (m_revision_pins == nullptr) m_revision_pins = std::make_shared<RevisionPins>(); 281 20 : return m_revision_pins; 282 : } 283 : 284 56 : Result ConfigFolder::load_pins_file() 285 : { 286 56 : PinsFile pins_file; 287 56 : Result result = pins_file.open(get_pins_file_path()); 288 56 : if (result.error()) return result; 289 56 : set_revision_pins(pins_file.get_or_new_pins()); 290 56 : return ESYSREPO_RESULT(ResultCode::OK); 291 56 : } 292 : 293 0 : Result ConfigFolder::write_pins_file() 294 : { 295 0 : PinsFile pins_file; 296 0 : pins_file.set_pins(get_or_new_revision_pins()); 297 0 : Result result = pins_file.write(get_pins_file_path()); 298 0 : boost::filesystem::path rel_path = boost::filesystem::relative(get_pins_file_path()); 299 0 : if (result.error()) 300 0 : error("Couldn't write the esysrepo pins file : " + rel_path.string()); 301 : else 302 0 : debug(0, "Wrote the esysrepo pins file : " + rel_path.string()); 303 0 : return result; 304 0 : } 305 : 306 28 : bool ConfigFolder::is_config_folder(const std::string &path) 307 : { 308 28 : boost::filesystem::path folder = path; 309 28 : folder /= ".esysrepo"; 310 28 : return boost::filesystem::exists(folder); 311 28 : } 312 : 313 : } // namespace esys::repo