! * \brief Initializes JavaScript processing for the specified \a args. * \remarks * - Comes with its own QCoreApplication. Only for use within the CLI parts of the app! * - Exits the app on fatal errors. * - Logs status/problems directly in accordance with other parts of the CLI. */
| 505 | * - Logs status/problems directly in accordance with other parts of the CLI. |
| 506 | */ |
| 507 | JavaScriptProcessor::JavaScriptProcessor(const SetTagInfoArgs &args) |
| 508 | : args(args) |
| 509 | , argc(0) |
| 510 | , app(argc, nullptr) |
| 511 | , utility(new UtilityObject(&engine)) |
| 512 | { |
| 513 | // print status message |
| 514 | const auto jsPath = args.jsArg.firstValue(); |
| 515 | if (!jsPath) { |
| 516 | return; |
| 517 | } |
| 518 | if (!args.quietArg.isPresent()) { |
| 519 | std::cout << TextAttribute::Bold << "Loading JavaScript file \"" << jsPath << "\" ..." << Phrases::EndFlush; |
| 520 | } |
| 521 | |
| 522 | // print warnings later via the usual helper function for consistent formatting |
| 523 | engine.setOutputWarningsToStandardError(false); |
| 524 | QObject::connect(&engine, &QQmlEngine::warnings, &engine, |
| 525 | [this, context = std::string("loading JavaScript")](const auto &warnings) { addWarnings(diag, context, warnings); }); |
| 526 | |
| 527 | // assign utility object and load specified JavaScript file as module |
| 528 | engine.globalObject().setProperty(QStringLiteral("utility"), engine.newQObject(utility)); |
| 529 | module = engine.importModule(QString::fromUtf8(jsPath)); |
| 530 | if (module.isError()) { |
| 531 | std::cerr << Phrases::Error << "Unable to load the specified JavaScript file \"" << jsPath << "\":" << Phrases::End; |
| 532 | std::cerr << "Uncaught exception at line " << module.property(QStringLiteral("lineNumber")).toInt() << ':' << ' ' |
| 533 | << module.toString().toStdString() << '\n'; |
| 534 | std::exit(EXIT_FAILURE); |
| 535 | } |
| 536 | main = module.property(QStringLiteral("main")); |
| 537 | if (!main.isCallable()) { |
| 538 | std::cerr << Phrases::Error << "The specified JavaScript file \"" << jsPath << "\" does not export a main() function." << Phrases::End; |
| 539 | std::exit(EXIT_FAILURE); |
| 540 | } |
| 541 | |
| 542 | // assign settings specified via CLI argument |
| 543 | auto settings = engine.newObject(); |
| 544 | if (args.jsSettingsArg.isPresent()) { |
| 545 | for (const auto *const setting : args.jsSettingsArg.values()) { |
| 546 | if (const auto *const value = std::strchr(setting, '=')) { |
| 547 | settings.setProperty( |
| 548 | QString::fromUtf8(setting, static_cast<QString::size_type>(value - setting)), QJSValue(QString::fromUtf8(value + 1))); |
| 549 | } else { |
| 550 | settings.setProperty(QString::fromUtf8(setting), QJSValue()); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | engine.globalObject().setProperty(QStringLiteral("settings"), settings); |
| 555 | |
| 556 | // print warnings |
| 557 | printDiagMessages(diag, "Diagnostic messages:", args.verboseArg.isPresent(), &args.pedanticArg); |
| 558 | diag.clear(); |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | * \brief Calls the JavaScript's main() function for the specified \a mediaFileInfo populating \a diag. |
nothing calls this directly
no test coverage detected