2018-11-20 13:15:26 +01:00
package coursier.sbtcoursiershared
2018-11-22 14:14:35 +01:00
import java.io. { File , OutputStreamWriter }
import coursier. { Cache , Credentials , TermDisplay }
2018-11-21 15:29:50 +01:00
import coursier.core. { Configuration , Project , Publication }
2018-11-22 14:14:35 +01:00
import coursier.lmcoursier. { CreateLogger , FallbackDependency , SbtCoursierCache }
2018-11-21 11:18:35 +01:00
import sbt. { AutoPlugin , Classpaths , Compile , Setting , TaskKey , Test , settingKey , taskKey }
import sbt.Keys._
import sbt.librarymanagement. { Resolver , URLRepository }
2018-11-20 13:15:26 +01:00
object SbtCoursierShared extends AutoPlugin {
override def trigger = allRequirements
override def requires = sbt . plugins . JvmPlugin
object autoImport {
2018-11-20 17:17:53 +01:00
val coursierGenerateIvyXml = settingKey [ Boolean ] ( "" )
2018-11-20 13:15:26 +01:00
val coursierProject = TaskKey [ Project ] ( "coursier-project" )
val coursierInterProjectDependencies = TaskKey [ Seq [ Project ] ] ( "coursier-inter-project-dependencies" , "Projects the current project depends on, possibly transitively" )
2018-11-20 17:17:53 +01:00
val coursierPublications = TaskKey [ Seq [ ( Configuration , Publication ) ] ] ( "coursier-publications" )
2018-11-20 18:01:24 +01:00
val coursierKeepPreloaded = settingKey [ Boolean ] ( "Whether to take into account sbt preloaded repositories or not" )
val coursierReorderResolvers = settingKey [ Boolean ] (
"Whether resolvers should be re-ordered so that typically slow ones are given a lower priority"
)
val coursierResolvers = taskKey [ Seq [ Resolver ] ] ( "" )
val coursierRecursiveResolvers = taskKey [ Seq [ Resolver ] ] ( "Resolvers of the current project, plus those of all from its inter-dependency projects" )
2018-11-21 11:18:35 +01:00
val coursierSbtResolvers = taskKey [ Seq [ Resolver ] ] ( "" )
2018-11-21 13:21:41 +01:00
val coursierFallbackDependencies = taskKey [ Seq [ FallbackDependency ] ] ( "" )
2018-11-22 13:04:35 +01:00
val mavenProfiles = settingKey [ Set [ String ] ] ( "" )
2018-11-22 13:52:59 +01:00
val coursierUseSbtCredentials = settingKey [ Boolean ] ( "" )
val coursierCredentials = taskKey [ Map [ String , Credentials ] ] ( "" )
2018-11-22 14:14:35 +01:00
val coursierCreateLogger = taskKey [ CreateLogger ] ( "" )
val coursierCache = settingKey [ File ] ( "" )
type CoursierCreateLogger = coursier . lmcoursier . CreateLogger
val CoursierCreateLogger = coursier . lmcoursier . CreateLogger
2018-12-12 11:19:05 +01:00
val sbtCoursierVersion = Properties . version
2018-11-20 13:15:26 +01:00
}
import autoImport._
2018-11-20 17:17:53 +01:00
def publicationsSetting ( packageConfigs : Seq [ ( sbt . Configuration , Configuration ) ] ) : Setting [ _ ] =
coursierPublications : = ArtifactsTasks . coursierPublicationsTask ( packageConfigs : _ * ) . value
2018-11-20 18:01:24 +01:00
override def buildSettings : Seq [ Setting [ _ ] ] =
Seq (
coursierReorderResolvers : = true ,
2018-11-22 13:52:59 +01:00
coursierKeepPreloaded : = false ,
coursierUseSbtCredentials : = true ,
2018-11-22 14:14:35 +01:00
coursierCredentials : = Map . empty ,
coursierCreateLogger : = CreateLogger { ( ) => new TermDisplay ( new OutputStreamWriter ( System . err ) ) } ,
coursierCache : = Cache . default
2018-11-20 18:01:24 +01:00
)
2018-11-21 11:18:35 +01:00
private val pluginIvySnapshotsBase = Resolver . SbtRepositoryRoot . stripSuffix ( "/" ) + "/ivy-snapshots"
2018-11-20 18:01:24 +01:00
override def projectSettings = settings ( pubSettings = true )
def settings ( pubSettings : Boolean ) =
2018-11-20 17:17:53 +01:00
Seq [ Setting [ _ ] ] (
2018-11-20 17:39:19 +01:00
clean : = {
val noWarningPlz = clean . value
SbtCoursierCache . default . clear ( )
} ,
2018-11-20 17:17:53 +01:00
coursierGenerateIvyXml : = true ,
coursierProject : = InputsTasks . coursierProjectTask . value ,
2018-11-20 18:01:24 +01:00
coursierInterProjectDependencies : = InputsTasks . coursierInterProjectDependenciesTask . value
) ++ {
if ( pubSettings )
Seq (
publicationsSetting ( Seq ( Compile , Test ) . map ( c => c -> Configuration ( c . name ) ) )
)
else
Nil
} ++ Seq (
2018-11-20 17:49:20 +01:00
// Tests artifacts from Maven repositories are given this type.
// Adding it here so that these work straightaway.
2018-11-20 18:01:24 +01:00
classpathTypes += "test-jar" , // FIXME Should this go in buildSettings?
coursierResolvers : = RepositoriesTasks . coursierResolversTask . value ,
coursierRecursiveResolvers : = RepositoriesTasks . coursierRecursiveResolversTask . value ,
2018-11-21 11:18:35 +01:00
coursierSbtResolvers : = {
// TODO Add docker-based integration test for that, see https://github.com/coursier/coursier/issues/632
val resolvers =
sbt . Classpaths . bootRepositories ( appConfiguration . value ) . toSeq . flatten ++ // required because of the hack above it seems
externalResolvers . in ( updateSbtClassifiers ) . value
val pluginIvySnapshotsFound = resolvers . exists {
case repo : URLRepository =>
repo
. patterns
. artifactPatterns
. headOption
. exists ( _ . startsWith ( pluginIvySnapshotsBase ) )
case _ => false
}
val resolvers0 =
if ( pluginIvySnapshotsFound && ! resolvers . contains ( Classpaths . sbtPluginReleases ) )
resolvers : + Classpaths.sbtPluginReleases
else
resolvers
if ( SbtCoursierShared . autoImport . coursierKeepPreloaded . value )
resolvers0
else
resolvers0 . filter { r =>
! r . name . startsWith ( "local-preloaded" )
}
2018-11-21 13:21:41 +01:00
} ,
coursierFallbackDependencies : = InputsTasks . coursierFallbackDependenciesTask . value ,
2018-11-21 15:29:50 +01:00
ivyConfigurations : = {
val confs = ivyConfigurations . value
val names = confs . map ( _ . name ) . toSet
// Yes, adding those back in sbt 1.0. Can't distinguish between config test (whose jars with classifier tests ought to
// be added), and sources / docs else (if their JARs are in compile, they would get added too then).
val extraSources =
if ( names ( "sources" ) )
None
else
Some (
sbt . Configuration . of (
id = "Sources" ,
name = "sources" ,
description = "" ,
isPublic = true ,
extendsConfigs = Vector . empty ,
transitive = false
)
)
val extraDocs =
if ( names ( "docs" ) )
None
else
Some (
sbt . Configuration . of (
id = "Docs" ,
name = "docs" ,
description = "" ,
isPublic = true ,
extendsConfigs = Vector . empty ,
transitive = false
)
)
confs ++ extraSources . toSeq ++ extraDocs . toSeq
2018-11-22 13:04:35 +01:00
} ,
mavenProfiles : = Set . empty
2018-11-20 18:01:24 +01:00
) ++ {
if ( pubSettings )
IvyXml . generateIvyXmlSettings ( )
else
Nil
}
2018-11-20 13:15:26 +01:00
}