Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cpp-qt-client library #29

Merged
merged 4 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ config = {
'branch': 'main',
},
},
# FIXME: switch back to openapitools/openapi-generator-cli
# when https://github.com/OpenAPITools/openapi-generator/pull/11490 is merged
'openapi-generator-image': 'owncloudci/openapi-generator'
}

def main(ctx):
Expand Down Expand Up @@ -103,7 +106,7 @@ def linting(ctx):
'steps': [
{
'name': 'validate',
'image': 'openapitools/openapi-generator-cli',
'image': config['openapi-generator-image'],
'pull': 'always',
'commands': [
'/usr/local/bin/docker-entrypoint.sh validate -i api/openapi-spec/v0.0.yaml',
Expand Down Expand Up @@ -162,10 +165,11 @@ def generate(ctx, lang):
},
{
'name': 'generate-%s' % lang,
'image': 'openapitools/openapi-generator-cli',
'image': config['openapi-generator-image'],
'pull': 'always',
'commands': [
'/usr/local/bin/docker-entrypoint.sh generate -i api/openapi-spec/v0.0.yaml --additional-properties=packageName=libregraph --git-user-id=owncloud --git-repo-id=%s -g %s -o %s' % (config["languages"][lang]["repo-slug"], lang, config["languages"][lang]["src"]),
'test -d "templates/{0}" && TEMPLATE_ARG="-t templates/{0}" || TEMPLATE_ARG=""'.format(lang),
'/usr/local/bin/docker-entrypoint.sh generate --enable-post-process-file -i api/openapi-spec/v0.0.yaml $${TEMPLATE_ARG} --additional-properties=packageName=libregraph --git-user-id=owncloud --git-repo-id=%s -g %s -o %s' % (config["languages"][lang]["repo-slug"], lang, config["languages"][lang]["src"]),
],
},
{
Expand All @@ -187,7 +191,7 @@ def generate(ctx, lang):
"message": "%s" % ctx.build.message,
"branch": "%s" % config["languages"][lang]["branch"],
"path": "%s" % config["languages"][lang]["src"],
"author_email": "%s" % ctx.build.author_email,
"author_email": "%s" % ctx.build.author_email,
"author_name": "%s" % ctx.build.author_name,
"followtags": True,
"remote" : "https://github.com/owncloud/%s" % config["languages"][lang]["repo-slug"],
Expand Down Expand Up @@ -234,8 +238,7 @@ def validate(lang):
"cd %s" % config["languages"][lang]["src"],
"cmake -GNinja .",
"ninja -j1",
],
"failure": "ignore", # we need to fix the code generator
]
}
],
"go": [
Expand Down
79 changes: 79 additions & 0 deletions templates/cpp-qt-client/CMakeLists.txt.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
cmake_minimum_required(VERSION 3.2)
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]" APP_VERSION "{{appVersion}}")
string(REGEX REPLACE " " "" TARGET_NAME "{{appName}}")

project("{{appName}}" VERSION ${APP_VERSION} LANGUAGES CXX)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

if (MSVC)
string(REGEX REPLACE "/W[1-3]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-unused-variable")
endif ()

