From 5da2d1a319776603f890394a9cd2a2047d038356 Mon Sep 17 00:00:00 2001 From: Yang Bo Date: Fri, 28 Oct 2016 13:49:21 +0800 Subject: [PATCH] Rewrite this plugin and add tests. --- build.sbt | 21 +++-- project/plugins.sbt | 1 + .../sbtApiMappings/ApiMappings.scala | 84 +++++++++---------- .../sbt-api-mappings/all-libraries/build.sbt | 22 +++++ .../all-libraries/project/plugins.sbt | 8 ++ .../sbt-api-mappings/all-libraries/test | 4 + version.sbt | 2 +- 7 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 src/sbt-test/sbt-api-mappings/all-libraries/build.sbt create mode 100644 src/sbt-test/sbt-api-mappings/all-libraries/project/plugins.sbt create mode 100644 src/sbt-test/sbt-api-mappings/all-libraries/test diff --git a/build.sbt b/build.sbt index 6360705..1a3b626 100644 --- a/build.sbt +++ b/build.sbt @@ -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:git@github.com: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:git@github.com:ThoughtWorksInc/${name.value}.git"""))) pomExtra := @@ -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 \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 8ff4712..daf040c 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -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 diff --git a/src/main/scala/com/thoughtworks/sbtApiMappings/ApiMappings.scala b/src/main/scala/com/thoughtworks/sbtApiMappings/ApiMappings.scala index 8f8e26c..5766f1c 100644 --- a/src/main/scala/com/thoughtworks/sbtApiMappings/ApiMappings.scala +++ b/src/main/scala/com/thoughtworks/sbtApiMappings/ApiMappings.scala @@ -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 + } + ) } diff --git a/src/sbt-test/sbt-api-mappings/all-libraries/build.sbt b/src/sbt-test/sbt-api-mappings/all-libraries/build.sbt new file mode 100644 index 0000000..f290ed6 --- /dev/null +++ b/src/sbt-test/sbt-api-mappings/all-libraries/build.sbt @@ -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") diff --git a/src/sbt-test/sbt-api-mappings/all-libraries/project/plugins.sbt b/src/sbt-test/sbt-api-mappings/all-libraries/project/plugins.sbt new file mode 100644 index 0000000..5baa066 --- /dev/null +++ b/src/sbt-test/sbt-api-mappings/all-libraries/project/plugins.sbt @@ -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") + diff --git a/src/sbt-test/sbt-api-mappings/all-libraries/test b/src/sbt-test/sbt-api-mappings/all-libraries/test new file mode 100644 index 0000000..86a8f50 --- /dev/null +++ b/src/sbt-test/sbt-api-mappings/all-libraries/test @@ -0,0 +1,4 @@ +> check-scala +> show api-mappings +> +check-scala +> +show api-mappings diff --git a/version.sbt b/version.sbt index 0ed919e..8b9a52b 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.2.5-SNAPSHOT" \ No newline at end of file +version in ThisBuild := "1.0.0-SNAPSHOT" \ No newline at end of file