Skip to content

Commit

Permalink
Make function unshortAll return F (#227)
Browse files Browse the repository at this point in the history
* Refactoring method unshortAll

* Remove unsafe version of unshortAll

* Remove unused import cats.effect.IO
  • Loading branch information
d10xa authored Aug 1, 2020
1 parent 2cda62a commit 9c9edaa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
39 changes: 25 additions & 14 deletions src/main/scala/ru/d10xa/jadd/core/Loader.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.d10xa.jadd.core

import cats.Functor
import cats.data.Ior
import cats.data.IorNel
import cats.effect.Sync
Expand All @@ -17,34 +18,44 @@ trait Loader[F[_]] {

class LiveLoader[F[_]: Sync] private (
artifactInfoFinder: ArtifactInfoFinder,
repositoryShortcuts: RepositoryShortcuts)
repositoryShortcuts: RepositoryShortcuts,
versionTools: VersionTools = VersionTools)
extends Loader[F] {

override def load(ctx: Ctx): F[IorNel[ArtifactTrouble, List[Artifact]]] =
Pipeline
.extractArtifacts(ctx)
.map(artifacts => loadByString(ctx, artifacts.toList))
.flatMap(artifacts => loadByString(ctx, artifacts.toList))

def loadByString(
def withScalaVersion[M[_]: Functor](
ctx: Ctx,
artifacts: List[String]): IorNel[ArtifactTrouble, List[Artifact]] = {
val withScalaVersion: Seq[Artifact] => Seq[Artifact] = _.map(
artifacts: M[Artifact]
): M[Artifact] =
artifacts.map(
u =>
if (u.isScala)
u.copy(
maybeScalaVersion =
u.maybeScalaVersion.orElse(ctx.meta.scalaVersion))
else u
)
val unshorted: Seq[Artifact] = Utils
.unshortAll(artifacts, artifactInfoFinder)
val repositoriesUnshorted: Seq[String] =
ctx.config.repositories.map(repositoryShortcuts.unshortRepository)
loadAllArtifacts(
withScalaVersion(unshorted),
VersionTools,
repositoriesUnshorted)
}

def loadByString(
ctx: Ctx,
artifacts: List[String]
): F[IorNel[ArtifactTrouble, List[Artifact]]] =
for {
unshorted <- Utils
.unshortAll(artifacts, artifactInfoFinder)
repositoriesUnshorted = ctx.config.repositories
.map(repositoryShortcuts.unshortRepository)
.toList
x = loadAllArtifacts(
withScalaVersion(ctx, unshorted),
versionTools,
repositoriesUnshorted)
} yield x

def loadAllArtifacts(
artifacts: Seq[Artifact],
versionTools: VersionTools,
Expand Down
36 changes: 19 additions & 17 deletions src/main/scala/ru/d10xa/jadd/core/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ru.d10xa.jadd.core
import java.nio.file.Path

import cats.ApplicativeError
import cats.effect.IO
import cats.effect.Resource
import cats.effect.Sync
import com.typesafe.scalalogging.StrictLogging
Expand All @@ -20,23 +19,26 @@ import scala.io.BufferedSource
import scala.io.Source

object Utils extends StrictLogging {
def unshortAll(

def unshortAll[F[_]: Sync](
rawDependencies: List[String],
artifactInfoFinder: ArtifactInfoFinder): List[Artifact] =
rawDependencies
.flatMap(
raw =>
artifactInfoFinder
.artifactFromString[IO](raw)
.unsafeRunSync() match { // TODO unsafeRunSync
case Right(artifact) => artifact :: Nil
case Left(_: ArtifactNotFoundByAlias) =>
logger.info(s"$raw - artifact not found by shortcut")
Nil
case Left(trouble) =>
logger.info(s"some error occurred $trouble")
Nil
})
artifactInfoFinder: ArtifactInfoFinder): F[List[Artifact]] =
rawDependencies.flatTraverse(s => unshortOne(s, artifactInfoFinder))

def unshortOne[F[_]: Sync](
raw: String,
artifactInfoFinder: ArtifactInfoFinder): F[List[Artifact]] =
artifactInfoFinder
.artifactFromString[F](raw)
.map {
case Right(artifact) => artifact :: Nil
case Left(_: ArtifactNotFoundByAlias) =>
logger.info(s"$raw - artifact not found by shortcut")
Nil
case Left(trouble) =>
logger.info(s"some error occurred $trouble")
Nil
}

def sourceFromSpringUri(string: String): BufferedSource =
if (string.startsWith("classpath:")) Source.fromResource(string.drop(10))
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/ru/d10xa/jadd/pipelines/Pipeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ object Pipeline {
def extractArtifacts[F[_]: Sync](ctx: Ctx): F[Seq[String]] =
for {
fromReq <- fromRequirements(ctx.config)
fromConf = fromConfig(ctx.config)
} yield fromReq.orElse(fromConf).getOrElse(Seq.empty)
} yield fromReq.orElse(fromConfig(ctx.config)).getOrElse(Seq.empty)

}

0 comments on commit 9c9edaa

Please sign in to comment.