Skip to content

Commit

Permalink
add some checks, better regex, better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Sep 5, 2019
1 parent 80d1597 commit ecae673
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions androidprojectmodifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ QString AndroidProjectModifier::getFileContent(const QString filepath)
{
QFile file(m_directory.absolutePath() + filepath);
if(!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw QString("The file does not exist or could not be opened for reading");
throw QString("The file '" + file.fileName() + "' does not exist or could not be opened for reading");
}

QTextStream stream(&file);
Expand All @@ -88,7 +88,7 @@ void AndroidProjectModifier::updateFileContent(const QString filepath, const QSt
{
QFile file(m_directory.absolutePath() + filepath);
if(!file.exists() || !file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
throw QString("The file does not exist or could not be opened for writing");
throw QString("The file '" + file.fileName() + "' does not exist or could not be opened for writing");
}

QTextStream stream(&file);
Expand Down
12 changes: 6 additions & 6 deletions githelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void GitHelper::clone(QDir directory)
{
directory = QDir(directory);
if (directory.exists()) {
throw QString("The directory already exists");
throw QString("The directory '" + directory.absolutePath() + "' already exists");
}

git_repository *repository = nullptr;
Expand All @@ -34,8 +34,8 @@ void GitHelper::checkout(QString directory)

void GitHelper::checkout(QDir directory)
{
if(!directory.exists()) {
throw QString("The directory does not exist");
if (!directory.exists()) {
throw QString("The directory '" + directory.path() + "' does not exist");
}

git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
Expand Down Expand Up @@ -64,7 +64,7 @@ void GitHelper::reinitGitDirectory(QString directory)
void GitHelper::reinitGitDirectory(QDir directory)
{
if(!directory.exists()) {
throw QString("The directory does not exist");
throw QString("The directory '" + directory.path() + "' does not exist");
}

QDir gitDir = directory.filePath(".git");
Expand All @@ -86,7 +86,7 @@ void GitHelper::initialCommit(QString directory)
void GitHelper::initialCommit(QDir directory)
{
if(!directory.exists()) {
throw QString("The directory does not exist");
throw QString("The directory '" + directory.path() + "' does not exist");
}

git_index *index = nullptr;
Expand Down Expand Up @@ -134,6 +134,6 @@ void GitHelper::gitError(int status)
{
if(status < 0) {
const git_error *e = giterr_last();
throw QString::fromUtf8(e->message);
throw "[Git Error]: " + QString::fromUtf8(e->message);
}
}
15 changes: 0 additions & 15 deletions inputoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,3 @@ void InputOutput::writeln(QString text)
{
write(text + "\n");
}

/*QString InputOutput::readStdin()
{
std::string result;
std::cin >> result;
return QString::fromStdString(result);
}
QString InputOutput::ask(QString question)
{
write(question + ": ");
return readStdin();
}
*/
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ int main(int argc, char *argv[])

WebsiteParser parser(url, manifestPath);

auto icons = parser.getImages();
auto basicData = parser.getData();
basicData.insert("package", packageName);
auto icons = parser.getImages();

AndroidProjectModifier androidProject(outputDirectory);
androidProject.addSupportLibrary();
Expand Down
2 changes: 1 addition & 1 deletion pwa-to-twa.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CONFIG += c++14 console
CONFIG -= app_bundle

INCLUDEPATH += libgit/include
LIBS += -lgit2 -lssl -lcrypto
LIBS += -lgit2 -lssl -lcrypto -L"$$_PRO_FILE_PWD_/libgit/build"

exists(pwa-to-twa.local.pro) {
include(pwa-to-twa.local.pro)
Expand Down
33 changes: 30 additions & 3 deletions websiteparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const QString WebsiteParser::getManifestUrl()
if(!m_manifestPath.isNull() && !m_manifestPath.isEmpty()) {
manifestUrl = m_manifestPath;
} else {
QRegularExpression regex("(<link.*?rel=['\"]manifest['\"]).*?>");
QRegularExpression regex("(<link[^<>]*?rel=['\"]manifest['\"]).*?>");
auto matches = regex.match(getWebsiteContent());
if(!matches.hasMatch()) {
throw QString("Could not detect the manifest automatically, try using --manifest option");
Expand Down Expand Up @@ -89,6 +89,28 @@ const QHash<QString, QString> WebsiteParser::getData()

auto json = getManifestContent().object();

QStringList missingKeys;

if (!json.contains("short_name")) {
missingKeys << "short_name";
}

if (!json.contains("theme_color")) {
missingKeys << "theme_color";
}

if (!json.contains("background_color")) {
missingKeys << "background_color";
}

if (!json.contains("start_url")) {
missingKeys << "start_url";
}

if(!missingKeys.empty()) {
throw QString("The manifest at url '" + getManifestUrl() + "' is missing these keys: " + missingKeys.join(", "));
}

result.insert("hostname", m_url.host());
result.insert("short_name", json.take("short_name").toString());
result.insert("theme_color", json.take("theme_color").toString());
Expand All @@ -104,21 +126,26 @@ const QList<QHash<QString, QString>> WebsiteParser::getImages()

auto json = getManifestContent().object().take("icons").toArray();

if (json.empty()) {
throw QString("The manifest at url '" + getManifestUrl() + "' does not contain any icons");
}

QJsonArray::const_iterator iterator;

for(iterator = json.constBegin(); iterator != json.constEnd(); ++iterator) {
auto icon = (*iterator).toObject();

auto sizes = icon.take("sizes").toString().split("x");
auto url = icon.take("src").toString();

if(sizes.length() != 2 || sizes.at(0) != sizes.at(1)) {
qWarning() << "[Warning] The icon does not have a valid size attribute, ignoring";
qWarning() << "[Warning] The icon with url '" + url + "' does not have a valid size attribute, ignoring";
continue;
}

QHash<QString, QString> iconHash;
iconHash.insert("size", sizes.at(0));
iconHash.insert("url", getUrl(icon.take("src").toString()));
iconHash.insert("url", getUrl(url));
result.append(iconHash);
}

Expand Down

0 comments on commit ecae673

Please sign in to comment.