<commit_before>package org.gradlefx

/*
 * Copyright (c) 2011 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact
import org.gradle.api.tasks.Delete
import org.gradlefx.FlexType
import org.gradlefx.conventions.GradleFxConvention
import org.gradlefx.tasks.Compc
import org.gradlefx.tasks.CopyResources
import org.gradlefx.tasks.Mxmlc
import org.gradlefx.tasks.Publish
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.gradle.api.artifacts.dsl.ArtifactHandler
import org.gradle.api.artifacts.Configuration

class GradleFxPlugin implements Plugin<Project> {

	public static final String COMPILE_TASK_NAME = 'compile'
    public static final String BUILD_TASK_NAME = 'build'
    public static final String PUBLISH_TASK_NAME = 'publish'
    public static final String COPY_RESOURCES_TASK_NAME = 'copyresources'
	public static final String CLEAN_TASK_NAME = 'clean'

    // configurations
    public static final String INTERNAL_CONFIGURATION_NAME = 'internal'
    public static final String EXTERNAL_CONFIGURATION_NAME = 'external'
    public static final String MERGE_CONFIGURATION_NAME = 'merged'
    public static final String RSL_CONFIGURATION_NAME = 'rsl'
    public static final String TEST_CONFIGURATION_NAME = 'test'

	Logger log = LoggerFactory.getLogger('flex')

    private Project project
	
	public void apply(Project project) {
        this.project = project

		GradleFxConvention pluginConvention = new GradleFxConvention()
		project.convention.plugins.flex = pluginConvention

        addDefaultConfigurations()

		addBuild()
        addCopyResources()
		addClean()
        addPublish()

        //do these tasks in the afterEvaluate phase because they need property access
        project.afterEvaluate {
            addCompile(pluginConvention)
            addDependsOnOtherProjects()
            addDefaultArtifact()
        }
	}

    private void addDefaultConfigurations() {
        project.configurations.add(INTERNAL_CONFIGURATION_NAME)
        project.configurations.add(EXTERNAL_CONFIGURATION_NAME)
        project.configurations.add(MERGE_CONFIGURATION_NAME)
        project.configurations.add(RSL_CONFIGURATION_NAME)
        project.configurations.add(TEST_CONFIGURATION_NAME)
    }
	
	private void addBuild() {
		DefaultTask buildTask = project.tasks.add(BUILD_TASK_NAME, DefaultTask)
		buildTask.setDescription("Assembles and tests this project.")
        buildTask.dependsOn(COMPILE_TASK_NAME)
	}
	
	private void addCompile(GradleFxConvention pluginConvention) {
        def compile = null

        if(project.type == FlexType.swc) {
            log.info "Adding ${COMPILE_TASK_NAME} task using compc to project ${project.name}"
            compile = project.tasks.add(COMPILE_TASK_NAME, Compc)
            compile.outputs.dir project.buildDir
            pluginConvention.output = "${project.name}.swc"
        }
        else if(project.type == FlexType.swf) {
            log.info "Adding ${COMPILE_TASK_NAME} task using mxmlc to project ${project.name}"
            compile = project.tasks.add(COMPILE_TASK_NAME, Mxmlc)
            compile.outputs.dir project.buildDir
            pluginConvention.output = "${project.name}.swf"
        }
        else {
            log.warn "Adding ${COMPILE_TASK_NAME} task using default implementation"
            compile = project.tasks.add(COMPILE_TASK_NAME, DefaultTask)
            compile.description = "Oops - we couldn't figure out if ${project.name} is a Flex component or a Flex application/module project."
        }

        compile.dependsOn(COPY_RESOURCES_TASK_NAME)
	}

    private void addCopyResources() {
        project.tasks.add(COPY_RESOURCES_TASK_NAME, CopyResources)
    }
	
	private void addClean() {
		Delete clean = project.tasks.add(CLEAN_TASK_NAME, Delete)
		clean.description = "Deletes the build directory."
		clean.delete { project.buildDir }
	}

    private void addPublish() {
        project.tasks.add(PUBLISH_TASK_NAME, Publish)
    }
	
	private void addDependsOnOtherProjects() {
        // dependencies need to be added as a closure as we don't have the information at the moment to wire them up
        project.tasks.compile.dependsOn {
            Set dependentTasks = new HashSet()
            project.configurations.each { Configuration configuration ->
                Set deps = project.configurations."${configuration.name}".getDependencies(ProjectDependency)
                deps.each { projectDependency ->
                    //def projectDependency = (ProjectDependency) dependency
                    println "path to dependency: ${projectDependency.dependencyProject.path}"
                    dependentTasks.add(projectDependency.dependencyProject.path + ':compile')
                }
            }
            dependentTasks
        }
	}

    /**
     * If this is an implementation project (compiles a swc of swf), it adds an artifact
     * of the given project to every configuration.
     * @param project
     */
	private void addDefaultArtifact() {
        if(isImplementationProject()) {
            addProjectArtifactToConfigurations()
        }
	}

    /**
     * This project is an implementation project when it compiles to a swc of swf file.
     * @return
     */
    private Boolean isImplementationProject() {
        return project.type != null;
    }

    /**
     * Adds an artifact to every configuration.
     * @param project
     */
    private void addProjectArtifactToConfigurations() {
        project.artifacts { ArtifactHandler artifactHandler ->
            project.configurations.each { Configuration configuration ->
                artifactHandler."${configuration.name}" new DefaultPublishArtifact(project.name, project.type.toString(), project.type.toString(), null, new Date(), new File(project.output))
            }
        }
    }<commit_msg>- fixed the location of the project's artifact - the project's artifact is now added to the 'default' configuration instead of to every configuration<commit_after>package org.gradlefx

