From bf76868ddb684696a28b020a6f8b4fc85b04bc75 Mon Sep 17 00:00:00 2001 From: Eric Peters Date: Fri, 1 Jan 2021 14:09:03 -0800 Subject: [PATCH] WIP - start refactoring into two projects and adding github remote cache support --- build.sbt | 46 ++++++++++--- .../src/main/scala/sbtgh/GitHubResolver.scala | 40 +++++++++++ .../src/main/scala/sbtgh}/TokenSource.scala | 2 +- .../sbtgh/packages}/GitHubPackagesKeys.scala | 12 ++-- .../packages}/GitHubPackagesPlugin.scala | 45 ++---------- .../sbt-test/sbtghpackages/publish/build.sbt | 0 .../publish/project/build.properties | 0 .../sbtghpackages/publish/project/plugins.sbt | 0 .../src}/sbt-test/sbtghpackages/publish/test | 0 .../sbt-test/sbtghpackages/startup/build.sbt | 0 .../startup/project/build.properties | 0 .../sbtghpackages/startup/project/plugins.sbt | 0 .../src}/sbt-test/sbtghpackages/startup/test | 0 project/build.properties | 2 +- .../remote_cache/GitHubRemoteCacheKeys.scala | 46 +++++++++++++ .../GitHubRemoteCachePlugin.scala | 69 +++++++++++++++++++ 16 files changed, 206 insertions(+), 56 deletions(-) create mode 100644 common/src/main/scala/sbtgh/GitHubResolver.scala rename {src/main/scala/sbtghpackages => common/src/main/scala/sbtgh}/TokenSource.scala (97%) rename {src/main/scala/sbtghpackages => packages/src/main/scala/sbtgh/packages}/GitHubPackagesKeys.scala (72%) rename {src/main/scala/sbtghpackages => packages/src/main/scala/sbtgh/packages}/GitHubPackagesPlugin.scala (70%) rename {src => packages/src}/sbt-test/sbtghpackages/publish/build.sbt (100%) rename {src => packages/src}/sbt-test/sbtghpackages/publish/project/build.properties (100%) rename {src => packages/src}/sbt-test/sbtghpackages/publish/project/plugins.sbt (100%) rename {src => packages/src}/sbt-test/sbtghpackages/publish/test (100%) rename {src => packages/src}/sbt-test/sbtghpackages/startup/build.sbt (100%) rename {src => packages/src}/sbt-test/sbtghpackages/startup/project/build.properties (100%) rename {src => packages/src}/sbt-test/sbtghpackages/startup/project/plugins.sbt (100%) rename {src => packages/src}/sbt-test/sbtghpackages/startup/test (100%) create mode 100644 remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCacheKeys.scala create mode 100644 remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCachePlugin.scala diff --git a/build.sbt b/build.sbt index 2ce20e6..f68acb0 100644 --- a/build.sbt +++ b/build.sbt @@ -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 diff --git a/common/src/main/scala/sbtgh/GitHubResolver.scala b/common/src/main/scala/sbtgh/GitHubResolver.scala new file mode 100644 index 0000000..00410ec --- /dev/null +++ b/common/src/main/scala/sbtgh/GitHubResolver.scala @@ -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 ""})" +} \ No newline at end of file diff --git a/src/main/scala/sbtghpackages/TokenSource.scala b/common/src/main/scala/sbtgh/TokenSource.scala similarity index 97% rename from src/main/scala/sbtghpackages/TokenSource.scala rename to common/src/main/scala/sbtgh/TokenSource.scala index 85ad3ae..37515ce 100644 --- a/src/main/scala/sbtghpackages/TokenSource.scala +++ b/common/src/main/scala/sbtgh/TokenSource.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package sbtghpackages +package sbtgh sealed trait TokenSource extends Product with Serializable { def ||(that: TokenSource): TokenSource = diff --git a/src/main/scala/sbtghpackages/GitHubPackagesKeys.scala b/packages/src/main/scala/sbtgh/packages/GitHubPackagesKeys.scala similarity index 72% rename from src/main/scala/sbtghpackages/GitHubPackagesKeys.scala rename to packages/src/main/scala/sbtgh/packages/GitHubPackagesKeys.scala index 41d91fb..f8ae393 100644 --- a/src/main/scala/sbtghpackages/GitHubPackagesKeys.scala +++ b/packages/src/main/scala/sbtgh/packages/GitHubPackagesKeys.scala @@ -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)") diff --git a/src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala b/packages/src/main/scala/sbtgh/packages/GitHubPackagesPlugin.scala similarity index 70% rename from src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala rename to packages/src/main/scala/sbtgh/packages/GitHubPackagesPlugin.scala index ad2fdc7..ca63b82 100644 --- a/src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala +++ b/packages/src/main/scala/sbtgh/packages/GitHubPackagesPlugin.scala @@ -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 @@ -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"), @@ -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) diff --git a/src/sbt-test/sbtghpackages/publish/build.sbt b/packages/src/sbt-test/sbtghpackages/publish/build.sbt similarity index 100% rename from src/sbt-test/sbtghpackages/publish/build.sbt rename to packages/src/sbt-test/sbtghpackages/publish/build.sbt diff --git a/src/sbt-test/sbtghpackages/publish/project/build.properties b/packages/src/sbt-test/sbtghpackages/publish/project/build.properties similarity index 100% rename from src/sbt-test/sbtghpackages/publish/project/build.properties rename to packages/src/sbt-test/sbtghpackages/publish/project/build.properties diff --git a/src/sbt-test/sbtghpackages/publish/project/plugins.sbt b/packages/src/sbt-test/sbtghpackages/publish/project/plugins.sbt similarity index 100% rename from src/sbt-test/sbtghpackages/publish/project/plugins.sbt rename to packages/src/sbt-test/sbtghpackages/publish/project/plugins.sbt diff --git a/src/sbt-test/sbtghpackages/publish/test b/packages/src/sbt-test/sbtghpackages/publish/test similarity index 100% rename from src/sbt-test/sbtghpackages/publish/test rename to packages/src/sbt-test/sbtghpackages/publish/test diff --git a/src/sbt-test/sbtghpackages/startup/build.sbt b/packages/src/sbt-test/sbtghpackages/startup/build.sbt similarity index 100% rename from src/sbt-test/sbtghpackages/startup/build.sbt rename to packages/src/sbt-test/sbtghpackages/startup/build.sbt diff --git a/src/sbt-test/sbtghpackages/startup/project/build.properties b/packages/src/sbt-test/sbtghpackages/startup/project/build.properties similarity index 100% rename from src/sbt-test/sbtghpackages/startup/project/build.properties rename to packages/src/sbt-test/sbtghpackages/startup/project/build.properties diff --git a/src/sbt-test/sbtghpackages/startup/project/plugins.sbt b/packages/src/sbt-test/sbtghpackages/startup/project/plugins.sbt similarity index 100% rename from src/sbt-test/sbtghpackages/startup/project/plugins.sbt rename to packages/src/sbt-test/sbtghpackages/startup/project/plugins.sbt diff --git a/src/sbt-test/sbtghpackages/startup/test b/packages/src/sbt-test/sbtghpackages/startup/test similarity index 100% rename from src/sbt-test/sbtghpackages/startup/test rename to packages/src/sbt-test/sbtghpackages/startup/test diff --git a/project/build.properties b/project/build.properties index 6adcdc7..d91c272 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.3.3 +sbt.version=1.4.6 diff --git a/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCacheKeys.scala b/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCacheKeys.scala new file mode 100644 index 0000000..47582f6 --- /dev/null +++ b/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCacheKeys.scala @@ -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) +} \ No newline at end of file diff --git a/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCachePlugin.scala b/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCachePlugin.scala new file mode 100644 index 0000000..8de2560 --- /dev/null +++ b/remote_cache/src/main/scala/sbtgh/remote_cache/GitHubRemoteCachePlugin.scala @@ -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) + //} + ??? + } +}