Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.13 KB | None | 0 0
  1. /*
  2.  * Copyright 2014 toxbee.se
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. apply from: 'config.gradle'
  18.  
  19. evaluationDependsOn(ext.robospock)
  20.  
  21. ext.sdkDir = project(project.ext.robospock).android.sdkDirectory
  22.  
  23. buildscript {
  24.     repositories {
  25.         mavenCentral()
  26.     }
  27.     dependencies {
  28.         // RoboSpock - Testing for Android.
  29.         classpath 'org.robospock:robospock-plugin:0.4.0'
  30.     }
  31. }
  32.  
  33. repositories {
  34.     mavenCentral()
  35.     //Again the robospock-plugin does not add all the correct android dependencies
  36.     maven { url "${sdkDir}/extras/android/m2repository" }
  37.     maven { url "${sdkDir}/extras/google/m2repository" }
  38. }
  39.  
  40. apply plugin: 'groovy'
  41.  
  42. dependencies {
  43.     compile "org.codehaus.groovy:groovy-all:$groovyVersion"
  44.     compile "org.spockframework:spock-core:$spockVersion"
  45.     compile "org.robospock:robospock:$roboSpockVersion"
  46.     testCompile 'cglib:cglib-nodep:3.1'
  47.     testCompile 'org.objenesis:objenesis:2.1'
  48. }
  49.  
  50. // Disable the default test task otherwise commandline build fails because
  51. // gradle test task tries to run the specs as if they were standard spock tests
  52. test.onlyIf { false }
  53.  
  54. /**
  55.  * The findCompileDependencies method of the org.robospock:robospock-plugin does not work with the
  56.  * latest gradle and android build tools.
  57.  * Consider fixing the plugin and submitting
  58.  */
  59. org.robospock.RobospockAction.metaClass.findCompileDependencies = { Project androidProject ->
  60.     androidProject.configurations.all.find {
  61.         it.name == 'compile'
  62.     }.getAllDependencies()
  63. }
  64.  
  65. def dumpCfgs( Project project, show = [] ) {
  66.     println "  Project:" + project.name
  67.     def cfgs = project.configurations
  68.  
  69.     if ( !show.isEmpty() ) {
  70.         cfgs = cfgs.findAll { show.contains( it.name ) }
  71.     }
  72.  
  73.     cfgs.each { conf ->
  74.         println "    Configuration: ${conf.name}"
  75.         conf.allDependencies.each { dep ->
  76.             println "      ${dep.group}:${dep.name}:${dep.version}"
  77.         }
  78.     }
  79. }
  80.  
  81. def isAndroid( Project proj ) {
  82.     return proj.hasProperty( 'android' )
  83. }
  84.  
  85. /**
  86.  * The findCompileDependencies execute of the org.robospock:robospock-plugin does not take into account
  87.  * that the generated variants and placed in the 'generated' directory.
  88.  * Consider fixing the plugin and submitting
  89.  */
  90.     org.robospock.RobospockAction.metaClass.execute = { Project project ->
  91.         // Project under test
  92.         def put = project.project( project.ext.robospock )
  93.         if ( !isAndroid ( put ) ) {
  94.             throw new IllegalArgumentException( put.name + ' is not an android project.' )
  95.         }
  96.  
  97.         /*
  98.          * Add dependencies
  99.          */
  100.  
  101.         // collect and extract compiled classes in library project
  102.         def subProjects = getSubprojects( put ).unique()
  103.         def allProjects = subProjects + put
  104.  
  105.         // separate android & normal projects from each other.
  106.         def androidProjects = allProjects.findAll { isAndroid( it ) }
  107.         def normalProjects  = allProjects.findAll { !isAndroid( it ) }
  108.  
  109.         // collect and forward all maven dependencies
  110.         def mavenDependencies = collectMavenDependencies( allProjects ).unique()
  111.         mavenDependencies.each { dep ->
  112.             project.dependencies {
  113.                 compile group: dep.group, name: dep.name, version: dep.version
  114.             }
  115.         }
  116.  
  117.         // add normal projects to dependencies.
  118.         normalProjects.each { proj ->
  119.             project.dependencies { compile proj }
  120.         }
  121.  
  122.         dumpCfgs( project, ['compile'] )
  123.  
  124.         // add android projects to dependencies, in an odd way.
  125.         def main   = project.sourceSets.main
  126.         def ssJava = main.java.srcDirs
  127.         def ssRes  = main.resources.srcDirs
  128.  
  129.         androidProjects.each { proj ->
  130.             // Find generated dir.
  131.             def variants = proj.plugins.hasPlugin( 'android-library' )
  132.                          ? proj.android.libraryVariants
  133.                          : proj.android.applicationVariants
  134.             def genDir = variants.find { it.name == 'debug' }.dirName
  135.  
  136.             // Add resource sets.
  137.             def androidMain = proj.android.sourceSets.main
  138.             ssRes.addAll( androidMain.res.srcDirs )
  139.  
  140.             // Add source sets.
  141.             ssJava.addAll( androidMain.java.srcDirs )
  142.             def sourceDir =  proj.buildDir.path + "/generated/source/"
  143.             ["r/", "buildConfig/"].each {
  144.                 ssJava.addAll( sourceDir + it + genDir )
  145.             }
  146.         }
  147.  
  148.         ssJava.each { println it }
  149.  
  150.         project.sourceSets.main {
  151.             java.srcDirs = ssJava
  152.             resources.srcDirs = ssRes
  153.         }
  154.  
  155.         /*
  156.          * Create the task
  157.          */
  158.         Task test = task robospock(
  159.             type: Test,
  160.             group: JavaBasePlugin.VERIFICATION_GROUP,
  161.             name: org.robospock.RobospockAction.ROBOSPOCK_TASK_NAME,
  162.             description: "Runs the unit tests using Robospock.",
  163.         ) {
  164.             workingDir = "${put.projectDir}/src/main"
  165.         }
  166.         project.getTasks().getByName( JavaBasePlugin.CHECK_TASK_NAME ).dependsOn( test )
  167.     }
  168.  
  169. //Plugin must be applied last and must use qualified name when applied from a gradle script
  170. apply plugin: 'robospock'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement