All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ksoichiro.eclipse.aar.generator.ParentClasspathFileGenerator.groovy Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package com.github.ksoichiro.eclipse.aar.generator

import org.gradle.api.Project

class ParentClasspathFileGenerator extends MetaDataFileGenerator {
    Project project

    @Override
    String generateContent(File file) {
        String result
        def classpathFile = file

        // Use srcDirs definition for classpath entry
        def androidSrcDirs = []
        project.android?.sourceSets?.findAll { it.name in ['main', 'debug'] }?.each {
            if (it.java?.srcDirs) {
                it.java.srcDirs.each { srcDir ->
                    if (srcDir.exists()) {
                        androidSrcDirs << srcDir
                    }
                }
            }
        }
        if (0 == androidSrcDirs.size()) {
            androidSrcDirs = ['src/main/java']
        }
        if (!androidSrcDirs.contains('gen')) {
            androidSrcDirs << 'gen'
        }
        def androidSrcPaths = []
        androidSrcDirs.each {
            androidSrcPaths << (it.toString() - project.projectDir.path).replaceFirst("^[/\\\\]", '')
        }
        List srcPaths = []
        if (classpathFile.exists()) {
            // Aggregate src paths and dependencies
            def classPaths = new XmlSlurper().parseText(classpathFile.text)
            def srcPathEntries = classPaths.classpathentry?.findAll { it.@kind?.text() == 'src' }
            srcPaths = srcPathEntries.collect { [email protected]() }

            result = classpathFile.text
        } else {
            // Create minimum classpath file
            srcPaths = androidSrcPaths
            def srcPathEntries = androidSrcPaths.collect {
                """
                |\t""".stripMargin()
            }.join('')
            result = """\
                |
                |${srcPathEntries}
                |\t
                |\t
                |\t
                |\t
                |
                |""".stripMargin()
        }

        androidSrcPaths = androidSrcPaths.findAll { srcPaths.find { path -> path == it } == null }
        if (androidSrcPaths) {
            def entriesToAdd = androidSrcPaths.collect { it -> "\t" }
            def lines = result.readLines()?.findAll { it != '' }
            lines += entriesToAdd
            lines += "${System.getProperty('line.separator')}"
            result = lines.join(System.getProperty('line.separator'))
        }
        result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy