<commit_before>/*
 * BndPlugin for Gradle.
 *
 * The bnd classes must be available on the classpath for this script.
 *
 * If the bndWorkspace property is set, it will be used for the bnd Workspace.
 *
 * If the bnd_defaultTask property is set, it will be used for the the default
 * task.
 *
 * If the bnd_preCompileRefresh property is set to 'true', the project
 * properties will be refreshed just before compiling the project.
 */

package aQute.bnd.gradle

import aQute.bnd.build.Workspace
import aQute.bnd.osgi.Constants
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.LogLevel

public class BndPlugin implements Plugin<Project> {
  private Project project
  private bndProject
  private boolean preCompileRefresh

  void apply(Project p) {
    p.configure(p) { project ->
      this.project = project
      if (!rootProject.hasProperty('bndWorkspace')) {
        rootProject.ext.bndWorkspace = Workspace.getWorkspace(rootDir)
        if (bndWorkspace == null) {
          throw new GradleException("Unable to load bnd workspace ${rootDir}")
        }
      }
      if (!rootProject.hasProperty('bndWorkspaceInitialized')) {
        bndWorkspace.driver = Constants.BNDDRIVER_GRADLE
        bndWorkspace.addGestalt(Constants.GESTALT_BATCH, null)
        if (rootProject.hasProperty('bnd_gestalt')) {
          rootProject.bnd_gestalt.trim().split(/\s*,\s*/).each {
            bndWorkspace.addGestalt(it, null)
          }
        }
        rootProject.ext.bndWorkspaceInitialized = true
      }
      this.bndProject = bndWorkspace.getProject(name)
      if (bndProject == null) {
        throw new GradleException("Unable to load bnd project ${name} from workspace ${rootDir}")
      }
      bndProject.prepare();
      if (!bndProject.isValid()) {
        checkErrors()
        throw new GradleException("Project ${bndProject.name} is not a valid bnd project")
      }
      this.preCompileRefresh = project.hasProperty('bnd_preCompileRefresh') ? parseBoolean(bnd_preCompileRefresh) : false
      extensions.create('bnd', BndProperties, bndProject)
      bnd.ext.project = bndProject
      convention.plugins.bnd = new BndPluginConvention(this)

      buildDir = relativePath(bndProject.targetDir)
      plugins.apply 'java'
      libsDirName = '.'

      if (project.hasProperty('bnd_defaultTask')) {
        defaultTasks = bnd_defaultTask.trim().split(/\s*,\s*/)
      }

      /* Set up configurations */
      configurations {
        dependson.description = 'bnd project dependencies.'
        runtime.artifacts.clear()
        archives.artifacts.clear()
      }
      /* Set up deliverables */
      bndProject.deliverables.each { deliverable ->
        artifacts {
          runtime deliverable.file
          archives deliverable.file
        }
      }
      /* Set up dependencies */
      dependencies {
        compile compilePath()
        runtime runtimePath()
        testCompile testCompilePath()
        testRuntime testRuntimePath()
      }
      /* Set up source sets */
      sourceSets {
        /* bnd uses the same directory for java and resources. */
        main {
          java.srcDirs = resources.srcDirs = files(relativePath(bndProject.src))
          output.classesDir = output.resourcesDir = relativePath(bndProject.srcOutput)
        }
        test {
          java.srcDirs = resources.srcDirs = files(relativePath(bndProject.testSrc))
          output.classesDir = output.resourcesDir = relativePath(bndProject.testOutput)
        }
      }
      bnd.ext.allSrcDirs = files(bndProject.allsourcepath)
      /* Set up compile tasks */
      sourceCompatibility = bnd('javac.source', sourceCompatibility)
      def javacTarget = bnd('javac.target', targetCompatibility)
      def bootclasspath = files(bndProject.bootclasspath*.file)
      if (javacTarget == 'jsr14') {
        javacTarget = '1.5'
        bootclasspath = files(bndProject.getBundle('ee.j2se', '1.5', null, ['strategy':'lowest']).file)
      }
      targetCompatibility = javacTarget
      def javac = bnd('javac')
      def javacProfile = bnd('javac.profile', '')
      def javacDebug = parseBoolean(bnd('javac.debug'))
      def javacDeprecation = parseBoolean(bnd('javac.deprecation', 'true'))
      def javacEncoding = bnd('javac.encoding', 'UTF-8')
      def compileOptions = {
        if (javacDebug) {
          options.debugOptions.debugLevel = 'source,lines,vars'
        }
        options.verbose = logger.isEnabled(LogLevel.DEBUG)
        options.listFiles = logger.isEnabled(LogLevel.INFO)
        options.deprecation = javacDeprecation
        options.encoding = javacEncoding
        if (javac != 'javac') {
          options.fork = true
          options.forkOptions.executable = javac
        }
        if (!bootclasspath.empty) {
          options.fork = true
          options.bootClasspath = bootclasspath.asPath
        }
        if (!javacProfile.empty) {
          options.compilerArgs += ['-profile', javacProfile]
        }
      }

      compileJava {
        configure compileOptions
        if (logger.isEnabled(LogLevel.INFO)) {
          doFirst {
            logger.info "Compile ${sourceSets.main.java.srcDirs} to ${destinationDir}"
            if (javacProfile.empty) {
              logger.info "-source ${sourceCompatibility} -target ${targetCompatibility}"
            } else {
              logger.info "-source ${sourceCompatibility} -target ${targetCompatibility} -profile ${javacProfile}"
            }
            logger.info "-classpath ${classpath.asPath}"
            if (options.bootClasspath != null) {
              logger.info "-bootclasspath ${options.bootClasspath}"
            }
          }
        }
        if (preCompileRefresh) {
          doFirst {
            logger.info 'Refreshing the bnd Project before compilation.'
            bndProject.refresh()
            bndProject.propertiesChanged()
            bndProject.clear()
            bndProject.prepare()
            classpath = compilePath()
          }
        }
      }

      compileTestJava {
        configure compileOptions
        if (preCompileRefresh) {
          doFirst {
            logger.info 'Refreshing the bnd Project before compilation.'
            bndProject.refresh()
            bndProject.propertiesChanged()
            bndProject.clear()
            bndProject.prepare()
            classpath = files(testCompilePath(), runtimePath(), compilePath())
          }
        }
      }

      processResources {
        def srcDirs = sourceSets.main.resources.srcDirs
        def outputDir = sourceSets.main.output.resourcesDir
        inputs.files.each {
          def input = it.absolutePath
          srcDirs.each {
            input -= it
          }
          outputs.file new File(outputDir, input)
        }
      }

      processTestResources {
        def srcDirs = sourceSets.test.resources.srcDirs
        def outputDir = sourceSets.test.output.resourcesDir
        inputs.files.each {
          def input = it.absolutePath
          srcDirs.each {
            input -= it
          }
          outputs.file new File(outputDir, input)
        }
      }

      jar {
        description 'Assemble the project bundles.'
        deleteAllActions() /* Replace the standard task actions */
        enabled !bndProject.noBundles
        if (enabled) {
          /* bnd can include any class on the buildpath */
          inputs.files compilePath().collect {
            it.file ? it : fileTree(it)
          }
          /* all other files in the project like bnd and resources */
          inputs.files fileTree(projectDir) {
            exclude sourceSets.main.java.srcDirs.collect { relativePath(it) }
            exclude sourceSets.test.java.srcDirs.collect { relativePath(it) }
            exclude sourceSets.test.output.files.collect { relativePath(it) }
            exclude relativePath(buildDir)
          }
          outputs.files configurations.archives.artifacts.files, new File(buildDir, Constants.BUILDFILES)
          doLast {
            def built
            try {
              built = bndProject.build()
            } catch (Exception e) {
              throw new GradleException("Project ${bndProject.name} failed to build", e)
            }
            checkErrors()
            if (built != null) {
              logger.info 'Generated bundles:'
              built.each {
                logger.info "${it}"
              }
            }
          }
        }
      }

      assemble {
        dependsOn jar
      }

      task('release') {
        description 'Release the project to the release repository.'
        dependsOn assemble
        group 'release'
        enabled !bndProject.noBundles && !bnd(Constants.RELEASEREPO, 'unset').empty
        if (enabled) {
          inputs.files configurations.archives.artifacts.files
          doLast {
            try {
              bndProject.release()
            } catch (Exception e) {
              throw new GradleException("Project ${bndProject.name} failed to release", e)
            }
            checkErrors()
          }
        }
      }

      task('releaseNeeded') {
        description 'Release the project and all projects it depends on.'
        dependsOn release
        group 'release'
      }

      test {
        enabled !parseBoolean(bnd(Constants.NOJUNIT, 'false')) && !parseBoolean(bnd('no.junit', 'false'))
      }<commit_msg>gradle plugin: Use builtBy to connect artifact to task<commit_after>/*
 * BndPlugin for Gradle.
 *
 * The bnd classes must be available on the classpath for this script.
 *
 * If the bndWorkspace property is set, it will be used for the bnd Workspace.
 *
 * If the bnd_defaultTask property is set, it will be used for the the default
 * task.
 *
 * If the bnd_preCompileRefresh property is set to 'true', the project
 * properties will be refreshed just before compiling the project.
 */

package aQute.bnd.gradle

import aQute.bnd.build.Workspace
import aQute.bnd.osgi.Constants
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.LogLevel

public class BndPlugin implements Plugin<Project> {
  private Project project
  private bndProject
  private boolean preCompileRefresh

  void apply(Project p) {
    p.configure(p) { project ->
      this.project = project
      if (!rootProject.hasProperty('bndWorkspace')) {
        rootProject.ext.bndWorkspace = Workspace.getWorkspace(rootDir)
        if (bndWorkspace == null) {
          throw new GradleException("Unable to load bnd workspace ${rootDir}")
        }
      }
      if (!rootProject.hasProperty('bndWorkspaceInitialized')) {
        bndWorkspace.driver = Constants.BNDDRIVER_GRADLE
        bndWorkspace.addGestalt(Constants.GESTALT_BATCH, null)
        if (rootProject.hasProperty('bnd_gestalt')) {
          rootProject.bnd_gestalt.trim().split(/\s*,\s*/).each {
            bndWorkspace.addGestalt(it, null)
          }
        }
        rootProject.ext.bndWorkspaceInitialized = true
      }
      this.bndProject = bndWorkspace.getProject(name)
      if (bndProject == null) {
        throw new GradleException("Unable to load bnd project ${name} from workspace ${rootDir}")
      }
      bndProject.prepare();
      if (!bndProject.isValid()) {
        checkErrors()
        throw new GradleException("Project ${bndProject.name} is not a valid bnd project")
      }
      this.preCompileRefresh = project.hasProperty('bnd_preCompileRefresh') ? parseBoolean(bnd_preCompileRefresh) : false
      extensions.create('bnd', BndProperties, bndProject)
      bnd.ext.project = bndProject
      convention.plugins.bnd = new BndPluginConvention(this)

      buildDir = relativePath(bndProject.targetDir)
      plugins.apply 'java'
      libsDirName = '.'

      if (project.hasProperty('bnd_defaultTask')) {
        defaultTasks = bnd_defaultTask.trim().split(/\s*,\s*/)
      }

      /* Set up configurations */
      configurations {
        dependson.description = 'bnd project dependencies.'
        runtime.artifacts.clear()
        archives.artifacts.clear()
      }
      /* Set up deliverables */
      bndProject.deliverables.each { deliverable ->
        artifacts {
          runtime(deliverable.file) {
             builtBy jar
          }
          archives(deliverable.file) {
             builtBy jar
          }
        }
      }
      /* Set up dependencies */
      dependencies {
        compile compilePath()
        runtime runtimePath()
        testCompile testCompilePath()
        testRuntime testRuntimePath()
      }
      /* Set up source sets */
      sourceSets {
        /* bnd uses the same directory for java and resources. */
        main {
          java.srcDirs = resources.srcDirs = files(relativePath(bndProject.src))
          output.classesDir = output.resourcesDir = relativePath(bndProject.srcOutput)
        }
        test {
          java.srcDirs = resources.srcDirs = files(relativePath(bndProject.testSrc))
          output.classesDir = output.resourcesDir = relativePath(bndProject.testOutput)
        }
      }
      bnd.ext.allSrcDirs = files(bndProject.allsourcepath)
      /* Set up compile tasks */
      sourceCompatibility = bnd('javac.source', sourceCompatibility)
      def javacTarget = bnd('javac.target', targetCompatibility)
      def bootclasspath = files(bndProject.bootclasspath*.file)
      if (javacTarget == 'jsr14') {
        javacTarget = '1.5'
        bootclasspath = files(bndProject.getBundle('ee.j2se', '1.5', null, ['strategy':'lowest']).file)
      }
      targetCompatibility = javacTarget
      def javac = bnd('javac')
      def javacProfile = bnd('javac.profile', '')
      def javacDebug = parseBoolean(bnd('javac.debug'))
      def javacDeprecation = parseBoolean(bnd('javac.deprecation', 'true'))
      def javacEncoding = bnd('javac.encoding', 'UTF-8')
      def compileOptions = {
        if (javacDebug) {
          options.debugOptions.debugLevel = 'source,lines,vars'
        }
        options.verbose = logger.isEnabled(LogLevel.DEBUG)
        options.listFiles = logger.isEnabled(LogLevel.INFO)
        options.deprecation = javacDeprecation
        options.encoding = javacEncoding
        if (javac != 'javac') {
          options.fork = true
          options.forkOptions.executable = javac
        }
        if (!bootclasspath.empty) {
          options.fork = true
          options.bootClasspath = bootclasspath.asPath
        }
        if (!javacProfile.empty) {
          options.compilerArgs += ['-profile', javacProfile]
        }
      }

      compileJava {
        configure compileOptions
        if (logger.isEnabled(LogLevel.INFO)) {
          doFirst {
            logger.info "Compile ${sourceSets.main.java.srcDirs} to ${destinationDir}"
            if (javacProfile.empty) {
              logger.info "-source ${sourceCompatibility} -target ${targetCompatibility}"
            } else {
              logger.info "-source ${sourceCompatibility} -target ${targetCompatibility} -profile ${javacProfile}"
            }
            logger.info "-classpath ${classpath.asPath}"
            if (options.bootClasspath != null) {
              logger.info "-bootclasspath ${options.bootClasspath}"
            }
          }
        }
        if (preCompileRefresh) {
          doFirst {
            logger.info 'Refreshing the bnd Project before compilation.'
            bndProject.refresh()
            bndProject.propertiesChanged()
            bndProject.clear()
            bndProject.prepare()
            classpath = compilePath()
          }
        }
      }

      compileTestJava {
        configure compileOptions
        if (preCompileRefresh) {
          doFirst {
            logger.info 'Refreshing the bnd Project before compilation.'
            bndProject.refresh()
            bndProject.propertiesChanged()
            bndProject.clear()
            bndProject.prepare()
            classpath = files(testCompilePath(), runtimePath(), compilePath())
          }
        }
      }

      processResources {
        def srcDirs = sourceSets.main.resources.srcDirs
        def outputDir = sourceSets.main.output.resourcesDir
        inputs.files.each {
          def input = it.absolutePath
          srcDirs.each {
            input -= it
          }
          outputs.file new File(outputDir, input)
        }
      }

      processTestResources {
        def srcDirs = sourceSets.test.resources.srcDirs
        def outputDir = sourceSets.test.output.resourcesDir
        inputs.files.each {
          def input = it.absolutePath
          srcDirs.each {
            input -= it
          }
          outputs.file new File(outputDir, input)
        }
      }

      jar {
        description 'Assemble the project bundles.'
        deleteAllActions() /* Replace the standard task actions */
        enabled !bndProject.noBundles
        if (enabled) {
          /* bnd can include any class on the buildpath */
          inputs.files compilePath().collect {
            it.file ? it : fileTree(it)
          }
          /* all other files in the project like bnd and resources */
          inputs.files fileTree(projectDir) {
            exclude sourceSets.main.java.srcDirs.collect { relativePath(it) }
            exclude sourceSets.test.java.srcDirs.collect { relativePath(it) }
            exclude sourceSets.test.output.files.collect { relativePath(it) }
            exclude relativePath(buildDir)
          }
          outputs.files configurations.archives.artifacts.files, new File(buildDir, Constants.BUILDFILES)
          doLast {
            def built
            try {
              built = bndProject.build()
            } catch (Exception e) {
              throw new GradleException("Project ${bndProject.name} failed to build", e)
            }
            checkErrors()
            if (built != null) {
              logger.info 'Generated bundles:'
              built.each {
                logger.info "${it}"
              }
            }
          }
        }
      }

      task('release') {
        description 'Release the project to the release repository.'
        dependsOn assemble
        group 'release'
        enabled !bndProject.noBundles && !bnd(Constants.RELEASEREPO, 'unset').empty
        if (enabled) {
          inputs.files configurations.archives.artifacts.files
          doLast {
            try {
              bndProject.release()
            } catch (Exception e) {
              throw new GradleException("Project ${bndProject.name} failed to release", e)
            }
            checkErrors()
          }
        }
      }

      task('releaseNeeded') {
        description 'Release the project and all projects it depends on.'
        dependsOn release
        group 'release'
      }

      test {
        enabled !parseBoolean(bnd(Constants.NOJUNIT, 'false')) && !parseBoolean(bnd('no.junit', 'false'))
      }
{'fraction_non_alphanumeric': 0.09699102032242819, 'fraction_numerical': 0.0008402037494092317, 'mean_word_length': 2.0850477887574925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '675112', 'n_tokens_mistral': 5231, 'n_tokens_neox': 5159, 'n_words': 1143}