-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproject-info.cpp
182 lines (158 loc) · 4.94 KB
/
project-info.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "project-info.h"
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <QtXml/QDomDocument>
#include <QFileInfo>
ProjectInfo::ProjectInfo(const QString &pomOrBackupFilePath)
: m_isVerbose(true)
, m_isParsed(ThreeState::Unknown)
, m_type(Self::UnknownType)
{
m_path = pomOrBackupFilePath;
m_basePath = this->baseFromPom(pomOrBackupFilePath);
}
bool ProjectInfo::parse()
{
QString errorStr;
int errorLine;
int errorColumn;
if (m_isParsed != ThreeState::Unknown) {
return m_isParsed;
}
m_isParsed = ThreeState::False;
// Parse.
QFile pomFile(this->inputPomPath());
if ( ! pomFile.exists()) {
return false;
}
QDomDocument doc;
if ( ! doc.setContent(&pomFile, true, &errorStr, &errorLine, &errorColumn)) {
qWarning() << "Failed to parse: " << inputPomPath()
<< "at line" << errorLine << "column" << errorColumn
<< "message:" << errorStr;
return false;
}
// Detects type.
QDomElement root = doc.documentElement();
const QString &packaging = root.firstChildElement("packaging").text();
const QString &typeName = packaging.trimmed().toLower();
if (typeName == QLL("pom")) {
this->m_type = Self::PomOnly;
} else if (typeName == QLL("jar") || typeName.isEmpty()) {
this->m_type = Self::Jar;
} else if (typeName == QLL("aar")) {
this->m_type = Self::Aar;
} else if (typeName == QLL("apk")) {
this->m_type = Self::Apk;
} else {
this->m_type = Self::UnknownType;
}
m_isParsed = ThreeState::True;
return true;
}
bool ProjectInfo::isDownloaded() const
{
if (this->isParent()) {
return true;
}
if (this->isJar() || this->isUnknown()) {
QFileInfo jarInfo(this->jarPath());
QFileInfo jarInfoForPlatform(this->jarPathForPlatform());
if (jarInfo.exists() || jarInfoForPlatform.exists()) {
return true;
}
}
if (this->isAar() || this->isUnknown()) {
QFileInfo aarInfo(this->aarPath());
QFileInfo aarInfoForPlatform(this->aarPathForPlatform());
if (aarInfo.exists() || aarInfoForPlatform.exists()) {
return true;
}
}
if (this->isApk()) {
QFileInfo apkInfo(this->apkPath());
QFileInfo apkInfoForPlatform(this->apkPathForPlatform());
if (apkInfo.exists() || apkInfoForPlatform.exists()) {
return true;
}
}
return false;
}
bool ProjectInfo::isComplete() const
{
QFileInfo fileInfo(this->pomPath());
if (fileInfo.exists()) {
return this->isDownloaded();
}
return false;
}
bool ProjectInfo::restoreIncompleteLib(const QString &path)
{
QString pomFile = path;
pomFile.chop(3);
pomFile += QLatin1Literal("pom");
ProjectInfo info(pomFile);
return info.restoreIncomplete();
}
ThreeState::Type ProjectInfo::binarySame(const QString &firstFile, const QString &otherFile)
{
QFile left(firstFile);
left.open(QFile::ReadOnly);
QFile right(otherFile);
right.open(QFile::ReadOnly);
// Prevent out of memory (just in case).
if (left.size() > 100 * Self::MB) {
return ThreeState::Unknown;
}
return (left.size() == right.size()
&& left.readAll() == right.readAll())
? ThreeState::True
: ThreeState::False;
}
bool ProjectInfo::restoreIncomplete()
{
// Don't restore backup if already has needed files.
QString pomPath = this->pomPath();
ProjectInfo info(pomPath);
if (info.parse()) {
// If backup is same or invalid, no need to keep it.
if (this->isBackup()
&& (this->binarySameTo(pomPath) || ! this->parse())
) {
return QFile::remove(this->inputPomPath());
}
if (info.isComplete()) {
return false;
}
}
// Restore POM file from backup.
QString pomBackupPath = info.pomBackupPath();
if (QFile::exists(pomBackupPath)) {
if (QFile::exists(pomPath)) {
if ( ! QFile::remove(pomPath)) {
qWarning("can not delete: %s", qPrintable(pomPath));
return false;
}
}
if (QFile::rename(pomBackupPath, pomPath)) {
if (m_isVerbose) {
qDebug("restored: %s", qPrintable(pomBackupPath));
}
return true;
} else {
qWarning("can not rename: %s", qPrintable(pomBackupPath));
}
}
return false;
}
ThreeState::Type ProjectInfo::binarySameTo(const QString &otherFile) const
{
return Self::binarySame(this->inputPomPath(), otherFile);
}
QString ProjectInfo::baseFromPom(const QString &pomOrBackupPath) const
{
const QLatin1Literal pomBackupExt = getPomBackupExtension();
if(pomOrBackupPath.endsWith(pomBackupExt, Qt::CaseInsensitive))
return pomOrBackupPath.left(pomOrBackupPath.size() - pomBackupExt.size());
return pomOrBackupPath.left(pomOrBackupPath.size() - 4);
}