Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release][v.1.5] Load dependencies in one shot and mirror code refactoring #3

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: scala
scala:
- 2.10.6
- 2.11.11
- 2.12.8
script:
- sbt clean coverage test coverageReport
after_success:
- bash <(curl -s https://codecov.io/bash)
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[![Build Status](https://travis-ci.org/radicalbit/sbt-dependency-from-file.svg?branch=master)](https://travis-ci.org/radicalbit/sbt-dependency-from-file)

# sbt-dependency-from-file
`sbt-dependency-from-file` is a fresh-made library for dynamic load sbt dependencies using json files.

## Advantages

By using `sbt-dependency-from-file`, users will be able to dynamically change project dependencies by adding or overwriting a simple file; this comes really handy to automation software.

## Usage

In order to use `sbt-dependency-from-file` in a sbt project, add sbt plugin in `project/plugins.sbt`:

addSbtPlugin("io.radicalbit" % "sbt-dependency-from-file" % "1.5-SNAPSHOT")

## Example

Suppose that you want to load different sbt dependencies according to *specific environment*, you just need to define a json file that contains dependencies in the root folder of your project:

```json
[
{
"groupId":"org.typelevel",
"artifactId":"cats-core",
"version":"2.0.0-RC1",
"scalaVersion": "2.12",
"resolver":{
"name":"Local Maven Repository",
"url":".m2/repository",
}
}
]
```
And then, in your `build.sbt`:

```scala
dependenciesJsonPath := baseDirectory.value / "dev.json"
libraryDependencies ++= dependenciesFromJson.value.dependencies
```

## Authors
* **Francesco Frontera** - [[email protected]](mailto:[email protected]) [@francescofrontera](https://github.com/francescofrontera)
* **Mauro Cortellazzi** - [[email protected]](mailto:[email protected]) [@maocorte](https://github.com/maocorte)
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lazy val root = (project in file("."))
scalaVersion := "2.12.8",
name := "sbt-dependency-from-file",
organization := "io.radicalbit",
version := "1.0-SNAPSHOT",
version := "1.5-SNAPSHOT",
libraryDependencies ++= Dependencies.pluginDependencies,
sbtPlugin := true
)
Expand Down
7 changes: 7 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ object Dependencies {
lazy val core = namespace %% "play-json" % version
}

object catsEffect {
lazy val version = "1.3.0"
lazy val namespace = "org.typelevel"
lazy val core = namespace %% "cats-effect" % version
}

lazy val pluginDependencies = Seq(
playjson.core,
catsEffect.core,
scalatest.core % Test
)
}
5 changes: 5 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.6")

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")

addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.4.0")
56 changes: 56 additions & 0 deletions src/main/scala/io/radicalbit/DependenciesFromJsonPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 Radicalbit
*
* 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 io.radicalbit

import java.io.File

import cats.effect.IO
import io.radicalbit.extractor.Extractor
import io.radicalbit.models.DependenciesStructures
import sbt._

sealed trait KeysSetting {
lazy val dependenciesJsonPath = settingKey[File]("Dependencies file path")
lazy val dependenciesFromJson =
settingKey[DependenciesStructures]("Extracted information")
}

object DependenciesFromJsonPlugin extends AutoPlugin {
implicit val extractor: Extractor[IO] = Extractor.dependenciesExtractor

object autoImports extends KeysSetting
import autoImports._

override def trigger: PluginTrigger = noTrigger

override def requires = sbt.plugins.JvmPlugin

override def projectSettings: Seq[Def.Setting[_]] =
Seq(dependenciesFromJson := {
Extractor[IO]
.load(dependenciesJsonPath.value)
.use(extractDepResAndCred.run)
.unsafeRunSync()
})

private[this] def extractDepResAndCred =
for {
d <- Extractor[IO].extractedModuleId
r <- Extractor[IO].extractedResolvers
c <- Extractor[IO].extractedCredentials
} yield DependenciesStructures(d, r, c)
}

This file was deleted.

95 changes: 95 additions & 0 deletions src/main/scala/io/radicalbit/extractor/Extractor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2019 Radicalbit
*
* 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 io.radicalbit.extractor

import java.io.File

import cats.data.Kleisli
import cats.effect._
import cats.implicits._
import io.radicalbit.errors._
import io.radicalbit.models._
import play.api.libs.json.Json
import sbt.{ModuleID, MavenRepository, Credentials => SbtCredentials}

sealed trait Extractor[F[_]] {
def load(file: File): Resource[F, Seq[Dependency]]

def extractedResolvers: Kleisli[F, Seq[Dependency], Seq[MavenRepository]]

def extractedCredentials: Kleisli[F, Seq[Dependency], Seq[SbtCredentials]]

def extractedModuleId: Kleisli[F, Seq[Dependency], Seq[ModuleID]]
}

object Extractor {
def apply[F[_]](implicit extractor: Extractor[F]): extractor.type = extractor

def dependenciesExtractor[F[_]: Sync] = new DependenciesExtractor[F]

class DependenciesExtractor[F[_]: Sync] extends Extractor[F] {
def load(file: File): Resource[F, Seq[Dependency]] =
Resource
.fromAutoCloseable(Sync[F].delay(scala.io.Source.fromFile(file)))
.evalMap { buffer =>
Sync[F].fromEither {
Json
.parse(buffer.getLines().mkString(""))
.validate[Seq[Dependency]]
.asEither
.leftMap(errors => InvalidFieldException(errors.mkString(", ")))
}
}

def extractedResolvers: Kleisli[F, Seq[Dependency], Seq[MavenRepository]] =
Kleisli(
dependencies =>
dependencies
.groupBy(_.resolver.url)
.map {
case (url, dependencies) =>
dependencies.toSet.headOption
.fold(ReducingResolverException(
s"No Resolver was found in json for this resolver url $url").throwEx) {
dependencySet =>
MavenRepository(dependencySet.resolver.name, url)
}
}
.toSeq
.pure[F]
)

def extractedCredentials: Kleisli[F, Seq[Dependency], Seq[SbtCredentials]] =
Kleisli(
dependencies =>
dependencies
.flatMap(_.resolver.credentials)
.distinct
.map { c =>
SbtCredentials(realm = c.realm,
host = c.host,
userName = c.user,
passwd = c.password)
}
.pure[F]
)

def extractedModuleId: Kleisli[F, Seq[Dependency], Seq[ModuleID]] =
Kleisli(_.toModuleId.pure[F])
}

}
21 changes: 21 additions & 0 deletions src/main/scala/io/radicalbit/models/DependenciesStructures.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 Radicalbit
*
* 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 io.radicalbit.models

sealed case class DependenciesStructures(dependencies: Seq[sbt.ModuleID],
resolvers: Seq[sbt.MavenRepository],
credentials: Seq[sbt.Credentials])
Loading