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 all 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 .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version = 2.7.5
style = defaultWithAlign
project.git = true
maxColumn = 120
unindentTopLevelOperators = true
danglingParentheses = true
spaces.inImportCurlyBraces = true

rewrite.rules = [ RedundantBraces, SortImports ]
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)
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[![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")

And enable the plugin:
```
lazy val core = project
.in(file("modules/core"))
.settings(commonSettings: _*)
.enablePlugins(DependenciesFromJsonPlugin)
```


## 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)
17 changes: 16 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import sbt._

lazy val root = (project in file("."))
.enablePlugins(SbtPlugin)
.settings(
scalaVersion := "2.12.8",
name := "sbt-dependency-from-file",
organization := "io.radicalbit",
version := "1.0-SNAPSHOT",
version := "1.5-SNAPSHOT",
scalafmtOnCompile := true,
organizationName := "Radicalbit",
startYear := Some(2019),
licenses += ("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt")),
libraryDependencies ++= Dependencies.pluginDependencies,
developers := List(
Developer(id = "francescofrontera",
name = "Francesco Frontera",
email = "[email protected]",
url = url("https://github.com/francescofrontera")),
Developer(id = "maocorte",
name = "Mauro Cortellazzi",
email = "[email protected]",
url = url("https://github.com/maocorte"))
),
sbtPlugin := true
)
.settings(PublishSettings.settings: _*)
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
)
}
7 changes: 7 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.6")

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

addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.4.0")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0")
51 changes: 51 additions & 0 deletions src/main/scala/io/radicalbit/DependenciesFromJsonPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 cats.effect._
import io.radicalbit.extractor.{ DependencyLoader, ExtractorBehaviour }
import io.radicalbit.models.DependenciesStructures
import sbt._
import sbt.plugins.JvmPlugin

import java.io.File

object DependenciesFromJsonPlugin extends AutoPlugin {
object autoImport {
lazy val dependenciesJsonPath = settingKey[File]("Dependencies file path")
lazy val dependenciesFromJson = settingKey[DependenciesStructures]("Extracted information")
}

import autoImport._
override def trigger: PluginTrigger = noTrigger
override def requires: JvmPlugin.type = sbt.plugins.JvmPlugin
override def projectSettings: Seq[Def.Setting[_]] =
Seq(
dependenciesFromJson := loadAndRun[IO](dependenciesJsonPath.value).unsafeRunSync()
)

private def loadAndRun[F[_]: Effect](file: File): F[DependenciesStructures] = {
val behaviour =
for {
d <- ExtractorBehaviour[F].modulesID
r <- ExtractorBehaviour[F].resolvers
c <- ExtractorBehaviour[F].credentials
} yield DependenciesStructures(d, r, c)

DependencyLoader[F].load(file).use(behaviour.run)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.errors

trait DependencyFromJsonException { self: Throwable =>
Expand Down
20 changes: 17 additions & 3 deletions src/main/scala/io/radicalbit/errors/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/*
* 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

package object errors {
case class InvalidFieldException(message: String)
extends RuntimeException(message)
with DependencyFromJsonException
case class InvalidFieldException(message: String) extends RuntimeException(message) with DependencyFromJsonException

case class ReducingResolverException(message: String)
extends RuntimeException(message)
Expand Down
50 changes: 50 additions & 0 deletions src/main/scala/io/radicalbit/extractor/DependencyLoader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 cats.effect._
import cats.implicits._
import io.radicalbit.errors._
import io.radicalbit.models._
import play.api.libs.json.Json

import java.io.{ File, FileInputStream }

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

object DependencyLoader {
def apply[F[_]: Effect](implicit extractor: DependencyLoader[F]): DependencyLoader[F] = loader[F]

implicit def loader[F[_]](implicit E: Effect[F]): DependencyLoader[F] =
new DependencyLoader[F] {
def load(file: File): Resource[F, Seq[Dependency]] =
Resource
.fromAutoCloseable(E.delay(scala.io.Source.fromFile(file)))
.evalMap { buffer =>
E.fromEither {
Json
.parse(buffer.getLines().mkString(""))
.validate[Seq[Dependency]]
.asEither
.leftMap(errors => InvalidFieldException(errors.mkString(", ")))
}
}
}

}
Loading