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

Add a normal task with no arguments #68

Open
steinybot opened this issue Jan 27, 2018 · 3 comments · May be fixed by #69
Open

Add a normal task with no arguments #68

steinybot opened this issue Jan 27, 2018 · 3 comments · May be fixed by #69

Comments

@steinybot
Copy link

The user guide suggests that to run scalastyle as part of another task you need to add:

lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")

// scalastyle >= 0.9.0
compileScalastyle := scalastyle.in(Compile).toTask("").value
// scalastyle <= 0.8.0
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value

(compile in Compile) := ((compile in Compile) dependsOn compileScalastyle).value

But note that this makes the compile task dependent on the scalastyle task - the scalastyle task executes first. This can cause problems because if the code doesn’t compile, you’ll get the scalastyle errors messages, not the scalac ones. Great though scalastyle is, it can’t match the error messages produced by the compiler. :-)

There is actually another way which doesn't loose the compiler errors:

compile := compileScalastlyeTask(Compile / compile, Compile / scalastyle).value

def compileScalastlyeTask(compileTask: TaskKey[CompileAnalysis],
                          scalastyleTask: InputKey[Unit]): Def.Initialize[Task[CompileAnalysis]] = Def.task {
  val analysis = compileTask.value
  val args: Seq[String] = Seq.empty
  val scalastyleSourcesV = scalastyleSources.value
  val configV = scalastyleConfig.value
  val configUrlV = scalastyleConfigUrl.value
  val streamsV = streams.value
  val failOnErrorV = scalastyleFailOnError.value
  val failOnWarningV = scalastyleFailOnWarning.value
  val scalastyleTargetV = scalastyleTarget.value
  val configRefreshHoursV = scalastyleConfigRefreshHours.value
  val targetV = target.value
  val configCacheFileV = scalastyleConfigUrlCacheFile.value
  org.scalastyle.sbt.Tasks.doScalastyle(args,
    configV,
    configUrlV,
    failOnErrorV,
    failOnWarningV,
    scalastyleSourcesV,
    scalastyleTargetV,
    streamsV,
    configRefreshHoursV,
    targetV,
    configCacheFileV)
  analysis
}

The idea here is really simple which is to first call the compile task, then the scalastlye task and finally return the result of the compile task. This is quite verbose and not future proof since it is essentially reimplementing the scalastyle input task.

It would be far simpler if there was a normal task (not an input task) provided by the plugin.

@matthewfarwell
Copy link
Member

Thanks for your input. I agree that we can change things to make this easier, and I think your idea of having a normal input task is a good one, and would cover the bases. Now all we have to do is think of a good name: how about scalastyleTask

@steinybot
Copy link
Author

My comment saying that we "first call the compile task" is a bit misleading since we are not really calling it, sbt will figure out the order for us. There is also an unused scalastyleTask argument which if it was used would not work since then the order is not defined. It works in this case but only because it calls doScalastyle directly. This is all a little bit silly.

I went to the sbt docs to go and lookup Defining a sequential task with Def.sequential and low and behold the example is exactly what we want. Keeping in mind that it should be a "raw task" so that users can choose the configuration to use (for reference Provide raw settings and configured settings).

As for the name, that is a hard one. I would say that it should probably start with compile. The most descriptive would be something like compileThenCheckStyle.

In terms of completeness I would be inclined to do something like:

  1. Add a val scalastyleDefaultArgs = Setting[Seq[String]] for the default args which defaults to Seq.empty
  2. A non-input task that does something like scalastyleWithDefaultArgs := scalastyle.toTask(scalastyleDefaultArgs.mkString(" ")).value
  3. A raw task for compiling and checking the style compileThenCheckStyle := Def.sequential(compile, scalastyleWithDefaultArgs).value

If I remember this later I'll double check that it works and submit a PR.

@steinybot steinybot linked a pull request Mar 13, 2018 that will close this issue
@er1c
Copy link

er1c commented Aug 28, 2020

@steinybot this would be really helpful!

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

Successfully merging a pull request may close this issue.

3 participants