How should one start on creating a sbt plugin? - plugins

I want to create a sbt plugin for Scala project.
Please any one suggest me how we start?
I referred Plugins documentation but unable to understand steps.

The first step in becoming a sbt plugin author is understanding sbt build definition. The best resource for it is Getting Started guide. For plugins, it's essential that you understand the concept of scoping. Some of my blog posts like an unofficial guide to sbt 0.10 v2.0 and traveling through the 4th dimension with sbt 0.13 discuss the topic.
Next, try reading the source code for existing plugins:
sbt/sbt-appengine
sbt-appengine adds defines appengineSettings, which the build user can include in his or her build definition to add appengineDeploy and other appengine related tasks.
sbt/sbt-man
sbt-man on the other hand overrides settings and adds man command.
These are roughly two patterns for plugins. Once you understand them fully, try making your own plugin.
Another source of inspiration is the source for sbt itself. Whenever I'm writing a plugin, I'd consult Defaults.scala to see how sbt implements a particular task that I'm interested in. Once you're comfortable, you should also read Plugins Best Practices too.

Related

scala sbt: how to find out and download dependencies automatically just like go get?

As known, in golang, go get dir/... would find out and download all dependencies of package specified by dir.
Then in scala, does sbt provide similar function? So that we don't need to add dependencies into build file?
I am afraid the scala sbt doesn't provide the exact kind of feature you are looking. However, sbt does provide several ways to automate the process of downloading dependencies. The closest technique related to your use case would be using ivy configuration and setting files.
You can read about that here:
http://www.scala-sbt.org/0.12.4/docs/Detailed-Topics/Library-Management.html

What's the point of downloading the source jars in a grails project?

I've noticed that in eclipse if you Right click on a project -> Grails Tools -> You have the option to 'Download Source Jars'.
What is the point of this and what are some common reasons as to why you would want to do this?
Grails 2.2.3
Edit:
I'm not even sure what grails does instead of that.
Many (most) libraries (JARs, "artifacts" in the Maven terminology) publish a sources archive alongside their binary artifacts in the repositories. This can be useful for Eclipse to show you the Javadoc and source code when you're using the library in your projects. As #JonSkeet commented above, it's very useful to have source code available directly in the IDE when using a library.
By default, Grails does not download the sources for artifacts; this option triggers it to do so and attach the sources to the binary JARs.
Agreed with E-Riz.
Here are the reasons I use the sources:
i want to have a deeper understanding of how the library works when debugging my own depending code
i want to find a possible bug in the library, so I can fork it and apply my own patch. i will possibly share this with the maintainers as a pull request if I'm willing to spend that much time on it.
i want to find out what logging systems it uses that might be poorly documented, so I can see better what their code is doing during runtime, to troubleshooting complicated problems.

Publish Site using SBT

