forked from getgauge/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectacle.go
96 lines (84 loc) · 2.61 KB
/
spectacle.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
package main
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"github.com/getgauge/spectacle/constant"
"github.com/getgauge/spectacle/conv"
"github.com/getgauge/spectacle/gauge_messages"
"github.com/getgauge/spectacle/json"
"github.com/getgauge/spectacle/util"
"google.golang.org/grpc"
)
const (
dotHtml = ".html"
localhost = "localhost"
gaugeSpecsDir = "GAUGE_SPEC_DIRS"
gaugeApiPort = "GAUGE_API_PORT"
space = " "
fileSeparator = "||"
indexFile = "index.html"
styleCSS = "style.css"
)
var outDir = util.GetOutDir()
var projectRoot = util.GetProjectRoot()
const tenGB = 1024 * 1024 * 1024 * 10
type handler struct {
server *grpc.Server
}
func (h *handler) GenerateDocs(c context.Context, m *gauge_messages.SpecDetails) (*gauge_messages.Empty, error) {
var files []string
for _, arg := range strings.Split(os.Getenv(gaugeSpecsDir), fileSeparator) {
files = append(files, util.GetFiles(arg)...)
}
util.CreateDirectory(outDir)
json.WriteJS(conv.GetSpecs(m), files, outDir, dotHtml)
writeCSS()
for i, file := range files {
conv.ConvertFile(file, files, i)
}
createIndex()
fmt.Printf("Succesfully converted specs to html => %s\n", filepath.Join(outDir, indexFile))
return &gauge_messages.Empty{}, nil
}
func (h *handler) Kill(c context.Context, m *gauge_messages.KillProcessRequest) (*gauge_messages.Empty, error) {
defer h.stopServer()
return &gauge_messages.Empty{}, nil
}
func (h *handler) stopServer() {
h.server.Stop()
}
func main() {
os.Chdir(projectRoot)
address, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
util.Fatal("failed to start server.", err)
}
l, err := net.ListenTCP("tcp", address)
if err != nil {
util.Fatal("failed to start server.", err)
}
server := grpc.NewServer(grpc.MaxRecvMsgSize(1024 * 1024 * 10))
h := &handler{server: server}
gauge_messages.RegisterDocumenterServer(server, h)
fmt.Println(fmt.Sprintf("Listening on port:%d", l.Addr().(*net.TCPAddr).Port))
server.Serve(l)
}
func createIndex() {
f, err := os.Create(outDir + string(filepath.Separator) + indexFile)
util.Fatal("Unable to create index.html", err)
style := fmt.Sprintf(conv.IncludeCSS, conv.StyleCSS)
header := fmt.Sprintf(constant.IndexContent, strings.Title(filepath.Base(projectRoot)))
input := style + constant.DataFile + header + constant.IndexJS
f.WriteString("<article class='markdown-body'><meta charset=\"utf-8\">" + input + "</article>")
f.Close()
}
func writeCSS() {
f, err := os.Create(outDir + string(filepath.Separator) + styleCSS)
util.Fatal("Error while creating style.css file", err)
f.Write([]byte(constant.CSS))
f.Close()
}