Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sbt parsing logic moved from SbtShowCommand to another trait
- Loading branch information
Showing
4 changed files
with
111 additions
and
66 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/scala/ru/d10xa/jadd/code/scalameta/SbtArtifactsParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ru.d10xa.jadd.code.scalameta | ||
|
||
import cats.Applicative | ||
import coursier.core.Version | ||
import ru.d10xa.jadd.core.Artifact | ||
import ru.d10xa.jadd.core.Scope | ||
import ru.d10xa.jadd.core.types.GroupId | ||
import ru.d10xa.jadd.core.types.ScalaVersion | ||
|
||
import scala.meta.Source | ||
import scala.meta.Term | ||
import cats.syntax.all._ | ||
|
||
trait SbtArtifactsParser[F[_]] { | ||
def parse( | ||
scalaVersion: ScalaVersion, | ||
sources: Vector[Source] | ||
): F[Vector[Artifact]] | ||
} | ||
|
||
object SbtArtifactsParser { | ||
def make[F[_]: Applicative]( | ||
sbtModuleIdFinder: SbtModuleIdFinder, | ||
sbtStringValFinder: SbtStringValFinder | ||
): F[SbtArtifactsParser[F]] = { | ||
def parsePure( | ||
scalaVersion: ScalaVersion, | ||
sources: Vector[Source] | ||
): Vector[Artifact] = { | ||
val moduleIds: Vector[ModuleId] = | ||
sources.flatMap(sbtModuleIdFinder.find).distinct | ||
val stringValsMap: Map[String, String] = sources | ||
.flatMap(sbtStringValFinder.find) | ||
.map { case StringVal(name, value) => name -> value } | ||
.toMap | ||
moduleIds.map(m => | ||
Artifact( | ||
groupId = GroupId(m.groupId), | ||
artifactId = if (m.percentsCount > 1) { | ||
s"${m.artifactId}%%" | ||
} else m.artifactId, | ||
maybeVersion = m.version match { | ||
case VersionString(v) => Version(v).some | ||
case VersionVal(v) => stringValsMap.get(v).map(Version(_)) | ||
}, | ||
maybeScalaVersion = if (m.percentsCount > 1) { | ||
scalaVersion.some | ||
} else None, | ||
scope = m.terms | ||
.find { | ||
case Term.Name("Test") => true | ||
case _ => false | ||
} | ||
.map(_ => Scope.Test) | ||
) | ||
) | ||
} | ||
new SbtArtifactsParser[F] { | ||
override def parse( | ||
scalaVersion: ScalaVersion, | ||
sources: Vector[Source] | ||
): F[Vector[Artifact]] = parsePure(scalaVersion, sources).pure[F] | ||
}.pure[F] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters