Skip to content

Commit 42573f8

Browse files
committed
Add support for downloading released executables from GitHub
1 parent d677fb7 commit 42573f8

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

build.sbt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,22 @@ lazy val tools = project("tools")
145145

146146
lazy val sbtPlugin = project("sbt-scala-native-bindgen", ScriptedPlugin)
147147
.dependsOn(tools)
148+
.enablePlugins(BuildInfoPlugin)
148149
.settings(
149150
Keys.sbtPlugin := true,
150151
scriptedLaunchOpts += s"-Dplugin.version=${version.value}",
151152
scriptedLaunchOpts += {
152153
val rootDir = (ThisBuild / baseDirectory).value
153154
s"-Dbindgen.path=$rootDir/bindgen/target/scala-native-bindgen"
154155
},
156+
buildInfoPackage := "org.scalanative.bindgen.sbt",
157+
buildInfoKeys := Seq[BuildInfoKey](
158+
version,
159+
organization,
160+
BuildInfoKey.map(scmInfo) {
161+
case (k, v) => "projectUrl" -> v.get.browseUrl
162+
}
163+
),
155164
publishLocal := publishLocal.dependsOn(tools / publishLocal).value
156165
)
157166

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.2")
55
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")
66
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.9")
77
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
8+
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
89

910
libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value

sbt-scala-native-bindgen/src/main/scala/org/scalanative/bindgen/sbt/ScalaNativeBindgenPlugin.scala

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package org.scalanative.bindgen.sbt
33
import sbt._
44
import sbt.Keys._
55

6+
import java.nio.file.Files
7+
import java.nio.file.attribute.{PosixFileAttributeView, PosixFilePermission}
8+
69
import org.scalanative.bindgen.Bindgen
710

811
/**
@@ -42,8 +45,9 @@ import org.scalanative.bindgen.Bindgen
4245
object 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)

sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ scalaVersion := "2.11.12"
55

66
inConfig(Compile)(
77
Def.settings(
8-
nativeBindgenPath := Option(System.getProperty("bindgen.path")).map(file),
8+
nativeBindgenPath := file(System.getProperty("bindgen.path")),
99
nativeBindgenHeader := (resourceDirectory in Compile).value / "header.h",
1010
nativeBindgenPackage := Some("org.example.app"),
1111
nativeBindgenLink := Some("app"),

0 commit comments

Comments
 (0)