find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED){{#authMethods}}{{#isOAuth}}
find_package(Qt5Gui REQUIRED){{/isOAuth}}{{/authMethods}}{{#contentCompression}}
find_package(ZLIB REQUIRED){{/contentCompression}}

add_library(client STATIC
{{#models}}
{{#model}}
{{classname}}.cpp
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{classname}}.cpp
{{/operations}}
{{/apis}}
{{/apiInfo}}
{{prefix}}Helpers.cpp
{{prefix}}HttpRequest.cpp
{{prefix}}HttpFileElement.cpp
{{prefix}}Oauth.cpp
)
target_include_directories(client PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/OpenAPI/${TARGET_NAME}>
)

add_library(OpenAPI::${TARGET_NAME} ALIAS client)

target_link_libraries(client PRIVATE Qt5::Core Qt5::Network{{#authMethods}}{{#isOAuth}} Qt5::Gui{{/isOAuth}}{{/authMethods}}{{#contentCompression}} ${ZLIB_LIBRARIES}{{/contentCompression}})


set_target_properties(client PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
OUTPUT_NAME ${TARGET_NAME}
EXPORT_NAME ${TARGET_NAME})

set(HEADER
{{#models}}
{{#model}}
{{classname}}.h
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{classname}}.h
{{/operations}}
{{/apis}}
{{/apiInfo}}
{{prefix}}Enum.h
{{prefix}}Helpers.h
{{prefix}}HttpRequest.h
{{prefix}}HttpFileElement.h
{{prefix}}Oauth.h
{{prefix}}Object.h
)
install(FILES ${HEADER} DESTINATION include/OpenAPI/${TARGET_NAME})
install(TARGETS client EXPORT ${TARGET_NAME}Config RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
install(EXPORT ${TARGET_NAME}Config DESTINATION lib/cmake/${TARGET_NAME} NAMESPACE OpenAPI::)
151 changes: 151 additions & 0 deletions templates/cpp-qt-client/HttpFileElement.cpp.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// HttpFileElement.cpp.mustache

{{>licenseInfo}}
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>

#include "{{prefix}}HttpFileElement.h"

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

void {{prefix}}HttpFileElement::setMimeType(const QString &mime) {
mime_type = mime;
}

void {{prefix}}HttpFileElement::setFileName(const QString &name) {
local_filename = name;
}

void {{prefix}}HttpFileElement::setVariableName(const QString &name) {
variable_name = name;
}

void {{prefix}}HttpFileElement::setRequestFileName(const QString &name) {
request_filename = name;
}

bool {{prefix}}HttpFileElement::isSet() const {
return !local_filename.isEmpty() || !request_filename.isEmpty();
}

QString {{prefix}}HttpFileElement::asJson() const {
QFile file(local_filename);
QByteArray bArray;
bool result = false;
if (file.exists()) {
result = file.open(QIODevice::ReadOnly);
bArray = file.readAll();
file.close();
}
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return QString(bArray);
}

QJsonValue {{prefix}}HttpFileElement::asJsonValue() const {
QFile file(local_filename);
QByteArray bArray;
bool result = false;
if (file.exists()) {
result = file.open(QIODevice::ReadOnly);
bArray = file.readAll();
file.close();
}
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return QJsonDocument::fromJson(bArray.data()).object();
#else
return QJsonDocument::fromBinaryData(bArray.data()).object();
#endif
}

bool {{prefix}}HttpFileElement::fromStringValue(const QString &instr) {
QFile file(local_filename);
bool result = false;
if (file.exists()) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
file.write(instr.toUtf8());
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;
}
return result;
}

bool {{prefix}}HttpFileElement::fromJsonValue(const QJsonValue &jval) {
QFile file(local_filename);
bool result = false;
if (file.exists()) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
file.write(QJsonDocument(jval.toObject()).toJson());
#else
file.write(QJsonDocument(jval.toObject()).toBinaryData());
#endif
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;
}
return result;
}

QByteArray {{prefix}}HttpFileElement::asByteArray() const {
QFile file(local_filename);
QByteArray bArray;
bool result = false;
if (file.exists()) {
result = file.open(QIODevice::ReadOnly);
bArray = file.readAll();
file.close();
}
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return bArray;
}

bool {{prefix}}HttpFileElement::fromByteArray(const QByteArray &bytes) {
QFile file(local_filename);
bool result = false;
if (file.exists()) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
file.write(bytes);
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;
}
return result;
}

bool {{prefix}}HttpFileElement::saveToFile(const QString &varName, const QString &localFName, const QString &reqFname, const QString &mime, const QByteArray &bytes) {
setMimeType(mime);
setFileName(localFName);
setVariableName(varName);
setRequestFileName(reqFname);
return fromByteArray(bytes);
}

QByteArray {{prefix}}HttpFileElement::loadFromFile(const QString &varName, const QString &localFName, const QString &reqFname, const QString &mime) {
setMimeType(mime);
setFileName(localFName);
setVariableName(varName);
setRequestFileName(reqFname);
return asByteArray();
}

{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}
43 changes: 43 additions & 0 deletions templates/cpp-qt-client/HttpFileElement.h.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// HttpFileElement.h.mustache

{{>licenseInfo}}
#ifndef {{prefix}}_HTTP_FILE_ELEMENT_H
#define {{prefix}}_HTTP_FILE_ELEMENT_H

#include <QJsonValue>
#include <QMetaType>
#include <QString>

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

class {{prefix}}HttpFileElement {

public:
QString variable_name;
QString local_filename;
QString request_filename;
QString mime_type;
void setMimeType(const QString &mime);
void setFileName(const QString &name);
void setVariableName(const QString &name);
void setRequestFileName(const QString &name);
bool isSet() const;
bool fromStringValue(const QString &instr);
bool fromJsonValue(const QJsonValue &jval);
bool fromByteArray(const QByteArray &bytes);
bool saveToFile(const QString &variable_name, const QString &local_filename, const QString &request_filename, const QString &mime, const QByteArray &bytes);
QString asJson() const;
QJsonValue asJsonValue() const;
QByteArray asByteArray() const;
QByteArray loadFromFile(const QString &variable_name, const QString &local_filename, const QString &request_filename, const QString &mime);
};

{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}

Q_DECLARE_METATYPE({{#cppNamespaceDeclarations}}{{this}}::{{/cppNamespaceDeclarations}}{{prefix}}HttpFileElement)

#endif // {{prefix}}_HTTP_FILE_ELEMENT_H
Loading