remove obsolete code
remove obsolete code

package de.gesellix.gradle.docker.tasks

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.gradle.api.tasks.bundling.Tar
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

  DockerBuildTask() {
    description = "builds an image from the given build context"

//    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()) {
      tarOfBuildcontextTask = project.task(["type": Tar], "tarOfBuildcontext") {
        description = "tar of buildcontext"
        from getBuildContextDirectory()
        baseName = "buildContext_${getNormalizedImageName()}"
        destinationDir getTemporaryDir()
      }
      dependsOn tarOfBuildcontextTask
    }
    return configureResult
  }

  @TaskAction
  def build() {
    logger.info "running build..."

    if (getBuildContextDirectory()) {
      // only one of buildContext and buildContextDirectory shall be provided
      assert !getBuildContext()

      assert tarOfBuildcontextTask
      logger.info "temporary buildContext: ${tarOfBuildcontextTask.archivePath}"
      buildContext = new FileInputStream(tarOfBuildcontextTask.archivePath as File)
    }

    // at this point we need the buildContext
    assert getBuildContext()

    def imageId = getDockerClient().build(getBuildContext())
    if (getImageName()) {
      logger.info "tag $imageId as '${getImageName()}'..."
      getDockerClient().tag(imageId, getImageName())
    }
    return imageId
  }

  def getNormalizedImageName() {
    if (!getImageName()) {
      return UUID.randomUUID().toString()
    }
    return getImageName().replaceAll("\\W", "_")
  }
}

package de.gesellix.gradle.docker.tasks

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.gradle.api.tasks.bundling.Tar
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

  DockerBuildTask() {
    description = "builds an image from the given build context"

//    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()) {
      tarOfBuildcontextTask = project.task(["type": Tar], "tarOfBuildcontext") {
        description = "tar of buildcontext"
        from getBuildContextDirectory()
        baseName = "buildContext_${getNormalizedImageName()}"
        destinationDir getTemporaryDir()
      }
      dependsOn tarOfBuildcontextTask
    }
    return configureResult
  }

  @TaskAction
  def build() {
    logger.info "running build..."

    if (getBuildContextDirectory()) {
      // only one of buildContext and buildContextDirectory shall be provided
      assert !getBuildContext()

      assert tarOfBuildcontextTask
      logger.info "temporary buildContext: ${tarOfBuildcontextTask.archivePath}"
      buildContext = new FileInputStream(tarOfBuildcontextTask.archivePath as File)
    }

    // at this point we need the buildContext
    assert getBuildContext()

    def imageId = getDockerClient().build(getBuildContext())
    if (getImageName()) {
      logger.info "tag $imageId as '${getImageName()}'..."
      getDockerClient().tag(imageId, getImageName())
    }
    return imageId
  }

  def createTemporaryBuildContext() {
    def temporaryBuildContext = new File(getTemporaryDir(), "buildContext_${getNormalizedImageName()}")
    project.buildDir.mkdirs()
    temporaryBuildContext.createNewFile()
    return temporaryBuildContext
  }

  def getNormalizedImageName() {
    if (!getImageName()) {
      return UUID.randomUUID().toString()
    }
    return getImageName().replaceAll("\\W", "_")
  }
}
