@@ -3,6 +3,9 @@ package org.scalanative.bindgen.sbt
33import sbt ._
44import sbt .Keys ._
55
6+ import java .nio .file .Files
7+ import java .nio .file .attribute .{PosixFileAttributeView , PosixFilePermission }
8+
69import org .scalanative .bindgen .Bindgen
710
811/**
@@ -42,8 +45,9 @@ import org.scalanative.bindgen.Bindgen
4245object ScalaNativeBindgenPlugin extends AutoPlugin {
4346
4447 object autoImport {
48+ val ScalaNativeBindgen = config(" scala-native-bindgen" ).hide
4549 val nativeBindgenPath =
46- taskKey[Option [ File ] ](" Path to the scala-native-bindgen executable" )
50+ taskKey[File ](" Path to the scala-native-bindgen executable" )
4751 val nativeBindgenHeader = taskKey[File ](" C header file" )
4852 val nativeBindgenPackage =
4953 settingKey[Option [String ]](" Package for the generated code" )
@@ -57,7 +61,46 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
5761 override def requires = plugins.JvmPlugin
5862
5963 override def projectSettings : Seq [Setting [_]] =
60- nativeBindgenScopedSettings(Compile )
64+ inConfig(ScalaNativeBindgen )(Defaults .configSettings) ++
65+ nativeBindgenScopedSettings(Compile ) ++
66+ Def .settings(
67+ ivyConfigurations += ScalaNativeBindgen ,
68+ version in nativeBindgen := BuildInfo .version,
69+ libraryDependencies ++= {
70+ artifactName.map { name =>
71+ val bindgenVersion = (version in nativeBindgen).value
72+ val url =
73+ s " ${BuildInfo .projectUrl}/releases/download/v $bindgenVersion/ $name"
74+
75+ BuildInfo .organization % name % bindgenVersion % ScalaNativeBindgen from (url)
76+ }.toSeq
77+ },
78+ nativeBindgenPath := {
79+ val scalaNativeBindgenUpdate = (update in ScalaNativeBindgen ).value
80+
81+ val artifactFile = artifactName match {
82+ case None =>
83+ sys.error(
84+ " No downloadable binaries available for your OS, " +
85+ " please provide path via `nativeBindgenPath`" )
86+ case Some (name) =>
87+ scalaNativeBindgenUpdate
88+ .select(artifact = artifactFilter(name = name))
89+ .head
90+ }
91+
92+ // Set the executable bit on the expected path to fail if it doesn't exist
93+ for (view <- Option (
94+ Files .getFileAttributeView(artifactFile.toPath,
95+ classOf [PosixFileAttributeView ]))) {
96+ val permissions = view.readAttributes.permissions
97+ if (permissions.add(PosixFilePermission .OWNER_EXECUTE ))
98+ view.setPermissions(permissions)
99+ }
100+
101+ artifactFile
102+ }
103+ )
61104
62105 private implicit class BindgenOps (val bindgen : Bindgen ) extends AnyVal {
63106 def maybe [T ](opt : Option [T ], f : Bindgen => T => Bindgen ): Bindgen =
@@ -67,23 +110,28 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
67110 }
68111 }
69112
113+ private val artifactName =
114+ Option (System .getProperty(" os.name" )).collect {
115+ case " Mac OS X" => " scala-native-bindgen-darwin"
116+ case " Linux" => " scala-native-bindgen-linux"
117+ }
118+
70119 def nativeBindgenScopedSettings (conf : Configuration ): Seq [Setting [_]] =
71120 inConfig(conf)(
72- Seq (
121+ Def .settings (
73122 nativeBindgenHeader := {
74123 sys.error(" nativeBindgenHeader not configured" )
75124 },
76- nativeBindgenPath := None ,
77125 nativeBindgenPackage := None ,
78126 nativeBindgenExclude := None ,
79127 resourceDirectories in nativeBindgen := resourceDirectories.value,
80128 sourceGenerators += Def .task { Seq (nativeBindgen.value) },
81129 name in nativeBindgen := " ScalaNativeBindgen" ,
82130 nativeBindgen := {
83- val output = sourceManaged.value / " sbt-scala-native-bindgen" / " nativeBindgen .scala"
131+ val output = sourceManaged.value / " sbt-scala-native-bindgen" / " ScalaNativeBindgen .scala"
84132
85133 Bindgen ()
86- .bindgenExecutable(nativeBindgenPath.value.get )
134+ .bindgenExecutable(nativeBindgenPath.value)
87135 .header(nativeBindgenHeader.value)
88136 .name((name in nativeBindgen).value)
89137 .maybe(nativeBindgenLink.value, _.link)
0 commit comments