<commit_before>/**
 * 
 */
package com.github.ginvavilon.android_eclipse;

import com.android.build.gradle.api.BaseVariant

import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.tasks.SourceSet
import org.gradle.internal.impldep.org.apache.ivy.tools.analyser.DependencyAnalyser
import org.gradle.plugins.ide.eclipse.model.EclipseModel
import org.gradle.plugins.ide.eclipse.model.SourceFolder

/**
 * @author vbaraznovsky
 *
 */
public class AndroidEclipseVariantConfigurator {
    static final String MANIFEST = 'AndroidManifest.xml'
    static final String RES = 'res'

    static final String SOURCES_GENERATED = 'generated'
    static final String SOURCES_LINKED = 'linked'

    static final String PREFIX_SOURCESETS = 'android_'


    static final String PROJECT_PROPERTY_COMMENTS='# This file is automatically generated by Building Tools.\n'+
    '# Do not modify this file -- YOUR CHANGES WILL BE ERASED!\n'+
    '#\n'+
    '# This file must not be checked in Version Control Systems.\n'+
    '#\n'+
    '# To customize properties used by the Ant build system edit\n'+
    '# "ant.properties", and override values to adapt the script to your\n'+
    '# project structure.\n'+
    '#\n'+
    '# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):\n'+
    '#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt\n'+
    '\n'+
    '# Project target.'

    static final String GEN = 'gen';

    public EclipseModel eclipse
    public BaseVariant variant
    public def androidPlugin
    public def project
    private def prefixSource=""





    void run() {
        updateProjectProperties()
        AndroidEclipseExtension ext=project.extensions.getByName('androidEclipse')

        def configurations = project.configurations
        def library = androidPlugin in com.android.build.gradle.LibraryExtension;

        def buildDir=project.buildDir;

        def eclipseProject = eclipse.project
        def pathVariant = variant.dirName

        def manifestFile
        if (ext.manifest == null){
            manifestFile = null
        } else if (ext.manifest == AndroidEclipseExtension.GENERATED){
            if (library){
                manifestFile=new File("$buildDir/intermediates/manifests/aapt/$pathVariant/AndroidManifest.xml");
            } else {
                manifestFile=new File("$buildDir/intermediates/manifests/full/$pathVariant/AndroidManifest.xml");
            }
        } else {
            manifestFile = project.file(ext.manifest
                    .replaceAll('%buildDir%',"$buildDir")
                    .replaceAll('%pathVariant%',"$pathVariant")
                    )
            if (manifestFile.directory){
                manifestFile= new File(manifestFile,MANIFEST)
            }

        }

        def resFile
        if (ext.resLink == null){
            resFile = null
        } else if (ext.resLink == AndroidEclipseExtension.GENERATED){
            resFile=new File("$buildDir/intermediates/res/$pathVariant");
        } else {
            resFile = project.file(ext.resLink)
        }

        def rFile=new File("$buildDir/generated/source/r/$pathVariant");
        if (manifestFile!=null){
            eclipseProject.linkedResource(name: MANIFEST, type: '1', location: manifestFile.absolutePath);
        }
        if (resFile!=null){
            eclipseProject.linkedResource(name: RES, type: '2', location: resFile.absolutePath);
        }
        eclipseProject.linkedResource(name: GEN, type: '2', location: rFile.absolutePath);



        final def eclipseClasspathSourceSets = eclipse.classpath.sourceSets
        def linkedSourceSets = getSourceSets(eclipseClasspathSourceSets,SOURCES_LINKED)
        def libs=new HashSet()
        def configLibs=new HashSet()
        def linkedSources=new HashSet()
        linkedSources += "gen";
        def projectAbsolutePath = project.file('.').absolutePath

        variant.sourceSets.each { sourceSet ->
            final def name=sourceSet.name.toString()
            def sourceSetName = PREFIX_SOURCESETS+name
            SourceSet mainSourceSet = getSourceSets(eclipseClasspathSourceSets, sourceSetName)
            sourceSet.javaDirectories.each { dir->
                boolean areRelated = dir.absolutePath.startsWith(projectAbsolutePath);
                if (areRelated){
                    mainSourceSet.getJava().srcDir(dir);
                } else {
                    if (dir.exists()){
                        def path = String.valueOf("src-$name")
                        eclipseProject.linkedResource(name: path, type: '2', location: dir.absolutePath);
                        linkedSources += path;
                    }
                }

            }


        }

        addClasspathConfiguration(variant.compileConfiguration)
        addClasspathConfiguration(variant.runtimeConfiguration)
        addClasspathConfiguration(configurations.androidEclipse)
        
        project.dependencies{
            libsFromVariant variant.compileConfiguration
            libsFromVariant variant.runtimeConfiguration
            libsFromVariant configurations.androidEclipse
        }

        eclipse.classpath.plusConfigurations+=[
            configurations.libsFromVariant
        ]
        eclipse.classpath.minusConfigurations+=[
            configurations.excludeByVariant
        ]

        def generatedSourceSets = getSourceSets(eclipseClasspathSourceSets,SOURCES_GENERATED)

        ext.generatedDirs.each { dir ->
            generatedSourceSets.getJava().srcDir(project.file("$dir/$pathVariant"));
        }

        eclipse.classpath {
            file {

                whenMerged {
                    entries.removeAll { entry -> entry.kind == 'src' && linkedSources.contains(entry.path) }
                    for (link in linkedSources) {
                        entries += new SourceFolder(link,null)
                    }
                }

            }
        }
        new VariantProperty(project, variant).store()

    }

    private addClasspathConfiguration(Configuration config) {

        project.dependencies{
            config.allDependencies.each({ dependency->
                if (dependency in ProjectDependency) {
                    def dependencyProject = dependency.dependencyProject
                    dependencyProject.afterEvaluate{
                        def plugins=dependencyProject.plugins
                        if ((dependency.targetConfiguration == null)
                        &&(plugins.hasPlugin('com.android.library'))
                        ) {
                            ProjectDependency updated= dependency.copy()
                            updated.targetConfiguration='default'
                            libsFromVariant updated
                            excludeByVariant dependency
                            return
                        }
                    }
                }
            })
        }
    }

    private SourceSet getSourceSets(def eclipseClasspathSourceSets, def sourceSetName) {
        SourceSet mainSourceSet = eclipseClasspathSourceSets.findByName(sourceSetName);
        if (mainSourceSet==null){
            mainSourceSet = eclipseClasspathSourceSets.create(prefixSource+sourceSetName);<commit_msg>Add using variant.getCompileClasspath<commit_after>/**
 * 
 */
package com.github.ginvavilon.android_eclipse;

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant

import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.tasks.SourceSet
import org.gradle.internal.impldep.org.apache.ivy.tools.analyser.DependencyAnalyser
import org.gradle.plugins.ide.eclipse.model.EclipseModel
import org.gradle.plugins.ide.eclipse.model.SourceFolder

/**
 * @author vbaraznovsky
 *
 */
public class AndroidEclipseVariantConfigurator {
    static final String MANIFEST = 'AndroidManifest.xml'
    static final String RES = 'res'

    static final String SOURCES_GENERATED = 'generated'
    static final String SOURCES_LINKED = 'linked'

    static final String PREFIX_SOURCESETS = 'android_'


    static final String PROJECT_PROPERTY_COMMENTS='# This file is automatically generated by Building Tools.\n'+
    '# Do not modify this file -- YOUR CHANGES WILL BE ERASED!\n'+
    '#\n'+
    '# This file must not be checked in Version Control Systems.\n'+
    '#\n'+
    '# To customize properties used by the Ant build system edit\n'+
    '# "ant.properties", and override values to adapt the script to your\n'+
    '# project structure.\n'+
    '#\n'+
    '# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):\n'+
    '#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt\n'+
    '\n'+
    '# Project target.'

    static final String GEN = 'gen';

    static final String JAVA_OUTPUT = "build/eclipse-classes";

    public EclipseModel eclipse
    public BaseVariant variant
    public BaseExtension androidPlugin
    public def project
    private def prefixSource=""
    private Set<File> projectLibs=new HashSet<>();





    void run() {
        updateProjectProperties()
        AndroidEclipseExtension ext=project.extensions.getByName('androidEclipse')

        def configurations = project.configurations
        def library = androidPlugin in com.android.build.gradle.LibraryExtension;

        def buildDir=project.buildDir;

        def eclipseProject = eclipse.project
        def pathVariant = variant.dirName

        def manifestFile
        if (ext.manifest == null){
            manifestFile = null
        } else if (ext.manifest == AndroidEclipseExtension.GENERATED){
            if (library){
                manifestFile=new File("$buildDir/intermediates/manifests/aapt/$pathVariant/AndroidManifest.xml");
            } else {
                manifestFile=new File("$buildDir/intermediates/manifests/full/$pathVariant/AndroidManifest.xml");
            }
        } else {
            manifestFile = project.file(ext.manifest
                    .replaceAll('%buildDir%',"$buildDir")
                    .replaceAll('%pathVariant%',"$pathVariant")
                    )
            if (manifestFile.directory){
                manifestFile= new File(manifestFile,MANIFEST)
            }

        }

        def resFile
        if (ext.resLink == null){
            resFile = null
        } else if (ext.resLink == AndroidEclipseExtension.GENERATED){
            resFile=new File("$buildDir/intermediates/res/$pathVariant");
        } else {
            resFile = project.file(ext.resLink)
        }

        def rFile=new File("$buildDir/generated/source/r/$pathVariant");
        if (manifestFile!=null){
            eclipseProject.linkedResource(name: MANIFEST, type: '1', location: manifestFile.absolutePath);
        }
        if (resFile!=null){
            eclipseProject.linkedResource(name: RES, type: '2', location: resFile.absolutePath);
        }
        eclipseProject.linkedResource(name: GEN, type: '2', location: rFile.absolutePath);



        final def eclipseClasspathSourceSets = eclipse.classpath.sourceSets
        def linkedSourceSets = getSourceSets(eclipseClasspathSourceSets,SOURCES_LINKED)
        def libs=new HashSet()
        def configLibs=new HashSet()
        def linkedSources=new HashSet()
        linkedSources += "gen";
        def projectAbsolutePath = project.file('.').absolutePath

        variant.sourceSets.each { sourceSet ->
            final def name=sourceSet.name.toString()
            def sourceSetName = PREFIX_SOURCESETS+name
            SourceSet mainSourceSet = getSourceSets(eclipseClasspathSourceSets, sourceSetName)
            sourceSet.javaDirectories.each { dir->
                boolean areRelated = dir.absolutePath.startsWith(projectAbsolutePath);
                if (areRelated){
                    mainSourceSet.getJava().srcDir(dir);
                } else {
                    if (dir.exists()){
                        def path = String.valueOf("src-$name")
                        eclipseProject.linkedResource(name: path, type: '2', location: dir.absolutePath);
                        linkedSources += path;
                    }
                }

            }


        }


        addClasspathConfiguration(variant.compileConfiguration, true)
        addClasspathConfiguration(variant.runtimeConfiguration, true)
        addClasspathConfiguration(configurations.androidEclipse)

        def classpathLibs = project.files(variant.getCompileClasspath(null))
                .filter({ File file->
                   projectLibs.find({ 
                            file.absolutePath.startsWith(it.absolutePath)
                        })==null
                })
        
        project.dependencies{
            libsFromVariant classpathLibs
        }

        eclipse.classpath.plusConfigurations+=[
            configurations.libsFromVariant
        ]
        eclipse.classpath.minusConfigurations+=[
            configurations.excludeByVariant
        ]

        def generatedSourceSets = getSourceSets(eclipseClasspathSourceSets,SOURCES_GENERATED)

        ext.generatedDirs.each { dir ->
            generatedSourceSets.getJava().srcDir(project.file("$dir/$pathVariant"));
        }

        eclipse.classpath {
            file {

                whenMerged {
                    entries.removeAll { entry -> entry.kind == 'src' && linkedSources.contains(entry.path) }
                    for (link in linkedSources) {
                        entries += new SourceFolder(link,null)
                    }

                    entries.each { source ->
                        if (source.kind == 'src' && source.hasProperty('output')) {
                            source.output = JAVA_OUTPUT
                        }
                    }
                }

            }
        }
        new VariantProperty(project, variant).store()

    }

    private addClasspathConfiguration(Configuration config, boolean onlyProjects=false) {

        project.dependencies{
            config.allDependencies.each({ dependency->
                if (dependency in ProjectDependency) {
                    Project dependencyProject = dependency.dependencyProject
                    //dependencyProject.afterEvaluate{
                        def plugins=dependencyProject.plugins
                        if ((dependency.targetConfiguration == null)
                        //&&(plugins.hasPlugin('com.android.library'))
                        ) {
                            ProjectDependency updated= dependency.copy()
                            updated.targetConfiguration='default'
                            libsFromVariant updated
                            //  excludeByVariant dependency
                        } else {
                            libsFromVariant dependency
                        }

                        
                    //}
                    projectLibs += dependencyProject.buildDir
                }else {
                    if (!onlyProjects) {
                        libsFromVariant dependency
                    }
                }
            })
        }
    }

    private SourceSet getSourceSets(def eclipseClasspathSourceSets, def sourceSetName) {
        SourceSet mainSourceSet = eclipseClasspathSourceSets.findByName(sourceSetName);
        if (mainSourceSet==null){
            mainSourceSet = eclipseClasspathSourceSets.create(prefixSource+sourceSetName);
{'fraction_non_alphanumeric': 0.08952939676061727, 'fraction_numerical': 0.0005101390128810101, 'mean_word_length': 2.0476097940147686, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3962356', 'n_tokens_mistral': 4005, 'n_tokens_neox': 3753, 'n_words': 776}