Skip to content
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ csrConfiguration := csrConfiguration.value.withProtocolHandlerDependencies(
)
```

For publish settings in `build.sbt` use:
```scala
publishTo := Some(
dev.rolang.sbt.gar.ArtifactRegistryIvyResolver.create(
"My Registry", "https://${LOCATION}-maven.pkg.dev/${GCP_PROJECT}/maven"
)
)
```

The plugin is automatically enabled for all projects and will install a global `artifactregistry://` handler
as well as adding the protocol handler to coursier.

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ lazy val core = project
Seq(file)
}.taskValue,
libraryDependencies ++= Seq(
"com.google.cloud" % "google-cloud-storage" % "2.62.0"
"com.google.cloud" % "google-cloud-storage" % "2.67.0"
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ object ArtifactRegistryUrlHandlerFactory {
new NetHttpTransport()
}

def createURLStreamHandler(logger: Logger): ArtifactRegistryUrlHandler = {
def createRequestFactory(logger: Logger): HttpRequestFactory = {
val httpTransport = httpTransportFactory.create()
val googleHttpRequestFactory = googleCredentials(logger) match {
googleCredentials(logger) match {
case Some(credentials) =>
val requestInitializer = new HttpCredentialsAdapter(credentials)
httpTransport.createRequestFactory(requestInitializer)
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credentials))
case None => httpTransport.createRequestFactory()
}

new ArtifactRegistryUrlHandler(googleHttpRequestFactory)(logger)
}

def createURLStreamHandler(logger: Logger): ArtifactRegistryUrlHandler =
new ArtifactRegistryUrlHandler(createRequestFactory(logger))(logger)

def install(logger: Logger) =
try {
new URL("artifactregistry://example.com")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.rolang.sbt.gar

import com.google.api.client.http.{ByteArrayContent, GenericUrl, HttpRequestFactory}
import dev.rolang.gar.{ArtifactRegistryUrlHandlerFactory, Logger}
import org.apache.ivy.core.module.descriptor.Artifact
import org.apache.ivy.plugins.repository.{AbstractRepository, Resource}
import org.apache.ivy.plugins.repository.url.URLResource
import org.apache.ivy.plugins.resolver.IBiblioResolver
import org.apache.ivy.util.Message
import sbt.librarymanagement.{RawRepository, Resolver}

import java.io.File
import java.net.{URI, URL}
import java.nio.file.{Files, StandardCopyOption}

class ArtifactRegistryIvyRepository(logger: Logger) extends AbstractRepository {

private lazy val requestFactory: HttpRequestFactory =
ArtifactRegistryUrlHandlerFactory.createRequestFactory(logger)

override def getName: String = "ArtifactRegistry"

override def getResource(source: String): Resource =
new URLResource(URI.create(source).toURL)

override def get(source: String, destination: File): Unit = {
val response = requestFactory.buildGetRequest(toHttpsUrl(source)).execute()
val is = response.getContent
try Files.copy(is, destination.toPath, StandardCopyOption.REPLACE_EXISTING)
finally is.close()
}

override def put(artifact: Artifact, src: File, destination: String, overwrite: Boolean): Unit = {
logger.info(s"Uploading artifact to: $destination")
val bytes = Files.readAllBytes(src.toPath)
requestFactory
.buildPutRequest(toHttpsUrl(destination), new ByteArrayContent(null, bytes))
.execute()
}

override def list(parent: String): java.util.List[String] =
java.util.Collections.emptyList()

private def toHttpsUrl(raw: String): GenericUrl = {
val url = URI.create(raw).toURL
val g = new GenericUrl()
g.setScheme("https")
g.setHost(url.getHost)
g.appendRawPath(url.getPath)
g
}
}

object ArtifactRegistryIvyResolver {

def create(name: String, root: String): Resolver = {
val logger = new Logger {
def info(msg: String): Unit = Message.info(msg)
def error(msg: String): Unit = Message.error(msg)
def debug(msg: String): Unit = Message.debug(msg)
}
val resolver = new IBiblioResolver
resolver.setName(name)
resolver.setRoot(root)
resolver.setM2compatible(true)
resolver.setUseMavenMetadata(true)
resolver.setRepository(new ArtifactRegistryIvyRepository(logger))
new RawRepository(resolver, name)
}
}
9 changes: 6 additions & 3 deletions modules/sbt/src/main/scala/dev/rolang/sbt/gar/GarPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package dev.rolang.sbt.gar

import sbt._
import sbt.Keys._
import java.io.File

import scala.util.{Failure, Success, Try}

object GarPlugin extends AutoPlugin {
Expand Down Expand Up @@ -37,6 +35,11 @@ object GarPlugin extends AutoPlugin {
},
csrConfiguration := csrConfiguration.value.withProtocolHandlerDependencies(
Seq("dev.rolang" % "gar-coursier_2.13" % dev.rolang.gar.version.value)
)
),
publishTo := publishTo.value.map {
case m: sbt.librarymanagement.MavenRepository if m.root.startsWith("artifactregistry://") =>
ArtifactRegistryIvyResolver.create(m.name, m.root)
case other => other
}
) ++ super.projectSettings
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.12.0
sbt.version=1.12.11
Loading