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

Extract out a GitHub plugin #31

Merged
merged 9 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 12 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ ThisBuild / tlCiReleaseSnapshots := true
ThisBuild / tlCiReleaseBranches := Seq("main")

ThisBuild / developers := List(
Developer("armanbilge", "Arman Bilge", "@armanbilge", url("https://github.com/armanbilge")),
Developer("rossabaker", "Ross A. Baker", "@rossabaker", url("https://github.com/rossabaker")),
Developer(
"ChristopherDavenport",
"Christopher Davenport",
"@ChristopherDavenport",
url("https://github.com/ChristopherDavenport")),
Developer("djspiewak", "Daniel Spiewak", "@djspiewak", url("https://github.com/djspiewak"))
tlGitHubDev("armanbilge", "Arman Bilge"),
tlGitHubDev("rossabaker", "Ross A. Baker"),
tlGitHubDev("ChristopherDavenport", "Christopher Davenport"),
tlGitHubDev("djspiewak", "Daniel Spiewak")
)
ThisBuild / homepage := Some(url("https://github.com/typelevel/sbt-typelevel"))
ThisBuild / licenses += "Apache-2.0" -> url("http://www.apache.org/licenses/")
Expand Down Expand Up @@ -61,6 +57,14 @@ lazy val settings = project
)
.dependsOn(kernel)

lazy val github = project
.in(file("github"))
.enablePlugins(SbtPlugin)
.settings(
name := "sbt-typelevel-github"
)
.dependsOn(kernel)

lazy val versioning = project
.in(file("versioning"))
.enablePlugins(SbtPlugin)
Expand Down
2 changes: 2 additions & 0 deletions github/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to be careful with our versioning strategy here. I'm rolling this back from 1.8 because otherwise I can't use it with http4s-dom which is currently blocked by upstream issues with 1.8, see http4s/http4s-dom#60 (comment).

addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.typelevel.sbt

import sbt._, Keys._

import scala.util.Try

object TypelevelGitHubPlugin extends AutoPlugin {

override def trigger = allRequirements
override def requires = TypelevelKernelPlugin

object autoImport {

/**
* Helper to create a `Developer` entry from a GitHub username.
*/
def tlGitHubDev(user: String, fullName: String): Developer = {
Developer(user, fullName, s"@$user", url(s"https://github.com/$user"))
}
}

override def buildSettings = Seq(
scmInfo := getScmInfo()
)

def getScmInfo(): Option[ScmInfo] = {
import scala.sys.process._

def gitHubScmInfo(user: String, repo: String) =
ScmInfo(
url(s"https://github.com/$user/$repo"),
s"scm:git:https://github.com/$user/$repo.git",
s"scm:git:git@github.com:$user/$repo.git"
)

val identifier = """([^\/]+?)"""
val GitHubHttps = s"https://github.com/$identifier/$identifier(?:\\.git)?".r
val GitHubGit = s"git://github.com:$identifier/$identifier(?:\\.git)?".r
val GitHubSsh = s"git@github.com:$identifier/$identifier(?:\\.git)?".r
Try {
val remote = List("git", "ls-remote", "--get-url", "origin").!!.trim()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth calling out this is how it works. Not everybody calls origin origin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I was mostly concerned with what the CI runners do, since you want to get this right on publish.

remote match {
case GitHubHttps(user, repo) => Some(gitHubScmInfo(user, repo))
case GitHubGit(user, repo) => Some(gitHubScmInfo(user, repo))
case GitHubSsh(user, repo) => Some(gitHubScmInfo(user, repo))
case _ => None
}
}.toOption.flatten
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package org.typelevel.sbt
import sbt._, Keys._
import org.scalajs.sbtplugin.ScalaJSPlugin
import com.typesafe.sbt.SbtGit.git
import org.typelevel.sbt.kernel.GitHelper

object TypelevelScalaJSSettingsPlugin extends AutoPlugin {
object TypelevelScalaJSGitHubPlugin extends AutoPlugin {
override def trigger = allRequirements
override def requires = ScalaJSPlugin && TypelevelSettingsPlugin

Expand All @@ -15,7 +16,7 @@ object TypelevelScalaJSSettingsPlugin extends AutoPlugin {
val flag = if (tlIsScala3.value) "-scalajs-mapSourceURI:" else "-P:scalajs:mapSourceURI:"

val tagOrHash =
TypelevelSettingsPlugin.getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)
GitHelper.getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)

val l = (LocalRootProject / baseDirectory).value.toURI.toString

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ object GitHelper {
}.getOrElse(List.empty).sorted.reverse
}

def getTagOrHash(tags: Seq[String], hash: Option[String]): Option[String] =
tags.collect { case v @ V.Tag(_) => v }.headOption.orElse(hash)

}
1 change: 1 addition & 0 deletions project/github.sbt
1 change: 1 addition & 0 deletions project/src/main/scala/github
2 changes: 0 additions & 2 deletions settings/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.8.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.2")
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import sbt._, Keys._
import com.typesafe.sbt.GitPlugin
import com.typesafe.sbt.SbtGit.git
import org.typelevel.sbt.kernel.V

import scala.util.Try
import org.typelevel.sbt.kernel.GitHelper

object TypelevelSettingsPlugin extends AutoPlugin {
override def trigger = allRequirements
Expand All @@ -24,10 +23,6 @@ object TypelevelSettingsPlugin extends AutoPlugin {
Def.derive(scalaVersion := crossScalaVersions.value.last, default = true)
)

override def buildSettings = Seq(
scmInfo := getScmInfo()
)

override def projectSettings = Seq(
versionScheme := Some("early-semver"),
pomIncludeRepository := { _ => false },
Expand Down Expand Up @@ -137,7 +132,8 @@ object TypelevelSettingsPlugin extends AutoPlugin {
Seq("-sourcepath", (LocalRootProject / baseDirectory).value.getAbsolutePath)
else {

val tagOrHash = getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)
val tagOrHash =
GitHelper.getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)

val infoOpt = scmInfo.value
tagOrHash.toSeq flatMap { vh =>
Expand Down Expand Up @@ -165,32 +161,4 @@ object TypelevelSettingsPlugin extends AutoPlugin {
}
)

def getTagOrHash(tags: Seq[String], hash: Option[String]): Option[String] =
tags.collect { case v @ V.Tag(_) => v }.headOption.orElse(hash)

def getScmInfo(): Option[ScmInfo] = {
import scala.sys.process._

def gitHubScmInfo(user: String, repo: String) =
ScmInfo(
url(s"https://github.com/$user/$repo"),
s"scm:git:https://github.com/$user/$repo.git",
s"scm:git:git@github.com:$user/$repo.git"
)

val identifier = """([^\/]+?)"""
val GitHubHttps = s"https://github.com/$identifier/$identifier(?:\\.git)?".r
val GitHubGit = s"git://github.com:$identifier/$identifier(?:\\.git)?".r
val GitHubSsh = s"git@github.com:$identifier/$identifier(?:\\.git)?".r
Try {
val remote = List("git", "ls-remote", "--get-url", "origin").!!.trim()
remote match {
case GitHubHttps(user, repo) => Some(gitHubScmInfo(user, repo))
case GitHubGit(user, repo) => Some(gitHubScmInfo(user, repo))
case GitHubSsh(user, repo) => Some(gitHubScmInfo(user, repo))
case _ => None
}
}.toOption.flatten
}

}