Skip to content

Commit

Permalink
Rewrite this plugin and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Oct 28, 2016
1 parent 4269404 commit 5da2d1a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 49 deletions.
21 changes: 16 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ startYear := Some(2015)

licenses := Seq("Apache License, Version 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0.html"))

scmInfo := Some(ScmInfo(
url(raw"""https://github.com/ThoughtWorksInc/${name.value}"""),
raw"""scm:git:https://github.com/ThoughtWorksInc/${name.value}.git""",
Some(raw"""scm:git:[email protected]:ThoughtWorksInc/${name.value}.git""")))
scmInfo := Some(
ScmInfo(url(raw"""https://github.com/ThoughtWorksInc/${name.value}"""),
raw"""scm:git:https://github.com/ThoughtWorksInc/${name.value}.git""",
Some(raw"""scm:git:[email protected]:ThoughtWorksInc/${name.value}.git""")))

pomExtra :=
<developers>
Expand All @@ -34,9 +34,20 @@ pomExtra :=
scalacOptions += "-deprecation"

releaseProcess := {
releaseProcess.value.patch(releaseProcess.value.indexOf(pushChanges), Seq[ReleaseStep](releaseStepCommand("sonatypeRelease")), 0)
releaseProcess.value
.patch(releaseProcess.value.indexOf(pushChanges), Seq[ReleaseStep](releaseStepCommand("sonatypeRelease")), 0)
}

releaseProcess -= runClean

releaseProcess -= runTest

libraryDependencies += "com.thoughtworks.extractor" %% "extractor" % "1.0.4"

scriptedSettings

scriptedLaunchOpts += s"-Dplugin.version=${version.value}"

scriptedBufferLog := false

test := scripted.toTask("").value
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.5.0")

addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.1")

libraryDependencies += "org.scala-sbt" % "scripted-plugin" % sbtVersion.value
84 changes: 41 additions & 43 deletions src/main/scala/com/thoughtworks/sbtApiMappings/ApiMappings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,60 @@ 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 com.thoughtworks.sbtApiMappings

import sbt._
import Keys._
import com.thoughtworks.Extractor._
import scala.Ordering.Implicits._

