Home | History | Annotate | Download | only in ndkSampleGen
      1 
      2 
      3 
      4 
      5 // BEGIN_EXCLUDE
      6 import com.example.android.samples.build.SampleGenPlugin
      7 apply plugin: SampleGenPlugin
      8 
      9 samplegen {
     10   pathToBuild "../../../../build"
     11   pathToSamplesCommon "../../common"
     12 }
     13 
     14 task emitBrowseableAll {
     15     println "Executing all tasks"
     16 }
     17 
     18 task emitGradleAll {
     19     println "Executing all tasks"
     20 }
     21 
     22 task emitGradleZipAll {
     23     println "Executing all tasks"
     24 }
     25 
     26 def tasksBrowseable = []
     27 def tasksGradle = []
     28 def tasksGradleZip = []
     29 def samplesXml = (new XmlParser()).parse('samples.xml')
     30 samplesXml.sample.each { sample ->
     31   println "Creating task : " +  sample.@name
     32 
     33   //
     34   //Browseable task
     35   //
     36   task "task${sample.@name}Browseable" (type:Copy) {
     37     def outputPath =outPath("browseable", sample.@name);
     38     //Get absolute path of input path
     39     FileTree inputFileTree = project.fileTree(sample.@path)
     40     def inputPath = inputFileTree.getFiles().iterator().next().parentFile.absolutePath //sample.@path
     41 
     42     println  "Input path : " + inputPath
     43 
     44     mkdir outputPath
     45     into outputPath
     46 
     47     from("${inputPath}/AndroidManifest.xml")
     48     from("${inputPath}/lint.xml")
     49     from("${inputPath}/project.properties")
     50     into("res") {
     51       from("${inputPath}/res")
     52     }
     53     into("assets") {
     54       from("${inputPath}/assets")
     55     }
     56 
     57     //Collect C++ files
     58     into("jni") {
     59       from("${inputPath}/jni")
     60     }
     61 
     62     //Collect java files
     63     def javaDirs = ["${inputPath}/src"]
     64     FileTree javaTree = null;
     65     javaDirs.each { dir ->
     66       FileTree tree = project.fileTree("${dir}")
     67       javaTree = (javaTree == null) ? tree : javaTree.plus(tree)
     68     }
     69 
     70     println javaTree;
     71     println javaDirs
     72     Map collapsedPaths = collapsePaths(javaTree, javaDirs)
     73     println collapsedPaths
     74 
     75     javaDirs.each { srcPath ->
     76       println srcPath
     77       into("src") {
     78         def javaPath = "${srcPath}"
     79         from(javaPath)
     80         setIncludeEmptyDirs(false)
     81         include(["**/*.java", "**/*.xml"])
     82         eachFile { FileCopyDetails fcd ->
     83           if (fcd.file.isFile()) {
     84             def filename = fcd.name;
     85             String collapsed = collapsedPaths.get(fcd.file.path);
     86             fcd.path = "src/${collapsed}";
     87           } else {fcd.exclude()}
     88         }
     89         println "***************** done"
     90       }
     91     }
     92   }
     93 
     94   //
     95   //Zipfile task
     96   //
     97   task "task${sample.@name}Gradle"(type:Copy) {
     98     //dependsOn(preflight)
     99 
    100     def outputPath =outPath("gradle", sample.@name);
    101     def inputPath = sample.@path
    102     // Copy entire sample into output -- since it's already in Gradle format, we'll explicitly exclude content that
    103     // doesn't belong here.
    104     mkdir outputPath
    105     into outputPath
    106     from("${inputPath}") {
    107         // Paths to exclude from output
    108         exclude ".classpath"
    109         exclude ".cproject"
    110         exclude ".project"
    111         exclude ".settings"
    112         exclude "bin"
    113         exclude "gen"
    114         exclude "obj"
    115         exclude "libs"
    116         exclude "**/proguard-project.txt"
    117         exclude "${samplegen.targetSampleModule()}/**/README*.txt"
    118     }
    119 
    120     // Remove BEGIN_EXCLUDE/END_EXCLUDE blocks from source files
    121     eachFile { file ->
    122         if (file.name.endsWith(".gradle") || file.name.endsWith(".java")) {
    123             // TODO(trevorjohns): Outputs a blank newline for each filtered line. Replace with java.io.FilterReader impl.
    124             boolean outputLines = true;
    125             def removeExcludeBlocksFilter = { line ->
    126                 if (line ==~ /\/\/ BEGIN_EXCLUDE/) {
    127                     outputLines = false;
    128                 } else if (line ==~ /\/\/ END_EXCLUDE/) {
    129                     outputLines = true;
    130                 } else if (outputLines) {
    131                     return line;
    132                 }
    133                 return ""
    134             }
    135             filter(removeExcludeBlocksFilter)
    136         }
    137     }
    138   }
    139 
    140   task "task${sample.@name}GradleZip"(type:Zip) {
    141     def outputPath =outPath("browseable", "");
    142     def folderName = "${sample.@name}"
    143     archiveName = "${sample.@name}.zip"
    144     def inputPath = outPath("gradle", sample.@name)
    145     from inputPath
    146     into folderName
    147     include "**"
    148     def outDir = project.file(outputPath)
    149     destinationDir = outDir
    150   }
    151 
    152   //Add tasks to list
    153   tasksBrowseable.add("task${sample.@name}Browseable")
    154   tasksGradle.add("task${sample.@name}Gradle")
    155   tasksGradleZip.add("task${sample.@name}GradleZip")
    156 }
    157 
    158 //Add generated tasks to dependency list
    159 emitBrowseableAll.dependsOn(tasksBrowseable)
    160 emitGradleAll.dependsOn(tasksGradle)
    161 emitGradleZipAll.dependsOn([tasksGradle, tasksGradleZip])
    162 
    163 String outPath(String buildType, String sampleName) {
    164 /*
    165     def repoInfo = "repo info platform/developers/build".execute().text
    166     def buildPath = (repoInfo =~ /Mount path: (.*)/)[0][1]
    167 */
    168     return "${samplegen.pathToBuild}/out/${buildType}/${sampleName}";
    169 }
    170 
    171 /**
    172  * Collapse a path "IntelliJ-style" by putting dots rather than slashes between
    173  * path components that have only one child. So the two paths
    174  *
    175  * com/example/android/foo/bar.java
    176  * com/example/android/bar/foo.java
    177  *
    178  * Become
    179  * com.example.android/foo/bar.java
    180  * com.example.android/bar/foo.java
    181  *
    182  * @param path
    183  * @param roots
    184  * @return
    185  */
    186 Map<String,String> collapsePaths(FileTree path, List<String> roots) {
    187     Map result = new HashMap<String,String>();
    188 
    189     println ("******************** Collapse *************************")
    190 
    191     path.visit { FileVisitDetails f ->
    192         if (f.isDirectory()) return;
    193         StringBuilder collapsedPath = new StringBuilder("${f.name}");
    194         File current = f.file;
    195 
    196         //
    197         // Starting at this file, walk back to the root of the path and
    198         // substitute dots for any directory that has only one child.
    199         //
    200 
    201         // Don't substitute a dot for the separator between the end of the
    202         // path and the filename, even if there's only one file in the directory.
    203         if (!f.isDirectory()) {
    204             current = current.parentFile;
    205             collapsedPath.insert(0, "${current.name}/")
    206         }
    207 
    208         // For everything else, use a dot if there's only one child and
    209         // a slash otherwise. Filter out the root paths, too--we only want
    210         // the relative path. But wait, Groovy/Gradle is capricious and
    211         // won't return the proper value from a call to roots.contains(String)!
    212         // I'm using roots.sum here instead of tracking down why a list of
    213         // strings can't return true from contains() when given a string that
    214         // it quite obviously does contain.
    215         current = current.parentFile;
    216         while((current != null)
    217                 && (roots.sum {String r-> return r.equals(current.absolutePath) ? 1 : 0 } == 0)) {
    218 
    219           //Exclude files starting with "."
    220           def files = 0
    221           current.list().each { file ->
    222             if (!file.startsWith("."))
    223               files++
    224           }
    225           char separator = files > 1 ? '/' : '.';
    226           collapsedPath.insert(0, "${current.name}${separator}");
    227           current = current.parentFile;
    228         }
    229         result.put(f.file.path, collapsedPath.toString());
    230     }
    231 
    232     println ("******************** Results *************************")
    233 
    234     result.each {entry -> println("${entry}\n\n");}
    235     return result
    236 }
    237 
    238 
    239 //apply from: "ndkbuild.gradle"
    240 // END_EXCLUDE
    241 
    242