Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #347 add ability to add arbitrary stuff from the classpath. Only #603

Merged
merged 1 commit into from
Jun 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 96 additions & 4 deletions src/main/scala/com/typesafe/sbt/packager/MappingsHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ package com.typesafe.sbt
package packager

import sbt._
import com.typesafe.sbt.packager.archetypes.JavaAppPackaging

/** A set of helper methods to simplify the writing of mappings */
object MappingsHelper {

/** return a Seq of mappings which effect is to add a whole directory in the generated package */
/**
* return a Seq of mappings which effect is to add a whole directory in the generated package
*
* @example
* {{{
* mappings in Universal ++= directory(baseDirectory.value / "extra")
* }}}
*
* @param sourceDir
* @return mappings
*/
def directory(sourceDir: File): Seq[(File, String)] = {
val parentFile = sourceDir.getParentFile
if (parentFile != null)
Expand All @@ -15,22 +26,103 @@ object MappingsHelper {
sourceDir.*** pair basic
}

/** It lightens the build file if one wants to give a string instead of file. */
/**
* It lightens the build file if one wants to give a string instead of file.
*
* @example
* {{{
* mappings in Universal ++= directory("extra")
* }}}
*
* @param sourceDir
* @return mappings
*/
def directory(sourceDir: String): Seq[(File, String)] = {
directory(file(sourceDir))
}

/**
* return a Seq of mappings which effect is to add the content of directory in the generated package,
* excluding the directory itself
* excluding the directory itself.
*
* @example
* {{{
* mappings in Universal ++= sourceDir(baseDirectory.value / "extra")
* }}}
*
* @param sourceDir
* @return mappings
*/
def contentOf(sourceDir: File): Seq[(File, String)] = {
(sourceDir.*** --- sourceDir) pair relativeTo(sourceDir)
}

/** It lightens the build file if one wants to give a string instead of file. */
/**
* It lightens the build file if one wants to give a string instead of file.
*
* @example
* {{{
* mappings in Universal ++= sourceDir("extra")
* }}}
*
* @param sourceDir as string representation
* @return mappings
*/
def contentOf(sourceDir: String): Seq[(File, String)] = {
contentOf(file(sourceDir))
}

/**
* Create mappings from your classpath. For example if you want to add additional
* dependencies, like test or model.
*
*
* @example Add all test artifacts to a separated test folder
* {{{
* mappings in Universal ++= fromClasspath((managedClasspath in Test).value, target = "test")
* }}}
*
* @param entries
* @param target
* @return a list of mappings
*/
def fromClasspath(entries: Seq[Attributed[File]], target: String): Seq[(File, String)] = {
fromClasspath(entries, target, _ => true)
}

/**
* Create mappings from your classpath. For example if you want to add additional
* dependencies, like test or model. You can also filter the artifacts that should
* be mapped to mappings.
*
* @example Filter all osgi bundles
* {{{
* mappings in Universal ++= fromClasspath(
* (managedClasspath in Runtime).value,
* "osgi",
* artifact => artifact.`type` == "bundle"
* )
* }}}
*
*
* @param entries from where mappings should be created from
* @param target folder, e.g. `model`. Must not end with a slash
* @param includeArtifact function to determine if an artifact should result in a mapping
* @param includeOnNoArtifact default is false. When there's no Artifact meta data remove it
*/
def fromClasspath(
entries: Seq[Attributed[File]],
target: String,
includeArtifact: Artifact => Boolean,
includeOnNoArtifact: Boolean = false): Seq[(File, String)] = {

entries
.filter(attr => attr.get(sbt.Keys.artifact.key) map includeArtifact getOrElse includeOnNoArtifact)
.map { attribute =>
val file = attribute.data
file -> s"$target/${file.getName}"
}
}

}