GithubHelp home page GithubHelp logo

Comments (1)

rtoomey-coatue avatar rtoomey-coatue commented on August 17, 2024

Crude POC using Global / onLoad with sbt 1.3.x - done in my own project as an autoplugin. Has "double pass" behavior if changes are detected:

import sbt.{ Def, _ }
import Keys._
import org.scalafmt.sbt.ScalafmtPlugin

// https://www.scala-sbt.org/1.x/docs/Howto-Startup.html
object ScalafmtOnLoad extends AutoPlugin {
  override def requires = ScalafmtPlugin
  override def trigger  = AllRequirements

  // This prepends the String you would type into the shell
  lazy val startupTransition: State => State = { s: State =>
    "scalafmtSbt" :: s
  }

  override def globalSettings: Seq[Def.Setting[_]] = Seq(
    // onLoad is scoped to Global because there's only one.
    Global / onLoad := {
      val old = (Global / onLoad).value
      // compose the new transition on top of the existing one
      // in case your plugins are using this hook.
      startupTransition.compose(old)
    }
  )
}

Note that I excluded ScalafmtOnLoad.scala using project.excludeFilters out of fear of infinite loop 😊

  1. sbt detects changes, reloads
  2. onLoad fires scalafmtSbt, build files are formatted (i intentionally added a changes that would get reformatted)
  3. sbt detects changes, reloads a second time
  4. this time, no changes detected, loop exits, life goes on, it’s all cool
sbt:foo> 
[info] build source files have changed
[info] modified files: 
[info]   /Users/rtoomey/workspace/foo/build.sbt
[info]   /Users/rtoomey/workspace/foo/project/ScalafmtOnLoad.scala
[info] Reloading sbt...
[info] Loading global plugins from /Users/rtoomey/.sbt/1.0/plugins
[info] Loading settings for project foo-build from plugins.sbt,build.sbt ...
[info] Loading project definition from /Users/rtoomey/workspace/foo/project
[info] Compiling 1 Scala source to /Users/rtoomey/workspace/foo/project/target/scala-2.12/sbt-1.0/classes ...
[info] Loading settings for project root from build.sbt ...
[info] Resolving key references (36593 settings) ...
[info] Set current project to foo (in build file:/Users/rtoomey/workspace/foo/)
[success] Total time: 3 s, completed Dec 6, 2019 12:24:11 PM
[info] build source files have changed
[info] modified files: 
[info]   /Users/rtoomey/workspace/foo/build.sbt
[info] Reloading sbt...
[info] Loading global plugins from /Users/rtoomey/.sbt/1.0/plugins
[info] Loading settings for project foo-build from plugins.sbt,build.sbt ...
[info] Loading project definition from /Users/rtoomey/workspace/foo/project
[info] Loading settings for project root from build.sbt ...
[info] Resolving key references (36593 settings) ...
[info] Set current project to foo (in build file:/Users/rtoomey/workspace/foo/)
[success] Total time: 3 s, completed Dec 6, 2019 12:24:17 PM

Further manual reload command is silent about formatting build files:

sbt:foo> reload
[info] Loading global plugins from /Users/rtoomey/.sbt/1.0/plugins
[info] Loading settings for project foo-build from plugins.sbt,build.sbt ...
[info] Loading project definition from /Users/rtoomey/workspace/foo/project
[info] Loading settings for project root from build.sbt ...
[info] Resolving key references (36593 settings) ...
[info] Set current project to foo (in build file:/Users/rtoomey/workspace/foo/)
[success] Total time: 3 s, completed Dec 6, 2019 12:27:53 PM

Seems to work! 🀞

from sbt-scalafmt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.