<commit_before>package com.typesafe.sbt
package packager
package debian

import Keys._
import sbt._
import sbt.Keys.{ target, name, normalizedName, TaskStreams }
import linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink }
import linux.Keys.{ linuxScriptReplacements, daemonShell }
import com.typesafe.sbt.packager.linux.LinuxPackageMapping
import scala.collection.JavaConversions._

import org.vafer.jdeb.{ DebMaker, DataProducer }
import org.vafer.jdeb.mapping._
import org.vafer.jdeb.producers._
import DebianPlugin.Names

/**
 * This provides a java based debian packaging implementation based
 * on the jdeb maven-plugin. To use this, put this into your build.sbt
 *
 * {{
 *    packageBin in Debian <<= debianJDebPackaging in Debian
 * }}
 *
 * @author <NAME>
 * @see https://github.com/tcurdt/jdeb/blob/master/src/main/java/org/vafer/jdeb/maven/DebMojo.java#L503
 *
 */
trait JDebPackaging { this: DebianPlugin with linux.LinuxPlugin =>

  private[debian] def debianJDebSettings: Seq[Setting[_]] = Seq(

    /**
     * Depends on the 'debianExplodedPackage' task as this creates all the files
     * which are defined in the mappings.
     */
    debianJDebPackaging <<= (debianExplodedPackage, linuxPackageMappings, linuxPackageSymlinks,
      debianControlFile, debianMaintainerScripts, debianConffilesFile,
      normalizedName, version, target, streams) map {
        (_, mappings, symlinks, controlfile, controlscripts, conffile,
        name, version, target, s) =>
          s.log.info("Building debian package with java based implementation 'jdeb'")
          val console = new JDebConsole(s.log)
          val debianFile = target.getParentFile / "%s_%s_all.deb".format(name, version)
          val debMaker = new DebMaker(console,
            fileAndDirectoryProducers(mappings, target) ++ linkProducers(symlinks),
            conffileProducers()
          )
          debMaker setDeb debianFile
          debMaker setControl (target / Names.Debian)

          // TODO set compression, gzip is default
          // TODO add signing with setKeyring, setKey, setPassphrase, setSignPackage, setSignMethod, setSignRole
          debMaker validate ()
          debMaker makeDeb ()
          debianFile
      })

  /**
   * Creating file and directory producers. These "produce" the
   * files for the debian packaging
   */
  private[debian] def fileAndDirectoryProducers(mappings: Seq[LinuxPackageMapping], target: File): Seq[DataProducer] = mappings.map {
    case LinuxPackageMapping(paths, perms, zipped) =>
      // TODO implement mappers here or use the maintainerscripts logic?
      val (dirs, files) = paths.partition(_._1.isDirectory)
      paths map {
        case (path, name) if path.isDirectory =>
          val permMapper = new PermMapper(-1, -1, perms.user, perms.group, null, perms.permissions, -1, null)
          new DataProducerDirectory(target / name, null, Array("**"), Array(permMapper))
        case (path, name) =>
          val permMapper = new PermMapper(-1, -1, perms.user, perms.group, perms.permissions, null, -1, null)<commit_msg>FIX #324 adding empty dirs to deb package<commit_after>package com.typesafe.sbt
package packager
package debian

import Keys._
import sbt._
import sbt.Keys.{ target, normalizedName }
import linux.{ LinuxSymlink }
import com.typesafe.sbt.packager.linux.LinuxPackageMapping
import scala.collection.JavaConversions._

import org.vafer.jdeb.{ DebMaker, DataProducer }
import org.vafer.jdeb.mapping._
import org.vafer.jdeb.producers._
import DebianPlugin.Names

/**
 * This provides a java based debian packaging implementation based
 * on the jdeb maven-plugin. To use this, put this into your build.sbt
 *
 * {{
 *    packageBin in Debian <<= debianJDebPackaging in Debian
 * }}
 *
 * @author <NAME>
 * @see https://github.com/tcurdt/jdeb/blob/master/src/main/java/org/vafer/jdeb/maven/DebMojo.java#L503
 *
 */
trait JDebPackaging { this: DebianPlugin with linux.LinuxPlugin =>

  private[debian] def debianJDebSettings: Seq[Setting[_]] = Seq(

    /**
     * Depends on the 'debianExplodedPackage' task as this creates all the files
     * which are defined in the mappings.
     */
    debianJDebPackaging <<= (debianExplodedPackage, linuxPackageMappings, linuxPackageSymlinks,
      debianControlFile, debianMaintainerScripts, debianConffilesFile,
      normalizedName, version, target, streams) map {
        (_, mappings, symlinks, controlfile, controlscripts, conffile,
        name, version, target, s) =>
          s.log.info("Building debian package with java based implementation 'jdeb'")
          val console = new JDebConsole(s.log)
          val debianFile = target.getParentFile / "%s_%s_all.deb".format(name, version)
          val debMaker = new DebMaker(console,
            fileAndDirectoryProducers(mappings, target) ++ linkProducers(symlinks),
            conffileProducers()
          )
          debMaker setDeb debianFile
          debMaker setControl (target / Names.Debian)

          // TODO set compression, gzip is default
          // TODO add signing with setKeyring, setKey, setPassphrase, setSignPackage, setSignMethod, setSignRole
          debMaker validate ()
          debMaker makeDeb ()
          debianFile
      })

  /**
   * Creating file and directory producers. These "produce" the
   * files for the debian packaging
   */
  private[debian] def fileAndDirectoryProducers(mappings: Seq[LinuxPackageMapping], target: File): Seq[DataProducer] = mappings.map {
    case LinuxPackageMapping(paths, perms, zipped) =>
      paths map {
        case (path, name) if path.isDirectory =>
          val permMapper = new PermMapper(-1, -1, perms.user, perms.group, null, perms.permissions, -1, null)
          val dirName = if (name.startsWith("/")) name.drop(1) else name
          new DataProducerDirectory(target, Array(dirName), null, Array(permMapper))
        case (path, name) =>
          val permMapper = new PermMapper(-1, -1, perms.user, perms.group, perms.permissions, null, -1, null)
{'fraction_non_alphanumeric': 0.09970039946737683, 'fraction_numerical': 0.003828229027962716, 'mean_word_length': 3.3511947863866762, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '5936791', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1750, 'n_words': 487}