LCOV - code coverage report
Current view: top level - src/esys/repo - configfolder.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 97 130 74.6 %
Date: 2026-08-01 10:43:40 Functions: 16 19 84.2 %

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

Generated by: LCOV version 1.14