Skip to content

Commit 4a2e722

Browse files
author
shawnZuo
committed
update to sbt and support MAC OSX
1 parent 8fe565e commit 4a2e722

File tree

6 files changed

+71
-180
lines changed

6 files changed

+71
-180
lines changed

build.sbt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name := "grazScala"
3+
4+
version := "0.1"
5+
6+
scalaVersion := "2.12.8"
7+
8+
enablePlugins(JavaAppPackaging)
9+
10+
libraryDependencies ++= Seq(
11+
"org.scalatest" %% "scalatest" % "3.0.5",
12+
"org.apache.commons" % "commons-lang3" % "3.5"
13+
)

pom.xml

-147
This file was deleted.

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.2.8

project/plugins.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.4")

src/main/scala/com/liangdp/graphviz4s/Backend.scala

+50-30
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ package com.liangdp.graphviz4s
33
import org.apache.commons.lang3.SystemUtils
44

55
/**
6-
* @author Depeng Liang
7-
*/
6+
* @author Depeng Liang
7+
*/
88
object Backend {
99

1010
// http://www.graphviz.org/cgi-bin/man?dot
1111
private val ENGINES = Set(
1212
"dot", "neato", "twopi", "circo", "fdp", "sfdp", "patchwork", "osage"
1313
)
1414

15+
val ENGINGSEXECPATH: Map[String, String] = ENGINES.foldLeft(Map[String, String]())({
16+
(m, st) => {
17+
import sys.process._
18+
Map[String, String](st -> s"which $st".!!) ++ m
19+
}
20+
})
21+
1522
// http://www.graphviz.org/doc/info/output.html
1623
private val FORMATS = Set(
1724
"bmp",
@@ -52,61 +59,74 @@ object Backend {
5259
"x11"
5360
)
5461

55-
require(SystemUtils.IS_OS_LINUX, "only support linux right now.")
62+
/**
63+
* Return command for open a file for default
64+
*/
65+
def viewFileCommand(): String = {
66+
if (SystemUtils.IS_OS_LINUX) {
67+
"xdg-open"
68+
}
69+
else if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
70+
"open"
71+
}
72+
else throw new RuntimeException("only support mac OSX or Linux ")
73+
}
5674

5775
/**
58-
* Return command for execution and name of the rendered file.
59-
*
60-
* @param engine The layout commmand used for rendering ('dot', 'neato', ...).
61-
* @param format The output format used for rendering ('pdf', 'png', ...).
62-
* @param filePath The output path of the source file.
63-
* @return render command to execute.
64-
* @return rendered file path.
65-
*/
76+
* Return command for execution and name of the rendered file.
77+
*
78+
* @param engine The layout commmand used for rendering ('dot', 'neato', ...).
79+
* @param format The output format used for rendering ('pdf', 'png', ...).
80+
* @param filePath The output path of the source file.
81+
* @return render command to execute.
82+
*/
6683
def command(engine: String, format: String, filePath: String = null): (String, String) = {
6784
require(ENGINES.contains(engine) == true, s"unknown engine: $engine")
6885
require(FORMATS.contains(format) == true, s"unknown format: $format")
6986
Option(filePath) match {
70-
case Some(path) => (s"$engine -T$format -O $path", s"$path.$format")
87+
case Some(path) => (s"${ENGINGSEXECPATH(engine)} -T$format -O $path", s"$path.$format")
7188
case None => (s"$engine -T$format", null)
7289
}
7390
}
7491

7592
/**
76-
* Render file with Graphviz engine into format, return result filename.
77-
*
78-
* @param engine The layout commmand used for rendering ('dot', 'neato', ...).
79-
* @param format The output format used for rendering ('pdf', 'png', ...).
80-
* @param filepath Path to the DOT source file to render.
81-
*/
93+
* Render file with Graphviz engine into format, return result filename.
94+
*
95+
* @param engine The layout commmand used for rendering ('dot', 'neato', ...).
96+
* @param format The output format used for rendering ('pdf', 'png', ...).
97+
* @param filePath Path to the DOT source file to render.
98+
*/
8299
@throws(classOf[RuntimeException])
83100
def render(engine: String = "dot", format: String = "pdf",
84-
filePath: String): String = {
101+
filePath: String): String = {
85102
val (args, rendered) = command(engine, format, filePath)
86103
import sys.process._
87104
try {
88105
args !
89-
} catch { case _ : Throwable =>
90-
val errorMsg = s"""failed to execute "$args", """ +
91-
""""make sure the Graphviz executables are on your systems' path"""
92-
throw new RuntimeException(errorMsg)
106+
} catch {
107+
case _: Throwable =>
108+
val errorMsg =
109+
s"""failed to execute "$args", """ +
110+
""""make sure the Graphviz executables are on your systems' path"""
111+
throw new RuntimeException(errorMsg)
93112
}
94113
rendered
95114
}
96115

97116
/**
98-
* Open filepath with its default viewing application (platform-specific).
99-
* For know only support linux.
100-
*/
117+
* Open filepath with its default viewing application (platform-specific).
118+
* For know only support linux.
119+
*/
101120
@throws(classOf[RuntimeException])
102121
def view(filePath: String): Unit = {
103-
val command = s"xdg-open $filePath"
122+
val command = s"$viewFileCommand $filePath"
104123
import sys.process._
105124
try {
106125
command !
107-
} catch { case _ : Throwable =>
108-
val errorMsg = s"failed to execute $command"
109-
throw new RuntimeException(errorMsg)
126+
} catch {
127+
case _: Throwable =>
128+
val errorMsg = s"failed to execute $command"
129+
throw new RuntimeException(errorMsg)
110130
}
111131
}
112132
}

src/main/scala/com/liangdp/graphviz4s/Dot.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.io.File
44
import java.io.PrintWriter
55
import scala.collection.mutable.ArrayBuffer
66
import scala.collection.mutable.Map
7-
7+
import com.liangdp.graphviz4s.Backend.viewFileCommand;
88
/**
99
* Assemble, save, and render DOT source code, open result in viewer.
1010
* @author Depeng Liang
@@ -213,10 +213,13 @@ abstract class Dot(
213213
*/
214214
protected def _view(filePath: String, format: String): Unit = {
215215
import sys.process._
216+
var command = viewFileCommand
216217
try {
217-
s"xdg-open $filePath" !
218+
s"$command $filePath" !
218219
} catch { case _ : Throwable =>
219-
val errorMsg = s"""no built-in viewer support for $format on Linux platform"""
220+
val errorMsg =
221+
s"""no built-in viewer support for $command $format
222+
|on Linux platform""".stripMargin
220223
throw new RuntimeException(errorMsg)
221224
}
222225
}

0 commit comments

Comments
 (0)