-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
156 lines (134 loc) · 5.9 KB
/
main.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
/******************************************************************************
* Copyright (C) 2016 Kitsune Ral <kitsune-ral@users.sf.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "analyzer.h"
#include "printer.h"
#include "translator.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <filesystem>
#include <iostream>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setOrganizationName("Quotient");
QCoreApplication::setApplicationName("GTAD");
QCoreApplication::setApplicationVersion("0.9");
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main",
"Matrix API source files generator"));
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption configPathOption("config",
QCoreApplication::translate("main", "API generator configuration in YAML format"),
"configfile");
parser.addOption(configPathOption);
QCommandLineOption outputDirOption("out",
QCoreApplication::translate("main", "Write generated files to <outputdir>."),
"outputdir");
parser.addOption(outputDirOption);
QCommandLineOption schemaRoleOption("role",
QCoreApplication::translate("main",
"For JSON Schema, generate code assuming <role>, one of:"
" i (input), o (output); all other values mean both directions"),
"role", "io");
parser.addOption(schemaRoleOption);
QCommandLineOption messagesRoleOption("messages",
QCoreApplication::translate("main",
"Configure the verbosity, one of: quiet, basic, and debug"),
"verbosity", "basic");
parser.addOption(messagesRoleOption);
parser.addPositionalArgument("files",
QCoreApplication::translate("main",
"Files or directories with API definition in Swagger format."
" Append a hyphen to exclude a file/directory."),
"files...");
parser.process(app);
try {
using namespace std;
namespace fs = filesystem;
const auto& verbosityArg = parser.value(messagesRoleOption);
const auto verbosity = verbosityArg == "quiet" ? Verbosity::Quiet
: verbosityArg == "debug" ? Verbosity::Debug
: Verbosity::Basic;
Translator translator {parser.value(configPathOption).toStdString(),
parser.value(outputDirOption).toStdString(),
verbosity};
vector<fs::path> paths, exclusions;
const auto& pathArgs = parser.positionalArguments();
for (const auto& path: pathArgs) {
if (path.endsWith('-'))
exclusions.emplace_back(path.left(path.size() - 1).toStdString());
else
paths.emplace_back(path.toStdString());
}
const auto& roleValue = parser.value(schemaRoleOption);
const auto role =
roleValue == "i" ? OnlyIn : roleValue == "o" ? OnlyOut : InAndOut;
for(const auto& path: paths) {
auto ftype = fs::status(path).type();
if (ftype == fs::file_type::regular)
Analyzer{translator, path.parent_path()}.loadModel(path.filename().string(), role);
if (ftype != fs::file_type::directory)
continue;
Analyzer a{translator, path};
for (const auto& f :
fs::directory_iterator(path, fs::directory_options::skip_permission_denied))
{
if (!f.is_regular_file())
continue;
auto&& fName = f.path().filename();
if (ranges::find(exclusions, fName) == exclusions.cend())
a.loadModel(fName.string(), role);
}
}
using namespace literals;
const char* clangFormatPath = getenv("CLANG_FORMAT");
string clangFormatCommand {clangFormatPath ? clangFormatPath
: "clang-format"sv};
clangFormatCommand += " -i -sort-includes"sv;
const char* clangFormatArgs = getenv("CLANG_FORMAT_ARGS");
if (clangFormatArgs)
clangFormatCommand += clangFormatArgs;
size_t filesCounter = 0;
for (const auto& [stem, model]: Analyzer::allModels()) {
if (model.empty() || model.trivial())
continue;
const auto targetDir = fs::path(stem).parent_path();
fs::create_directories(targetDir);
if (!fs::exists(targetDir))
throw Exception {"Cannot create output directory "
+ targetDir.string()};
const auto fileNames = translator.printer().print(stem, model);
for (const auto& fName : fileNames)
clangFormatCommand += ' ' + fName;
filesCounter += fileNames.size();
}
cout << "Formatting " << filesCounter << " files\n";
system(clangFormatCommand.c_str());
}
catch (Exception& e)
{
std::cerr << e.message << "\n";
return 3;
}
catch (std::exception& e) {
std::cerr << e.what() << "\n";
return 3;
}
return 0;
}