Skip to content

Commit

Permalink
Prepare sonatype release. Add documentation about modularization (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk authored Jul 3, 2024
1 parent ee3938d commit 0d3f833
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 9 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release
on:
push:
branches: [main]
tags: ["v*"]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: 'sbt'
- run: sbt ci-release
env:
PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }}
PGP_SECRET: ${{ secrets.PGP_SECRET }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
1 change: 1 addition & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
with:
java-version: '21'
distribution: 'temurin'
cache: 'sbt'

- name: Run tests
run: sbt test
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ grows.
The suggested approach would be:

```scala
import com.ihorvovk.cats_effect_simple_di.Allocator
import io.github.cats_effect_simple_di.Allocator

// create a Dependencies object:
class Dependencies(allocator: Allocator) {
Expand Down Expand Up @@ -58,7 +58,83 @@ If you want to see the order of initialization and finalization of resources, us
creating an `Allocator` object. This will log the allocation and finalization of resources in the order they happen:

```scala
import com.ihorvovk.cats_effect_simple_di.AllocationLifecycleListener
import io.github.cats_effect_simple_di.AllocationLifecycleListener

Allocator.create(runtime, LogbackAllocationListener)
```

## Modularization

You can have multiple dependencies objects and combine them together. In this case, you can either reuse the same
`Allocator` object or create a new one for each dependency object, but wrap their instantiation
in `allocator.allocate{}` so that they are shut down in the right order:

Example reusing the same `Allocator` object:
```scala
// AWS - specific dependencies
class AwsDependencies(allocator: Allocator) {
lazy val s3Client: S3Client = allocator.allocate {
S3ClientBuilder.default.build
}
}

// Main application dependencies
object Dependencies {
def create(runtime: IORuntime): Resource[IO, Dependencies] =
Allocator.create(runtime).map(new Dependencies(_))
}

class Dependencies(allocator: Allocator) {
val aws = new AwsDependencies(allocator)

lazy val http4sClient: Client[IO] = allocator.allocate {
EmberClientBuilder.default[IO].build
}
}

object App extends IOApp.Simple {
override def run: IO[Unit] = Dependencies.create(runtime).use { deps =>
// use aws.s3Client here
deps.aws.s3Client
}
}
```

Example creating a new `Allocator` object for each Dependencies object:

```scala
// AWS - specific dependencies
object AwsDependencies {
def create(runtime: IORuntime): Resource[IO, AwsDependencies] =
Allocator.create(runtime).map(new AwsDependencies(_))
}

class AwsDependencies(allocator: Allocator) {
lazy val s3Client: S3Client = allocator.allocate {
S3ClientBuilder.default.build
}
}

// Main application dependencies
object Dependencies {
def create(runtime: IORuntime): Resource[IO, Dependencies] =
Allocator.create(runtime).map(new Dependencies(_))
}

class Dependencies(allocator: Allocator) {
lazy val aws = allocator.allocate {
AwsDependencies.create(IORuntime.global)
}

lazy val http4sClient: Client[IO] = http4sAllocator.allocate {
EmberClientBuilder.default[IO].build
}
}

object App extends IOApp.Simple {
override def run: IO[Unit] = Dependencies.create(runtime).use { deps =>
// use aws.s3Client here
deps.aws.s3Client
}
}
```
19 changes: 15 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@

organization := "com.ihorvovk"
name := "cats-effect-simple-di"
version := "0.0.1"

scalaVersion := "3.4.2"
scalaVersion := "3.3.3"
scalacOptions ++= Seq("-unchecked", "-feature", "-deprecation", "-Xfatal-warnings", "-Wunused:imports")

inThisBuild(List(
organization := "io.github.igor-vovk",
homepage := Some(url("https://github.com/igor-vovk/cats-effect-simple-di")),
licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
developers := List(
Developer(
"igor-vovk",
"Ihor Vovk",
"[email protected]",
url("https://github.com/igor-vovk")
)
)
))

lazy val Versions = new {
val catsEffect = "3.5.4"
val scalatest = "3.2.18"
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ihorvovk.cats_effect_simple_di
package io.github.cats_effect_simple_di

import cats.effect.IO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ihorvovk.cats_effect_simple_di
package io.github.cats_effect_simple_di

import cats.Show
import cats.effect.unsafe.IORuntime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ihorvovk.cats_effect_simple_di
package io.github.cats_effect_simple_di

import cats.effect.unsafe.IORuntime
import cats.effect.unsafe.implicits.global
Expand Down

0 comments on commit 0d3f833

Please sign in to comment.