From bb2a53ac9c75dca37fd5121df97b3f60b5cb723e Mon Sep 17 00:00:00 2001 From: Mykola Yashchenko Date: Thu, 27 Jul 2017 14:27:59 +0300 Subject: [PATCH] added additional properties to manage Android version numbers more easily (#6160) --- .../languages/AndroidClientCodegen.java | 45 +++++++++++++++++++ .../src/main/resources/android/build.mustache | 20 +++++++++ .../android/AndroidClientOptionsTest.java | 6 +++ .../options/AndroidClientOptionsProvider.java | 6 +++ .../httpclient/.swagger-codegen/VERSION | 2 +- .../android/volley/.swagger-codegen/VERSION | 2 +- 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java index 73a8d9859c7..3796826e71a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidClientCodegen.java @@ -22,6 +22,9 @@ import org.slf4j.LoggerFactory; public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(AndroidClientCodegen.class); public static final String USE_ANDROID_MAVEN_GRADLE_PLUGIN = "useAndroidMavenGradlePlugin"; + public static final String ANDROID_GRADLE_VERSION = "androidGradleVersion"; + public static final String ANDROID_SDK_VERSION = "androidSdkVersion"; + public static final String ANDROID_BUILD_TOOLS_VERSION = "androidBuildToolsVersion"; protected String invokerPackage = "io.swagger.client"; protected String groupId = "io.swagger"; protected String artifactId = "swagger-android-client"; @@ -29,6 +32,9 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi protected String projectFolder = "src/main"; protected String sourceFolder = projectFolder + "/java"; protected Boolean useAndroidMavenGradlePlugin = true; + protected String androidGradleVersion; + protected String androidSdkVersion; + protected String androidBuildToolsVersion; protected Boolean serializableModel = false; // requestPackage and authPackage are used by the "volley" template/library @@ -93,6 +99,9 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC)); cliOptions.add(CliOption.newBoolean(USE_ANDROID_MAVEN_GRADLE_PLUGIN, "A flag to toggle android-maven gradle plugin.") .defaultValue(Boolean.TRUE.toString())); + cliOptions.add(new CliOption(ANDROID_GRADLE_VERSION, "gradleVersion version for use in the generated build.gradle")); + cliOptions.add(new CliOption(ANDROID_SDK_VERSION, "compileSdkVersion version for use in the generated build.gradle")); + cliOptions.add(new CliOption(ANDROID_BUILD_TOOLS_VERSION, "buildToolsVersion version for use in the generated build.gradle")); cliOptions.add(CliOption.newBoolean(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC)); @@ -383,6 +392,18 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi additionalProperties.put(USE_ANDROID_MAVEN_GRADLE_PLUGIN, useAndroidMavenGradlePlugin); } + if (additionalProperties.containsKey(ANDROID_GRADLE_VERSION)) { + this.setAndroidGradleVersion((String) additionalProperties.get(ANDROID_GRADLE_VERSION)); + } + + if (additionalProperties.containsKey(ANDROID_SDK_VERSION)) { + this.setAndroidSdkVersion((String) additionalProperties.get(ANDROID_SDK_VERSION)); + } + + if (additionalProperties.containsKey(ANDROID_BUILD_TOOLS_VERSION)) { + this.setAndroidBuildToolsVersion((String) additionalProperties.get(ANDROID_BUILD_TOOLS_VERSION)); + } + if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) { this.setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY)); } @@ -496,10 +517,34 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi return useAndroidMavenGradlePlugin; } + public String getAndroidGradleVersion() { + return androidGradleVersion; + } + + public String getAndroidSdkVersion() { + return androidSdkVersion; + } + + public String getAndroidBuildToolsVersion() { + return androidBuildToolsVersion; + } + public void setUseAndroidMavenGradlePlugin(Boolean useAndroidMavenGradlePlugin) { this.useAndroidMavenGradlePlugin = useAndroidMavenGradlePlugin; } + public void setAndroidGradleVersion(String androidGradleVersion) { + this.androidGradleVersion = androidGradleVersion; + } + + public void setAndroidSdkVersion(String androidSdkVersion) { + this.androidSdkVersion = androidSdkVersion; + } + + public void setAndroidBuildToolsVersion(String androidBuildToolsVersion) { + this.androidBuildToolsVersion = androidBuildToolsVersion; + } + public void setInvokerPackage(String invokerPackage) { this.invokerPackage = invokerPackage; } diff --git a/modules/swagger-codegen/src/main/resources/android/build.mustache b/modules/swagger-codegen/src/main/resources/android/build.mustache index 8a5855dc0c4..2900eaf0bcd 100644 --- a/modules/swagger-codegen/src/main/resources/android/build.mustache +++ b/modules/swagger-codegen/src/main/resources/android/build.mustache @@ -8,7 +8,12 @@ buildscript { jcenter() } dependencies { + {{#androidGradleVersion}} + classpath 'com.android.tools.build:gradle:{{{androidGradleVersion}}}' + {{/androidGradleVersion}} + {{^androidGradleVersion}} classpath 'com.android.tools.build:gradle:2.3.+' + {{/androidGradleVersion}} {{#useAndroidMavenGradlePlugin}} classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' {{/useAndroidMavenGradlePlugin}} @@ -28,12 +33,27 @@ apply plugin: 'com.github.dcendents.android-maven' {{/useAndroidMavenGradlePlugin}} android { + {{#androidSdkVersion}} + compileSdkVersion {{{androidSdkVersion}}} + {{/androidSdkVersion}} + {{^androidSdkVersion}} compileSdkVersion 25 + {{/androidSdkVersion}} + {{#androidBuildToolsVersion}} + buildToolsVersion '{{{androidBuildToolsVersion}}}' + {{/androidBuildToolsVersion}} + {{^androidBuildToolsVersion}} buildToolsVersion '25.0.2' + {{/androidBuildToolsVersion}} useLibrary 'org.apache.http.legacy' defaultConfig { minSdkVersion 14 + {{#androidSdkVersion}} + targetSdkVersion {{{androidSdkVersion}}} + {{/androidSdkVersion}} + {{^androidSdkVersion}} targetSdkVersion 25 + {{/androidSdkVersion}} } compileOptions { {{#java8}} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/android/AndroidClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/android/AndroidClientOptionsTest.java index d3e97de43b8..d083265fa94 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/android/AndroidClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/android/AndroidClientOptionsTest.java @@ -44,6 +44,12 @@ public class AndroidClientOptionsTest extends AbstractOptionsTest { times = 1; clientCodegen.setUseAndroidMavenGradlePlugin(Boolean.valueOf(AndroidClientOptionsProvider.ANDROID_MAVEN_GRADLE_PLUGIN_VALUE)); times = 1; + clientCodegen.setAndroidGradleVersion(AndroidClientOptionsProvider.ANDROID_GRADLE_VERSION_VALUE); + times = 1; + clientCodegen.setAndroidSdkVersion(AndroidClientOptionsProvider.ANDROID_SDK_VERSION_VALUE); + times = 1; + clientCodegen.setAndroidBuildToolsVersion(AndroidClientOptionsProvider.ANDROID_BUILD_TOOLS_VERSION_VALUE); + times = 1; clientCodegen.setLibrary(AndroidClientOptionsProvider.LIBRARY_VALUE); times = 1; clientCodegen.setSerializableModel(Boolean.valueOf(AndroidClientOptionsProvider.SERIALIZABLE_MODEL_VALUE)); diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/AndroidClientOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/AndroidClientOptionsProvider.java index c17a567798e..4b6d7dd277c 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/AndroidClientOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/AndroidClientOptionsProvider.java @@ -18,6 +18,9 @@ public class AndroidClientOptionsProvider implements OptionsProvider { public static final String ARTIFACT_VERSION_VALUE = "1.0.0-SNAPSHOT"; public static final String SOURCE_FOLDER_VALUE = "src/main/java/test"; public static final String ANDROID_MAVEN_GRADLE_PLUGIN_VALUE = "true"; + public static final String ANDROID_GRADLE_VERSION_VALUE = "2.3.0"; + public static final String ANDROID_SDK_VERSION_VALUE = "26"; + public static final String ANDROID_BUILD_TOOLS_VERSION_VALUE = "26.0.0"; public static final String LIBRARY_VALUE = "httpclient"; public static final String SERIALIZABLE_MODEL_VALUE = "false"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; @@ -40,6 +43,9 @@ public class AndroidClientOptionsProvider implements OptionsProvider { .put(CodegenConstants.ARTIFACT_VERSION, ARTIFACT_VERSION_VALUE) .put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE) .put(AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN, ANDROID_MAVEN_GRADLE_PLUGIN_VALUE) + .put(AndroidClientCodegen.ANDROID_GRADLE_VERSION, ANDROID_GRADLE_VERSION_VALUE) + .put(AndroidClientCodegen.ANDROID_SDK_VERSION, ANDROID_SDK_VERSION_VALUE) + .put(AndroidClientCodegen.ANDROID_BUILD_TOOLS_VERSION, ANDROID_BUILD_TOOLS_VERSION_VALUE) .put(CodegenConstants.LIBRARY, LIBRARY_VALUE) .put(CodegenConstants.SERIALIZABLE_MODEL, SERIALIZABLE_MODEL_VALUE) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) diff --git a/samples/client/petstore/android/httpclient/.swagger-codegen/VERSION b/samples/client/petstore/android/httpclient/.swagger-codegen/VERSION index 7fea99011a6..f9f7450d135 100644 --- a/samples/client/petstore/android/httpclient/.swagger-codegen/VERSION +++ b/samples/client/petstore/android/httpclient/.swagger-codegen/VERSION @@ -1 +1 @@ -2.2.3-SNAPSHOT \ No newline at end of file +2.3.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/android/volley/.swagger-codegen/VERSION b/samples/client/petstore/android/volley/.swagger-codegen/VERSION index 7fea99011a6..f9f7450d135 100644 --- a/samples/client/petstore/android/volley/.swagger-codegen/VERSION +++ b/samples/client/petstore/android/volley/.swagger-codegen/VERSION @@ -1 +1 @@ -2.2.3-SNAPSHOT \ No newline at end of file +2.3.0-SNAPSHOT \ No newline at end of file