Skip to content

Commit

Permalink
Merge pull request #18 from ThoughtWorksInc/javadoc.io
Browse files Browse the repository at this point in the history
Prefer javadoc.io over sonatype.org
  • Loading branch information
Atry authored Apr 4, 2018
2 parents 27a9c0c + 8f7de83 commit adef762
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 15 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.*
target/

# Files created for deployment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thoughtworks.sbtApiMappings

import sbt._
import com.thoughtworks.Extractor._
import sbt.internal.librarymanagement.mavenint.PomExtraDependencyAttributes

object JavadocIoApiMappingRule extends AutoPlugin {

import ApiMappings.autoImport._

override def requires = ApiMappings

override def trigger = allRequirements

private def nonSbtModuleID: Attributed[File] => Option[(String, String, String)] = { jar =>
for {
moduleID <- jar.get(Keys.moduleID.key)
if !moduleID.extraAttributes.contains(PomExtraDependencyAttributes.SbtVersionKey)
} yield (moduleID.organization, moduleID.name, moduleID.revision)
}

private def javadocIoRule: PartialFunction[Attributed[File], URL] = {
case nonSbtModuleID.extract(organization, libraryName, revision) =>
val organizationPath = organization.replace('.', '/')
url(s"https://javadoc.io/page/$organization/$libraryName/$revision/index.html")
}

override def projectSettings = {
apiMappingRules := apiMappingRules.value.orElse(javadocIoRule)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ package com.thoughtworks.sbtApiMappings

import sbt._
import com.thoughtworks.Extractor._
import sbt.internal.librarymanagement.mavenint.PomExtraDependencyAttributes

/** Mapping to sonatype.org URL for all artifacts */
object SonatypeApiMappingRule extends AutoPlugin {

import ApiMappings.autoImport._

override def requires = ApiMappings

override def trigger = allRequirements
override def trigger = noTrigger

private val JarBaseNameRegex = """(.*)\.jar""".r

private def baseNameAndModuleID: Attributed[File] => Option[(String, String, String, String)] = { jar =>
private def nonSbtModuleID: Attributed[File] => Option[(String,String, String, String)] = { jar =>
jar.data.getName match {
case JarBaseNameRegex(baseName) =>
jar.get(Keys.moduleID.key).map { moduleID =>
(baseName, moduleID.organization, moduleID.name, moduleID.revision)
}
for {
moduleID <- jar.get(Keys.moduleID.key)
if !moduleID.extraAttributes.contains(PomExtraDependencyAttributes.SbtVersionKey)
} yield (baseName, moduleID.organization, moduleID.name, moduleID.revision)
case _ =>
None
}
}

private def sonatypeRule: PartialFunction[Attributed[File], URL] = {
case baseNameAndModuleID.extract(baseName, organization, libraryName, revision) =>
case nonSbtModuleID.extract(baseName, organization, libraryName, revision) =>
val organizationPath = organization.replace('.', '/')
url(s"https://oss.sonatype.org/service/local/repositories/public/archive/$organizationPath/$libraryName/$revision/$baseName-javadoc.jar/!/index.html")
url(
s"https://oss.sonatype.org/service/local/repositories/public/archive/$organizationPath/$libraryName/$revision/$baseName-javadoc.jar/!/index.html")
}

override def projectSettings = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.thoughtworks.sbtApiMappings

import sbt._
import com.thoughtworks.Extractor._
import sbt.internal.librarymanagement.mavenint.PomExtraDependencyAttributes

/** Mapping to sonatype.org URL only for artifacts that are not supported by javadoc.io */
object SonatypeApiMappingRuleForSbtPlugins extends AutoPlugin {

import ApiMappings.autoImport._

override def requires = ApiMappings

override def trigger = allRequirements

private val JarBaseNameRegex = """(.*)\.jar""".r

private def sbtModuleID: Attributed[File] => Option[(String, String, String, String, String, String)] = { jar =>
jar.data.getName match {
case JarBaseNameRegex(baseName) =>
for {
moduleID <- jar.get(Keys.moduleID.key)
sbtVerion <- moduleID.extraAttributes.get(PomExtraDependencyAttributes.SbtVersionKey)
scalaVerion <- moduleID.extraAttributes.get(PomExtraDependencyAttributes.ScalaVersionKey)
} yield (baseName, moduleID.organization, moduleID.name, moduleID.revision, scalaVerion, sbtVerion)
case _ =>
None
}
}

private def sonatypeRule: PartialFunction[Attributed[File], URL] = {
case sbtModuleID.extract(baseName, organization, libraryName, revision, scalaVersion, sbtVersion) =>
val organizationPath = organization.replace('.', '/')
url(
s"https://oss.sonatype.org/service/local/repositories/public/archive/$organizationPath/${libraryName}_${scalaVersion}_${sbtVersion}/$revision/$baseName-javadoc.jar/!/index.html")
}

override def projectSettings = {
apiMappingRules := apiMappingRules.value.orElse(sonatypeRule)
}

}
14 changes: 7 additions & 7 deletions src/sbt-test/sbt-api-mappings/all-libraries-2.12/build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
def isDownloadableApiDocumentation(url: URL) = {
def assertDownloadableApiDocumentation(url: URL) = {
import java.net.HttpURLConnection
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
try {
connection.getResponseCode == 200
assert((200 to 399).contains(connection.getResponseCode),
s"Unexpected HTTP response code ${connection.getResponseCode} when fetching $url")
} finally {
connection.disconnect()
}
}

def assertDownloadableApiDocumentation(url: URL) = {
assert(isDownloadableApiDocumentation(url), s"Cannot open $url!")
}

val check = TaskKey[Unit]("check")

val scalacheckModuleId = "org.scalacheck" %% "scalacheck" % "1.13.4"
Expand All @@ -23,7 +20,10 @@ check := {
scalacheckModuleId,
Artifact("scalacheck"))
val Some((_, url)) = (apiMappings in Test in doc).value.find(_._1.getName == scalacheckJarName)
assertDownloadableApiDocumentation(url)

// The expected URL is browsable but not accessible from java.net.HttpURLConnection
val expectedUrl = "https://javadoc.io/page/org.scalacheck/scalacheck_2.12/1.13.4/index.html"
assert(url.toString == expectedUrl)
assert(!(apiMappings in Compile in doc).value.exists(_._1.getName == scalacheckJarName))
}

Expand Down

0 comments on commit adef762

Please sign in to comment.