|
| 1 | +package mill.internal |
| 2 | + |
| 3 | +import java.nio.file.Files |
| 4 | + |
| 5 | +object MillPathSerializer { |
| 6 | + def setupSymlinks(wd: os.Path, workspace: os.Path): Unit = { |
| 7 | + for ((base, link) <- defaultMapping(workspace)) { |
| 8 | + os.makeDir.all(wd / link / "..") |
| 9 | + os.remove(wd / link) |
| 10 | + Files.createSymbolicLink((wd / link).toNIO, base.wrapped) |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + def defaultMapping(workspace: os.Path): Seq[(os.Path, os.SubPath)] = Seq( |
| 15 | + workspace -> os.sub / "out/mill-workspace", |
| 16 | + os.home -> os.sub / "out/mill-home" |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +class MillPathSerializer(mapping: Seq[(os.Path, os.SubPath)]) extends os.Path.Serializer { |
| 21 | + |
| 22 | + private def mapPathPrefixes(path: os.Path, mapping: Seq[(os.Path, os.SubPath)]): os.FilePath = { |
| 23 | + mapping |
| 24 | + .collectFirst { case (from, to) if path.startsWith(from) => to / path.subRelativeTo(from) } |
| 25 | + .getOrElse(path) |
| 26 | + } |
| 27 | + |
| 28 | + private def mapPathPrefixes( |
| 29 | + path: java.nio.file.Path, |
| 30 | + mapping: Seq[(os.SubPath, os.Path)] |
| 31 | + ): java.nio.file.Path = { |
| 32 | + mapping |
| 33 | + .collectFirst { |
| 34 | + case (from, to) if path.startsWith(from.toNIO) || from.segments.isEmpty => |
| 35 | + to.wrapped.resolve( |
| 36 | + // java.nio.Path#relativize misbehaves on empty paths |
| 37 | + if (from.segments.isEmpty) path else from.toNIO.relativize(path) |
| 38 | + ) |
| 39 | + } |
| 40 | + .getOrElse(path) |
| 41 | + } |
| 42 | + |
| 43 | + def normalizePath(path: os.Path): os.FilePath = mapPathPrefixes(path, mapping) |
| 44 | + |
| 45 | + def denormalizePath(path: java.nio.file.Path): java.nio.file.Path = |
| 46 | + mapPathPrefixes(path, mapping.map(_.swap)) |
| 47 | + |
| 48 | + override def serializeString(p: os.Path): String = normalizePath(p) match { |
| 49 | + case p: os.SubPath => p.toNIO.toString |
| 50 | + case p: os.Path => p.wrapped.toString |
| 51 | + case p: os.RelPath => p.toNIO.toString |
| 52 | + } |
| 53 | + |
| 54 | + override def serializeFile(p: os.Path): java.io.File = normalizePath(p) match { |
| 55 | + case p: os.SubPath => p.toNIO.toFile |
| 56 | + case p: os.Path => p.wrapped.toFile |
| 57 | + case p: os.RelPath => p.toNIO.toFile |
| 58 | + } |
| 59 | + |
| 60 | + override def serializePath(p: os.Path): java.nio.file.Path = normalizePath(p) match { |
| 61 | + case p: os.SubPath => p.toNIO |
| 62 | + case p: os.Path => p.wrapped |
| 63 | + case p: os.RelPath => p.toNIO |
| 64 | + } |
| 65 | + |
| 66 | + override def deserialize(s: String): java.nio.file.Path = |
| 67 | + denormalizePath(os.Path.defaultPathSerializer.deserialize(s)) |
| 68 | + |
| 69 | + override def deserialize(s: java.io.File): java.nio.file.Path = |
| 70 | + denormalizePath(os.Path.defaultPathSerializer.deserialize(s)) |
| 71 | + |
| 72 | + override def deserialize(s: java.nio.file.Path): java.nio.file.Path = |
| 73 | + denormalizePath(os.Path.defaultPathSerializer.deserialize(s)) |
| 74 | + |
| 75 | + override def deserialize(s: java.net.URI): java.nio.file.Path = |
| 76 | + denormalizePath(os.Path.defaultPathSerializer.deserialize(s)) |
| 77 | +} |
0 commit comments