Adding qt project generation fix #7784 (#7799)

* 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
This commit is contained in:
Martin Delille 2018-03-14 17:31:03 +01:00 committed by William Cheng
parent 25a6a9d444
commit d82499944b
6 changed files with 109 additions and 27 deletions

View File

@ -26,6 +26,7 @@ import java.util.Set;
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>();
@ -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();
@ -82,6 +84,7 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
// 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
@ -114,6 +117,9 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
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>();
@ -160,6 +166,14 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
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();
@ -183,6 +197,12 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
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);
}
}
/**
@ -425,4 +445,8 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}
}

View File

@ -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

View File

@ -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
@ -26,6 +27,7 @@ public class Qt5CPPOptionsProvider implements OptionsProvider {
.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();
}

View File

@ -28,6 +28,8 @@ public class Qt5CPPOptionsTest extends AbstractOptionsTest {
new Expectations(clientCodegen) {{
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(Qt5CPPOptionsProvider.SORT_PARAMS_VALUE));
times = 1;
clientCodegen.setOptionalProjectFileFlag(true);
times = 1;
}};
}
}

View File

@ -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
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
HEADERS += PetApiTests.h

View File

@ -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