I have an SBT project using SBT 0.13.2. I am using the awesome sbt-site plugin to generate a pamflet site.
What is the best way using SBT to get the generated sbt-site onto a remote server for others to access?
We have this already setup and working wonderfully using Maven and WEBDAV.
I am hoping that there is a simple answer, barring creating my own sbt tasks.
I had to create my own SBT WEBDAV plugin to do the publishing. If people are interested in what I did, feel free to contact me (note: unclear on how to post contact info on here).
It wasn't hard, but rather a pain. I would love to open source it, but that would take time I don't have presently :(

Setting up a Scala project in Eclipse, together with JUnit & Scalatest

I have recently completed the Scala course on Coursera, and since then I have been looking forward to getting my hands dirty with Scala again. I have written code for some years but I neither educated to be nor work as a programmer, so it took me a while to get a good opportunity but now that I have some time to invest and a good project to work on it's time...
Except I can't seem to get things set up properly, which I find really frustrating. I have OpenJDK 1.7.0_25 running on my Linux machine. I have downloaded and installed the Bundle Scala IDE build for Eclipse (just like we used in the course). And I got ScalaTest both as a jar file and the Eclipse plug-in.
I have a simple project (so far) and no matter what I do I can't seem to get my builds and tests in order. First off how exactly am I supposed to set up my project so that my classes and tests are actually run properly? All the assignments we got were projects that had the same structure, so do I have to have:
project
|--src
|--main
|--scala
|--test
|--scala
structure? If so why is it not the default way the project is setup when I create a new project? Do I create these folders manually, as packages or as source folders? The whole thing gets pretty murky..
I should mention that I tried to "Mavenize" the project using the contextual menu in Eclipse, added my ScalaTest dependency. The first thing that happens is that I get compile errors, at every point of dependency in my code. So clearly the library is not visible, in other words Maven does not seem to be doing much of management. I thought the whole point of Maven was to get and maintain dependencies as the project evolves. I concluded that I do not fully understand the way Maven works and thus I eventually gave up on Maven, once again, and went back to doing things manually.
Secondly, I can't seem to run my tests; the Run As... menu item does not include ScalaTest as it's mentioned in the documentation of ScalaTest Eclipse Plug-in. I have double checked that the plugin is installed. If I instead try to run using JUnitRunner then my tests are not recognized as valid tests. I have JUnit and ScalaTest on my build path, so it's got to be something else.
I suppose my overarching question is as follows:
given the Scala IDE build of Eclipse and ScalaTest, just exactly how am I supposed to set up my project (in Eclipse) so that I can just focus on writing my code and testing it, and hopefully not have any other headaches?
I work alone, and this project is not a product I need to deliver to some client. In other words I do not need to adhere to strict professionalism here. Honestly I just want to be able to code, get better acquainted with Scala and hopefully build a small data analysis tool that I will be using from time to time.
Thanks in advance!
Try using the sbt eclipse plugin:
https://github.com/typesafehub/sbteclipse
This is of course assumes that you use sbt as you build tool. If you don't at the moment you can find instructions on installation and usage here: http://www.scala-sbt.org/
Personally I've been using typesafe giter8 template (https://github.com/typesafehub/scala-sbt.g8) to setup my Scala projects, and then I use the sbt plugin mentioned above to generate eclipse project files.
Scala is somewhat Maven-based (sometimes implicitly), that's why you use that structure.
The easiest way I think is to create a simple Sbt/Maven POM and create the Eclipse project configurations (like with sbt eclipse). There you can set the dependencies (like the actual version of JUnit, Scalatest to use), so you can use the ScalaTest plugin easily.
In case of other issues, feel free to ask at the ScalaTest mailing list, Chee Seng and Bill Venners can help you a lot there.
The Scala IDE website has a full documentation on how to run unit testing frameworks with the IDE, have a look ! If you find missing elements, the bug tracker of the scala-IDE project is here.

Is it possible to use reflection from SBT?

I am trying to generate some boilerplate with SBT (tool which is totally new to me). I am using shapeless sbt files as my main reference for the task. I have seen that this project uses code generation from scratch, but my case is slightly different, since I would like to generate some classes from another ones. I pretend to use the new Scala 2.10.0-M4 reflection capabilities for doing so. What basic configuration is needed to have reflection available from a SBT build?
By now, the sbt is unable to find the scala.reflect.runtime.universe package, and I do not know if the problem comes either from the new Scala jar division or from a bad configuration. Besides, my sbt about says:
[info] This is sbt 0.13.0-20120530-052139
[info] The current project is {file:/home/jlg/sandbox/abc/}abc
[info] The current project is built against Scala 2.10.0-SNAPSHOT
[info]
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2
By the way, does anybody know other projects using SBT to generate source code?
Current SBT releases are based on Scala 2.9, and source code generation runs together with SBT with the same libraries. There are basically two choices:
be extremely bleeding-edge: get an SBT release running on Scala 2.10 (not even the 0.13 branch does), or waiting for it. The biggest problem is not just that you'd have to recompile SBT yourself, it's recompiling every single SBT plugin you'll need for Scala 2.10. In the long-term, this is maybe the best strategy to do what you ask, but it might be a lot of effort for now. However, beware that you cannot use reflection on your compiled code without evil tricks, since code generation is supposed to happen before compilation. If you need to do that, consider instead generating code at compile-time within the program using macros. This excludes SBT and is much more standard, but I'm not sure if you can generate complete classes in this release (this is I think planned for the future).
go with the old: stick with Scala 2.9 and use scalap's capabilities (ScalaSigParser) for compile-time reflection. The problem is that the API is different (not sure how deeply) and not really supported for public use, although various people have been using it for ages. For a project I'm running, a colleague implemented approach and I integrated it within SBT for my project (https://github.com/ps-mr/LinqOnSteroids/); on top of that, I use Scalate to write the templates to use for code generation, which is quite powerful.
See in particular build.sbt, which invokes
project/Generator.scala and project/src/main/scala/ivm/generation/ScalaSigHelpers.scala (some non-fully-generic wrappers for ScalaSigParser). Scalate Templates for generated code are in
src/main/resources, the most relevant here is src/main/resources/WrappedClassInlined.ssp.
Even more stuff is involved, I fear you'll pratically need a checkout and playing with it to see what it does exactly—but feel free to ask questions.
Please note that the code is protected by a BSD license, so you need to keep the original copyright if you copy the code.
Note: all the links (except the license) are to the current HEAD for stability, so that they won't disappear so easily even if the files are moved/removed in future versions.
If you're using 2.10.0-SNAPSHOT, then you should go for scala.reflect.runtime.universe. Take a look at http://dcsobral.blogspot.ch/2012/07/json-serialization-with-reflection-in.html for more information.