Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #188 from eed3si9n/wip/split
Browse files Browse the repository at this point in the history
Add sbt-bintray-remote-cache
  • Loading branch information
eed3si9n committed Oct 25, 2020
2 parents 1ca2307 + 9f73254 commit 85b5dee
Show file tree
Hide file tree
Showing 36 changed files with 414 additions and 86 deletions.
22 changes: 6 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
language: scala
sudo: false
jdk: openjdk8

jobs:
include:
- stage: test
script: sbt test scripted

- stage: test
script: sbt ^^1.0.0 test scripted

- stage: deploy-sbt-0.13
script: skip
deploy:
provider: script
script: sbt publish
on:
tags: true
repo: sbt/sbt-bintray

- stage: deploy-sbt-1.0
script: skip
deploy:
provider: script
script: sbt ^^1.0.0 publish
script: sbt publish
on:
tags: true
repo: sbt/sbt-bintray

before_cache:
- find $HOME/.ivy2 -name "ivydata-*.properties" -print -delete
- find $HOME/.sbt -name "*.lock" -print -delete
- find $HOME/.cache/coursier/v1 -name "ivydata-*.properties" -delete
- find $HOME/.ivy2 -name "ivydata-*.properties" -delete
- find $HOME/.sbt -name "*.lock" -delete
- rm -r $(find $HOME/.sbt/boot -name "*-SNAPSHOT") || true

cache:
directories:
- $HOME/.cache/coursier/v1
- $HOME/.ivy2/cache
- $HOME/.sbt/boot

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

