How to write dependsOn in Custom plugin - plugins

I have a task in my build.gradle :
task makeJar(type: Copy) {
delete('dist/')
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
makeJar.dependsOn('clearTask', build)
Now, I want to remove this from my build.gradle and create a custom plugin like this : MakeJarTask.groovy (this is in eclipse project)
class MakeJarPluginTask extends Copy{
#TaskAction
def makeJar(){
logger.lifecycle("creating a jar *********************")
delete('dist/')
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
Calling this Task in callGroovy.class (that implements Plugin)
project.tasks.create("makeJarPlugin1", MakeJarPluginTask.class){
dependsOn("clearDist", "build")
}
But this doesn't give the correct output .
The error is in last part, I think this is not the correct way to use dependsOn but I am not able to get how to use this. Any help will be highly appreciated.

Task task = project.tasks.create("makeJarPlugin1", MakeJarPluginTask.class);
task.dependsOn("clearDist", "build")

Related

Integrate µTest (micro test) for Scala into a Gradle project

I'm using Gradle for my Scala projects and a bit frustrated about documentation of scalatest. So I searched for a alternative test framework. The only one I found was µTest (micro test). But so far I could not found a way to integrate µTest into Gradle.
After time of investigation I found a solution. If I have a sample test:
import utest._
object SampleTests extends TestSuite {
val tests:Tests = Tests {
var x = 0
'outer1 - {
x += 1
'inner1 - {
x += 2
assert(x == 3) // 0 + 1 + 2
x
}
'inner2 - {
x += 3
assert(x == 4) // 0 + 1 + 3
x
}
}
'outer2 - {
x += 4
'inner3 - {
x += 5
assert(x == 9) // 0 + 4 + 5
x
}
}
}
def main(args: Array[String]): Unit = {
val results = TestRunner.runAndPrint(SampleTests.tests, "SampleTests")
}
}
Importent is that there is a main funtion which calls a method of the TestRunner with a parameter of type Tests. This parameter can be a value or a method defined with def.
Furthermore the test code should be inside test source location (test instead of main).
For triggering this code you need to modify the build.gradle file. There you can insert a user defined task like this:
task microTest(type: JavaExec) {
main = 'package.SampleTests'
classpath = sourceSets.test.runtimeClasspath
}
Of course you need to declare the dependency inside build.gradle to the test framework:
testImplementation "com.lihaoyi:utest_2.a:x.y.z"
Fazit:
There is a way to trigger the tests with
./gradlew microTest
or click with the mouse inside your IDE at the listed gradle task. The output of the test framework micro test will be printed to the console in the expected way. But when you call this task indirectly by defining the following line inside build.gradle:
test.finalizedBy microTest
with click at the test task in the IDE (Intellij), then the colored output is replaced by special characters.
When not clicking (entering the command line: ./gradlew test) all output is printed correctly.

Mill: How to add additional Resources to a module

I have some Files out side my Module that I need to have on my classpath for testing.
Listing all possibilities (mill resolve tests._) I think to extend resources is the way to go.
I tried a lot - here my last attempt:
object test extends Tests {
override def resources =
new Sources({
super.resources.self.map(_ :+ (millSourcePath / up / 'data / 'global / 'bpmn))
},
super.resources.ctx
)
...
}
Is overwriting resources the way to go?
How is it done correctly?
resources is a "task of sources" as defined here. Thus, in order to add something to the resources path you can do
override def resources = T.sources {
super.resources() :+ PathRef(millSourcePath / up / 'data / 'global / 'bpm)
}

How can I print all the settings in Test configuration for a project build using SBT?

I have a scala-js project, adding a particular library dependency, is affecting the way project test cases are running. Without the library dependency everything's fine, the moment I add them, tests doesn't execute. I want to check all sbt settings, if those are getting affected. Is there any way I can print all settings and check?
BuildStructure.data seems to give access to all the settings by scope. We could access it by defining a custom command printAllTestSettings like so:
def printAllTestSettings = Command.command("printAllTestSettings") { state =>
val structure = Project.extract(state).structure
val testScope =
Scope(
Select(ProjectRef(new File("/home/mario/sandbox/hello-world-scala/"), "root")),
Select(ConfigKey("test")),
Zero,
Zero
)
structure
.data
.keys(testScope)
.foreach(key => println(s"${key.label} = ${structure.data.get(testScope, key).get}"))
state
}
commands ++= Seq(printAllTestSettings)
Here is output snippet:
...
managedSourceDirectories = List(/home/mario/sandbox/hello-world-scala/target/scala-2.12/src_managed/test)
managedResourceDirectories = List(/home/mario/sandbox/hello-world-scala/target/scala-2.12/resource_managed/test)
testLoader = Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/home/mario/sandbox/hello-world-scala/,root)), Select(ConfigKey(test)), Zero, Zero),testLoader)))
packageBin = Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/home/mario/sandbox/hello-world-scala/,root)), Select(ConfigKey(test)), Zero, Zero),packageBin)))
...

