All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mill.scalalib.UnresolvedPath.scala Maven / Gradle / Ivy

There is a newer version: 0.12.0-RC2-17-07e173
Show newest version
package mill.scalalib

import mill.define.{Segment, Segments}
import mill.eval.{EvaluatorPaths, EvaluatorPathsResolver}
import os.Path
import upickle.default.{ReadWriter, macroRW}

/**
 * An unresolved path is relative to some unspecified destination
 * which depends on the actual configuration at evaluation time.
 * Hence, you need to call [[#resolve]] with an instance of
 * [[mill.eval.EvaluatorPathsResolver]] to get the final [[os.Path]].
 */
sealed trait UnresolvedPath {
  def resolve(pathResolver: EvaluatorPathsResolver): Path
}
object UnresolvedPath {
  case class ResolvedPath private (path: String) extends UnresolvedPath {
    override def resolve(pathResolver: EvaluatorPathsResolver): Path = os.Path(path)
  }
  object ResolvedPath {
    def apply(path: os.Path): ResolvedPath = ResolvedPath(path.toString)

    implicit def upickleRW: ReadWriter[ResolvedPath] = macroRW
  }

  case class DestPath private (
      subPath: String,
      segments: Seq[String],
      foreignSegments: Option[Seq[String]] = None
  ) extends UnresolvedPath {
    override def resolve(pathResolver: EvaluatorPathsResolver): Path = {
      pathResolver.resolveDest(
        Segments(segments.map(Segment.Label(_))),
        foreignSegments.map(o => Segments(o.map(Segment.Label(_))))
      ).dest / os.SubPath(subPath)
    }
  }
  object DestPath {
    def apply(
        subPath: os.SubPath,
        segments: Segments,
        foreignSegments: Option[Segments]
    ): DestPath = {
      DestPath(
        subPath.toString(),
        EvaluatorPaths.makeSegmentStrings(segments),
        foreignSegments.map(EvaluatorPaths.makeSegmentStrings(_))
      )
    }

    implicit def upickleRW: ReadWriter[DestPath] = macroRW
  }

  implicit def upickleRW: ReadWriter[UnresolvedPath] =
    ReadWriter.merge(ResolvedPath.upickleRW, DestPath.upickleRW)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy