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

Attempt to resolve issue 161 - Add human readable methods to map directories #165

Merged
merged 4 commits into from
Feb 18, 2014
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 5 additions & 2 deletions src/main/scala/com/typesafe/sbt/PackagerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ object SbtNativePackager extends Plugin
with GenericPackageSettings {

val NativePackagerKeys = packager.Keys


val NativePackagerHelper = packager.MappingsHelper

def packagerSettings = linuxSettings ++
debianSettings ++
rpmSettings ++
Expand Down Expand Up @@ -47,7 +49,8 @@ object SbtNativePackager extends Plugin
def java_server: Seq[Setting[_]] =
genericMappingSettings ++ archetypes.JavaServerAppPackaging.settings
}



// TODO - Add a few targets that detect the current OS and build a package for that OS.

}
31 changes: 31 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/MappingsHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.typesafe.sbt
package packager

import sbt._

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. these are definitely the most frequently used.


/** return a Seq of mappings which effect is to add a whole directory in the generated package */
def directory(sourceDir: File): Seq[(File, String)] = {
sourceDir.*** pair relativeTo(sourceDir.getParentFile)
}

/** It lightens the build file if one wants to give a string instead of file. */
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 */
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. */
def contentOf(sourceDir: String): Seq[(File, String)] = {
contentOf(file(sourceDir))
}

}

16 changes: 16 additions & 0 deletions src/sphinx/universal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ generates a function with a ``file -> Option[String]`` mapping, which tries to g
string path from ``dir.getParentFile`` to the passed in file. ``pair`` uses the ``relativeTo``
function to generate a mapping ``File -> String``, which is _your file_ to _relative destination_.

It exists some helper methods to map a complete directory in more human readable way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the src/universal src/linux, src/rpm and src/debian directory mappings probably aren't covered in the docs either. Were you aware that those work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you mean by that @jsuereth ? That one can place files in side these directories and they get automatically added to the corresponding package, e.g.

  • src/universal/readme.md -> will appear in all build packages at the root level
  • src/universal/debian.md -> will only appear in debian packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was aware these work even if I did not test them yet. You mean that we should add some documentation for directory mapping for debian, linux ,rpm ... ? If yes I can do it as soon as possible.

Correct me if I am wrong but adding directory mappings with such helper methods in Linux for example the user have to use the packageMapping method as explained in the linux.rst document ?
Since the type of packageMapping is (File, String)* -> Unit and the directory or contentOf helper methods are of type String -> Seq[(File, String)], we should be able to write something like the following to perform mappings in Linux by transforming the output of helper methods to the approriate type using the : _* operator.

packageMapping(directory("MyResourceDir"): _*) with....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muuki88 What I mean is all files in src/universal shows up in mappings in Universal as well as all files in src/linux shows up in linuxPackageMappings in Liux etc. etc.

All I'm suggesting is we add docs for that "default lookup" as well as "here, you can also customize"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh also, this would work with packageMapping with the :_* (varargs syntax).


.. code-block:: scala

import NativePackagerHelper._

mappings in Universal ++= directory("SomeResourcesToInclude")

Mapping the content of a directory.

.. code-block:: scala
Expand All @@ -177,6 +185,14 @@ Mapping the content of a directory.

The ``dir`` gets excluded and is used as root for ``relativeTo(dir)``.

It also exists some helper methods to simplify writing of such mapping.

.. code-block:: scala

import NativePackagerHelper._

mappings in Universal ++= contentOf("SomeResourcesToInclude")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works for static files, which are already present. I think an example which depends on a task is neccessary, like

mappings in Universal <++= (packageBin in Compile, target) map { (_, target) =>
   contentOf(target / "somefolder")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. This explains why I was not able to use the targetfrom sbt Keys like this:

mapping in Universal ++= directory(target / "scala-2.10" / "api")

The above snippet does not compile due to bad type java.io.File. Using target... requires a kind of task scope.

I'll add it to the doc !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before adding it to the documentation, I did test this and unfortunately, it does not compile. might be a type problem I do not understand yet. I have put the following in my build.sbt :

mappings in Universal <++= (packageBin in Compile, target) map { (_, target) =>
  directory(target / "streams")
}

And loading the project, I have obtained the following error:

> reload
[info] Loading project definition from /Users/ivan/****
/Users/ivan/****/build.sbt:49: error: value / is not a member of java.io.File
  directory(target / "streams")
                   ^
[error] Type error in expression

Do you have any idea about this problem ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sbt 0.13:

mappings in Universal ++= {
  val ensureRun = (packageBin in Compile).value
  directory(target.value / "streams")
}

What I think you're running into above is some shadowing flipping out the implicits. Try to use a different name for target:

mappings in Universal <++= (packageBin in Compile, target) map { (_, t) =>
  directory(t / "streams")
}

Also +1 on an example which depends on a task.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn, sorry for the confusion. Didn't test it either, just quickly wrote it.


Commands
--------

Expand Down