Skip to content

Commit

Permalink
Adding qt project generation fix #7784 (#7799)
Browse files Browse the repository at this point in the history
* starting adding qt project generation

* update sample after running bin/qt5-petstore.sh

* Add Project.mustache starter

* Fix processOpts function

* Write Project.mustache

* Add prefix
  • Loading branch information
MartinDelille authored and wing328 committed Mar 14, 2018
1 parent 25a6a9d commit d824999
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig {
public static final String CPP_NAMESPACE = "cppNamespace";
public static final String CPP_NAMESPACE_DESC = "C++ namespace (convention: name::space::for::api).";
public static final String OPTIONAL_PROJECT_FILE_DESC = "Generate client.pri.";

protected final String PREFIX = "SWG";
protected Set<String> foundationClasses = new HashSet<String>();
Expand All @@ -35,6 +36,7 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
protected Map<String, String> namespaces = new HashMap<String, String>();
protected Set<String> systemIncludes = new HashSet<String>();
protected String cppNamespace = "Swagger";
protected boolean optionalProjectFileFlag = true;

public Qt5CPPGenerator() {
super();
Expand Down Expand Up @@ -82,6 +84,7 @@ public Qt5CPPGenerator() {

// CLI options
addOption(CPP_NAMESPACE, CPP_NAMESPACE_DESC, this.cppNamespace);
addSwitch(CodegenConstants.OPTIONAL_PROJECT_FILE, OPTIONAL_PROJECT_FILE_DESC, this.optionalProjectFileFlag);

/*
* Additional Properties. These values can be passed to the templates and
Expand Down Expand Up @@ -114,6 +117,9 @@ public Qt5CPPGenerator() {
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, PREFIX + "HttpRequest.cpp"));
supportingFiles.add(new SupportingFile("modelFactory.mustache", sourceFolder, PREFIX + "ModelFactory.h"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, PREFIX + "Object.h"));
if (optionalProjectFileFlag) {
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, "client.pri"));
}

super.typeMapping = new HashMap<String, String>();

Expand Down Expand Up @@ -160,6 +166,14 @@ protected void addOption(String key, String description, String defaultValue) {
cliOptions.add(option);
}

protected void addSwitch(String key, String description, Boolean defaultValue) {
CliOption option = CliOption.newBoolean(key, description);
if (defaultValue != null)
option.defaultValue(defaultValue.toString());
cliOptions.add(option);
}


@Override
public void processOpts() {
super.processOpts();
Expand All @@ -183,6 +197,12 @@ public void processOpts() {
importMapping.put("SWGHttpRequestInputFileElement", "#include \"" + modelNamePrefix + "HttpRequest.h\"");
additionalProperties().put("prefix", modelNamePrefix);
}

if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_FILE)) {
setOptionalProjectFileFlag(convertPropertyToBooleanAndWriteBack(CodegenConstants.OPTIONAL_PROJECT_FILE));
} else {
additionalProperties.put(CodegenConstants.OPTIONAL_PROJECT_FILE, optionalProjectFileFlag);
}
}

/**
Expand Down Expand Up @@ -425,4 +445,8 @@ public String escapeQuotationMark(String input) {
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}

public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}
}
42 changes: 42 additions & 0 deletions modules/swagger-codegen/src/main/resources/qt5cpp/Project.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
QT += network

HEADERS += \
# Models
{{#models}}
{{#model}}
$${PWD}/{{classname}}.h \
{{/model}}
{{/models}}
# APIs
{{#apiInfo}}
{{#apis}}
{{#operations}}
$${PWD}/{{classname}}.h \
{{/operations}}
{{/apis}}
{{/apiInfo}}
# Others
$${PWD}/{{prefix}}Helpers.h \
$${PWD}/{{prefix}}HttpRequest.h \
$${PWD}/{{prefix}}ModelFactory.h \
$${PWD}/{{prefix}}Object.h

SOURCES += \
# Models
{{#models}}
{{#model}}
$${PWD}/{{classname}}.cpp \
{{/model}}
{{/models}}
# APIs
{{#apiInfo}}
{{#apis}}
{{#operations}}
$${PWD}/{{classname}}.cpp \
{{/operations}}
{{/apis}}
{{/apiInfo}}
# Others
$${PWD}/{{prefix}}Helpers.cpp \
$${PWD}/{{prefix}}HttpRequest.cpp

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Qt5CPPOptionsProvider implements OptionsProvider {
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String CPP_NAMESPACE_VALUE = "Swagger";
public static final String OPTIONAL_PROJECT_FILE_VALUE = "true";


@Override
Expand All @@ -26,6 +27,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(Qt5CPPGenerator.CPP_NAMESPACE, CPP_NAMESPACE_VALUE)
.put(CodegenConstants.OPTIONAL_PROJECT_FILE, OPTIONAL_PROJECT_FILE_VALUE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ protected void setExpectations() {
new Expectations(clientCodegen) {{
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(Qt5CPPOptionsProvider.SORT_PARAMS_VALUE));
times = 1;
clientCodegen.setOptionalProjectFileFlag(true);
times = 1;
}};
}
}
32 changes: 4 additions & 28 deletions samples/client/petstore/qt5cpp/PetStore/PetStore.pro
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,9 @@ CONFIG += c++11

TEMPLATE = app

include(../client/client.pri)

SOURCES += main.cpp \
../client/SWGCategory.cpp \
../client/SWGHelpers.cpp \
../client/SWGHttpRequest.cpp \
../client/SWGOrder.cpp \
../client/SWGPet.cpp \
../client/SWGPetApi.cpp \
../client/SWGStoreApi.cpp \
../client/SWGTag.cpp \
../client/SWGUser.cpp \
../client/SWGUserApi.cpp \
../client/SWGApiResponse.cpp \
PetApiTests.cpp

HEADERS += \
../client/SWGCategory.h \
../client/SWGHelpers.h \
../client/SWGHttpRequest.h \
../client/SWGObject.h \
../client/SWGOrder.h \
../client/SWGPet.h \
../client/SWGPetApi.h \
../client/SWGStoreApi.h \
../client/SWGTag.h \
../client/SWGUser.h \
../client/SWGUserApi.h \
PetApiTests.h \
../client/SWGApiResponse.h \
../client/SWGModelFactory.h
PetApiTests.cpp

HEADERS += PetApiTests.h
36 changes: 36 additions & 0 deletions samples/client/petstore/qt5cpp/client/client.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
QT += network

HEADERS += \
# Models
$${PWD}/SWGApiResponse.h \
$${PWD}/SWGCategory.h \
$${PWD}/SWGOrder.h \
$${PWD}/SWGPet.h \
$${PWD}/SWGTag.h \
$${PWD}/SWGUser.h \
# APIs
$${PWD}/SWGPetApi.h \
$${PWD}/SWGStoreApi.h \
$${PWD}/SWGUserApi.h \
# Others
$${PWD}/SWGHelpers.h \
$${PWD}/SWGHttpRequest.h \
$${PWD}/SWGModelFactory.h \
$${PWD}/SWGObject.h

SOURCES += \
# Models
$${PWD}/SWGApiResponse.cpp \
$${PWD}/SWGCategory.cpp \
$${PWD}/SWGOrder.cpp \
$${PWD}/SWGPet.cpp \
$${PWD}/SWGTag.cpp \
$${PWD}/SWGUser.cpp \
# APIs
$${PWD}/SWGPetApi.cpp \
$${PWD}/SWGStoreApi.cpp \
$${PWD}/SWGUserApi.cpp \
# Others
$${PWD}/SWGHelpers.cpp \
$${PWD}/SWGHttpRequest.cpp

0 comments on commit d824999

Please sign in to comment.