|
| 1 | +package me.ptrdom.sbt.scripted.sources |
| 2 | + |
| 3 | +import java.nio.file.Files |
| 4 | +import java.nio.file.Path |
| 5 | + |
| 6 | +import sbt.* |
| 7 | +import sbt.AutoPlugin |
| 8 | +import sbt.Keys.* |
| 9 | +import sbt.ScriptedPlugin |
| 10 | +import sbt.ScriptedPlugin.autoImport.sbtTestDirectory |
| 11 | +import sbt.ScriptedPlugin.autoImport.scripted |
| 12 | +import sbt.internal.util.ManagedLogger |
| 13 | +import sbt.nio.Keys.watchTriggers |
| 14 | + |
| 15 | +import scala.io.Source |
| 16 | +import scala.jdk.CollectionConverters.* |
| 17 | +import scala.util.Using |
| 18 | + |
| 19 | +object ScriptedSourcesPlugin extends AutoPlugin { |
| 20 | + override def requires = ScriptedPlugin |
| 21 | + |
| 22 | + object autoImport { |
| 23 | + val scriptedSourcesSync = |
| 24 | + taskKey[Boolean]("Sync scripted tests with their sources") |
| 25 | + val scriptedSourcesCheck = |
| 26 | + taskKey[Unit]("Check if scripted tests and their sources are in sync") |
| 27 | + } |
| 28 | + |
| 29 | + import autoImport.* |
| 30 | + |
| 31 | + private val sourcesConfigFileName = ".sources" |
| 32 | + |
| 33 | + private def processScriptedSources[T]( |
| 34 | + log: Logger |
| 35 | + )( |
| 36 | + baseDirectoryV: File, |
| 37 | + sbtTestDirectoryV: File |
| 38 | + )( |
| 39 | + handler: Iterator[(Path, Set[sbt.File])] => T |
| 40 | + ): T = { |
| 41 | + Using( |
| 42 | + Files.walk(sbtTestDirectoryV.toPath) |
| 43 | + ) { stream => |
| 44 | + val dataForHandler = stream |
| 45 | + .iterator() |
| 46 | + .asScala |
| 47 | + .flatMap { testDirectory => |
| 48 | + val sourcesConfig = |
| 49 | + new File(testDirectory.toFile, sourcesConfigFileName) |
| 50 | + if (!sourcesConfig.exists()) { |
| 51 | + log.debug( |
| 52 | + s"scripted sources config missing [${sourcesConfig.absolutePath}]" |
| 53 | + ) |
| 54 | + List.empty |
| 55 | + } else { |
| 56 | + val sourcesForTest: Set[File] = |
| 57 | + Using(Source.fromFile(sourcesConfig)) { source => |
| 58 | + source |
| 59 | + .getLines() |
| 60 | + .map { sourceForTest => |
| 61 | + val sourceForTestDirectory = baseDirectoryV / sourceForTest |
| 62 | + if (!sourceForTestDirectory.exists()) { |
| 63 | + sys.error( |
| 64 | + s"Source for test is missing [${sourceForTestDirectory.getAbsolutePath}]" |
| 65 | + ) |
| 66 | + } else { |
| 67 | + log.debug( |
| 68 | + s"Source for test [${sourceForTestDirectory.getAbsolutePath}] exists" |
| 69 | + ) |
| 70 | + sourceForTestDirectory |
| 71 | + } |
| 72 | + } |
| 73 | + .toSet |
| 74 | + }.fold( |
| 75 | + ex => |
| 76 | + throw new RuntimeException( |
| 77 | + s"Failed to read source config [${sourcesConfig.getAbsolutePath}]", |
| 78 | + ex |
| 79 | + ), |
| 80 | + identity |
| 81 | + ) |
| 82 | + List((testDirectory, sourcesForTest)) |
| 83 | + } |
| 84 | + } |
| 85 | + handler(dataForHandler) |
| 86 | + } |
| 87 | + .fold(ex => throw ex, identity) |
| 88 | + } |
| 89 | + |
| 90 | + private def runScriptedSource( |
| 91 | + dry: Boolean, |
| 92 | + log: ManagedLogger |
| 93 | + )(baseDirectoryV: File, sbtTestDirectoryV: File): Boolean = { |
| 94 | + processScriptedSources(log)(baseDirectoryV, sbtTestDirectoryV)(_.flatMap { |
| 95 | + case (testDirectory, sourcesForTest) => |
| 96 | + sourcesForTest.map(sourceForTest => (testDirectory, sourceForTest)) |
| 97 | + } |
| 98 | + .foldLeft(true) { case (pristine, (testDirectory, sourceForTest)) => |
| 99 | + Using( |
| 100 | + Files |
| 101 | + .walk(sourceForTest.toPath) |
| 102 | + )( |
| 103 | + _.iterator().asScala |
| 104 | + .map(_.toFile) |
| 105 | + .filter( |
| 106 | + _.isFile |
| 107 | + ) |
| 108 | + .foldLeft(pristine) { case (pristine, file) => |
| 109 | + val targetFile = new File( |
| 110 | + file.getAbsolutePath.replace( |
| 111 | + sourceForTest.getAbsolutePath, |
| 112 | + testDirectory.toFile.getAbsolutePath |
| 113 | + ) |
| 114 | + ) |
| 115 | + if ( |
| 116 | + !Hash(file).sameElements( |
| 117 | + Hash(targetFile) |
| 118 | + ) || !targetFile.exists() |
| 119 | + ) { |
| 120 | + log.debug( |
| 121 | + s"File changed [${file.getAbsolutePath}], copying to [${targetFile.getAbsolutePath}]" |
| 122 | + ) |
| 123 | + if (!dry) { |
| 124 | + IO.copyFile( |
| 125 | + file, |
| 126 | + targetFile |
| 127 | + ) |
| 128 | + } |
| 129 | + false |
| 130 | + } else { |
| 131 | + log.debug( |
| 132 | + s"File not changed [${file.getAbsolutePath}]" |
| 133 | + ) |
| 134 | + pristine |
| 135 | + } |
| 136 | + } |
| 137 | + ).fold(ex => throw ex, identity) |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + override lazy val projectSettings: Seq[Setting[?]] = Seq( |
| 142 | + scriptedSourcesSync := { |
| 143 | + val log = streams.value.log |
| 144 | + |
| 145 | + log.debug("Running scripted sources sync") |
| 146 | + |
| 147 | + val baseDirectoryV = baseDirectory.value |
| 148 | + val sbtTestDirectoryV = sbtTestDirectory.value |
| 149 | + |
| 150 | + runScriptedSource(dry = false, log)(baseDirectoryV, sbtTestDirectoryV) |
| 151 | + }, |
| 152 | + scriptedSourcesCheck := { |
| 153 | + val log = streams.value.log |
| 154 | + |
| 155 | + log.debug("Running scripted sources check") |
| 156 | + |
| 157 | + val baseDirectoryV = baseDirectory.value |
| 158 | + val sbtTestDirectoryV = sbtTestDirectory.value |
| 159 | + |
| 160 | + val pristine = |
| 161 | + runScriptedSource(dry = true, log)(baseDirectoryV, sbtTestDirectoryV) |
| 162 | + if (!pristine) { |
| 163 | + sys.error("Scripted sources not in sync!") |
| 164 | + } |
| 165 | + }, |
| 166 | + scripted := scripted.dependsOn(scriptedSourcesSync).evaluated, |
| 167 | + scripted / watchTriggers ++= { |
| 168 | + val log = Keys.sLog.value |
| 169 | + |
| 170 | + log.debug("Setting scripted sources as watch triggers") |
| 171 | + |
| 172 | + val baseDirectoryV = baseDirectory.value |
| 173 | + val sbtTestDirectoryV = sbtTestDirectory.value |
| 174 | + |
| 175 | + processScriptedSources(log)(baseDirectoryV, sbtTestDirectoryV)( |
| 176 | + _.flatMap { case (_, sourcesForTest) => |
| 177 | + sourcesForTest |
| 178 | + .map { sourceForTest => |
| 179 | + Glob(sourceForTest, RecursiveGlob) |
| 180 | + } |
| 181 | + }.toList |
| 182 | + ) |
| 183 | + } |
| 184 | + ) |
| 185 | +} |
0 commit comments