@@ -3,15 +3,22 @@ package com.liangdp.graphviz4s
3
3
import org .apache .commons .lang3 .SystemUtils
4
4
5
5
/**
6
- * @author Depeng Liang
7
- */
6
+ * @author Depeng Liang
7
+ */
8
8
object Backend {
9
9
10
10
// http://www.graphviz.org/cgi-bin/man?dot
11
11
private val ENGINES = Set (
12
12
" dot" , " neato" , " twopi" , " circo" , " fdp" , " sfdp" , " patchwork" , " osage"
13
13
)
14
14
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
+
15
22
// http://www.graphviz.org/doc/info/output.html
16
23
private val FORMATS = Set (
17
24
" bmp" ,
@@ -52,61 +59,74 @@ object Backend {
52
59
" x11"
53
60
)
54
61
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
+ }
56
74
57
75
/**
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
+ */
66
83
def command (engine : String , format : String , filePath : String = null ): (String , String ) = {
67
84
require(ENGINES .contains(engine) == true , s " unknown engine: $engine" )
68
85
require(FORMATS .contains(format) == true , s " unknown format: $format" )
69
86
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" )
71
88
case None => (s " $engine -T $format" , null )
72
89
}
73
90
}
74
91
75
92
/**
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
+ */
82
99
@ throws(classOf [RuntimeException ])
83
100
def render (engine : String = " dot" , format : String = " pdf" ,
84
- filePath : String ): String = {
101
+ filePath : String ): String = {
85
102
val (args, rendered) = command(engine, format, filePath)
86
103
import sys .process ._
87
104
try {
88
105
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)
93
112
}
94
113
rendered
95
114
}
96
115
97
116
/**
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
+ */
101
120
@ throws(classOf [RuntimeException ])
102
121
def view (filePath : String ): Unit = {
103
- val command = s " xdg-open $filePath"
122
+ val command = s " $viewFileCommand $filePath"
104
123
import sys .process ._
105
124
try {
106
125
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)
110
130
}
111
131
}
112
132
}
0 commit comments