guardian / sbt-packages-to-directories   0.1.0

Apache License 2.0 GitHub

sbt plugin to make directories structure correspond to package structure

Scala versions: 2.12
sbt plugins: 1.x

sbt-packages-to-directories Maven Central

Add under project/plugins.sbt

addSbtPlugin("com.gu" % "sbt-packages-to-directories" % version)

According to Tour of Scala: Packages and Imports

One convention is to name the package the same as the directory containing the Scala file.

This sbt plugin provides task

val packageStructureToDirectoryStructure = taskKey[Unit]("Make directory structure match package structure")

which reads through source files, extracts their package statements, and then moves the file to the matching directory structure. Make sure to backup the project before executing it as file are moved around. For example,

package vim.users

class User

is moved to

src/main/scala/vim/users/*.scala

and

package vim
package users

package object linux {
  class User
}

is moved to

src/main/scala/vim/users/linux/*.scala

and

package users {
  package vim {
    class NormalUser
  }
  package emacs {
    class AbnormalUser
  }
}

is moved to

src/main/scala/users/*.scala