Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

scala - 如何在Scala中合并多个导入?(How to merge multiple imports in scala?)

Assume I have a library a.com.

(假设我有一个图书馆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.

(另外,如果要将a.com更改为a.net ,也必须更改每个文件。)

Is there anyway to prevent this?

(反正有防止这种情况发生的方法吗?)

  ask by worldterminator translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can generate sources

(您可以生成源)

build.sbt

(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

(项目/build.sbt)

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

project/Generator.scala

(项目/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

(项目/ 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

(在/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" )

(out / target / scala-2.13 / src_managed / main / scala / com / example / App.scala (在sbt "; project out; clean; compile"之后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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...