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

Add option to pass lib dependencies to coursier #2628

Open
wants to merge 3 commits into
base: main
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
5 changes: 5 additions & 0 deletions docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All command line arguments for the `scala-steward` application.
Usage:
scala-steward --workspace <file> --repos-file <uri> [--repos-file <uri>]... [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--forge-type <forge-type>] [--forge-api-host <uri>] --forge-login <string> [--do-not-fork] [--add-labels] [--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-use-default-reviewers] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--gitlab-required-reviewers <integer>] [--gitlab-remove-source-branch] [--azure-repos-organization <string>] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>]... [--default-maven-repo <string>] [--refresh-backoff-period <duration>]
scala-steward validate-repo-config
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 @@ -94,6 +95,10 @@ 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)
--coursier-dependencies <string>
Additional coursier dependencies in the format <groupId>:<artifactId>:<version>, i.e. additional URL handlers. (can be used multiple times)

Subcommands:
validate-repo-config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ object Cli {
private val azureReposCfg: Opts[AzureReposCfg] =
azureReposOrganization.map(AzureReposCfg.apply)

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 @@ -355,7 +362,8 @@ object Cli {
gitHubApp,
urlCheckerTestUrls,
defaultMavenRepo,
refreshBackoffPeriod
refreshBackoffPeriod,
coursierDependencies
).mapN(Config.apply).map(Usage.Regular.apply)

private val validateRepoConfig: Opts[Usage] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ final case class Config(
githubApp: Option[GitHubApp],
urlCheckerTestUrls: Nel[Uri],
defaultResolver: Resolver,
refreshBackoffPeriod: FiniteDuration
refreshBackoffPeriod: FiniteDuration,
coursierDependencies: List[String]
) {
def forgeSpecificCfg: ForgeSpecificCfg =
forgeCfg.tpe match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.scalasteward.core.buildtool.mill.MillAlg
import org.scalasteward.core.buildtool.sbt.SbtAlg
import org.scalasteward.core.buildtool.scalacli.ScalaCliAlg
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.data.Repo
import org.scalasteward.core.edit.EditAlg
import org.scalasteward.core.edit.hooks.HookExecutor
Expand Down Expand Up @@ -175,7 +175,9 @@ object Context {
implicit val scalafixCli: ScalafixCli[F] = new ScalafixCli[F]
implicit val scalafmtAlg: ScalafmtAlg[F] = new ScalafmtAlg[F](config.defaultResolver)
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 @@ -22,6 +22,7 @@ import cats.implicits._
import coursier.cache.{CachePolicy, FileCache}
import coursier.core.{Authentication, Project}
import coursier.{Fetch, Module, ModuleName, Organization}
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.Resolver.Credentials
import org.scalasteward.core.data.{Dependency, Resolver, Version}
import org.scalasteward.core.util.uri
Expand All @@ -37,16 +38,29 @@ trait CoursierAlg[F[_]] {
}

object CoursierAlg {
def create[F[_]](implicit
def create[F[_]](config: Config)(implicit
logger: Logger[F],
parallel: Parallel[F],
F: Async[F]
F: Async[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 getMetadata(
Expand All @@ -64,38 +78,40 @@ object CoursierAlg {
repositories: List[coursier.Repository],
acc: DependencyMetadata
): F[DependencyMetadata] = {
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(acc)
case Right(result) =>
val maybeProject = result.resolution.projectCache
.get(dependency.moduleVersion)
.map { case (_, project) => project }

maybeProject.fold(F.pure(acc)) { project =>
val metadata = acc.enrichWith(metadataFrom(project))
val recurse = Option.when(metadata.repoUrl.isEmpty)(())
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(acc)
case Right(result) =>
val maybeProject = result.resolution.projectCache
.get(dependency.moduleVersion)
.map { case (_, project) => project }
maybeProject.fold(F.pure(acc)) { project =>
val metadata = acc.enrichWith(metadataFrom(project))
val recurse = Option.when(metadata.repoUrl.isEmpty)(())
(recurse >> parentOf(project)).fold(F.pure(metadata)) { parent =>
getMetadataImpl(parent, repositories, metadata)
getMetadataImpl(parent, repositories, metadata)
}
}
}
}
}
}

override def getVersions(dependency: Dependency, resolver: Resolver): F[List[Version]] =
convertResolver(resolver).flatMap { repository =>
val module = toCoursierModule(dependency)
repository.versions(module, cacheNoTtl.fetch).run.flatMap {
case Left(message) =>
logger.debug(message) >> F.raiseError[List[Version]](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[List[Version]](new Throwable(message))
case Right((versions, _)) => F.pure(versions.available.map(Version.apply).sorted)
}
}
}

private def convertResolver(resolver: Resolver): F[coursier.Repository] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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: Async[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))
}
}
Loading