|
| 1 | +#include <util/stream/file.h> |
| 2 | +#include <util/stream/output.h> |
| 3 | +#include <util/string/builder.h> |
| 4 | +#include <util/system/src_location.h> |
| 5 | +#include <ydb/core/protos/flat_scheme_op.pb.h> |
| 6 | +#include <google/protobuf/descriptor.pb.h> |
| 7 | + |
| 8 | +#include <jinja2cpp/template_env.h> |
| 9 | +#include <jinja2cpp/template.h> |
| 10 | +#include <jinja2cpp/value.h> |
| 11 | +#include <jinja2cpp/reflected_value.h> |
| 12 | +#include <cstdint> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | +std::string replace( |
| 18 | + const std::string& str, |
| 19 | + const std::string& from, |
| 20 | + const std::string& to) |
| 21 | +{ |
| 22 | + std::string result; |
| 23 | + |
| 24 | + size_t pos = str.find(from); |
| 25 | + result.append(str, 0, pos); |
| 26 | + result.append(to); |
| 27 | + result.append(str, pos + from.size(), std::string::npos); |
| 28 | + |
| 29 | + return result; |
| 30 | +} |
| 31 | + |
| 32 | +int main(int argc, char** argv) { |
| 33 | + if (argc < 3) { |
| 34 | + Cerr << "Usage: " << argv[0] << " INPUT OUTPUT ..." << Endl; |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + std::vector<std::string> opTypes; |
| 39 | + const auto* d = NKikimrSchemeOp::EOperationType_descriptor(); |
| 40 | + for (int i = 0; i < d->value_count(); ++i) { |
| 41 | + const auto* v = d->value(i); |
| 42 | + if (v) { |
| 43 | + auto name = v->full_name(); |
| 44 | + name = replace(name, ".", "::"); |
| 45 | + opTypes.emplace_back(name); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + jinja2::TemplateEnv env; |
| 50 | + env.AddGlobal("generator", jinja2::Reflect(std::string(__SOURCE_FILE__))); |
| 51 | + env.AddGlobal("opTypes", jinja2::Reflect(opTypes)); |
| 52 | + |
| 53 | + for (int i = 1; i < argc; i += 2) { |
| 54 | + if (!(i + 1 < argc)) { |
| 55 | + Cerr << "ERROR: missing output for " << argv[i] << Endl; |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + jinja2::Template t(&env); |
| 60 | + auto loaded = t.Load(TFileInput(argv[i]).ReadAll(), argv[i]); |
| 61 | + if (!loaded) { |
| 62 | + Cerr << "ERROR: " << loaded.error().ToString() << Endl; |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + auto rendered = t.RenderAsString({}); |
| 67 | + if (!rendered) { |
| 68 | + Cerr << "ERROR: " << rendered.error().ToString() << Endl; |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + TFileOutput(argv[i + 1]).Write(rendered.value()); |
| 73 | + Cout << "Generated " << argv[i + 1] << " from " << argv[i] << Endl; |
| 74 | + } |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
0 commit comments