Skip to content

Commit

Permalink
Implement JsonDependencyRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Sep 27, 2018
1 parent e5267e9 commit f215995
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package eu.timepit.scalasteward.dependency

import io.circe.Decoder
import io.circe.generic.semiauto._
import io.circe.{Decoder, Encoder}

final case class Dependency(
groupId: String,
Expand All @@ -30,4 +30,7 @@ final case class Dependency(
object Dependency {
implicit val dependencyDecoder: Decoder[Dependency] =
deriveDecoder

implicit val dependencyEncoder: Encoder[Dependency] =
deriveEncoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,44 @@

package eu.timepit.scalasteward.dependency.json

import better.files.File
import cats.MonadError
import cats.implicits._
import eu.timepit.scalasteward.application.WorkspaceService
import eu.timepit.scalasteward.dependency.{Dependency, DependencyRepository}
import eu.timepit.scalasteward.git.Sha1
import eu.timepit.scalasteward.github.data.Repo
import eu.timepit.scalasteward.io.FileService
import io.circe.parser.decode
import io.circe.syntax._

class JsonDependencyRepository[F[_]](
fileService: FileService[F],
workspaceService: WorkspaceService[F]
) extends DependencyRepository[F] {

workspaceService.root

// file operations
)(implicit F: MonadError[F, Throwable])
extends DependencyRepository[F] {

override def findSha1(repo: Repo): F[Option[Sha1]] =
???
readJson.map(_.store.get(repo).map(_.sha1))

override def setDependencies(repo: Repo, sha1: Sha1, dependencies: List[Dependency]): F[Unit] =
???
readJson.flatMap { store =>
val updated = store.store.updated(repo, RepoValues(sha1, dependencies))
writeJson(Store(updated))
}

def jsonFile: F[File] =
workspaceService.root.map(_ / "repos.json")

def readJson: F[Store] =
jsonFile.flatMap { file =>
fileService.readFile(file).flatMap { content =>
F.fromEither(decode[Store](content))
}
}

def writeJson(store: Store): F[Unit] =
jsonFile.flatMap { file =>
fileService.writeFile(file, store.asJson.toString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@ package eu.timepit.scalasteward.dependency.json

import eu.timepit.scalasteward.dependency.Dependency
import eu.timepit.scalasteward.git.Sha1
import io.circe.generic.semiauto._
import io.circe.{Decoder, Encoder}

final case class RepoValues(sha1: Sha1, dependencies: List[Dependency])

object RepoValues {
implicit val repoValuesDecoder: Decoder[RepoValues] =
deriveDecoder

implicit val repoValuesEncoder: Encoder[RepoValues] =
deriveEncoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@
package eu.timepit.scalasteward.dependency.json

import eu.timepit.scalasteward.github.data.Repo
import io.circe.generic.semiauto._
import io.circe.{Decoder, Encoder}

final case class Store(store: Map[Repo, RepoValues])

object Store {
implicit val storeDecoder: Decoder[Store] =
deriveDecoder

implicit val storeEncoder: Encoder[Store] =
deriveEncoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package eu.timepit.scalasteward.git

import cats.Eq
import cats.implicits._
import io.circe.Decoder
import io.circe.{Decoder, Encoder}

final case class Sha1(value: String)

Expand All @@ -28,4 +28,7 @@ object Sha1 {

implicit val sha1Decoder: Decoder[Sha1] =
Decoder[String].map(Sha1.apply)

implicit val sha1Encoder: Encoder[Sha1] =
Encoder[String].contramap(_.value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,24 @@

package eu.timepit.scalasteward.github.data

import io.circe.{KeyDecoder, KeyEncoder}

final case class Repo(
owner: String,
repo: String
) {
def show: String = s"$owner:$repo"
}

object Repo {
implicit val repoKeyDecoder: KeyDecoder[Repo] =
KeyDecoder.instance { key =>
val parts = key.split('/')
if (parts.length == 2 && parts.forall(_.nonEmpty))
Some(Repo(parts(0), parts(1)))
else None
}

implicit val repoKeyEncoder: KeyEncoder[Repo] =
KeyEncoder.instance(repo => s"${repo.owner}/${repo.repo}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2018 scala-steward contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.timepit.scalasteward.io

import better.files.File

trait FileService[F[_]] {
def readFile(file: File): F[String]

def writeFile(file: File, content: String): F[Unit]
}

0 comments on commit f215995

Please sign in to comment.