1

Assume I have a library a.com. Everytime and in each file, I need to import a lot of package like

import a.com._
import a.com.b._
import a.com.c
import a.com.Implicits._

I don't want to write these code every time in each file of another project.

Also if I want to change a.com to a.net, I have to change every file.

Is there anyway to prevent this?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
worldterminator
  • 2,968
  • 6
  • 33
  • 52

1 Answers1

0

You can generate sources

build.sbt

lazy val commonSettings = Seq(
  scalaVersion := "2.13.1",
)

lazy val in = project
  .settings(
    commonSettings,
  )

lazy val out = project
  .settings(
    sourceGenerators in Compile += Def.task {
      Generator.gen(
        inputDir  = sourceDirectory.in(in, Compile).value,
        outputDir = sourceManaged.in(Compile).value
      )
    }.taskValue,

    commonSettings,
  )

project/build.sbt

libraryDependencies += "org.scalameta" %% "scalameta" % "4.2.3"

project/Generator.scala

import sbt._

object Generator {
  def gen(inputDir: File, outputDir: File): Seq[File] = {
    val finder: PathFinder = inputDir ** "*.scala"

    for(inputFile <- finder.get) yield {
      val inputStr = IO.read(inputFile)
      val outputFile = outputDir / inputFile.toURI.toString.stripPrefix(inputDir.toURI.toString)
      val outputStr = Transformer.transform(inputStr)
      IO.write(outputFile, outputStr)
      outputFile
    }
  }
}

project/Transformer.scala

import scala.meta._

object Transformer {
  def transform(input: String): String = transform(input.parse[Source].get).toString

  def transform(input: Tree): Tree = input match {
    case source"..${List(q"package $eref { ..$stats }")}" =>
      q"""package $eref {
         import a.com._
         import a.com.b._
         import a.com.c
         import a.com.Implicits._
         ..$stats
      }"""
  }
}

in/src/main/scala/com/example/App.scala

package com.example

object App {

}

out/target/scala-2.13/src_managed/main/scala/com/example/App.scala (after sbt "; project out; clean; compile")

package com.example
import a.com._
import a.com.b._
import a.com.c
import a.com.Implicits._
object App
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • I feel code generation is not friendly to IDE. Maybe not the best way to solve this problem. – worldterminator Nov 01 '19 at 10:30
  • @worldterminator Well, I don't insist that this is the best way to solve this problem. Actually I would agree with cchantep's comment. But to clearify, why do you consider code generation not friendly to IDE? I would agree that macros/macro annotations/compiler plugins are not friendly to IDE. But why code generation? I guess IDE indexes scalameta-generated sources well. Or maybe I misunderstand something. – Dmytro Mitin Nov 01 '19 at 10:36
  • @worldterminator This is not (deprecated) scalameta macros, this is scalameta code generation. – Dmytro Mitin Nov 01 '19 at 10:42
  • Well, I guess that's depends on IDE. Usually when we write code `object App` without writing `importing ...`, I guess IDE won't figure it out that those `import` should be "attached" at the head of the source code. – worldterminator Nov 01 '19 at 10:56
  • I did think out of one way, which is `creating the package object in a.com, use macro or other code generation to alias all types/constructor/constants/implicts into package object`, still not a quite nice method. I only export several things into package object manually – worldterminator Nov 01 '19 at 10:59