aboutsummaryrefslogtreecommitdiff
path: root/src/profile.cpp
blob: 5bdbdf433d699897b06684c1a2e8fc8aba7a42a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2007-2014 Xagasoft, All rights reserved.
 *
 * This file is part of the Xagasoft Build and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "profile.h"
#include "ast.h"
#include "astbranch.h"
#include "astleaf.h"
#include "condition.h"

#include "conditionplugger.h"

#include <bu/sio.h>
using namespace Bu;

Profile::Profile( const class AstBranch *pRoot ) :
    pRoot( pRoot ),
    pCond( NULL ),
    pAst( NULL )
{
    sName = dynamic_cast<const AstLeaf *>(
        (*pRoot->getBranchBegin()).first()
        )->getStrValue();

    setCondition();
}

Profile::Profile( const Profile &rSrc ) :
    sName( rSrc.sName ),
    pRoot( rSrc.pRoot ),
    pCond( rSrc.pCond->clone() ),
    pAst( NULL )
{
}

Profile::~Profile()
{
    delete pAst;
    pAst = NULL;
}

const Bu::String &Profile::getName() const
{
    return sName;
}

const AstBranch *Profile::getRoot() const
{
    return pRoot;
}

const Condition *Profile::getCond() const
{
    return pCond;
}

bool Profile::shouldExec( class Runner &r, class Target &rTarget ) const
{
    return pCond->shouldExec( r, rTarget );
}

Profile *Profile::genDefaultClean()
{
    Ast *pAst = new Ast();
    pAst->addNode( AstNode::typeProfile );
        pAst->openBranch();
            pAst->addNode( AstNode::typeString, "clean" );
        pAst->openBranch();
            pAst->addNode( AstNode::typeCondition, "fileExists" );
            pAst->addNode( AstNode::typeFunction );
                pAst->openBranch();
                    pAst->addNode( AstNode::typeString, "unlink" );
                        pAst->openBranch();
                            pAst->addNode( AstNode::typeVariable, "OUTPUT" );
                    pAst->closeNode();
            pAst->closeNode();
    //pAst->closeNode();
    Profile *pRet = new Profile(
        dynamic_cast<const AstBranch *>(*pAst->getNodeBegin())
        );
    pRet->pAst = pAst;

    return pRet;
}

void Profile::setCondition()
{
    for( AstBranch::NodeList::const_iterator i =
         (*(pRoot->getBranchBegin()+1)).begin(); i; i++ )
    {
        if( (*i)->getType() == AstNode::typeCondition )
        {
            Bu::String sCond = dynamic_cast<const AstLeaf *>(*i)->getStrValue();
            delete pCond;
            pCond = ConditionPlugger::getInstance().instantiate( sCond );
        }
    }

    if( pCond == NULL )
    {
        // The default condition
        pCond = ConditionPlugger::getInstance().instantiate("fileTime");
    }
}