~ubuntu-branches/ubuntu/trusty/kdevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to projectbuilders/ninjabuilder/ninjajob.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-12-09 18:54:59 UTC
  • mfrom: (1.7.19)
  • Revision ID: package-import@ubuntu.com-20131209185459-zlxv7jo7up8gthne
Tags: 4:4.6.0-0ubuntu1
* New upstream release (LP: #1259220)
* Update install files 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KDevelop
 
2
    Copyright 2012 Aleix Pol Gonzalez <aleixpol@kde.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License version 2 as published by the Free Software Foundation.
 
7
 
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public License
 
14
   along with this library; see the file COPYING.LIB.  If not, write to
 
15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
   Boston, MA 02110-1301, USA.
 
17
*/
 
18
 
 
19
#include "ninjajob.h"
 
20
#include <KProcess>
 
21
#include <KUrl>
 
22
#include <KDebug>
 
23
#include <KLocalizedString>
 
24
#include <KConfigGroup>
 
25
#include <interfaces/iproject.h>
 
26
#include <outputview/outputmodel.h>
 
27
#include <project/interfaces/ibuildsystemmanager.h>
 
28
#include <project/projectmodel.h>
 
29
#include <interfaces/iproject.h>
 
30
#include <interfaces/icore.h>
 
31
#include <interfaces/iprojectcontroller.h>
 
32
#include <QFile>
 
33
 
 
34
NinjaJob::NinjaJob(KDevelop::ProjectBaseItem* item, const QStringList& arguments, const QByteArray& signal, QObject* parent)
 
35
    : OutputExecuteJob(parent)
 
36
    , m_isInstalling(false)
 
37
    , m_idx(item->index())
 
38
    , m_signal(signal)
 
39
{
 
40
    setToolTitle(i18n("Ninja"));
 
41
    setCapabilities(Killable);
 
42
    setStandardToolView( KDevelop::IOutputView::BuildView );
 
43
    setBehaviours(KDevelop::IOutputView::AllowUserClose | KDevelop::IOutputView::AutoScroll );
 
44
    setFilteringStrategy( KDevelop::OutputModel::CompilerFilter );
 
45
    setProperties( NeedWorkingDirectory | PortableMessages | DisplayStderr | IsBuilderHint | PostProcessOutput );
 
46
 
 
47
    *this << "ninja";
 
48
    *this << arguments;
 
49
 
 
50
    QStringList targets;
 
51
    foreach( const QString& arg, arguments ) {
 
52
        if( !arg.startsWith( '-' ) ) {
 
53
            targets << arg;
 
54
        }
 
55
    }
 
56
    QString title;
 
57
    if( !targets.isEmpty() )
 
58
        title = i18n("Ninja (%1): %2", item->text(), targets.join(" "));
 
59
    else
 
60
        title = i18n("Ninja (%1)", item->text());
 
61
    setJobName( title );
 
62
 
 
63
    connect(this, SIGNAL(finished(KJob*)), SLOT(emitProjectBuilderSignal(KJob*)));
 
64
}
 
65
 
 
66
void NinjaJob::setIsInstalling( bool isInstalling )
 
67
{
 
68
    m_isInstalling = isInstalling;
 
69
}
 
70
 
 
71
KUrl NinjaJob::workingDirectory() const
 
72
{
 
73
    KDevelop::ProjectBaseItem* it = item();
 
74
    if(!it)
 
75
        return KUrl();
 
76
    KDevelop::IBuildSystemManager* bsm = it->project()->buildSystemManager();
 
77
    KUrl workingDir = bsm->buildDirectory( it );
 
78
    while( !QFile::exists( workingDir.toLocalFile( KUrl::AddTrailingSlash ) + "build.ninja" ) ) {
 
79
        KUrl upWorkingDir = workingDir.upUrl();
 
80
        if( upWorkingDir.isEmpty() || upWorkingDir == workingDir ) {
 
81
            return bsm->buildDirectory( it->project()->projectItem() );
 
82
        }
 
83
        workingDir = upWorkingDir;
 
84
    }
 
85
    return workingDir;
 
86
}
 
87
 
 
88
QStringList NinjaJob::privilegedExecutionCommand() const
 
89
{
 
90
    KDevelop::ProjectBaseItem* it = item();
 
91
    if(!it)
 
92
        return QStringList();
 
93
    KSharedConfig::Ptr configPtr = it->project()->projectConfiguration();
 
94
    KConfigGroup builderGroup( configPtr, "NinjaBuilder" );
 
95
 
 
96
    bool runAsRoot = builderGroup.readEntry( "Install As Root", false );
 
97
    if ( runAsRoot && m_isInstalling )
 
98
    {
 
99
        int suCommand = builderGroup.readEntry( "Su Command", 0 );
 
100
        QStringList arguments;
 
101
        QString suCommandName;
 
102
        switch( suCommand ) {
 
103
            case 1:
 
104
                return QStringList() << "kdesudo" << "-t";
 
105
 
 
106
            case 2:
 
107
                return QStringList() << "sudo";
 
108
 
 
109
            default:
 
110
                return QStringList() << "kdesu" << "-t";
 
111
        }
 
112
    }
 
113
    return QStringList();
 
114
}
 
115
 
 
116
void NinjaJob::emitProjectBuilderSignal(KJob* job)
 
117
{
 
118
    Q_ASSERT(!m_signal.isEmpty());
 
119
    
 
120
    KDevelop::ProjectBaseItem* it = item();
 
121
    if(!it)
 
122
        return;
 
123
    if(job->error()==0)
 
124
        QMetaObject::invokeMethod(parent(), m_signal, Q_ARG(KDevelop::ProjectBaseItem*, it));
 
125
    else
 
126
        QMetaObject::invokeMethod(parent(), "failed", Q_ARG(KDevelop::ProjectBaseItem*, it));
 
127
}
 
128
 
 
129
void NinjaJob::postProcessStderr( const QStringList& lines )
 
130
{
 
131
    appendLines( lines );
 
132
}
 
133
 
 
134
void NinjaJob::postProcessStdout( const QStringList& lines )
 
135
{
 
136
    appendLines( lines );
 
137
}
 
138
 
 
139
void NinjaJob::appendLines(const QStringList& lines)
 
140
{
 
141
    if(lines.isEmpty())
 
142
        return;
 
143
    
 
144
    QStringList ret(lines);
 
145
    bool prev = false;
 
146
    for(QStringList::iterator it=ret.end(); it!=ret.begin(); ) {
 
147
        --it;
 
148
        bool curr = it->startsWith('[');
 
149
        if((prev && curr) || it->endsWith("] "))
 
150
            it = ret.erase(it);
 
151
        prev = curr;
 
152
    }
 
153
 
 
154
    model()->appendLines(ret);
 
155
}
 
156
 
 
157
KDevelop::ProjectBaseItem* NinjaJob::item() const
 
158
{
 
159
    return KDevelop::ICore::self()->projectController()->projectModel()->itemFromIndex(m_idx);
 
160
}