Skip to content

Commit

Permalink
Add option to pass lib dependencies to coursier
Browse files Browse the repository at this point in the history
This can be used to add custom url handlers to coursier, so
scala-steward could for example reach s3 buckets, google cloud storage
or artifactregistry.
See https://get-coursier.io/docs/extra.html#extra-protocols and
coursier/coursier#1987
  • Loading branch information
987Nabil committed Jun 8, 2022
1 parent 883f5db commit 6203ee3
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 31 deletions.
4 changes: 3 additions & 1 deletion docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
All command line arguments for the `scala-steward` application.

```
Usage: scala-steward --workspace <file> --repos-file <file> [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--vcs-type <vcs-type>] [--vcs-api-host <uri>] --vcs-login <string> [--do-not-fork] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>] [--default-maven-repo <string>] [--refresh-backoff-period <duration>]
Usage: scala-steward --workspace <file> --repos-file <file> [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--vcs-type <vcs-type>] [--vcs-api-host <uri>] --vcs-login <string> [--do-not-fork] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>] [--default-maven-repo <string>] [--refresh-backoff-period <duration>] [--coursier-dependencies <string>]...
Expand Down Expand Up @@ -76,4 +76,6 @@ Options and flags:
default: https://repo1.maven.org/maven2/
--refresh-backoff-period <duration>
Period of time a failed build won't be triggered again; default: 0days
--coursier-dependencies <string>
Additional coursier dependencies in the format <groupId>:<artifactId>:<version>, i.e. additional URL handlers. (can be used multiple times)
```
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ object Cli {
private val gitHubApp: Opts[Option[GitHubApp]] =
(githubAppId, githubAppKeyFile).mapN(GitHubApp.apply).orNone

private val coursierDependencies: Opts[List[String]] =
options[String](
"coursier-dependencies",
"Additional coursier dependencies in the format <groupId>:<artifactId>:<version>, " +
"i.e. additional URL handlers. (can be used multiple times)"
).orEmpty

private val refreshBackoffPeriod: Opts[FiniteDuration] = {
val default = 0.days
val help = "Period of time a failed build won't be triggered again" +
Expand Down Expand Up @@ -267,7 +274,8 @@ object Cli {
gitHubApp,
urlCheckerTestUrl,
defaultMavenRepo,
refreshBackoffPeriod
refreshBackoffPeriod,
coursierDependencies
).mapN(Config.apply)

val command: Command[Config] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ final case class Config(
githubApp: Option[GitHubApp],
urlCheckerTestUrl: Uri,
defaultResolver: Resolver,
refreshBackoffPeriod: FiniteDuration
refreshBackoffPeriod: FiniteDuration,
coursierDependencies: List[String]
) {
def vcsUser[F[_]](implicit
processAlg: ProcessAlg[F],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.scalasteward.core.buildtool.maven.MavenAlg
import org.scalasteward.core.buildtool.mill.MillAlg
import org.scalasteward.core.buildtool.sbt.SbtAlg
import org.scalasteward.core.client.ClientConfiguration
import org.scalasteward.core.coursier.{CoursierAlg, VersionsCache}
import org.scalasteward.core.coursier.{CoursierAlg, CoursierDependenciesFetchAlg, VersionsCache}
import org.scalasteward.core.edit.EditAlg
import org.scalasteward.core.edit.hooks.HookExecutor
import org.scalasteward.core.edit.scalafix._
Expand Down Expand Up @@ -158,7 +158,9 @@ object Context {
implicit val scalafixCli: ScalafixCli[F] = new ScalafixCli[F]
implicit val scalafmtAlg: ScalafmtAlg[F] = new ScalafmtAlg[F](config)
implicit val selfCheckAlg: SelfCheckAlg[F] = new SelfCheckAlg[F](config)
implicit val coursierAlg: CoursierAlg[F] = CoursierAlg.create[F]
implicit val coursierDependenciesFetchAlg: CoursierDependenciesFetchAlg[F] =
CoursierDependenciesFetchAlg.create[F]
implicit val coursierAlg: CoursierAlg[F] = CoursierAlg.create[F](config)
implicit val versionsCache: VersionsCache[F] =
new VersionsCache[F](config.cacheTtl, versionsStore)
implicit val updateAlg: UpdateAlg[F] = new UpdateAlg[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import coursier.cache.{CachePolicy, FileCache}
import coursier.core.{Authentication, Project}
import coursier.{Fetch, Info, Module, ModuleName, Organization}
import org.http4s.Uri
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.Resolver.Credentials
import org.scalasteward.core.data.{Dependency, Resolver, Scope, Version}
import org.scalasteward.core.util.uri
Expand All @@ -45,15 +46,29 @@ trait CoursierAlg[F[_]] {
}

object CoursierAlg {
def create[F[_]](implicit
def create[F[_]](config: Config)(implicit
logger: Logger[F],
parallel: Parallel[F],
F: Sync[F]
F: Sync[F],
fetchAlg: CoursierDependenciesFetchAlg[F]
): CoursierAlg[F] = {
val fetch: Fetch[F] = Fetch[F](FileCache[F]())
val fetch: F[Fetch[F]] = fetchAlg
.classLoader(config.coursierDependencies)
.map { loader =>
Fetch[F](
FileCache[F]().withClassLoaders(loader :: Nil)
)
}

val cacheNoTtl: FileCache[F] =
FileCache[F]().withTtl(None).withCachePolicies(List(CachePolicy.Update))
val cacheNoTtl: F[FileCache[F]] =
fetchAlg
.classLoader(config.coursierDependencies)
.map { loader =>
FileCache[F]()
.withTtl(None)
.withCachePolicies(List(CachePolicy.Update))
.withClassLoaders(loader :: Nil)
}

new CoursierAlg[F] {
override def getArtifactUrl(dependency: Scope.Dependency): F[Option[Uri]] =
Expand All @@ -63,24 +78,27 @@ object CoursierAlg {
dependency: coursier.Dependency,
repositories: List[coursier.Repository]
): F[Option[Uri]] = {
val fetchArtifacts = fetch
.withArtifactTypes(Set(coursier.Type.pom, coursier.Type.ivy))
.withDependencies(List(dependency))
.withRepositories(repositories)
fetchArtifacts.ioResult.attempt.flatMap {
case Left(throwable) =>
logger.debug(throwable)(s"Failed to fetch artifacts of $dependency").as(None)
case Right(result) =>
val maybeProject = result.resolution.projectCache
.get(dependency.moduleVersion)
.map { case (_, project) => project }
maybeProject.traverseFilter { project =>
getScmUrlOrHomePage(project.info) match {
case Some(url) => F.pure(Some(url))
case None =>
getParentDependency(project).traverseFilter(getArtifactUrlImpl(_, repositories))
val fetchArtifacts = fetch.map(
_.withArtifactTypes(Set(coursier.Type.pom, coursier.Type.ivy))
.withDependencies(List(dependency))
.withRepositories(repositories)
)
fetchArtifacts.flatMap {
_.ioResult.attempt.flatMap {
case Left(throwable) =>
logger.debug(throwable)(s"Failed to fetch artifacts of $dependency").as(None)
case Right(result) =>
val maybeProject = result.resolution.projectCache
.get(dependency.moduleVersion)
.map { case (_, project) => project }
maybeProject.traverseFilter { project =>
getScmUrlOrHomePage(project.info) match {
case Some(url) => F.pure(Some(url))
case None =>
getParentDependency(project).traverseFilter(getArtifactUrlImpl(_, repositories))
}
}
}
}
}
}

Expand All @@ -90,10 +108,12 @@ object CoursierAlg {
logger.error(message) >> F.raiseError(new Throwable(message))
case Right(repository) =>
val module = toCoursierModule(dependency)
repository.versions(module, cacheNoTtl.fetch).run.flatMap {
case Left(message) =>
logger.debug(message) >> F.raiseError(new Throwable(message))
case Right((versions, _)) => F.pure(versions.available.map(Version.apply).sorted)
cacheNoTtl.flatMap { cacheNoTtl =>
repository.versions(module, cacheNoTtl.fetch).run.flatMap {
case Left(message) =>
logger.debug(message) >> F.raiseError(new Throwable(message))
case Right((versions, _)) => F.pure(versions.available.map(Version.apply).sorted)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018-2022 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 org.scalasteward.core.coursier

import cats.Parallel
import cats.effect._
import cats.implicits._
import coursier._
import coursier.cache.FileCache

import java.net.URLClassLoader

/**
* An interface to fetch dependencies for the coursier alg via coursier.
*/
trait CoursierDependenciesFetchAlg[F[_]] {
def classLoader(dependencies: Seq[String]): F[ClassLoader]
}

object CoursierDependenciesFetchAlg {
def create[F[_]](implicit parallel: Parallel[F], F: Sync[F]): CoursierDependenciesFetchAlg[F] =
(dependencies: Seq[String]) =>
Fetch[F](FileCache[F]())
.withDependencies(dependencies.map { dep =>
val Array(org, name, version) = dep.split(':')
Dependency(Module(Organization(org), ModuleName(name)), version)
})
.io
.map(result => new URLClassLoader(result.map(_.toURI.toURL).toArray))
}

0 comments on commit 6203ee3

Please sign in to comment.