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

Change sourceDirectory and/or stageDirectory in Universal from Settings to Tasks #34

Closed
pdalpra opened this issue Sep 5, 2013 · 4 comments

Comments

@pdalpra
Copy link
Member

pdalpra commented Sep 5, 2013

Some context first :

In my SBT build, I need to repackage a bundle, previously built with sbt-native-packager by another build and published in my Ivy repo, in order to add some files in it.
This means that I need to :

  • Fetch the bundle from the repo
  • Unzip it
  • Use the unzipped bundle as a source directory for sbt-native-packager

The first two steps have to be done in a task, as I can't expect that the bundle has been already fetched when the project loads.

However, this means that sourceDirectory or stagingDirectory in Universal must be tasks, as settings can't depend on tasks...

Would it be possible to change those (or one of those) two settings to tasks ? Looking at the code of UniversalPlugin, it seems that the impact would be minimal.
In my case, changing stagingDirectory to a task would be sufficient : the structure is already there, so the unzipped bundle would be a perfectly fine staging directory.
I can submit a PR if you like :)

@jsuereth
Copy link
Member

jsuereth commented Sep 6, 2013

So, in general, you shouldn't be doing anything like that with "sourceDirectory". These are default settings. The actual meat of the plugin is configured with mappings in Universal which is a task. This is where you attach your tasks that do work. The other two are just like arguments to methods. You should create a task which downloads your zip file, unzips it and then returns the "mappings" you want to have for Universal packaging.

This is actually a common error in sbt: Trying to push your tasks into the wrong point. In sbt, the build takes a more functional flow. For every "settings" that we want users to touch, there's usually an associated task that does the actual work, e.g. "sources" task vs. "sourceDirectory" task. We take a similar approach here.

You should be able to get what you want without any changes. Let me know if you have issues. In particular (in sbt 0.13 syntax):

val downloadZipUrl = settingKey[URI]("Where I download the zip file from")

val downloadZipLocation = settingKey[File]("Where I download the zip file to ")

val downloadZipFile = taskKey[File]("Download the zip file")

val zipFileMappings = taskKey[Mappings]("Contents of the zip file I want to package")

downloadZipUrl := new java.net.URI("My awesome location")

downloadZipLocation := target.value / "my-zip-exploded"

downloadZipFile := {
  val tmp = java.io.File.createTempFile("zip", "file")
  IO.download(downloadZipUrl.value, tmp)
  IO.unzip(tmp, downloadZipLocation.value)
}

zipFileMappings := {
  val location: File = downloadZipLocation.value
  // Here's the Path API crazyness
  val finder: PathFinder = (location.*** --- location)
  // If you've never seen the `x` method before, it takes the path finder (file finder) and
  // gives you a mapping from java.io.File -> some String.  In this case, `relativeTo` gives
  // use the relative file name to "location" (the zip root).
  val mappings: Seq[(File, String)] = finder x relativeTo(location)
}

// Here we add our zip file to the universal packaging
mappings in Universal ++= zipFileMappings.value

Hope that helps!

@jsuereth jsuereth closed this as completed Sep 6, 2013
@pdalpra
Copy link
Member Author

pdalpra commented Sep 6, 2013

Thanks for your suggestions, I'll try it out !
I'm fairly new to SBT, so I haven't fully grasped its concepts yet ;)

@jsuereth
Copy link
Member

jsuereth commented Sep 6, 2013

NO problem! I used to the same thing myself (try to use settings as tasks and hook the wrong entry points). I'd say it's the second biggest hurdle to overcome, just trying to make sure you have a smoother path to success :)

@pdalpra
Copy link
Member Author

pdalpra commented Sep 6, 2013

I tried your solution, and it worked perfectly :)
I still have a few issues in my build, but nothing related to sbt-native-packager anymore (Great plugin BTW !).
Thanks for your help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants