|
| 1 | +package scala.build |
| 2 | + |
| 3 | +import scala.build.internal.CodeWrapper |
| 4 | +import scala.build.options.{BuildOptions, BuildRequirements, HasBuildRequirements} |
| 5 | +import scala.build.preprocessing._ |
| 6 | + |
| 7 | +final case class CrossSources( |
| 8 | + paths: Seq[HasBuildRequirements[(os.Path, os.RelPath)]], |
| 9 | + inMemory: Seq[HasBuildRequirements[(Either[String, os.Path], os.RelPath, String, Int)]], |
| 10 | + mainClass: Option[String], |
| 11 | + resourceDirs: Seq[os.Path], |
| 12 | + buildOptions: Seq[HasBuildRequirements[(Either[String, os.Path], BuildOptions)]] |
| 13 | +) { |
| 14 | + |
| 15 | + def sources(baseOptions: BuildOptions): Sources = { |
| 16 | + |
| 17 | + val sharedOptions = buildOptions |
| 18 | + .filter(_.requirements.isEmpty) |
| 19 | + .map(_.value._2) |
| 20 | + .foldLeft(baseOptions)(_ orElse _) |
| 21 | + |
| 22 | + val retainedScalaVersion = sharedOptions.scalaParams.scalaVersion |
| 23 | + |
| 24 | + val buildOptionsWithScalaVersion = buildOptions |
| 25 | + .flatMap(_.withScalaVersion(retainedScalaVersion).toSeq) |
| 26 | + .filter(_.requirements.isEmpty) |
| 27 | + .map(_.value._2) |
| 28 | + .foldLeft(sharedOptions)(_ orElse _) |
| 29 | + |
| 30 | + val platform = |
| 31 | + if (buildOptionsWithScalaVersion.scalaJsOptions.enable) |
| 32 | + BuildRequirements.Platform.JS |
| 33 | + else if (buildOptionsWithScalaVersion.scalaNativeOptions.enable) |
| 34 | + BuildRequirements.Platform.Native |
| 35 | + else |
| 36 | + BuildRequirements.Platform.JVM |
| 37 | + |
| 38 | + // FIXME Not 100% sure the way we compute the intermediate and final BuildOptions |
| 39 | + // is consistent (we successively filter out / retain options to compute a scala |
| 40 | + // version and platform, which might not be the version and platform of the final |
| 41 | + // BuildOptions). |
| 42 | + |
| 43 | + Sources( |
| 44 | + paths |
| 45 | + .flatMap(_.withScalaVersion(retainedScalaVersion).toSeq) |
| 46 | + .flatMap(_.withPlatform(platform).toSeq) |
| 47 | + .map(_.value), |
| 48 | + inMemory |
| 49 | + .flatMap(_.withScalaVersion(retainedScalaVersion).toSeq) |
| 50 | + .flatMap(_.withPlatform(platform).toSeq) |
| 51 | + .map(_.value), |
| 52 | + mainClass, |
| 53 | + resourceDirs, |
| 54 | + buildOptions |
| 55 | + .flatMap(_.withScalaVersion(retainedScalaVersion).toSeq) |
| 56 | + .flatMap(_.withPlatform(platform).toSeq) |
| 57 | + .map(_.value._2) |
| 58 | + .foldLeft(BuildOptions() /* not baseOptions */ )(_ orElse _) |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +object CrossSources { |
| 65 | + |
| 66 | + def forInputs( |
| 67 | + inputs: Inputs, |
| 68 | + preprocessors: Seq[Preprocessor] |
| 69 | + ): CrossSources = { |
| 70 | + |
| 71 | + val preprocessedSources = inputs.flattened().flatMap { elem => |
| 72 | + preprocessors.iterator.flatMap(p => p.preprocess(elem).iterator).toStream.headOption |
| 73 | + .getOrElse(Nil) // FIXME Warn about unprocessed stuff? |
| 74 | + } |
| 75 | + |
| 76 | + val buildOptions = preprocessedSources.flatMap { |
| 77 | + case d: PreprocessedSource.OnDisk => |
| 78 | + d.options.toSeq.map { opt => |
| 79 | + HasBuildRequirements( |
| 80 | + d.requirements.getOrElse(BuildRequirements()), |
| 81 | + (Right(d.path), opt) |
| 82 | + ) |
| 83 | + } |
| 84 | + case m: PreprocessedSource.InMemory => |
| 85 | + m.options.toSeq.map { opt => |
| 86 | + HasBuildRequirements( |
| 87 | + m.requirements.getOrElse(BuildRequirements()), |
| 88 | + (m.reportingPath, opt) |
| 89 | + ) |
| 90 | + } |
| 91 | + case n: PreprocessedSource.NoSourceCode => |
| 92 | + val elem = HasBuildRequirements( |
| 93 | + n.requirements.getOrElse(BuildRequirements()), |
| 94 | + (Right(n.path), n.options.getOrElse(BuildOptions())) |
| 95 | + ) |
| 96 | + Seq(elem) |
| 97 | + case _ => |
| 98 | + Nil |
| 99 | + } |
| 100 | + |
| 101 | + val mainClassOpt = inputs.mainClassElement |
| 102 | + .collect { |
| 103 | + case elem: Inputs.SingleElement => |
| 104 | + preprocessors.iterator |
| 105 | + .flatMap(p => p.preprocess(elem).iterator) |
| 106 | + .toStream.headOption |
| 107 | + .getOrElse(Nil) |
| 108 | + .flatMap(_.mainClassOpt.toSeq) |
| 109 | + .headOption |
| 110 | + } |
| 111 | + .flatten |
| 112 | + |
| 113 | + val paths = preprocessedSources.collect { |
| 114 | + case d: PreprocessedSource.OnDisk => |
| 115 | + HasBuildRequirements( |
| 116 | + d.requirements.getOrElse(BuildRequirements()), |
| 117 | + (d.path, d.path.relativeTo(inputs.workspace)) |
| 118 | + ) |
| 119 | + } |
| 120 | + val inMemory = preprocessedSources.collect { |
| 121 | + case m: PreprocessedSource.InMemory => |
| 122 | + HasBuildRequirements( |
| 123 | + m.requirements.getOrElse(BuildRequirements()), |
| 124 | + (m.reportingPath, m.relPath, m.code, m.ignoreLen) |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + val resourceDirs = inputs.elements.collect { |
| 129 | + case r: Inputs.ResourceDirectory => |
| 130 | + r.path |
| 131 | + } |
| 132 | + |
| 133 | + CrossSources(paths, inMemory, mainClassOpt, resourceDirs, buildOptions) |
| 134 | + } |
| 135 | +} |
0 commit comments