Skip to content

Commit

Permalink
gradle assemble testClasses works.
Browse files Browse the repository at this point in the history
Incremental velocity processing.
Hook up `-PandroidApi=xx` in gradle build.

Process `robolectric-version.properties` in gradle as in maven.

Get `org.robolectric.internal.ShadowProvider` into the proper `META-INF/services`.

`android-all` should be on compile classpath but not runtime.

Apply java plugin to all projects.

One shadow-core gradle subproject per SDK target.
Install to maven local repo.
  • Loading branch information
xian committed Jul 1, 2016
1 parent 92b0bd0 commit 3ccf232
Show file tree
Hide file tree
Showing 23 changed files with 447 additions and 177 deletions.
120 changes: 120 additions & 0 deletions build.gradle
@@ -0,0 +1,120 @@
allprojects {
repositories {
mavenLocal()
mavenCentral()
}

apply plugin: "java"
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

apply plugin: "maven-publish"

publishing {
publications {
maven(MavenPublication) {
// robolectric-shadows/shadows-core/v16 -> shadows-core-v16
def projNameParts = artifactId.split(/\//) as List
if (projNameParts[0] == "robolectric-shadows") {
projNameParts = projNameParts.drop(1)
artifactId = projNameParts.join("-")
}

from components.java
}
}
}
}

def subshadowRE = /robolectric-shadows\/shadows-core\/v(.*)$/
configure(subprojects.findAll { project -> project.name =~ subshadowRE }) {
apply plugin: ShadowsPlugin
apply plugin: ProvidedPlugin

def match = project.name =~ subshadowRE
def androidApi = Integer.parseInt(match[0][1])

def robolectricVersion = AndroidSdk.versions[androidApi]
logger.info "[${project.name}] Android API is $androidApi$robolectricVersion"

shadows {
packageName "org.robolectric"
}

task velocity(type: VelocityTask) {
def shadowsCoreProject = project.findProject(":robolectric-shadows/shadows-core")
source = shadowsCoreProject.files("src/main/resources").asFileTree.matching { include "/**/*.vm" }

doFirst {
contextValues = [api: androidApi]
if (androidApi >= 21) {
contextValues.putAll(ptrClass: "long", ptrClassBoxed: "Long")
} else {
contextValues.putAll(ptrClass: "int", ptrClassBoxed: "Integer")
}
}

outputDir = project.file("${project.buildDir}/generated-shadows")

doLast {
def shadowsCoreProj = project.findProject(":robolectric-shadows/shadows-core")
project.copy {
from shadowsCoreProj.files("src/main/java")
into outputDir
}

project.copy {
from shadowsCoreProj.fileTree("src/main/resources").include("META-INF/**")
into project.file("${project.buildDir}/resources/main")
}
}
}

generateShadowProvider {
dependsOn velocity
}

dependencies {
// Project dependencies
compile project(":robolectric-annotations")
compile project(":robolectric-resources")
compile project(":robolectric-utils")

// Compile dependencies
compile "com.intellij:annotations:12.0"
compile "com.almworks.sqlite4java:sqlite4java:0.282"
provided("org.robolectric:android-all:$robolectricVersion") { force = true }
compile "com.ibm.icu:icu4j:53.1"

runtime "com.github.axet.litedb:libsqlite:0.282-3:natives-mac-x86_64"
runtime "com.github.axet.litedb:libsqlite:0.282-3:natives-linux-x86"
runtime "com.github.axet.litedb:libsqlite:0.282-3:natives-linux-x86_64"
runtime "com.github.axet.litedb:libsqlite:0.282-3:natives-windows-x86"
runtime "com.github.axet.litedb:libsqlite:0.282-3:natives-windows-x86_64"

// Testing dependencies
testCompile "junit:junit:4.8.2"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.assertj:assertj-core:2.0.0"
}

task copyNatives(type: Copy) {
outputs.dir file("${project.buildDir}/resources/main")

project.configurations.runtime.forEach { File file ->
def nativeJarMatch = file.name =~ /lib.*-natives-(.*)\.jar/
if (nativeJarMatch) {
inputs.file file

def platformName = match[0][1]
from(zipTree(file)) { rename { f -> "$platformName/$f" } }
}

into project.file("${project.buildDir}/resources/main")
}
}

assemble {
dependsOn copyNatives
}
}
12 changes: 12 additions & 0 deletions buildSrc/build.gradle
@@ -0,0 +1,12 @@
apply plugin: 'groovy'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compile gradleApi()
compile localGroovy()
compile 'org.apache.velocity:velocity:1.7'
}
13 changes: 13 additions & 0 deletions buildSrc/src/main/groovy/android-sdk.groovy
@@ -0,0 +1,13 @@
class AndroidSdk {
static final Map<Integer, String> versions = [
16: "4.1.2_r1-robolectric-0",
17: "4.2.2_r1.2-robolectric-0",
18: "4.3_r2-robolectric-0",
19: "4.4_r1-robolectric-1",
21: "5.0.0_r2-robolectric-1",
22: "5.1.1_r9-robolectric-1",
23: "6.0.0_r1-robolectric-0",
]

static final int latestVersion = 23
}
17 changes: 17 additions & 0 deletions buildSrc/src/main/groovy/provided.groovy
@@ -0,0 +1,17 @@
import org.gradle.api.Plugin
import org.gradle.api.Project

class ProvidedPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.configurations {
provided
}

project.sourceSets {
main.compileClasspath += project.configurations.provided
test.compileClasspath += project.configurations.provided
test.runtimeClasspath += project.configurations.provided
}
}
}
58 changes: 58 additions & 0 deletions buildSrc/src/main/groovy/shadows.groovy
@@ -0,0 +1,58 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.util.GFileUtils

class ShadowsPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.extensions.create("shadows", ShadowsPluginExtension)

project.configurations {
robolectricProcessor
}

project.dependencies {
robolectricProcessor project.project(":robolectric-processor")
}

def generatedSourcesDir = "${project.buildDir}/generated-shadows"

project.sourceSets.main.java.srcDirs += project.files(generatedSourcesDir)

project.task("generateShadowProvider", type: JavaCompile, description: "Generate Shadows.shadowOf()s class") { task ->
classpath = project.configurations.robolectricProcessor
source = project.sourceSets.main.java
destinationDir = project.file(generatedSourcesDir)

doFirst {
logger.info "Generating Shadows.java for ${project.name}"

// reset our classpath at the last minute, since other plugins might mutate
// compileJava's classpath and we want to pick up any changes…
classpath = project.tasks['compileJava'].classpath + project.configurations.robolectricProcessor

options.compilerArgs.addAll(
"-proc:only",
"-processor", "org.robolectric.annotation.processing.RobolectricProcessor",
"-Aorg.robolectric.annotation.processing.shadowPackage=${project.shadows.packageName}"
)
}

doLast {
def src = project.file("$generatedSourcesDir/META-INF/services/org.robolectric.internal.ShadowProvider")
def dest = project.file("${project.buildDir}/resources/main/META-INF/services/org.robolectric.internal.ShadowProvider")

GFileUtils.mkdirs(dest.getParentFile());
GFileUtils.copyFile(src, dest);
}
}

def compileJavaTask = project.tasks["compileJava"]
compileJavaTask.dependsOn("generateShadowProvider")
}
}

class ShadowsPluginExtension {
String packageName
}
85 changes: 85 additions & 0 deletions buildSrc/src/main/groovy/velocity.groovy
@@ -0,0 +1,85 @@
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.VelocityEngine
import org.apache.velocity.runtime.log.SystemLogChute
import org.gradle.api.GradleException
import org.gradle.api.file.EmptyFileVisitor
import org.gradle.api.file.FileTree
import org.gradle.api.file.FileVisitDetails
import org.gradle.api.file.RelativePath
import org.gradle.api.tasks.*
import org.gradle.api.tasks.incremental.IncrementalTaskInputs

public class VelocityTask extends SourceTask {

private Map<String, Object> contextValues;
private File outputDir;

@Input
@Optional
public Map<String, Object> getContextValues() {
return contextValues;
}

public void setContextValues(Map<String, Object> contextValues) {
this.contextValues = contextValues;
}

@OutputDirectory
public File getOutputDir() {
return outputDir;
}

public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}

@TaskAction
public void runVelocity(IncrementalTaskInputs inputs) throws Exception {
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());
engine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_CACHE, "true");

VelocityContext context = new VelocityContext();
if (contextValues != null) {
contextValues.each { k, v -> context.put(k, v) }
}

if (!inputs.incremental) {
project.delete(outputDir.listFiles())
}

Set<File> outOfDateFiles = new HashSet<>()
inputs.outOfDate { change -> outOfDateFiles.add(change.file) }

final FileTree inputFiles = getSource();
inputFiles.visit(new EmptyFileVisitor() {
@Override
public void visitFile(FileVisitDetails fvd) {
File inputFile = fvd.file

if (outOfDateFiles.contains(inputFile)) {
RelativePath inputFilePath = fvd.relativePath

try {
def outputFileName = inputFilePath.getLastName().replaceFirst("\\.vm\$", "")
File outputFile = inputFilePath.replaceLastName(outputFileName).getFile(getOutputDir());

if (logger.debugEnabled) {
logger.debug("Preprocessing " + inputFile + " -> " + outputFile);
}

inputFile.withReader { reader ->
outputFile.parentFile.mkdirs();
outputFile.withWriter { writer ->
engine.evaluate((VelocityContext) context.clone(), writer, inputFilePath.toString(), reader);
}
}
} catch (IOException e) {
throw new GradleException("Failed to process " + fvd, e);
}
}
}
})
}
}
2 changes: 2 additions & 0 deletions gradle.properties
@@ -0,0 +1,2 @@
group=org.robolectric
version=3.2-SNAPSHOT
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu Dec 18 14:55:04 PST 2014
#Fri Jun 17 16:13:48 PDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-all.zip
12 changes: 0 additions & 12 deletions provided.gradle

This file was deleted.

12 changes: 2 additions & 10 deletions robolectric-annotations/build.gradle
@@ -1,15 +1,7 @@
apply plugin: "java"

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

repositories {
mavenLocal()
mavenCentral()
}
apply plugin: ProvidedPlugin

dependencies {
// Compile dependencies
compile "com.intellij:annotations:12.0"
compile "org.robolectric:android-all:5.0.0_r2-robolectric-0"
provided "org.robolectric:android-all:6.0.0_r1-robolectric-0"
}
15 changes: 3 additions & 12 deletions robolectric-processor/build.gradle
@@ -1,23 +1,14 @@
apply plugin: "java"

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
// Project dependencies
compile project(":robolectric-annotations")

// Compile dependencies
compile "com.google.guava:guava:17.0"
compile "com.google.guava:guava:19.0-rc2"
compile "com.intellij:annotations:12.0"
compile files("${System.properties['java.home']}/../lib/tools.jar")

// Testing dependencies
testCompile "junit:junit-dep:4.8.2"
testCompile "junit:junit:4.8.2"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "com.google.testing.compile:compile-testing:0.6"
}

0 comments on commit 3ccf232

Please sign in to comment.