Skip to content

Commit

Permalink
Changing configuration format
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-itskovich committed Nov 20, 2015
1 parent 0e24db1 commit beb2575
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,28 +17,31 @@ class ConfigContainer {
parentContainer = (project.parent) ? project.parent.extensions.findByType(ConfigContainer) : null
}

public <C> C configure(Class<C> type) {
return configByType(type)
public <C> C configure(Class<C> 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> C configByType(Class<C> configClass) {
Expand All @@ -59,7 +63,7 @@ class ConfigContainer {

mergeProperties(type.superclass, config)
Object configPart = configByType(type)
mergeProperties(type, configPart, config)
mergeProperties(configPart, config)

return config
}
Expand All @@ -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' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RootProjectAioPlugin implements Plugin<Project> {
}

project.allprojects {
it.extensions.create('aioConfig', ConfigContainer, it)
it.extensions.aioConfig = new ConfigContainer(it)

it.apply plugin: "idea"
it.repositories {
Expand Down
22 changes: 18 additions & 4 deletions src/test/groovy/com/slimgears/gradleio/AioPluginsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit beb2575

Please sign in to comment.