Remove or Exclude WatchSource in sbt 1.0.x

Overview
After looking around the internet for a while, I have not found a good way to omit certain folders from being watched by sbt 1.0.x in a Play Framework application.
Solutions posted for older versions of sbt:
How to exclude a folder from compilation
How to not watch a file for changes in Play Framework
There are a few more, but all more or less the same.
And the release notes for 1.0.2 show that the += and ++= behavior was maintained, but everything else was dropped.
https://www.scala-sbt.org/1.x/docs/sbt-1.0-Release-Notes.html
Source code verifies: https://www.scala-sbt.org/1.0.4/api/sbt/Watched$.html
Would love to see if anyone using sbt 1.0.x has found a solution or workaround to this issue. Thanks!
Taking the approach of how SBT excludes managedSources from watchSources I was able to omit a custom folder from being watched like so:
watchSources := {
val directoryToExclude = "/Users/mgalic/sandbox/scala/scala-seed-project/src/main/scala/dirToExclude"
val filesToExclude = (new File(directoryToExclude) ** "*.scala").get.toSet
val customSourcesFilter = new FileFilter {
override def accept(pathname: File): Boolean = filesToExclude.contains(pathname)
override def toString = s"CustomSourcesFilter($filesToExclude)"
}
watchSources.value.map { source =>
new Source(
source.base,
source.includeFilter,
source.excludeFilter || customSourcesFilter,
source.recursive
)
}
},
Here we use PathFinder to get all the *.scala sources from directoryToExclude:
val filesToExclude = (new File(directoryToExclude) ** "*.scala").get.toSet
Then we create customSourcesFilter using filesToExclude, which we then add to every current WatchSource:
watchSources.value.map { source =>
new Source(
...
source.excludeFilter || customSourcesFilter,
...
)
}
Note the above solution is just something that worked for me, that is, I do not know what is the recommend approach of solving this problem.

Shell like application using sbt console

I would like to deploy some scala code, to be used very similar to sbt console
(command line interface, history, etc)
and would like to
customize it
and made it simple to deploy.
Can sbt console be used with these changes:
Removed startup info messages
Removed scala welcome message
Customized command prompt instead of "scala>" to be "myApp>"
No access to local nor global ivy/maven repositories (all jars
available, including sbt jars and dependencies)
Anybody passed this path ?
I have tried
Using sbt to build command line application
but with no much progress so far
(I guessed it was intented to very similar situation)
Are there ready made plugin available ?
Any other tool related or unrelated to sbt ?
Thank you
Actully, no need for sbt. To have it tweaked, scala code should be changed.
For the sbt "Customized command prompt" part, you have a good example with "sbt: Customize the Shell prompt in sbt" from Patrick Bailey (patmandenver).
create the ~/.sbt/0.13/global.sbt file:
vi ~/.sbt/0.13/global.sbt
And place the following in it.
shellPrompt := { state =>
def textColor(color: Int) = { s"\033[38;5;${color}m" }
def backgroundColor(color:Int) = { s"\033[48;5;${color}m" }
def reset = { s"\033[0m" }
def formatText(str: String)(txtColor: Int, backColor: Int) = {
s"${textColor(txtColor)}${backgroundColor(backColor)}${str}${reset}"
}
val red = 1
val green = 2
val yellow = 11
val white = 15
val black = 16
val orange = 166
formatText(s"[${name.value}]")(white, orange) +
"\n " +
formatText("\u276f")(green, black) +
formatText("\u276f")(yellow, black) +
formatText("\u276f ")(red, black)
}
Run reload in sbt and….
That can be amended/enhanced/completed to add other information you would need in your case.