Skip to content

Commit

Permalink
add option to specify manifest, fix wrong url resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Sep 4, 2019
1 parent 3b88943 commit 0c61e94
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ or compile it yourself.

## Usage

This tool takes exactly three parameters:
This tool takes three parameters:

1. The android package name (e.g. `com.example.my_pwa`)
2. The PWA url (e.g. `https://my_pwa.example.com`)
3. The output directory (e.g. `./my-cool-pwa`)

If the manifest path is not in html source code or could
not be detected for any other reason, you can add the
relative path to your manifest using the --manifest option.

Example:

`pwa-to-twa com.example.my_pwa https://my_pwa.example.com ./my-cool-pwa`

Example with --manifest option:

`pwa-to-twa com.example.my_pwa https://my_pwa.example.com ./my-cool-pwa --manifest /manifest.json`

> Note: Currently the splash image is not replaced from the default
value, it will be in a future version

Expand All @@ -58,8 +70,9 @@ that everything works run `./pwa-to-twa`, you should see
something like this:

```
Wrong input, usage: pwa-to-twa [androidPackageName] [pwaUrl] [outputPath]
Wrong input, usage: pwa-to-twa androidPackageName pwaUrl outputPath [--manifest path-to-manifest]
Example: pwa-to-twa com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa
Example: pwa-to-twa com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa --manifest relative/path/to/manifest
```

### Building statically
Expand Down
16 changes: 14 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ int main(int argc, char *argv[])
QCoreApplication app(argc, argv);

if(app.arguments().count() < 4) {
qCritical() << "Wrong input, usage: pwa-to-twa [androidPackageName] [pwaUrl] [outputPath]";
qCritical() << "Wrong input, usage: pwa-to-twa androidPackageName pwaUrl outputPath [--manifest path-to-manifest]";
qCritical() << "Example: pwa-to-twa com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa";
qCritical() << "Example: pwa-to-twa com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa --manifest relative/path/to/manifest";
return 1;
}

QString manifestPath;

if(app.arguments().contains("--manifest")) {
int index = app.arguments().indexOf("--manifest") + 1;
if(app.arguments().count() <= index) {
qCritical() << "You must set a value for --manifest";
return 1;
}
manifestPath = app.arguments().at(index);
}

auto url = app.arguments().at(2);
auto packageName = app.arguments().at(1);
auto outputDirectory = app.arguments().at(3);
Expand All @@ -27,7 +39,7 @@ int main(int argc, char *argv[])
gitHelper.checkout(outputDirectory);
gitHelper.reinitGitDirectory(outputDirectory);

WebsiteParser parser(url);
WebsiteParser parser(url, manifestPath);

auto icons = parser.getImages();
auto basicData = parser.getData();
Expand Down
39 changes: 25 additions & 14 deletions websiteparser.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "websiteparser.h"

WebsiteParser::WebsiteParser(QString url)
WebsiteParser::WebsiteParser(QString url, QString manifestPath)
{
m_url.setUrl(url);
m_manifestPath = manifestPath;
}

const QString WebsiteParser::getWebsiteContent()
Expand Down Expand Up @@ -32,21 +33,29 @@ const QString WebsiteParser::getWebsiteContent()

const QString WebsiteParser::getManifestUrl()
{
QRegularExpression regex("(<link.*?rel=['\"]manifest['\"]).*?>");
auto matches = regex.match(getWebsiteContent());
if(!matches.hasMatch()) {
throw QString("The website does not contain a manifest");
}
QString manifestUrl;

if(!m_manifestPath.isNull() && !m_manifestPath.isEmpty()) {
manifestUrl = m_manifestPath;
} else {
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");
}

QString linkTag = matches.captured(0);
QString linkTag = matches.captured(0);

QRegularExpression regexManifest("href=[\"'](.+?)[\"']");
auto matchesManifest = regexManifest.match(linkTag);
if(!matchesManifest.hasMatch()) {
throw QString("Could not find href inside the manifest link");
QRegularExpression regexManifest("href=[\"'](.+?)[\"']");
auto matchesManifest = regexManifest.match(linkTag);
if(!matchesManifest.hasMatch()) {
throw QString("Could not find href inside the manifest link");
}

manifestUrl = matchesManifest.captured(1);
}

return getUrl(matchesManifest.captured(1));
return getUrl(manifestUrl);
}

const QJsonDocument WebsiteParser::getManifestContent()
Expand Down Expand Up @@ -103,7 +112,8 @@ const QList<QHash<QString, QString>> WebsiteParser::getImages()
auto sizes = icon.take("sizes").toString().split("x");

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

QHash<QString, QString> iconHash;
Expand All @@ -112,7 +122,6 @@ const QList<QHash<QString, QString>> WebsiteParser::getImages()
result.append(iconHash);
}


return result;
}

Expand All @@ -129,6 +138,8 @@ QString WebsiteParser::getUrl(const QString url)

if(!url.startsWith("/")) {
tmp = "/" + url;
} else {
tmp = url;
}

QString baseUrl = m_url.toString();
Expand Down
3 changes: 2 additions & 1 deletion websiteparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class WebsiteParser
{
public:
WebsiteParser(QString url);
WebsiteParser(QString url, QString manifestPath);

const QHash<QString, QString> getData();
const QList<QHash<QString, QString>> getImages();
Expand All @@ -29,6 +29,7 @@ class WebsiteParser

QString m_websiteContent;
QString m_manifestContent;
QString m_manifestPath;
QUrl m_url;
};

Expand Down

0 comments on commit 0c61e94

Please sign in to comment.