/*
 * Copyright (c) 2011 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project

import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact
import org.gradle.api.tasks.Delete

import org.gradlefx.conventions.GradleFxConvention
import org.gradlefx.tasks.Compc
import org.gradlefx.tasks.CopyResources
import org.gradlefx.tasks.Mxmlc
import org.gradlefx.tasks.Publish
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.gradle.api.artifacts.dsl.ArtifactHandler
import org.gradle.api.artifacts.Configuration

class GradleFxPlugin implements Plugin<Project> {

	public static final String COMPILE_TASK_NAME = 'compile'
    public static final String BUILD_TASK_NAME = 'build'
    public static final String PUBLISH_TASK_NAME = 'publish'
    public static final String COPY_RESOURCES_TASK_NAME = 'copyresources'
	public static final String CLEAN_TASK_NAME = 'clean'

    // configurations
    public static final String DEFAULT_CONFIGURATION_NAME = 'default'
    public static final String INTERNAL_CONFIGURATION_NAME = 'internal'
    public static final String EXTERNAL_CONFIGURATION_NAME = 'external'
    public static final String MERGE_CONFIGURATION_NAME = 'merged'
    public static final String RSL_CONFIGURATION_NAME = 'rsl'
    public static final String TEST_CONFIGURATION_NAME = 'test'

	Logger log = LoggerFactory.getLogger('flex')

    private Project project
	
	public void apply(Project project) {
        this.project = project

		GradleFxConvention pluginConvention = new GradleFxConvention()
		project.convention.plugins.flex = pluginConvention

        addDefaultConfigurations()

		addBuild()
        addCopyResources()
		addClean()
        addPublish()

        //do these tasks in the afterEvaluate phase because they need property access
        project.afterEvaluate {
            addCompile(pluginConvention)
            addDependsOnOtherProjects()
            addDefaultArtifact()
        }
	}

    private void addDefaultConfigurations() {
        project.configurations.add(DEFAULT_CONFIGURATION_NAME)
        project.configurations.add(INTERNAL_CONFIGURATION_NAME)
        project.configurations.add(EXTERNAL_CONFIGURATION_NAME)
        project.configurations.add(MERGE_CONFIGURATION_NAME)
        project.configurations.add(RSL_CONFIGURATION_NAME)
        project.configurations.add(TEST_CONFIGURATION_NAME)
    }
	
	private void addBuild() {
		DefaultTask buildTask = project.tasks.add(BUILD_TASK_NAME, DefaultTask)
		buildTask.setDescription("Assembles and tests this project.")
        buildTask.dependsOn(COMPILE_TASK_NAME)
	}
	
	private void addCompile(GradleFxConvention pluginConvention) {
        def compile = null

        if(project.type == FlexType.swc) {
            log.info "Adding ${COMPILE_TASK_NAME} task using compc to project ${project.name}"
            compile = project.tasks.add(COMPILE_TASK_NAME, Compc)
            compile.outputs.dir project.buildDir
            pluginConvention.output = "${project.name}.swc"
        }
        else if(project.type == FlexType.swf) {
            log.info "Adding ${COMPILE_TASK_NAME} task using mxmlc to project ${project.name}"
            compile = project.tasks.add(COMPILE_TASK_NAME, Mxmlc)
            compile.outputs.dir project.buildDir
            pluginConvention.output = "${project.name}.swf"
        }
        else {
            log.warn "Adding ${COMPILE_TASK_NAME} task using default implementation"
            compile = project.tasks.add(COMPILE_TASK_NAME, DefaultTask)
            compile.description = "Oops - we couldn't figure out if ${project.name} is a Flex component or a Flex application/module project."
        }

        compile.dependsOn(COPY_RESOURCES_TASK_NAME)
	}

    private void addCopyResources() {
        project.tasks.add(COPY_RESOURCES_TASK_NAME, CopyResources)
    }
	
	private void addClean() {
		Delete clean = project.tasks.add(CLEAN_TASK_NAME, Delete)
		clean.description = "Deletes the build directory."
		clean.delete { project.buildDir }
	}

    private void addPublish() {
        project.tasks.add(PUBLISH_TASK_NAME, Publish)
    }
	
	private void addDependsOnOtherProjects() {
        // dependencies need to be added as a closure as we don't have the information at the moment to wire them up
        project.tasks.compile.dependsOn {
            Set dependentTasks = new HashSet()
            project.configurations.each { Configuration configuration ->
                Set deps = project.configurations."${configuration.name}".getDependencies(ProjectDependency)
                deps.each { projectDependency ->
                    //def projectDependency = (ProjectDependency) dependency
                    println "path to dependency: ${projectDependency.dependencyProject.path}"
                    dependentTasks.add(projectDependency.dependencyProject.path + ':compile')
                }
            }
            dependentTasks
        }
	}

    /**
     * If this is an implementation project (compiles a swc of swf), it adds an artifact
     * of the given project to the default configuration.
     * @param project
     */
	private void addDefaultArtifact() {
        if(isImplementationProject()) {
            addProjectArtifactToDefaultConfiguration()
        }
	}

    /**
     * This project is an implementation project when it compiles to a swc of swf file.
     * @return
     */
    private Boolean isImplementationProject() {
        return project.type != null;
    }

    /**
     * Adds an artifact to the default configuration.
     * @param project
     */
    private void addProjectArtifactToDefaultConfiguration() {
        project.artifacts { ArtifactHandler artifactHandler ->
            artifactHandler."${DEFAULT_CONFIGURATION_NAME}" new DefaultPublishArtifact(project.name, project.type.toString(), project.type.toString(), null, new Date(), new File(project.buildDir.path + "/" + project.output))
        }
    }
{'fraction_non_alphanumeric': 0.07805023923444976, 'fraction_numerical': 0.0014952153110047847, 'mean_word_length': 3.1363636363636362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7583743', 'n_tokens_mistral': 3735, 'n_tokens_neox': 3538, 'n_words': 967}