/* builder-source.cpp
 *
 * Copyright (C) 2023 OpenKylin, Inc
 */

#include "KMBuilderSource.h"
#include "KMBuilderContext.h"

/**
 * @brief : 源工厂方法，根据源类型创建对应的源类型对象
 * @param : [in]，sourceType 源类型
 * @return: std::shared_ptr<KMBuilderSource>，根据源类型创建对应的源类型对象或空
 */ 
std::shared_ptr<KMBuilderSource> KMBuilderSource::createSource(const std::string &sourceType)
{
    KMBuilderSource *pObjetct = dynamic_cast<KMBuilderSource *>(KMDynamicCreator::createObjectById("SourceType_" + sourceType));
    return std::shared_ptr<KMBuilderSource>(pObjetct);
}

/**
 * @brief : 反序列化
 * @param : [in]，sourceNode，清单中的源节点
 */ 
void KMBuilderSource::deserialize(YAML::Node& sourceNode)
{
    KMYAMLNodeHelper helper(sourceNode);

    m_dest = helper.getStringValue("dest");
    m_onlyArches = helper.getStringListValue("only-arches");
    m_skipArches = helper.getStringListValue("skip-arches");

    deserializeEx(sourceNode);
}

KMBuilderSource::KMBuilderSource()
{
}

KMBuilderSource::~KMBuilderSource()
{
}

void KMBuilderSource::setBaseDir(const std::string &baseDir)
{
    m_baseDir = baseDir;
}

std::string KMBuilderSource::getBaseDir() const
{
    return m_baseDir;
}

std::string KMBuilderSource::getDest() const
{
    return m_dest;
}

std::vector<std::string> KMBuilderSource::getOnlyArches() const
{
    return m_onlyArches;
}

std::vector<std::string> KMBuilderSource::getSkipArches() const
{
    return m_skipArches;
}

bool KMBuilderSource::isEnable()
{
    if (!m_onlyArches.empty() && !KMBuilderUtils::contains(m_onlyArches, builderContext.getArch()))
    {
        return false;
    }

    if (!m_skipArches.empty() && KMBuilderUtils::contains(m_skipArches, builderContext.getArch()))
    {
        return false;
    }

    return true;
}

bool KMBuilderSource::downloadSource(const std::string &destDir)
{
    return processNode(destDir).second;
}