An sbt plugin for publishing [bintray](https://bintray.com) packages.

See [remote caching](REMOTE_CACHE.md) for information about sbt-bintray-remote-cache.

## Consuming or publishing?

This plugin is for publishing. You don't need this plugin to consume
Expand All @@ -35,8 +37,7 @@ If you want to _publish_ to Bintray, read on.

### What you need

- sbt 0.13.16 or newer
- an account on [bintray](https://bintray.com) (get one [here](https://bintray.com/signup/index))
- an account on [bintray](https://bintray.com) (get one [here](https://bintray.com/signup/oss))
- a desire to build a more diverse Scala library ecosystem

Add the following to your sbt `project/plugins.sbt` file:
Expand Down
67 changes: 67 additions & 0 deletions REMOTE_CACHE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
sbt-bintray-remote-cache
========================

requirements
------------

- an account on [bintray](https://bintray.com) (get one [here](https://bintray.com/signup/oss))
- a desire to build a zero-second build

setup
-----

Add the following to your sbt `project/plugins.sbt` file:

![Bintray version](https://img.shields.io/bintray/v/sbt/sbt-plugin-releases/sbt-bintray.svg)

```scala
addSbtPlugin("org.foundweekends" % "sbt-bintray-remote-cache" % "x.y.z")
```

### Bintray repo and package

Go to `https://bintray.com/<your_bintray_user>/` and create a new **Generic** repository with the name **`remote-cache`**.

Next, create a _package_ within the remote-cache repo. The granularity should typically be one package for one build.

### build.sbt

Then in your `build.sbt`:

```scala
ThisBuild / bintrayRemoteCacheOrganization := "your_bintray_user or organization"
ThisBuild / bintrayRemoteCachePackage := "your_package_name"
```

usage
-----

### credentials

To push remote cache, you need to provide Bintray credentials (user name and API key) using a credential file or environment variables.

1. Environment variables

sbt-bintray-remote-cache will look for bintray user and pass in the environment variables `BINTRAY_REMOTE_CACHE_USER` and `BINTRAY_REMOTE_CACHE_PASS`. Note that these are different from sbt-bintray.

2. Credentials file

sbt-bintray-remote-cache will look for a credentials file under `~/.bintray/.credentials` used to authenticate publishing requests to bintray.

### pushing remote cache

From the CI machine, run

```
> pushRemoteCache
```

### cleaning up the old cache

From the CI machine, run

```
> bintrayRemoteCacheCleanOld
```

This will **remove** versions older than a month, while keeping minimum 100 cache entry.
82 changes: 50 additions & 32 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,53 +1,71 @@
lazy val unusedWarnings = Seq("-Ywarn-unused-import", "-Ywarn-unused")

ThisBuild / organization := "org.foundweekends"
ThisBuild / homepage := Some(url(s"https://github.com/sbt/${name.value}"))
ThisBuild / licenses := Seq("MIT" ->
url(s"https://github.com/sbt/${name.value}/blob/${version.value}/LICENSE"))
ThisBuild / description := "package publisher for bintray.com"
ThisBuild / developers := List(
Developer("softprops", "Doug Tangren", "@softprops", url("https://github.com/softprops"))
)
ThisBuild / scmInfo := Some(ScmInfo(url(s"https://github.com/sbt/${name.value}"), s"git@github.com:sbt/${name.value}.git"))
ThisBuild / scalaVersion := "2.12.12"

lazy val commonSettings: Seq[Setting[_]] = Seq(
organization in ThisBuild := "org.foundweekends",
homepage in ThisBuild := Some(url(s"https://github.com/sbt/${name.value}")),
licenses in ThisBuild := Seq("MIT" ->
url(s"https://github.com/sbt/${name.value}/blob/${version.value}/LICENSE")),
description in ThisBuild := "package publisher for bintray.com",
developers in ThisBuild := List(
Developer("softprops", "Doug Tangren", "@softprops", url("https://github.com/softprops"))
),
scmInfo in ThisBuild := Some(ScmInfo(url(s"https://github.com/sbt/${name.value}"), s"git@github.com:sbt/${name.value}.git")),
// crossScalaVersions in ThisBuild := Seq("2.10.7", "2.11.8", "2.12.6"),
scalaVersion := (crossScalaVersions in ThisBuild).value.last,
scalacOptions ++= Seq(Opts.compile.deprecation, "-Xlint", "-feature"),
scalacOptions ++= PartialFunction.condOpt(CrossVersion.partialVersion(scalaVersion.value)){
case Some((2, v)) if v >= 11 => unusedWarnings
}.toList.flatten,
publishArtifact in Test := false,
bintrayRepository := "sbt-plugin-releases",
bintrayOrganization := Some("sbt"),
bintrayPackage := "sbt-bintray"
bintrayPackage := "sbt-bintray",
scriptedBufferLog := true,
scriptedLaunchOpts ++= Seq(
"-Xmx1024M",
"-XX:MaxPermSize=256M",
"-Dbintray.user=username",
"-Dbintray.pass=password",
"-Dplugin.version=" + version.value
),
) ++ Seq(Compile, Test).flatMap(c =>
scalacOptions in (c, console) --= unusedWarnings
)

lazy val root = (project in file("."))
.aggregate(core, sbtBintray, sbtBintrayRemoteCache)
.settings(
publish / skip := true,
)

lazy val core = (project in file("core"))
.enablePlugins(SbtPlugin)
.settings(commonSettings)
.settings(
name := "sbt-bintray",
sbtPlugin := true,
crossSbtVersions := List("0.13.18", "1.0.0"),
scalaVersion := (CrossVersion partialVersion sbtCrossVersion.value match {
case Some((0, 13)) => "2.10.7"
case Some((1, _)) => "2.12.6"
case _ => sys error s"Unhandled sbt version ${sbtCrossVersion.value}"
}),
name := "sbt-bintray-core",
libraryDependencies ++= Seq(
"org.foundweekends" %% "bintry" % "0.5.2",
"org.slf4j" % "slf4j-nop" % "1.7.28"), // https://github.com/sbt/sbt-bintray/issues/26
"org.foundweekends" %% "bintry" % "0.6.0",
"org.slf4j" % "slf4j-nop" % "1.7.28", // https://github.com/sbt/sbt-bintray/issues/26
"com.eed3si9n.verify" %% "verify" % "0.2.0" % Test,
),
testFrameworks += new TestFramework("verify.runner.Framework"),
resolvers += Resolver.sonatypeRepo("releases"),
scriptedSettings,
scriptedBufferLog := true,
scriptedLaunchOpts ++= Seq(
"-Xmx1024M",
"-XX:MaxPermSize=256M",
"-Dbintray.user=username",
"-Dbintray.pass=password",
"-Dplugin.version=" + version.value
)
)

val sbtCrossVersion = sbtVersion in pluginCrossBuild
lazy val sbtBintray = (project in file("sbt-bintray"))
.enablePlugins(SbtPlugin)
.dependsOn(core)
.settings(commonSettings)
.settings(
name := "sbt-bintray",
pluginCrossBuild / sbtVersion := "1.0.0",
)

lazy val sbtBintrayRemoteCache = (project in file("sbt-bintray-remote-cache"))
.enablePlugins(SbtPlugin)
.dependsOn(core)
.settings(commonSettings)
.settings(
name := "sbt-bintray-remote-cache",
pluginCrossBuild / sbtVersion := "1.4.1",
)
41 changes: 24 additions & 17 deletions src/main/scala/Bintray.scala → core/src/main/scala/Bintray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ object Bintray {
case (true, _) =>
BintrayMavenResolver(
s"Bintray-Maven-Publish-${repo.subject}-${repo.repo}-${pkg.name}",
s"https://api.bintray.com/maven/${repo.subject}/${repo.repo}/${repo.repo}", pkg, release)
s"https://api.bintray.com/maven/${repo.subject}/${repo.repo}/${pkg.name}", pkg, release, ignoreExists = false)
case (false, _) =>
BintrayIvyResolver(
s"Bintray-${if (isSbtPlugin) "Sbt" else "Ivy"}-Publish-${repo.subject}-${repo.repo}-${pkg.name}",
pkg.version(version), release)
})

def remoteCache(repo: Client#Repo, pkg: Client#Repo#Package): Resolver =
RawRepository(
BintrayMavenResolver(
s"Bintray-Remote-Cache-${repo.subject}-${repo.repo}-${pkg.name}",
s"https://api.bintray.com/maven/${repo.subject}/${repo.repo}/${pkg.name}", pkg, true, true)
)

def whoami(creds: Option[BintrayCredentials], log: Logger): String =
{
val is = creds match {
Expand All @@ -49,9 +56,9 @@ object Bintray {
}
}

def withRepo[A](credsFile: File, org: Option[String], repoName: String, log: Logger)
def withRepo[A](context: BintrayCredentialContext, org: Option[String], repoName: String, log: Logger)
(f: BintrayRepo => A): Option[A] =
ensuredCredentials(credsFile, log) map { cred =>
ensuredCredentials(context, log) map { cred =>
val repo = cachedRepo(cred, org, repoName)
f(repo)
}
Expand All @@ -65,30 +72,30 @@ object Bintray {
}

private[bintray] def ensuredCredentials(
credsFile: File, log: Logger): Option[BintrayCredentials] =
propsCredentials
.orElse(envCredentials)
.orElse(BintrayCredentials.read(credsFile))
context: BintrayCredentialContext, log: Logger): Option[BintrayCredentials] =
propsCredentials(context)
.orElse(envCredentials(context))
.orElse(BintrayCredentials.read(context.credsFile))

private def propsCredentials =
private def propsCredentials(context: BintrayCredentialContext) =
for {
name <- sys.props.get("bintray.user")
pass <- sys.props.get("bintray.pass")
name <- sys.props.get(context.userNameProp)
pass <- sys.props.get(context.passProp)
} yield BintrayCredentials(name, pass)

private def envCredentials =
private def envCredentials(context: BintrayCredentialContext) =
for {
name <- sys.env.get("BINTRAY_USER")
pass <- sys.env.get("BINTRAY_PASS")
name <- sys.env.get(context.userNameEnv)
pass <- sys.env.get(context.passEnv)
} yield BintrayCredentials(name, pass)

/** assign credentials or ask for new ones */
private[bintray] def changeCredentials(credsFile: File, log: Logger): Unit =
Bintray.ensuredCredentials(credsFile, Logger.Null) match {
private[bintray] def changeCredentials(context: BintrayCredentialContext, log: Logger): Unit =
Bintray.ensuredCredentials(context, Logger.Null) match {
case None =>
saveBintrayCredentials(credsFile)(requestCredentials(), log)
saveBintrayCredentials(context.credsFile)(requestCredentials(), log)
case Some(BintrayCredentials(user, pass)) =>
saveBintrayCredentials(credsFile)(requestCredentials(Some(user), Some(pass)), log)
saveBintrayCredentials(context.credsFile)(requestCredentials(Some(user), Some(pass)), log)
}

private[bintray] def buildResolvers(creds: Option[BintrayCredentials], org: Option[String], repoName: String, mavenStyle: Boolean): Seq[Resolver] =
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/scala/BintrayCredentialContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bintray

import java.io.File

/**
* This abstract which environmental variable will be used by which plugin.
*/
case class BintrayCredentialContext(
credsFile: File,
userNameProp: String,
passProp: String,
userNameEnv: String,
passEnv: String
)

object BintrayCredentialContext {
def apply(credsFile: File): BintrayCredentialContext =
BintrayCredentialContext(
credsFile,
"bintray.user",
"bintray.pass",
"BINTRAY_USER",
"BINTRAY_PASS"
)

def remoteCache(credsFile: File): BintrayCredentialContext =
BintrayCredentialContext(
credsFile,
"bintray.remote.cache.user",
"bintray.remote.cache.pass",
"BINTRAY_REMOTE_CACHE_USER",
"BINTRAY_REMOTE_CACHE_PASS"
)
}
Loading

0 comments on commit 85b5dee

Please sign in to comment.