-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgoseq.go
184 lines (155 loc) · 4.19 KB
/
goseq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/howeyc/fsnotify"
"github.com/lmika/goseq/seqdiagram"
)
// Name of the output file
var flagOut = flag.String("o", "", "Output file")
// The style to use
var flagStyle = flag.String("s", "default", "The style to use")
// Generate an embedded SVG file
var flagEmbedded = flag.Bool("e", false, "Generate an embedded SVG file")
// Setup a watcher to regenerate the file when changed
var flagWatch = flag.Bool("w", false, "Watch for changes")
// Die with error
func die(msg string) {
fmt.Fprintf(os.Stderr, "goseq: %s\n", msg)
os.Exit(1)
}
// Construct and build image options based on the current configuration
func buildImageOptions() *seqdiagram.ImageOptions {
// Work out the style
style := seqdiagram.DefaultStyle
if altStyle, hasStyle := seqdiagram.StyleNames[*flagStyle]; hasStyle {
style = altStyle
}
return &seqdiagram.ImageOptions{
Style: style,
Embedded: *flagEmbedded,
}
}
// Processes a md file
func processMdFile(inFilename string, outFilename string, renderer Renderer) error {
srcFile, err := openSourceFile(inFilename)
if err != nil {
return err
}
defer srcFile.Close()
targetFile := ioutil.Discard
mf := &MarkdownFilter{srcFile, targetFile, func(codeblock string, output io.Writer) error {
fmt.Fprint(output, codeblock)
err := processSeqDiagram(strings.NewReader(codeblock), inFilename, "/dev/null", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "goseq: %s:embedded block - %s\n", inFilename, err.Error())
}
return nil
}}
return mf.Scan()
}
// Processes a seq file
func processSeqFile(inFilename string, outFilename string, renderer Renderer) error {
srcFile, err := openSourceFile(inFilename)
if err != nil {
return err
}
defer srcFile.Close()
return processSeqDiagram(srcFile, inFilename, outFilename, renderer)
}
// Processes a sequence diagram
func processSeqDiagram(infile io.Reader, inFilename string, outFilename string, renderer Renderer) error {
diagram, err := seqdiagram.ParseDiagram(infile, inFilename)
if err != nil {
return err
}
// Image options
imageOptions := buildImageOptions()
// If there's a process instruction, use it as the target of the diagram
// TODO: be a little smarter with the process instructions
for _, pr := range diagram.ProcessingInstructions {
if pr.Prefix == "goseq" {
outFilename = pr.Value
}
}
if renderer == nil {
renderer, err = chooseRendererBaseOnOutfile(outFilename)
if err != nil {
return err
}
}
err = renderer(diagram, imageOptions, outFilename)
if err != nil {
return err
}
return nil
}
// Processes a file. This switches based on the file extension
func processFile(inFilename string, outFilename string, renderer Renderer) error {
ext := filepath.Ext(inFilename)
if ext == ".md" {
return processMdFile(inFilename, outFilename, renderer)
} else {
return processSeqFile(inFilename, outFilename, renderer)
}
}
// Setup a watch process which will regenerate the files
func watchAndProcess(inFiles []string, outFile string, renderer Renderer) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
for _, inFile := range inFiles {
watcher.Watch(inFile)
}
for {
select {
case event := <-watcher.Event:
if event.IsModify() {
if err := processFile(event.Name, outFile, renderer); err == nil {
log.Println("Generating", event.Name, "->", outFile)
} else {
log.Println(event.Name, "-", err.Error())
}
}
}
}
}
func main() {
var err error
renderer := SvgRenderer
outFile := ""
flag.Parse()
// Select a suitable renderer (based on the suffix of the output file, if there is one)
if *flagOut != "" {
renderer, err = chooseRendererBaseOnOutfile(*flagOut)
if err != nil {
die(err.Error())
}
outFile = *flagOut
}
// Process each file (or stdin)
if flag.NArg() == 0 {
err := processFile("-", outFile, renderer)
if err != nil {
die("stdin - " + err.Error())
}
} else {
if *flagWatch {
watchAndProcess(flag.Args(), outFile, renderer)
} else {
for _, inFile := range flag.Args() {
err := processFile(inFile, outFile, renderer)
if err != nil {
die(inFile + " - " + err.Error())
}
}
}
}
}