From beb25754a04ecaef839f2ba1af0995c815fd2e54 Mon Sep 17 00:00:00 2001 From: slim-gears Date: Fri, 20 Nov 2015 11:36:45 +0200 Subject: [PATCH] Changing configuration format --- .../gradleaio/internal/ConfigContainer.groovy | 34 +++++++++++-------- .../publishing/PublishingConfig.groovy | 12 +++++-- .../root/RootProjectAioPlugin.groovy | 2 +- .../slimgears/gradleio/AioPluginsTest.groovy | 22 +++++++++--- .../gradleio/ConfigContainerTest.groovy | 4 +-- 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/main/groovy/com/slimgears/gradleaio/internal/ConfigContainer.groovy b/src/main/groovy/com/slimgears/gradleaio/internal/ConfigContainer.groovy index b8ecd60..3b3b402 100644 --- a/src/main/groovy/com/slimgears/gradleaio/internal/ConfigContainer.groovy +++ b/src/main/groovy/com/slimgears/gradleaio/internal/ConfigContainer.groovy @@ -4,6 +4,7 @@ import com.slimgears.gradleaio.android.AndroidAioApplicationConfig import com.slimgears.gradleaio.android.AndroidAioConfig import com.slimgears.gradleaio.java.JavaAioConfig import com.slimgears.gradleaio.publishing.PublishingConfig +import org.gradle.api.Action import org.gradle.api.Project class ConfigContainer { @@ -16,28 +17,31 @@ class ConfigContainer { parentContainer = (project.parent) ? project.parent.extensions.findByType(ConfigContainer) : null } - public C configure(Class type) { - return configByType(type) + public C configure(Class type, Closure initializer) { + C config = configByType(type) + initializer.delegate = config + initializer() + return config } - public AndroidAioConfig getAndroidAio() { - return configure(AndroidAioConfig) + public AndroidAioConfig androidAio(Closure initializer) { + return configure(AndroidAioConfig, initializer) } - public AndroidAioApplicationConfig getAndroidAppAio() { - return configure(AndroidAioApplicationConfig) + public AndroidAioApplicationConfig androidAppAio(Closure initializer) { + return configure(AndroidAioApplicationConfig, initializer) } - public BasicConfig getBasicAio() { - return configure(BasicConfig) + public BasicConfig basicAio(Closure initializer) { + return configure(BasicConfig, initializer) } - public JavaAioConfig getJavaAio() { - return configure(JavaAioConfig) + public JavaAioConfig javaAio(Closure initializer) { + return configure(JavaAioConfig, initializer) } - public PublishingConfig getPublishingAio() { - return configure(PublishingConfig) + public PublishingConfig publishingAio(Closure initializer) { + return configure(PublishingConfig, initializer) } public C configByType(Class configClass) { @@ -59,7 +63,7 @@ class ConfigContainer { mergeProperties(type.superclass, config) Object configPart = configByType(type) - mergeProperties(type, configPart, config) + mergeProperties(configPart, config) return config } @@ -68,12 +72,12 @@ class ConfigContainer { Class configClass = config.class C clonedConfig = configClass.newInstance() - mergeProperties(configClass, config, clonedConfig) + mergeProperties(config, clonedConfig) return clonedConfig } - private static void mergeProperties(Class type, Object src, Object dest) { + private static void mergeProperties(Object src, Object dest) { dest.properties.keySet() .findAll { String prop -> prop != 'class' && diff --git a/src/main/groovy/com/slimgears/gradleaio/publishing/PublishingConfig.groovy b/src/main/groovy/com/slimgears/gradleaio/publishing/PublishingConfig.groovy index df6b2be..171bd4f 100644 --- a/src/main/groovy/com/slimgears/gradleaio/publishing/PublishingConfig.groovy +++ b/src/main/groovy/com/slimgears/gradleaio/publishing/PublishingConfig.groovy @@ -1,5 +1,9 @@ package com.slimgears.gradleaio.publishing +import org.gradle.api.Action + +import java.util.concurrent.Callable + class PublishingConfig { class BintrayConfig { String user @@ -18,7 +22,11 @@ class PublishingConfig { String artifactId = null BintrayConfig bintray = null - BintrayConfig getBintray() { - return this.bintray = this.bintray ?: new BintrayConfig() + BintrayConfig bintray(Closure initializer) { + def config = this.bintray = this.bintray ?: new BintrayConfig() + initializer.delegate = config + initializer() + + return config } } diff --git a/src/main/groovy/com/slimgears/gradleaio/root/RootProjectAioPlugin.groovy b/src/main/groovy/com/slimgears/gradleaio/root/RootProjectAioPlugin.groovy index b49ed45..d2e47c7 100644 --- a/src/main/groovy/com/slimgears/gradleaio/root/RootProjectAioPlugin.groovy +++ b/src/main/groovy/com/slimgears/gradleaio/root/RootProjectAioPlugin.groovy @@ -18,7 +18,7 @@ class RootProjectAioPlugin implements Plugin { } project.allprojects { - it.extensions.create('aioConfig', ConfigContainer, it) + it.extensions.aioConfig = new ConfigContainer(it) it.apply plugin: "idea" it.repositories { diff --git a/src/test/groovy/com/slimgears/gradleio/AioPluginsTest.groovy b/src/test/groovy/com/slimgears/gradleio/AioPluginsTest.groovy index 85f6096..32973e7 100644 --- a/src/test/groovy/com/slimgears/gradleio/AioPluginsTest.groovy +++ b/src/test/groovy/com/slimgears/gradleio/AioPluginsTest.groovy @@ -30,22 +30,22 @@ class AioPluginsTest { def configContainer = project.extensions.findByType(ConfigContainer) - configContainer.androidAio.with { + configContainer.androidAio { minSdkVersion = 16 targetSdkVersion = 24 useSupportLibraries = ['recyclerview-v7'] usePlayServices = ['identity', 'plus', 'auth'] } - configContainer.androidAppAio.with { + configContainer.androidAppAio { keyStoreFile = 'test-file' keyStorePassword = 'test-password' keyPassword = 'test-password' } - configContainer.publishingAio.with { + configContainer.publishingAio { artifactId = 'test-artifact' - bintray.with { + bintray { user = 'test-user' key = 'test-key' } @@ -97,4 +97,18 @@ class AioPluginsTest { def localProject = ProjectBuilder.builder().build() localProject.apply plugin: AndroidAioApplicationPlugin } + + @Test void configurationUsingExtension_shouldNotThrow() { + project.aioConfig { + androidAio { + minSdkVersion = 14 + } + } + + Assert.assertEquals(14, + project.extensions + .findByType(ConfigContainer) + .configByType(AndroidAioConfig) + .minSdkVersion) + } } diff --git a/src/test/groovy/com/slimgears/gradleio/ConfigContainerTest.groovy b/src/test/groovy/com/slimgears/gradleio/ConfigContainerTest.groovy index fca442f..9d5886e 100644 --- a/src/test/groovy/com/slimgears/gradleio/ConfigContainerTest.groovy +++ b/src/test/groovy/com/slimgears/gradleio/ConfigContainerTest.groovy @@ -29,11 +29,11 @@ class ConfigContainerTest { @Test void configurationHierarchy_shouldReturnCorrectValues() { assertNotNull(configContainer) - rootProject.extensions.findByType(ConfigContainer).configure(BasicConfig).with { + rootProject.extensions.findByType(ConfigContainer).configure(BasicConfig) { junitVersion = '4.10' } - configContainer.configure(AndroidAioConfig).with { + configContainer.configure(AndroidAioConfig) { minSdkVersion = 1 targetSdkVersion = 24 }