Home | History | Annotate | Download | only in build
      1 /*
      2 * Copyright 2013 The Android Open Source Project
      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 package com.example.android.samples.build
     17 
     18 import org.gradle.api.Plugin
     19 import org.gradle.api.Project
     20 import org.gradle.api.tasks.GradleBuild
     21 /**
     22  * Plugin to expose build rules for sample generation and packaging.
     23  */
     24 class SampleGenPlugin implements Plugin {
     25 
     26     /**
     27      * Creates a new sample generator task based on the supplied sources.
     28      *
     29      * @param name Name of the new task
     30      * @param sources Source tree that this task should process
     31      */
     32     void createTask(
     33             Project project,
     34             String name,
     35             SampleGenProperties props,
     36             def sources,
     37             def destination) {
     38         project.task ([type:ApplyTemplates], name,  {
     39             sources.each { tree ->
     40                 source += tree
     41             }
     42             outputDir = destination
     43             include = props.templatesInclude()
     44             filenameTransform = {s -> props.getOutputForInput(s)}
     45             parameters = props.templateParams()
     46         })
     47     }
     48 
     49 
     50     @Override
     51     void apply(project) {
     52         project.extensions.create("samplegen", SampleGenProperties)
     53         project.samplegen.project = project
     54             SampleGenProperties samplegen = project.samplegen
     55             project.task('create') {
     56                 if (project.gradle.startParameter.taskNames.contains('create')) {
     57                     samplegen.getCreationProperties()
     58                 }
     59 
     60             }
     61 
     62             project.task('refresh') {
     63                 samplegen.getRefreshProperties()
     64             }
     65 
     66         project.afterEvaluate({
     67             createTask(project,
     68                     'processTemplates',
     69                     samplegen,
     70                     samplegen.templates(),
     71                     samplegen.targetProjectPath)
     72             createTask(project,
     73                     'processCommon',
     74                     samplegen,
     75                     samplegen.common(),
     76                     samplegen.targetCommonPath())
     77 
     78 
     79             project.task([type: GradleBuild], 'bootstrap', {
     80                 buildFile = "${samplegen.targetProjectPath}/build.gradle"
     81                 dir = samplegen.targetProjectPath
     82                 tasks = ["refresh"]
     83             })
     84             project.bootstrap.dependsOn(project.processTemplates)
     85             project.bootstrap.dependsOn(project.processCommon)
     86             project.create.dependsOn(project.bootstrap)
     87 
     88             project.refresh.dependsOn(project.processTemplates)
     89             project.refresh.dependsOn(project.processCommon)
     90 
     91             // People get nervous when they see a task with no actions, so...
     92             project.create << {println "Project creation finished."}
     93             project.refresh << {println "Project refresh finished."}
     94 
     95         })
     96     }
     97 
     98 
     99 }
    100