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

WIP - start refactoring into two projects and adding github remote ca… #32

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
46 changes: 35 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,44 @@
* limitations under the License.
*/

name := "sbt-github-packages"
inThisBuild(Seq(
baseVersion := "0.5",
organization := "com.codecommit",
publishGithubUser := "djspiewak",
publishFullName := "Daniel Spiewak",
bintrayVcsUrl := Some("git@github.com:djspiewak/sbt-spiewak.git"),
sbtPlugin := true,
sbtVersion := "1.4.6",
))

ThisBuild / baseVersion := "0.5"
val pluginSettings = Seq(
scriptedLaunchOpts ++= Seq("-Dplugin.version=" + version.value),
scriptedBufferLog := true
)

ThisBuild / organization := "com.codecommit"
ThisBuild / publishGithubUser := "djspiewak"
ThisBuild / publishFullName := "Daniel Spiewak"
lazy val common = project
.in(file("./common"))
.settings(
moduleName := "sbt-github-common"
)

ThisBuild / bintrayVcsUrl := Some("git@github.com:djspiewak/sbt-spiewak.git")
lazy val packages = project
.in(file("./packages"))
.enablePlugins(SbtPlugin)
.settings(pluginSettings)
.settings(
moduleName := "sbt-github-packages"
)
.dependsOn(common)

lazy val remote_cache = project
.in(file("./remote_cache"))
.enablePlugins(SbtPlugin)
.settings(pluginSettings)
.settings(
moduleName := "sbt-github-remote-cache"
)
.dependsOn(packages)

ThisBuild / sbtPlugin := true
ThisBuild / sbtVersion := "1.3.3"

enablePlugins(SbtPlugin)

scriptedLaunchOpts ++= Seq("-Dplugin.version=" + version.value)
scriptedBufferLog := true
40 changes: 40 additions & 0 deletions common/src/main/scala/sbtgh/GitHubResolver.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sbtgh

import sbt._

import scala.sys.process._
import scala.util.Try

object GitHubResolver {
implicit class GitHubResolverSyntax(val resolver: Resolver.type) extends AnyVal {
def githubPackages(owner: String, repo: String = "_"): MavenRepository =
realm(owner, repo) at s"https://maven.pkg.github.com/$owner/$repo"
}

def resolveTokenSource(tokenSource: TokenSource): Option[String] = {
tokenSource match {
case TokenSource.Or(primary, secondary) =>
resolveTokenSource(primary).orElse(
resolveTokenSource(secondary))

case TokenSource.Environment(variable) =>
sys.env.get(variable)

case TokenSource.GitConfig(key) =>
Try(s"git config $key".!!).map(_.trim).toOption
}
}

def inferredGitHubCredentials(user: String, tokenSource: TokenSource): Option[Credentials] = {
resolveTokenSource(tokenSource) map { token =>
Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
user,
token)
}
}

private def realm(owner: String, repo: String) =
s"GitHub Package Registry (${owner}${if (repo != "_") s"/$repo" else ""})"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package sbtghpackages
package sbtgh

