LCOV - code coverage report
Current view: top level - src/esys/repo/manifest - detect_manifest.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 41 69 59.4 %
Date: 2026-08-01 10:43:40 Functions: 9 10 90.0 %

          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           2 : Detect::Detect(const std::string &folder_path)
      28           2 :     : m_folder_path(folder_path)
      29             : {
      30           2 : }
      31             : 
      32           5 : Detect::~Detect() = default;
      33             : 
      34           2 : void Detect::set_folder_path(const std::string &folder_path)
      35             : {
      36           2 :     m_folder_path = folder_path;
      37           2 : }
      38             : 
      39           7 : const std::string &Detect::get_folder_path() const
      40             : {
      41           7 :     return m_folder_path;
      42             : }
      43             : 
      44           2 : Result Detect::detect(std::shared_ptr<Config> config)
      45             : {
      46           2 :     if (get_folder_path().empty()) return ESYSREPO_RESULT(ResultCode::EMPTY_PATH);
      47             : 
      48           2 :     if (config != nullptr) set_config(config);
      49           4 :     config = get_config_or_new();
      50             : 
      51           2 :     boost::filesystem::path file_path = get_folder_path();
      52           2 :     file_path /= ".esysrepo";
      53             : 
      54           2 :     if (boost::filesystem::exists(file_path))
      55             :     {
      56           1 :         std::shared_ptr<ConfigFolder> config_folder = std::make_shared<ConfigFolder>();
      57             : 
      58           2 :         config_folder->set_config(config);
      59           1 :         Result result = config_folder->open(get_folder_path());
      60           3 :         if (result.success()) set_config_folder(config_folder);
      61           2 :         return ESYSREPO_RESULT(result);
      62           2 :     }
      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           2 : }
     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           2 : std::shared_ptr<Config> Detect::get_config_or_new()
     126             : {
     127           2 :     if (m_config == nullptr) m_config = std::make_shared<Config>();
     128           2 :     return m_config;
     129             : }
     130             : 
     131           1 : void Detect::set_config_folder(std::shared_ptr<ConfigFolder> config_folder)
     132             : {
     133           1 :     m_config_folder = config_folder;
     134           1 : }
     135             : 
     136           3 : std::shared_ptr<ConfigFolder> Detect::get_config_folder()
     137             : {
     138           3 :     return m_config_folder;
     139             : }
     140             : 
     141             : } // namespace esys::repo::manifest

Generated by: LCOV version 1.14