2
* Copyright 2014 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 2.1.
8
* This program 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
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Benjamin Zeller <benjamin.zeller@canonical.com>
18
#include "ubuntuprojectguesser.h"
20
#include <projectexplorer/project.h>
21
#include <projectexplorer/target.h>
22
#include <projectexplorer/buildconfiguration.h>
23
#include <cmakeprojectmanager/cmakeproject.h>
27
#include <QTextStream>
28
#include <QRegularExpression>
34
const char SCOPES_TYPE_CACHE_PROPERTY[] = "__ubuntu_sdk_is_scopes_project_property";
35
const char SCOPES_INIFILE_CACHE_PROPERTY[] = "__ubuntu_sdk_scopes_project_inifile_property";
36
const char CLICK_TYPE_CACHE_PROPERTY[] = "__ubuntu_sdk_is_click_project_property";
38
UbuntuProjectGuesser::UbuntuProjectGuesser()
42
bool UbuntuProjectGuesser::isScopesProject(ProjectExplorer::Project *project, QString *iniFileName)
44
//query the project type from CMakeCache always, only fall back on searching the directory
45
QString type = projectTypeFromCacheOrProject(project);
46
qDebug()<<"Project type from CMake "<<type;
47
if (!type.isEmpty()) {
48
return QString::compare(type, QLatin1String("Scope") , Qt::CaseInsensitive) == 0;
51
QVariant cachedResult = project->property(SCOPES_TYPE_CACHE_PROPERTY);
52
if(cachedResult.isValid()) {
53
if(!cachedResult.toBool())
57
QVariant cachedIni = project->property(SCOPES_INIFILE_CACHE_PROPERTY);
58
*iniFileName = cachedIni.toString();
63
if(!qobject_cast<CMakeProjectManager::CMakeProject*>(project))
66
Utils::FileName iniFile = findScopesIniRecursive(Utils::FileName::fromString(project->projectDirectory()));
67
QFileInfo info = iniFile.toFileInfo();
68
if (iniFileName && info.exists()) {
69
*iniFileName = info.absolutePath();
73
project->setProperty(SCOPES_TYPE_CACHE_PROPERTY,true);
74
project->setProperty(SCOPES_INIFILE_CACHE_PROPERTY,iniFile.toString());
80
bool UbuntuProjectGuesser::isClickAppProject(ProjectExplorer::Project *project)
82
//query the project type from CMakeCache always, only fall back on searching the directory
83
QString type = projectTypeFromCacheOrProject(project);
84
qDebug()<<"Project type from CMake "<<type;
85
if (!type.isEmpty()) {
86
return QString::compare(type, QLatin1String("ClickApp") , Qt::CaseInsensitive) == 0;
89
QVariant cachedResult = project->property(CLICK_TYPE_CACHE_PROPERTY);
90
if(cachedResult.isValid()) {
91
return cachedResult.toBool();
94
if(!qobject_cast<CMakeProjectManager::CMakeProject*>(project))
97
QFile projectFile(project->projectFilePath());
98
if (!projectFile.open(QIODevice::ReadOnly))
101
bool usesClick = false;
102
QRegularExpression regExp(QLatin1String("include\\(Click\\)|CLICK_MODE"));
103
QTextStream in(&projectFile);
104
while (!in.atEnd()) {
105
QString contents = in.readLine();
106
QRegularExpressionMatch m = regExp.match(contents);
114
Utils::FileName iniFile = findFileRecursive(Utils::FileName::fromString(project->projectDirectory()),
115
QLatin1String("^.*desktop.in.*$"));
116
QFileInfo info = iniFile.toFileInfo();
118
project->setProperty(CLICK_TYPE_CACHE_PROPERTY,true);
122
iniFile = findFileRecursive(Utils::FileName::fromString(project->projectDirectory()),
123
QLatin1String("^.*desktop.*$"));
124
info = iniFile.toFileInfo();
126
project->setProperty(CLICK_TYPE_CACHE_PROPERTY,true);
131
project->setProperty(CLICK_TYPE_CACHE_PROPERTY,false);
135
Utils::FileName UbuntuProjectGuesser::findScopesIniRecursive(const Utils::FileName &searchdir)
137
return findFileRecursive(searchdir,QLatin1String("^.*-scope.ini.*$"));
140
Utils::FileName UbuntuProjectGuesser::findFileRecursive(const Utils::FileName &searchdir, const QString ®exp)
142
QRegularExpression regex(regexp);
143
return findFileRecursive(searchdir,regex);
146
Utils::FileName UbuntuProjectGuesser::findFileRecursive(const Utils::FileName &searchdir, const QRegularExpression ®exp)
148
QFileInfo dirInfo = searchdir.toFileInfo();
149
if(!dirInfo.exists())
150
return Utils::FileName();
153
return Utils::FileName();
155
QDir dir(dirInfo.absoluteFilePath());
156
QStringList entries = dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
158
foreach (const QString& entry, entries) {
159
QFileInfo info(dir.absoluteFilePath(entry));
161
Utils::FileName f = findFileRecursive(Utils::FileName::fromString(dir.absoluteFilePath(entry)),regexp);
168
QRegularExpressionMatch match = regexp.match(entry);
169
if(match.hasMatch()) {
170
return Utils::FileName(info);
174
return Utils::FileName();
177
QString UbuntuProjectGuesser::projectTypeFromCacheOrProject(ProjectExplorer::Project *project)
179
//First try to get the variable from the Cache file
180
if(project->activeTarget()
181
&& project->activeTarget()->activeBuildConfiguration())
183
QFile cache(project->activeTarget()->activeBuildConfiguration()->buildDirectory().toString()
185
+ QLatin1String("CMakeCache.txt"));
187
if(cache.exists() && cache.open(QIODevice::ReadOnly)) {
188
QRegularExpression regExp(QLatin1String("^UBUNTU_PROJECT_TYPE:(.*)=\\s*(\\S*)\\s*$"));
189
QTextStream in(&cache);
190
while (!in.atEnd()) {
191
QString contents = in.readLine();
192
QRegularExpressionMatch m = regExp.match(contents);
194
return m.captured(2);
200
QFile projectFile(project->projectFilePath());
201
if (!projectFile.exists() || !projectFile.open(QIODevice::ReadOnly)) {
202
QRegularExpression regExp(QLatin1String("^\\s*SET\\s*\\(\\s*UBUNTU_PROJECT_TYPE\\s*\"?(\\S*)\"?"));
203
QTextStream in(&projectFile);
204
while (!in.atEnd()) {
205
QString contents = in.readLine();
206
QRegularExpressionMatch m = regExp.match(contents);
208
return m.captured(1);
215
} // namespace Internal
216
} // namespace Ubuntu