<commit_before>package de.gesellix.gradle.docker.tasks

import de.gesellix.docker.client.BuildContextBuilder
import org.gradle.api.Task
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class DockerBuildTask extends AbstractDockerTask {

  private static Logger logger = LoggerFactory.getLogger(DockerBuildTask)

  @Input
  @Optional
  def imageName
  @Input
  @Optional
  def buildContext
  @InputDirectory
  @Optional
  File buildContextDirectory

  def tarOfBuildcontextTask
  def targetFile

  def imageId

  DockerBuildTask() {
    description = "Build an image from a Dockerfile"
    group = "Docker"

//    addValidator(new TaskValidator() {
//      @Override
//      void validate(TaskInternal task, Collection<String> messages) {
//        if (getBuildContextDirectory() && getBuildContext()) {
//          messages.add("Please provide only one of buildContext and buildContextDirectory")
//        }
//        if (!getBuildContextDirectory() && !getBuildContext()) {
//          messages.add("Please provide either buildContext or buildContextDirectory")
//        }
//      }
//    })
  }

  @Override
  Task configure(Closure closure) {
    def configureResult = super.configure(closure)
    if (getBuildContextDirectory()) {
      configureTarBuildContextTask()
      configureResult.getDependsOn().each { parentTaskDependency ->
        if (tarOfBuildcontextTask != parentTaskDependency) {
          tarOfBuildcontextTask.mustRunAfter parentTaskDependency
        }
      }
    }
    return configureResult
  }

  private def configureTarBuildContextTask() {
    if (tarOfBuildcontextTask == null) {
      targetFile = new File(getTemporaryDir(), "buildContext_${getNormalizedImageName()}.tar.gz")
      tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}").doLast {
        BuildContextBuilder.archiveTarFilesRecursively(getBuildContextDirectory(), targetFile)
      }
      tarOfBuildcontextTask.outputs.file(targetFile.absolutePath)
      tarOfBuildcontextTask.outputs.upToDateWhen { false }
      dependsOn tarOfBuildcontextTask
    }<commit_msg>create the target folder for the build context archive before using it<commit_after>package de.gesellix.gradle.docker.tasks

import de.gesellix.docker.client.BuildContextBuilder
import org.gradle.api.Task
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class DockerBuildTask extends AbstractDockerTask {

  private static Logger logger = LoggerFactory.getLogger(DockerBuildTask)

  @Input
  @Optional
  def imageName
  @Input
  @Optional
  def buildContext
  @InputDirectory
  @Optional
  File buildContextDirectory

  def tarOfBuildcontextTask
  def targetFile

  def imageId

  DockerBuildTask() {
    description = "Build an image from a Dockerfile"
    group = "Docker"

//    addValidator(new TaskValidator() {
//      @Override
//      void validate(TaskInternal task, Collection<String> messages) {
//        if (getBuildContextDirectory() && getBuildContext()) {
//          messages.add("Please provide only one of buildContext and buildContextDirectory")
//        }
//        if (!getBuildContextDirectory() && !getBuildContext()) {
//          messages.add("Please provide either buildContext or buildContextDirectory")
//        }
//      }
//    })
  }

  @Override
  Task configure(Closure closure) {
    def configureResult = super.configure(closure)
    if (getBuildContextDirectory()) {
      configureTarBuildContextTask()
      configureResult.getDependsOn().each { parentTaskDependency ->
        if (tarOfBuildcontextTask != parentTaskDependency) {
          tarOfBuildcontextTask.mustRunAfter parentTaskDependency
        }
      }
    }
    return configureResult
  }

  private def configureTarBuildContextTask() {
    if (tarOfBuildcontextTask == null) {
      targetFile = new File(getTemporaryDir(), "buildContext_${getNormalizedImageName()}.tar.gz")
      tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}").doLast {
        (targetFile as File).parentFile.mkdirs()
        BuildContextBuilder.archiveTarFilesRecursively(getBuildContextDirectory(), targetFile)
      }
      tarOfBuildcontextTask.outputs.file(targetFile.absolutePath)
      tarOfBuildcontextTask.outputs.upToDateWhen { false }
      dependsOn tarOfBuildcontextTask
    }
{'fraction_non_alphanumeric': 0.09388458225667529, 'fraction_numerical': 0.0008613264427217916, 'mean_word_length': 3.7446373850868233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '680128', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1264, 'n_words': 260}