@@ -2,7 +2,7 @@ package org.scalanative.bindgen
2
2
3
3
import java .io .File
4
4
5
- import scala .collection .immutable
5
+ import scala .collection .immutable . Seq
6
6
import scala .sys .process .Process
7
7
8
8
sealed trait Bindgen {
@@ -58,15 +58,14 @@ sealed trait Bindgen {
58
58
object Bindgen {
59
59
def apply (): Bindgen = Impl ()
60
60
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)
70
69
extends Bindgen {
71
70
72
71
def bindgenExecutable (executable : File ): Bindgen = {
@@ -109,38 +108,26 @@ object Bindgen {
109
108
copy(extraArgBefore = extraArgBefore ++ args)
110
109
}
111
110
112
- private def validateFields (): Unit = {
111
+ def generate (): Bindings = {
113
112
require(executable.isDefined, " The executable must be specified" )
114
113
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))
142
114
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, " --" )
144
131
145
132
val output = Process (cmd).lineStream.mkString(" \n " )
146
133
0 commit comments