object ApiMappings extends AutoPlugin {

private val ScalaInstanceLibraryRegex = """^([\w\-]+)(?:_[\d\.]+)?(?:\-[\d\.]+)?\.jar$""".r

private val ScalaLibraryRegex = """^.*[/\\]scala-library-([\d\.]+)\.jar$""".r

private val IvyRegex = """^.*[/\\]([\.\-_\w]+)[/\\]([\.\-_\w]+)[/\\](?:jars|bundles)[/\\]([\.\-_\w]+)\.jar$""".r

private val PlayRegex = """^.*[/\\]play(-[\w\-]+)?(_[\d\.]+)?-([\d\.]+).jar$""".r

override final def trigger = allRequirements

override final lazy val projectSettings = Seq(
autoAPIMappings := true,
apiMappings += scalaInstance.value.libraryJar -> url( raw"""http://scala-lang.org/files/archive/api/${scalaInstance.value.actualVersion}/index.html"""),
apiMappings ++= {
(for {
jarFile <- scalaInstance.value.jars
if jarFile != scalaInstance.value.libraryJar
ScalaInstanceLibraryRegex(libraryName) = jarFile.getName
} yield (jarFile -> url( raw"""http://scala-lang.org/files/archive/api/${scalaInstance.value.actualVersion}/$libraryName/index.html""")))(collection.breakOut(Map.canBuildFrom))
},
apiMappings ++= {
(for {
jar <- (dependencyClasspath in Compile in doc).value.toSet ++ (dependencyClasspath in Test in doc).value
fullyFile = jar.data
urlOption = fullyFile.getCanonicalPath match {
case ScalaLibraryRegex(v) => {
Some(url(raw"""http://scala-lang.org/files/archive/api/$v/index.html"""))
}
case PlayRegex(_, _, VersionNumber(Seq(majorVersion, minorVersion, _*), _, _)) => {
Some(url(raw"""https://playframework.com/documentation/$majorVersion.$minorVersion.x/api/scala/index.html"""))
}
case IvyRegex(organization, name, jarBaseFile) if jarBaseFile.startsWith(s"$name-") => {
val version = jarBaseFile.substring(name.length + 1, jarBaseFile.length)
organization match {
case "org.apache.spark" =>
Some(url(raw"""https://spark.apache.org/docs/$version/api/scala/index.html"""))
case _ =>
Some(url(raw"""https://oss.sonatype.org/service/local/repositories/public/archive/${organization.replace('.', '/')}/$name/$version/$jarBaseFile-javadoc.jar/!/index.html"""))
}
}
case _ => {
None
}
}
url <- urlOption
} yield (fullyFile -> url))(collection.breakOut(Map.canBuildFrom))
})
val getModuleID = { attributed: Attributed[File] =>
attributed.get(moduleID.key)
}
val jars = ((dependencyClasspath in Compile in doc).value ++ (dependencyClasspath in Test in doc).value).distinct
jars.collect {
case jar @ getModuleID.extract(
ModuleID("org.scala-lang", "scala-library", revision, _, _, _, _, _, _, _, _)) =>
jar.data -> url(s"http://scala-lang.org/files/archive/api/$revision/index.html")
case jar @ getModuleID.extract(ModuleID("org.scala-lang", libraryName, revision, _, _, _, _, _, _, _, _))
// Scala web-site only contains API documentation for specific libraries after version 2.11.0
if libraryName.startsWith("scala-") && VersionNumber(revision).numbers >= Seq(2, 11, 0) =>
jar.data -> url(s"http://scala-lang.org/files/archive/api/$revision/$libraryName/index.html")
case jar @ getModuleID.extract(
ModuleID(
"com.typesafe.play",
libraryName,
VersionNumber(Seq(majorVersion, minorVersion, _ *), _, _),
_,
_,
_,
_,
_,
_,
_,
_
)
) if libraryName == "play" || libraryName.startsWith("play-") =>
jar.data -> url(
s"https://playframework.com/documentation/$majorVersion.$minorVersion.x/api/scala/index.html")
case jar @ getModuleID.extract(ModuleID("org.apache.spark", _, revision, _, _, _, _, _, _, _, _)) =>
jar.data -> url(raw"""https://spark.apache.org/docs/$revision/api/scala/index.html""")
case jar @ getModuleID.extract(ModuleID(organization, libraryName, revision, _, _, _, _, _, _, _, _))
if !apiMappings.value.contains(jar.data) =>
jar.data -> url(raw"""https://oss.sonatype.org/service/local/repositories/public/archive/${organization
.replace('.', '/')}/$libraryName/$revision/$libraryName-$revision-javadoc.jar/!/index.html""")
}.toMap
}
)

}

Expand Down
22 changes: 22 additions & 0 deletions src/sbt-test/sbt-api-mappings/all-libraries/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.net.HttpURLConnection

enablePlugins(PlayScala)

sparkVersion := "2.0.1"

sparkComponents += "sql"

def isDownloadableApiDocumentation(url: URL) = {
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
try {
connection.getResponseCode == 200
} finally {
connection.disconnect()
}
}

TaskKey[Unit]("check-scala") := {
assert(isDownloadableApiDocumentation(apiMappings.value(scalaInstance.value.libraryJar)))
}

crossScalaVersions := Seq("2.10.6", "2.11.8")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % sys.props("plugin.version"))

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.4")

resolvers += "bintray-spark-packages" at "https://dl.bintray.com/spark-packages/maven/"

addSbtPlugin("org.spark-packages" % "sbt-spark-package" % "0.2.5")

4 changes: 4 additions & 0 deletions src/sbt-test/sbt-api-mappings/all-libraries/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> check-scala
> show api-mappings
> +check-scala
> +show api-mappings
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.2.5-SNAPSHOT"
version in ThisBuild := "1.0.0-SNAPSHOT"

0 comments on commit 5da2d1a

Please sign in to comment.