-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (79 loc) · 3.94 KB
/
Copy pathmain.cpp
File metadata and controls
96 lines (79 loc) · 3.94 KB
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
#include <QCoreApplication>
#include <QScopedPointer>
#include "inputoutput.h"
#include "githelper.h"
#include "websiteparser.h"
#include "androidprojectmodifier.h"
#include "coloredstring.h"
#include "newversionchecker.h"
int main(int argc, char *argv[])
{
InputOutput io;
try {
QCoreApplication app(argc, argv);
NewVersionChecker newVersionChecker;
if (newVersionChecker.isNewVersionAvailable()) {
io.writeError(ColoredString("<green>New version available at https://github.com/RikudouSage/PwaToTwa/releases/latest</green>"));
}
if (app.arguments().count() < 4 || app.arguments().contains("--help")) {
const auto binaryName = app.arguments().at(0);
io.writeError("Usage: " + binaryName + " androidPackageName pwaUrl outputPath [--manifest path-to-manifest] [--local-manifest path-to-manifest]");
io.writeError("Example: " + binaryName + " com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa");
io.writeError("Example: " + binaryName + " com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa --manifest relative/path/to/manifest");
io.writeError("Example: " + binaryName + " com.vendor.pwa https://pwa.vendor.com ./my-cool-pwa --local-manifest ./manifest.json");
return app.arguments().contains("--help") ? 0 : 1;
}
QString manifestPath;
QFile *localManifestFile = nullptr;
if (app.arguments().contains("--manifest")) {
auto index = app.arguments().indexOf("--manifest") + 1;
if (app.arguments().count() <= index) {
throw QString("You must set a value for --manifest");
}
manifestPath = app.arguments().at(index);
} else if (app.arguments().contains("--local-manifest")) {
auto index = app.arguments().indexOf("--local-manifest") + 1;
if (app.arguments().count() <= index) {
throw QString("You must set a value for --local-manifest");
}
localManifestFile = new QFile(app.arguments().at(index), &app);
if (!localManifestFile->exists()) {
throw QString("The specified local manifest file does not exist");
}
}
auto url = app.arguments().at(2);
auto packageName = app.arguments().at(1);
auto outputDirectory = app.arguments().at(3);
GitHelper gitHelper;
gitHelper.clone(outputDirectory);
gitHelper.checkout(outputDirectory);
gitHelper.reinitGitDirectory(outputDirectory);
QScopedPointer<WebsiteParser> parser(
localManifestFile == nullptr
? new WebsiteParser(url, manifestPath)
: new WebsiteParser(url, *localManifestFile)
);
auto basicData = parser->getData();
basicData.insert("package", packageName);
auto icons = parser->getImages();
AndroidProjectModifier androidProject(outputDirectory);
androidProject.addSupportLibrary();
androidProject.setBasicData(basicData);
androidProject.addImages(icons);
gitHelper.initialCommit(outputDirectory);
io.writeln("Successfully created, you can now open the project in Android Studio and build the apk");
io.writeln("");
io.writeln(ColoredString("Don't forget to include the Digital Asset Link on your website at <green>" + url + "/.well-known/assetlinks.json</>:"));
io.writeln(ColoredString(
"[{ \n"
" \"relation\": [\"delegate_permission/common.handle_all_urls\"], \n"
" \"target\": {\"namespace\": \"android_app\", \"package_name\": \"<green>" + packageName + "</>\", \n"
" \"sha256_cert_fingerprints\": [\"<green>hash_of_app_certificate</>\"]} \n"
"}]"
));
return 0;
} catch (QString e) {
io.writeln(e);
return 1;
}
}