Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooling project #76

Merged
merged 6 commits into from
Jul 2, 2018
Merged

Tooling project #76

merged 6 commits into from
Jul 2, 2018

Conversation

kornilova203
Copy link
Member

Part of #60

Currently contains only API without proper implementation

@jonas
Copy link
Member

jonas commented Jun 27, 2018

I suggest we use something similar to https://github.com/scala-native/scala-native/blob/master/tools/src/main/scala/scala/scalanative/build/Config.scala i.e. a trait to define the public API and a case class to implement it.

import java.io.{File, PrintWriter}

class Bindings(private val bindings: String) {
def writeToFile(pathToFile: String): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using java.nio.file.Path here would be a better fit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose just File is also fine

/**
* Set header file for which bindings will be generated
*/
def header(pathToHeader: String): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, let's use java.nio.file.Path here to indicate the type properly.

* contain given prefix
*/
def excludePrefix(prefix: String): Unit = {
if (!prefix.isEmpty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think we need validation code, then let's use require to fail on invalid input.

@kornilova203 kornilova203 requested a review from jonas June 29, 2018 14:20
private var packageName: String = _
private var excludePrefix: String = _
private val extraArg: mutable.Seq[String] = mutable.Seq()
private val extraArgBefore: mutable.Seq[String] = mutable.Seq()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much prefer to move these to the case class members:

private final case class Impl(
  execuable: File,
  library: String,
  // ...
  extraArgs: immutable.Seq[String],
  // ...
) extends Bindgen {
  def bindgenExecutable((executable: File): Bindgen {
     require(executable.exists())
     copy(executable = executable)
  }
  def extraArgs(args: String*): Bindgen = {
    require(args.forall(_.nonEmpty))
    copy(extraArgs = extraArgs ++ args)
  }
  // ...
}

This also allows to use immutable.Seq or List.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -1,9 +1,8 @@
package org.scalanative.bindgen

import java.io.{File, PrintWriter}
import java.io.{File}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{/} can be removed.

@kornilova203 kornilova203 requested a review from jonas June 30, 2018 18:25
Copy link
Member

@jonas jonas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me but there are severalhings that could be expressed in a more natural way.

* Name of Scala object that contains bindings.
* Default is set to library name.
*/
def scalaObjectName(scalaObjectName: String): Bindgen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest calling this name.

}

private def validateFields(): Unit = {
if (executable == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using require(executable.isDefined, "The executable must be specified") is more concise.

executable: File = null,
library: String = null,
header: File = null,
scalaObjectName: String = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional fields are better expressed using Option[T] so the compiler forces them to be handled explicitly.

def apply(): Bindgen = Impl()

private final case class Impl(
executable: File = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using Option[File] and default to scala-native-bindgen

executable.fold("scala-native-bindgen")(_.getAbsolutePath)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then we should warn that default value is set because user might simply forget to set executable, so I would not use default

if (this.scalaObjectName != null)
this.scalaObjectName
else
library
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Option[String] as the type this becomes

val name = this.name.getOrElse(library)

.packageName("org.scalanative.bindgen.samples")
.excludePrefix("__")
.generate()
.writeToFile(outputFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kornilova203 kornilova203 merged commit 11d65bd into master Jul 2, 2018
@kornilova203 kornilova203 deleted the tools branch July 2, 2018 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants