Merge pull request #6 from QuasarApp/qmlDir

Qml dir
This commit is contained in:
Andrei Yankovich 2018-08-24 16:20:14 +03:00 committed by GitHub
commit 3f2b71ed91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 188 additions and 13 deletions

View File

@ -12,6 +12,10 @@
#include <QFile>
#include <quasarapp.h>
#include <QProcess>
#include <QDirIterator>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
bool Deploy::getDeployQml() const {
@ -37,15 +41,22 @@ QString Deploy::getQmake() const {
void Deploy::setQmake(const QString &value) {
qmake = value;
QFileInfo info(qmake);
QDir dir = info.absoluteDir();
if (!dir.cdUp() || !dir.cd("qml")) {
qWarning() << "get qml fail!";
}
qmlDir = dir.absolutePath();
}
QString Deploy::getTarget() const {
return target;
}
bool Deploy::setTarget(const QString &value) {
target = value;
targetDir = QFileInfo(target).absolutePath();
bool Deploy::initDirs() {
if (!QFileInfo::exists(targetDir + QDir::separator() + "lib") &&
!QDir(targetDir).mkdir("lib")) {
@ -61,6 +72,18 @@ bool Deploy::setTarget(const QString &value) {
return true;
}
bool Deploy::setTarget(const QString &value) {
target = value;
if (target.isEmpty()) {
return false;
}
targetDir = QFileInfo(target).absolutePath();
return true;
}
bool Deploy::createRunScript() {
QString content =
@ -376,8 +399,79 @@ bool Deploy::copyFolder( QDir &from, QDir &to, const QString& filter,
return true;
}
bool Deploy::extractQml() {
auto qmlDir = QuasarAppUtils::getStrArg("qmlDir");
QStringList Deploy::findFilesInsideDir(const QString &name,
const QString &dirpath) {
QStringList files;
QDir dir(dirpath);
dir.setNameFilters(QStringList(name));
QDirIterator it(dir, QDirIterator::Subdirectories);
while (it.hasNext()) files << it.next();
return files;
}
QString Deploy::filterQmlPath(const QString& path) {
if (path.contains(qmlDir)) {
auto endIndex = path.indexOf(QDir::separator(), qmlDir.size() + 1);
QString module = path.mid(qmlDir.size() + 1, endIndex - qmlDir.size() - 1);
return qmlDir + QDir::separator() + module;
}
return "";
}
QStringList Deploy::extractImportsFromFiles(const QStringList &filepath){
QProcess p;
p.setProgram(qmlScaner);
p.setArguments(QStringList () << "-qmlFiles" << filepath
<< "-importPath" << qmlDir);
p.start();
qInfo() << "run extract qml";
if (!p.waitForFinished()) {
qWarning() << filepath << " not scaning!";
return QStringList();
}
auto data = QJsonDocument::fromJson(p.readAll());
if (!data.isArray()) {
qWarning() << "wrong data from qml scaner! of " << filepath;
}
auto array = data.array();
QStringList result;
for (auto object : array) {
auto module = filterQmlPath(object.toObject().value("path").toString());
if (module.isEmpty()) {
continue;
}
if (!result.contains(module)) {
result << module;
}
}
return result;
}
QStringList Deploy::extractImportsFromDir(const QString &dirpath) {
auto files = findFilesInsideDir("*.qml", dirpath);
QStringList result;
result.append(extractImportsFromFiles(files));
return result;
}
bool Deploy::extractQmlAll() {
if (!QFileInfo::exists(qmlDir)){
qWarning() << "qml dir wrong!";
@ -405,15 +499,75 @@ bool Deploy::extractQml() {
return false;
}
// for (auto item : listItems) {
// extract(item, false);
// }
for (auto item : listItems) {
extract(item, false);
}
return true;
return true;
}
bool Deploy::extractQmlFromSource(const QString sourceDir) {
qInfo() << "run extract qml";
QDir dirTo(targetDir);
if (!dirTo.cd("qml")) {
if (!dirTo.mkdir("qml")) {
return false;
}
if (!dirTo.cd("qml")) {
return false;
}
}
QStringList plugins = extractImportsFromDir(sourceDir);
for (auto plugin : plugins) {
QDir dir(plugin);
QStringList listItems;
if (!dirTo.cd(dir.dirName())) {
if (!dirTo.mkdir(dir.dirName())) {
return false;
}
if (!dirTo.cd(dir.dirName())) {
return false;
}
}
if (!copyFolder(dir, dirTo, ".so.debug", &listItems)) {
return false;
}
dirTo.cdUp();
for (auto item : listItems) {
extract(item, false);
}
}
return true;
}
bool Deploy::extractQml() {
if (QuasarAppUtils::isEndable("qmlDir")) {
return extractQmlFromSource(QuasarAppUtils::getStrArg("qmlDir"));
} else if (QuasarAppUtils::isEndable("allQmlDependes")){
return extractQmlAll();
} else {
return false;
}
}
void Deploy::clear() {
QDir dir(targetDir);
if (dir.cd("lib")) {

View File

@ -20,6 +20,7 @@ private:
QString qtDir = "";
QString target = "";
QString targetDir = "";
QString qmlDir = "";
QStringList QtLibs;
QStringList noQTLibs;
@ -43,6 +44,12 @@ private:
void strip(const QString &dir);
QStringList extractImportsFromDir(const QString &dirpath);
QStringList findFilesInsideDir(const QString &name, const QString &dirpath);
QStringList extractImportsFromFiles(const QStringList &filepath);
bool extractQmlAll();
bool extractQmlFromSource(const QString sourceDir);
QString filterQmlPath(const QString &path);
public:
Deploy();
bool getDeployQml() const;
@ -61,6 +68,7 @@ public:
void clear();
bool initDirs();
};
#endif // DEPLOY_H

View File

@ -21,7 +21,7 @@ void help() {
qInfo() << " help / h : show help.";
qInfo() << " always-overwrite : Copy files even if the target file exists.";
qInfo() << " -bin [params] : deployment binry.";
qInfo() << " -qmlDir [params] : qml datadir. for example -qmlDir ~/Qt/5.11.1/gcc_64/qml";
qInfo() << " -qmlDir [params] : qml datadir of project. for example -qmlDir ~/my/project/qml";
qInfo() << " noStrip : no strip deployed lib";
qInfo() << " deploy-not-qt : deploy all libs";
qInfo() << " -qmake [params] : qmake path. for example";
@ -32,6 +32,9 @@ void help() {
qInfo() << " clear : delete all old deploy data";
qInfo() << " -runScript [params] : set new name of out file (AppRun.sh by default)";
qInfo() << " | for example -runScript myApp.sh";
qInfo() << " allQmlDependes : This flag will force to extract all qml libraries.";
qInfo() << " | (not recommended, as it takes up a lot of memory)";
qInfo() << "";
@ -47,6 +50,7 @@ bool parseQt(Deploy& deploy) {
}
basePath = info.absolutePath();
deploy.setQmake(qmake);
auto scaner = basePath + QDir::separator() + "qmlimportscanner";
auto bin = QuasarAppUtils::getStrArg("bin");
@ -55,12 +59,17 @@ bool parseQt(Deploy& deploy) {
return false;
}
if (!deploy.setTarget(bin)) {
qCritical() << "error init targeet dir";
return false;
}
if (QuasarAppUtils::isEndable("clear")) {
qInfo() << "clear old data";
deploy.clear();
}
if (!deploy.setTarget(bin)) {
if (!deploy.initDirs()) {
qCritical() << "error init targeet dir";
return false;
}
@ -69,7 +78,12 @@ bool parseQt(Deploy& deploy) {
QDir dir(basePath);
if (QFileInfo::exists(qmlDir)) {
if (QFileInfo::exists(qmlDir) && QFileInfo::exists(scaner)) {
deploy.setDeployQml(true);
deploy.setQmlScaner(scaner);
} else if (QuasarAppUtils::isEndable("allQmlDependes")) {
deploy.setDeployQml(true);
} else {
qCritical () << "wrong qml dir!";
@ -83,7 +97,6 @@ bool parseQt(Deploy& deploy) {
return true;
}
int main(int argc, char *argv[])
{