Skip to content

Commit

Permalink
Merge remote-tracking branch 'private/issue/375' into 0.6-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
akirakw committed Mar 11, 2014
2 parents f8202e2 + 934d7cf commit e9f1910
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 0 deletions.
Expand Up @@ -42,6 +42,7 @@ import com.asakusafw.gradle.tasks.CompileBatchappTask
import com.asakusafw.gradle.tasks.CompileDmdlTask
import com.asakusafw.gradle.tasks.GenerateTestbookTask
import com.asakusafw.gradle.tasks.GenerateThunderGateDataModelTask
import com.asakusafw.gradle.tasks.RunBatchappTask

/**
* Gradle plugin for building application component blocks.
Expand Down Expand Up @@ -270,6 +271,7 @@ class AsakusafwPlugin implements Plugin<Project> {
defineJarBatchappTask()
extendAssembleTask()
defineGenerateTestbookTask()
defineTestRunBatchappTask()
defineGenerateThunderGateDataModelTask()
}

Expand Down Expand Up @@ -357,6 +359,20 @@ class AsakusafwPlugin implements Plugin<Project> {
}
}

private void defineTestRunBatchappTask() {
project.tasks.create('testRunBatchapp', RunBatchappTask) { RunBatchappTask task ->
group ASAKUSAFW_BUILD_GROUP
description 'Executes Asakusa Batch Application [Experimental].'
task.toolClasspath = project.files({ project.sourceSets.test.runtimeClasspath })
task.systemProperties.put 'asakusa.testdriver.batchapps', { project.tasks.compileBatchapp.outputDirectory }
task.conventionMapping.with {
logbackConf = { this.findLogbackConf() }
maxHeapSize = { project.asakusafw.maxHeapSize }
}
task.dependsOn project.tasks.compileBatchapp
}
}

private void defineGenerateThunderGateDataModelTask() {
def thundergate = project.asakusafw.thundergate
def task = project.task('generateThunderGateDataModel', type: GenerateThunderGateDataModelTask) {
Expand Down
@@ -0,0 +1,138 @@
/*
* Copyright 2011-2014 Asakusa Framework Team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.asakusafw.gradle.tasks

import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Nullable
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.tasks.options.Option
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.TaskAction
import org.gradle.process.JavaExecSpec

import com.asakusafw.gradle.tasks.internal.ResolutionUtils

/**
* Gradle Task for Running Asakusa batch application.
* @since 0.6.1
*/
class RunBatchappTask extends DefaultTask {

/**
* The logback configuration for the tool.
*/
@Nullable
File logbackConf

/**
* The max heap size for the tool.
*/
@Nullable
String maxHeapSize

/**
* The Java system properties.
*/
Map<Object, Object> systemProperties = [:]

/**
* The Java VM arguments.
*/
List<Object> jvmArgs = []

/**
* The tool class path.
*/
@InputFiles
FileCollection toolClasspath = project.files()

/**
* The target batch ID.
*/
@Input
String batchId

/**
* The target batch arguments.
*/
@Input
Map<Object, Object> batchArguments = [:]

/**
* Sets the target batch ID.
* @param batchId the target batch ID
*/
@Option(option = 'id', description = 'The target batch ID')
void setBatchId(String batchId) {
this.batchId = batchId
}

@Option(option = 'arguments', description = 'The target batch arguments separated by comma')
void setEncodedBatchArguments(String encoded) {
Map<String, String> args = [:]
StringBuilder buf = new StringBuilder()
boolean sawEscape = false
for (char c in encoded.toCharArray()) {
if (sawEscape) {
buf.append c
sawEscape = false
continue
} else if (c == '\\') {
sawEscape = true
} else if (c == ',') {
addArg buf.toString(), args
buf.setLength 0
} else {
buf.append c
}
}
addArg buf.toString(), args
getBatchArguments().putAll(args)
}

private void addArg(String string, Map<String, String> kvs) {
int index = string.indexOf '='
if (index < 0) {
throw new InvalidUserDataException("Invalid batch argument: \"${string}\"")
}
kvs.put string.substring(0, index), string.substring(index + 1)
}

/**
* Performs this task.
*/
@TaskAction
void perform() {
project.javaexec { JavaExecSpec spec ->
spec.main = 'com.asakusafw.testdriver.tools.runner.BatchTestRunner'
spec.classpath = this.getToolClasspath()
spec.jvmArgs = ResolutionUtils.resolveToStringList(this.getJvmArgs())
if (this.getMaxHeapSize()) {
spec.maxHeapSize = this.getMaxHeapSize()
}
if (this.getLogbackConf()) {
spec.systemProperties += ['logback.configurationFile' : this.getLogbackConf().absolutePath]
}
spec.systemProperties += ResolutionUtils.resolveToStringMap(this.getSystemProperties())
spec.args += ['--batch', this.getBatchId()]
for (Map.Entry<String, String> entry in ResolutionUtils.resolveToStringMap(this.getBatchArguments()).entrySet()) {
spec.args += ['-A', "${entry.key}=${entry.value}"]
}
}
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2011-2014 Asakusa Framework Team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.asakusafw.gradle.tasks.internal

import java.util.concurrent.Callable

/**
* Utilities for resolving values.
* @since 0.6.1
*/
final class ResolutionUtils {

/**
* Resolves a value to a string.
* @param value the target value
* @return the resolved string
*/
static String resolveToString(Object value) {
if (value == null) {
return null
} else if (value instanceof String) {
return value
} else if (value instanceof Closure<?>) {
Closure<?> c = value
return resolveToString(c.call())
} else if (value instanceof Callable<?>) {
Callable<?> c = value
return resolveToString(c.call())
} else {
return String.valueOf(value)
}
}

/**
* Resolves values to a string list.
* @param value the target values
* @return the resolved string list
*/
static List<String> resolveToStringList(Iterable<?> values) {
List<String> results = []
for (Object arg in values) {
resolveInto(arg, results)
}
return results
}

static Map<String, String> resolveToStringMap(Map<?, ?> values) {
Map<String, String> results = [:]
for (Map.Entry<?, ?> entry in values.entrySet()) {
String key = resolveToString(entry.key)
if (key != null) {
results.put key, resolveToString(entry.value)
}
}
return results
}

private static void resolveInto(Object arg, List<String> results) {
if (arg instanceof String) {
results.add(results)
} else if (arg instanceof Closure<?>) {
Closure<?> c = arg
resolveInto(c.call(), results)
} else if (arg instanceof Callable<?>) {
Callable<?> c = arg
resolveInto(c.call(), results)
} else if (arg instanceof Iterable<?>) {
for (Object element in (Iterable<?>) arg) {
resolveInto(element, results)
}
} else {
resolveInto(String.valueOf(arg), results)
}
}

private ResolutionUtils() {
}
}
Expand Up @@ -32,6 +32,7 @@ import com.asakusafw.gradle.tasks.CompileBatchappTask
import com.asakusafw.gradle.tasks.CompileDmdlTask
import com.asakusafw.gradle.tasks.GenerateTestbookTask
import com.asakusafw.gradle.tasks.GenerateThunderGateDataModelTask
import com.asakusafw.gradle.tasks.RunBatchappTask

/**
* Test for {@link AsakusafwPlugin}.
Expand Down Expand Up @@ -199,6 +200,27 @@ class AsakusafwPluginTest {
assert task.outputDirectory == project.file(convention.testtools.testDataSheetDirectory)
}

/**
* Test for {@code project.tasks.testRunBatchapp}.
*/
@Test
void tasks_testRunBatchapp() {
AsakusafwPluginConvention convention = project.asakusafw
convention.logbackConf 'testing/logback'
convention.maxHeapSize '1G'

convention.compiler.compiledSourceDirectory 'testing/compiled'

RunBatchappTask task = project.tasks.testRunBatchapp
assert task.logbackConf == project.file(convention.logbackConf)
assert task.maxHeapSize == convention.maxHeapSize
assert project.file(task.systemProperties['asakusa.testdriver.batchapps']) == project.file(convention.compiler.compiledSourceDirectory)
assert task.jvmArgs.isEmpty()

assert task.batchId == null
assert task.batchArguments.isEmpty()
}

/**
* Test for {@code project.tasks.generateThunderGateDataModel}.
*/
Expand Down

0 comments on commit e9f1910

Please sign in to comment.