|
| 1 | +package higherkindness.rules_scala |
| 2 | +package workers.common |
| 3 | + |
| 4 | +import com.google.devtools.build.buildjar.jarhelper.JarHelper |
| 5 | +import java.io.{File, InputStream, OutputStream, OutputStreamWriter} |
| 6 | +import java.nio.charset.StandardCharsets |
| 7 | +import java.nio.file.{Files, NoSuchFileException, Path, Paths} |
| 8 | +import java.nio.file.attribute.FileTime |
| 9 | +import java.util |
| 10 | +import java.util.concurrent.ConcurrentHashMap |
| 11 | +import java.util.LinkedHashMap |
| 12 | +import java.util.zip.{GZIPInputStream, GZIPOutputStream} |
| 13 | +import java.util.Optional |
| 14 | +import sbt.internal.inc.binary.converters.{ProtobufReaders, ProtobufWriters} |
| 15 | +import sbt.internal.inc.Schema.Type.{Projection, Structure} |
| 16 | +import sbt.internal.inc.{APIs, Analysis, FarmHash, Hash, LastModified, PlainVirtualFile, PlainVirtualFileConverter, Relations, Schema, SourceInfos, Stamp => StampImpl, Stamper, Stamps} |
| 17 | +import sbt.internal.inc.Schema.{Access, AnalyzedClass, Annotation, AnnotationArgument, ClassDefinition, ClassDependencies, ClassLike, Companions, MethodParameter, NameHash, ParameterList, Path => SchemaPath, Qualifier, Type, TypeParameter, UsedName, UsedNames, Values} |
| 18 | +import sbt.internal.shaded.com.google.protobuf.GeneratedMessageV3 |
| 19 | +import sbt.io.IO |
| 20 | +import scala.collection.immutable.TreeMap |
| 21 | +import xsbti.compile.analysis.{GenericMapper, ReadMapper, ReadWriteMappers, Stamp, WriteMapper} |
| 22 | +import xsbti.compile.{AnalysisContents, AnalysisStore, MiniSetup} |
| 23 | +import scala.jdk.CollectionConverters._ |
| 24 | +import xsbti.VirtualFileRef |
| 25 | +import java.util.Objects |
| 26 | + |
| 27 | +// TODO: fix git for this file. Make it a mv to keep history. |
| 28 | + |
| 29 | +object AnnexMapper { |
| 30 | + val rootPlaceholder = Paths.get("_ROOT_") |
| 31 | + def mappers(root: Path, isIncremental: Boolean) = { |
| 32 | + new ReadWriteMappers(new AnxReadMapper(root, isIncremental), new AnxWriteMapper(root)) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets a reproducible/consistent stamp that we can write to the analysis file and end up with reproducible output |
| 37 | + * across machines, jvms, builds, etc. |
| 38 | + * |
| 39 | + * Practically speaking, all we're doing is setting the timestamp in LastModified stamps to a constant value. |
| 40 | + */ |
| 41 | + final def getConsistentWriteStamp(stamp: Stamp): Stamp = { |
| 42 | + stamp match { |
| 43 | + case farmHash: FarmHash => farmHash |
| 44 | + case hash: Hash => hash |
| 45 | + case lastModified: LastModified => new LastModified(JarHelper.DEFAULT_TIMESTAMP) |
| 46 | + case _ => throw new Exception("Unexpected Stamp type encountered when writing.") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + final def getReadStamp(file: VirtualFileRef, stamp: Stamp, isIncremental: Boolean): Stamp = { |
| 51 | + if (isIncremental) { |
| 52 | + getIncrementalModeReadStamp(file, stamp) |
| 53 | + } else { |
| 54 | + stamp |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * When in incremental mode we do not want to rely on the timestamp from the AnalysisStore because we're assuming it |
| 60 | + * was set to a constant value when written to the AnalysisStore. |
| 61 | + * |
| 62 | + * Instead, for any LastModified stamps, we read the file's time stamp from disk. |
| 63 | + */ |
| 64 | + final def getIncrementalModeReadStamp(file: VirtualFileRef, stamp: Stamp): Stamp = { |
| 65 | + stamp match { |
| 66 | + case farmHash: FarmHash => farmHash |
| 67 | + case hash: Hash => hash |
| 68 | + case lastModified: LastModified => { |
| 69 | + Stamper.forLastModifiedP(PlainVirtualFileConverter.converter.toPath(file)) |
| 70 | + } |
| 71 | + case _ => throw new Exception("Unexpected Stamp type encountered when reading") |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +final class AnxWriteMapper(root: Path) extends WriteMapper { |
| 77 | + private[this] val rootAbs = root.toAbsolutePath |
| 78 | + |
| 79 | + private[this] def mapFile(path: Path): Path = { |
| 80 | + if (path.startsWith(rootAbs)) { |
| 81 | + AnnexMapper.rootPlaceholder.resolve(rootAbs.relativize(path)) |
| 82 | + } else { |
| 83 | + path |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private[this] def mapFile(virtualFileRef: VirtualFileRef): Path = { |
| 88 | + mapFile(PlainVirtualFileConverter.converter.toPath(virtualFileRef)) |
| 89 | + } |
| 90 | + |
| 91 | + override def mapSourceFile(sourceFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(sourceFile)) |
| 92 | + override def mapBinaryFile(binaryFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(binaryFile)) |
| 93 | + override def mapProductFile(productFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(productFile)) |
| 94 | + |
| 95 | + override def mapClasspathEntry(classpathEntry: Path): Path = mapFile(classpathEntry) |
| 96 | + override def mapJavacOption(javacOption: String): String = javacOption |
| 97 | + override def mapScalacOption(scalacOption: String): String = scalacOption |
| 98 | + |
| 99 | + override def mapOutputDir(outputDir: Path): Path = mapFile(outputDir) |
| 100 | + override def mapSourceDir(sourceDir: Path): Path = mapFile(sourceDir) |
| 101 | + |
| 102 | + override def mapSourceStamp(file: VirtualFileRef, sourceStamp: Stamp): Stamp = { |
| 103 | + AnnexMapper.getConsistentWriteStamp(sourceStamp) |
| 104 | + } |
| 105 | + override def mapBinaryStamp(file: VirtualFileRef, binaryStamp: Stamp): Stamp = { |
| 106 | + AnnexMapper.getConsistentWriteStamp(binaryStamp) |
| 107 | + } |
| 108 | + override def mapProductStamp(file: VirtualFileRef, productStamp: Stamp): Stamp = { |
| 109 | + AnnexMapper.getConsistentWriteStamp(productStamp) |
| 110 | + } |
| 111 | + |
| 112 | + override def mapMiniSetup(miniSetup: MiniSetup): MiniSetup = miniSetup |
| 113 | +} |
| 114 | + |
| 115 | +final class AnxReadMapper(root: Path, isIncremental: Boolean) extends ReadMapper { |
| 116 | + private[this] val rootAbs = root.toAbsolutePath |
| 117 | + |
| 118 | + private[this] def mapFile(virtualFileRef: VirtualFileRef): Path = { |
| 119 | + mapFile(PlainVirtualFileConverter.converter.toPath(virtualFileRef)) |
| 120 | + } |
| 121 | + |
| 122 | + private[this] def mapFile(path: Path): Path = { |
| 123 | + if (path.startsWith(AnnexMapper.rootPlaceholder)) { |
| 124 | + rootAbs.resolve(AnnexMapper.rootPlaceholder.relativize(path)) |
| 125 | + } else { |
| 126 | + path |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + override def mapSourceFile(sourceFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(sourceFile)) |
| 131 | + override def mapBinaryFile(binaryFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(binaryFile)) |
| 132 | + override def mapProductFile(productFile: VirtualFileRef): VirtualFileRef = PlainVirtualFile(mapFile(productFile)) |
| 133 | + |
| 134 | + override def mapClasspathEntry(classpathEntry: Path): Path = mapFile(classpathEntry) |
| 135 | + override def mapJavacOption(javacOption: String): String = javacOption |
| 136 | + override def mapScalacOption(scalacOption: String): String = scalacOption |
| 137 | + |
| 138 | + override def mapOutputDir(outputDir: Path): Path = mapFile(outputDir) |
| 139 | + override def mapSourceDir(sourceDir: Path): Path = mapFile(sourceDir) |
| 140 | + |
| 141 | + override def mapSourceStamp(file: VirtualFileRef, sourceStamp: Stamp): Stamp = { |
| 142 | + AnnexMapper.getReadStamp(file, sourceStamp, isIncremental) |
| 143 | + } |
| 144 | + override def mapBinaryStamp(file: VirtualFileRef, binaryStamp: Stamp): Stamp = { |
| 145 | + AnnexMapper.getReadStamp(file, binaryStamp, isIncremental) |
| 146 | + } |
| 147 | + override def mapProductStamp(file: VirtualFileRef, productStamp: Stamp): Stamp = { |
| 148 | + AnnexMapper.getReadStamp(file, productStamp, isIncremental) |
| 149 | + } |
| 150 | + |
| 151 | + override def mapMiniSetup(miniSetup: MiniSetup): MiniSetup = miniSetup |
| 152 | +} |
0 commit comments