Skip to content

Commit e2eab49

Browse files
authored
Make specifying the link argument optional (#99)
* Simplify command composition and use Seq.empty * Make specifying the link argument optional * Change var to val
1 parent 2a40276 commit e2eab49

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

tools/src/main/scala/org/scalanative/bindgen/Bindgen.scala

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.scalanative.bindgen
22

33
import java.io.File
44

5-
import scala.collection.immutable
5+
import scala.collection.immutable.Seq
66
import scala.sys.process.Process
77

88
sealed trait Bindgen {
@@ -58,15 +58,14 @@ sealed trait Bindgen {
5858
object Bindgen {
5959
def apply(): Bindgen = Impl()
6060

61-
private final case class Impl(
62-
executable: Option[File] = None,
63-
library: Option[String] = None,
64-
header: Option[File] = None,
65-
name: Option[String] = None,
66-
packageName: Option[String] = None,
67-
excludePrefix: Option[String] = None,
68-
extraArg: immutable.Seq[String] = immutable.Seq[String](),
69-
extraArgBefore: immutable.Seq[String] = immutable.Seq[String]())
61+
private final case class Impl(executable: Option[File] = None,
62+
library: Option[String] = None,
63+
header: Option[File] = None,
64+
name: Option[String] = None,
65+
packageName: Option[String] = None,
66+
excludePrefix: Option[String] = None,
67+
extraArg: Seq[String] = Seq.empty,
68+
extraArgBefore: Seq[String] = Seq.empty)
7069
extends Bindgen {
7170

7271
def bindgenExecutable(executable: File): Bindgen = {
@@ -109,38 +108,26 @@ object Bindgen {
109108
copy(extraArgBefore = extraArgBefore ++ args)
110109
}
111110

112-
private def validateFields(): Unit = {
111+
def generate(): Bindings = {
113112
require(executable.isDefined, "The executable must be specified")
114113
require(header.isDefined, "Header file must be specified")
115-
require(library.isDefined, "Library name must be specified")
116-
}
117-
118-
def generate(): Bindings = {
119-
validateFields()
120-
121-
val name = this.name.getOrElse(library.get)
122-
123-
var cmd = Seq(
124-
executable.get.getAbsolutePath,
125-
header.get.getAbsolutePath,
126-
"--name",
127-
name,
128-
"--link",
129-
library.get
130-
)
131-
132-
if (packageName.isDefined) {
133-
cmd ++= Seq("--package", packageName.get)
134-
}
135-
136-
if (excludePrefix.isDefined) {
137-
cmd ++= Seq("--exclude-prefix", excludePrefix.get)
138-
}
139-
140-
extraArg.foreach(arg => cmd ++= Seq("--extra-arg", arg))
141-
extraArgBefore.foreach(arg => cmd ++= Seq("--extra-arg-before", arg))
142114

143-
cmd :+= "--"
115+
val nameOrLibrary = name.orElse(library)
116+
require(nameOrLibrary.isDefined,
117+
"Name must be specified when no library name is given")
118+
119+
def withArgs(arg: String, values: Iterable[String]) =
120+
values.toSeq.flatMap(Seq(arg, _))
121+
122+
val cmd = Seq(executable.get.getAbsolutePath) ++
123+
withArgs("--name", nameOrLibrary) ++
124+
withArgs("--link", library) ++
125+
library.fold(Seq("--no-link"))(_ => Seq.empty) ++
126+
withArgs("--package", packageName) ++
127+
withArgs("--exclude-prefix", excludePrefix) ++
128+
withArgs("--extra-arg", extraArg) ++
129+
withArgs("--extra-arg-before", extraArgBefore) ++
130+
Seq(header.get.getAbsolutePath, "--")
144131

145132
val output = Process(cmd).lineStream.mkString("\n")
146133

0 commit comments

Comments
 (0)