sealed trait TokenSource extends Product with Serializable {
def ||(that: TokenSource): TokenSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
* limitations under the License.
*/

package sbtghpackages
package sbtgh.packages

import sbt._
import sbtgh.TokenSource

trait GitHubPackagesKeys {
val githubOwner = settingKey[String]("The github user or organization name")
val githubRepository = settingKey[String]("The github repository hosting this package")
val githubOwner = settingKey[String](
"The github user or organization name")
val githubRepository = settingKey[String](
"The github repository hosting this package")

val githubTokenSource = settingKey[TokenSource]("Where to get the API token used in publication (defaults to github.token in the git config)")
val githubTokenSource = settingKey[TokenSource](
"Where to get the API token used in publication (defaults to github.token in the git config)")

val githubSuppressPublicationWarning = settingKey[Boolean]("Whether or not to suppress the publication warning (default: false, meaning the warning will be printed)")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* limitations under the License.
*/

package sbtghpackages
package sbtgh.packages

import sbt._, Keys._

import scala.sys.process._
import scala.util.Try
import sbt._
import Keys._

object GitHubPackagesPlugin extends AutoPlugin {
@volatile
Expand All @@ -29,16 +27,12 @@ object GitHubPackagesPlugin extends AutoPlugin {
override def trigger = allRequirements

object autoImport extends GitHubPackagesKeys {
type TokenSource = sbtghpackages.TokenSource
val TokenSource = sbtghpackages.TokenSource

implicit class GHPackagesResolverSyntax(val resolver: Resolver.type) extends AnyVal {
def githubPackages(owner: String, repo: String = "_"): MavenRepository =
realm(owner, repo) at s"https://maven.pkg.github.com/$owner/$repo"
}
type TokenSource = sbtgh.TokenSource
val TokenSource = sbtgh.TokenSource
}

import autoImport._
import sbtgh.GitHubResolver._

val authenticationSettings = Seq(
githubTokenSource := TokenSource.Environment("GITHUB_TOKEN"),
Expand Down Expand Up @@ -113,33 +107,6 @@ object GitHubPackagesPlugin extends AutoPlugin {
publishMavenStyle := true) ++
authenticationSettings

def resolveTokenSource(tokenSource: TokenSource): Option[String] = {
tokenSource match {
case TokenSource.Or(primary, secondary) =>
resolveTokenSource(primary).orElse(
resolveTokenSource(secondary))

case TokenSource.Environment(variable) =>
sys.env.get(variable)

case TokenSource.GitConfig(key) =>
Try(s"git config $key".!!).map(_.trim).toOption
}
}

def inferredGitHubCredentials(user: String, tokenSource: TokenSource): Option[Credentials] = {
resolveTokenSource(tokenSource) map { token =>
Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
user,
token)
}
}

private def realm(owner: String, repo: String) =
s"GitHub Package Registry (${owner}${if (repo != "_") s"/$repo" else ""})"

override def projectSettings = packagePublishSettings

override def buildSettings = Seq(githubSuppressPublicationWarning := false)
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.3.3
sbt.version=1.4.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2020 Daniel Spiewak
*
* 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 sbtgh.remote_cache

import sbt._
import sbtgh.TokenSource

import scala.concurrent.duration._

trait GitHubRemoteCacheKeys {
val githubRemoteCacheTokenSource = settingKey[TokenSource](
"Where to get the API token used in publication (defaults to github.token in the git config)")
val githubRemoteCacheOrganization = settingKey[String](
"GitHub organization name to push to")
val githubRemoteCacheRepository = settingKey[String](
"GitHub repository to publish to (default: remote-cache)")
val githubRemoteCachePackage = settingKey[String](
"GitHub package name")
val githubRemoteCacheCleanOld = taskKey[Unit](
"Clean old remote cache")
val githubRemoteCacheMinimum = settingKey[Int](
s"Minimum number of cache to keep around (default: ${GitHubRemoteDefaults.minimum})")
val githubRemoteCacheTtl = settingKey[Duration](
s"Time to keep remote cache around (default: ${GitHubRemoteDefaults.ttl})")
}

object GitHubRemoteCacheKeys extends GitHubRemoteCacheKeys

object GitHubRemoteDefaults {
def minimum: Int = 100
def ttl: Duration = Duration(30, DAYS)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package sbtgh.remote_cache

import sbt._
import sbt.Keys.{aggregate, pushRemoteCacheTo, remoteCacheResolvers, streams}
import sbt.{AutoPlugin, Def, Setting, Task}
import sbtgh.packages.GitHubPackagesKeys

object GitHubRemoteCachePlugin extends AutoPlugin {
override def requires = sbt.plugins.JvmPlugin

override def trigger = allRequirements

object autoImport extends GitHubRemoteCacheKeys with GitHubPackagesKeys {
type TokenSource = sbtgh.TokenSource
val TokenSource = sbtgh.TokenSource
}

import autoImport._
import _root_.sbtgh.GitHubResolver._

override lazy val globalSettings: Seq[Setting[_]] = Seq(
githubRemoteCacheTokenSource := githubTokenSource.value,
githubRemoteCacheRepository := "remote-cache",
githubRemoteCacheMinimum := GitHubRemoteDefaults.minimum,
githubRemoteCacheTtl := GitHubRemoteDefaults.ttl
)

override lazy val buildSettings: Seq[Setting[_]] = Seq(
githubRemoteCacheCleanOld := packageCleanOldVersionsTask.value,
githubRemoteCacheCleanOld / aggregate := false
)

override lazy val projectSettings: Seq[Setting[_]] = Seq(
pushRemoteCacheTo := publishToGitHubSetting.value,
remoteCacheResolvers := {
val ghOrg = githubRemoteCacheOrganization.value
val repoName = githubRemoteCacheRepository.value
List(Resolver.githubPackages(ghOrg, repoName))
}
)

private def publishToGitHubSetting =
Def.setting {
val tokenSource = githubRemoteCacheTokenSource.value
val ghOrg = githubRemoteCacheOrganization.value
val repoName = githubRemoteCacheRepository.value
//val context = GitHubCredentialContext.remoteCache(credsFile)
//github.withRepo(context, Some(ghOrg), repoName, sLog.value) { repo =>
// repo.buildRemoteCacheResolver(githubRemoteCachePackage.value, sLog.value)
//}
???
}

def packageCleanOldVersionsTask: Def.Initialize[Task[Unit]] =
Def.task {
val tokenSource = githubRemoteCacheTokenSource.value
val ghOrg = githubRemoteCacheOrganization.value
val repoName = githubRemoteCacheRepository.value
//val context = GitHubCredentialContext.remoteCache(credsFile)
val pkg = githubRemoteCachePackage.value
val s = streams.value
val min = githubRemoteCacheMinimum.value
val ttl = githubRemoteCacheTtl.value
//GitHub.withRepo(context, Some(btyOrg), repoName, s.log) { repo =>
// repo.cleandOldVersions(pkg, min, ttl, s.log)
//}
???
}
}