diff --git a/constant/constants.go b/constant/constants.go
index 23094ec..ee35f4e 100644
--- a/constant/constants.go
+++ b/constant/constants.go
@@ -315,7 +315,3 @@ var IndexContent = `
`
var DataFile = ``
-
-var IncludeIndex = "="
-
-var IncludeCSS = ``
diff --git a/conv/conv.go b/conv/conv.go
new file mode 100644
index 0000000..b5ec173
--- /dev/null
+++ b/conv/conv.go
@@ -0,0 +1,73 @@
+package conv
+
+import (
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/getgauge/spectacle/gauge_messages"
+ "github.com/getgauge/spectacle/util"
+ "github.com/golang-commonmark/markdown"
+)
+
+const (
+ dotHtml = ".html"
+ dotCSS = ".css"
+ quotes = `""''`
+ linkTemplate = "%s"
+ htmlTemplate = `%s%s%s%s%s`
+ includeIndex = "- =
"
+ IncludeCSS = ``
+ indexHtml = "index.html"
+ StyleCSS = "style.css"
+)
+
+var outDir = util.GetOutDir()
+var projectRoot = util.GetProjectRoot()
+
+func ConvertFile(file string, files []string, index int) {
+ md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true), markdown.Quotes(quotes))
+ input, err := ioutil.ReadFile(file)
+ util.Fatal(fmt.Sprintf("Error while reading %s file", file), err)
+ output := md.RenderToString(input)
+ var next, prev string
+ if index+1 < len(files) {
+ next = fmt.Sprintf(linkTemplate, getRelFilePath(file, files[index+1], dotHtml), ">")
+ }
+ if index != 0 {
+ prev = fmt.Sprintf(linkTemplate, getRelFilePath(file, files[index-1], dotHtml), "<")
+ }
+ toc := fmt.Sprintf(includeIndex, getRelFilePath(file, filepath.Join(projectRoot, indexHtml), dotHtml))
+ style := fmt.Sprintf(IncludeCSS, getRelFilePath(file, filepath.Join(projectRoot, StyleCSS), dotCSS))
+ output = fmt.Sprintf(htmlTemplate, style, output, prev, toc, next)
+ relPath := getRelPath(projectRoot, file)
+ util.CreateDirectory(filepath.Join(outDir, filepath.Dir(relPath)))
+ name := strings.TrimSuffix(relPath, filepath.Ext(relPath)) + dotHtml
+ f, err := os.Create(filepath.Join(outDir, name))
+ util.Fatal(fmt.Sprintf("Error while creating %s file", filepath.Join(outDir, name)), err)
+ f.Write([]byte(output))
+ f.Close()
+}
+
+func GetSpecs(m *gauge_messages.SpecsResponse) []*gauge_messages.ProtoSpec {
+ specs := make([]*gauge_messages.ProtoSpec, 0)
+ for _, d := range m.Details {
+ if d.Spec != nil {
+ specs = append(specs, d.Spec)
+ }
+ }
+ return specs
+}
+
+func getRelFilePath(f, f1, ext string) string {
+ base := getRelPath(filepath.Dir(f), filepath.Dir(f1)) + string(filepath.Separator) + filepath.Base(f1)
+ return strings.TrimSuffix(base, filepath.Ext(f1)) + ext
+}
+
+func getRelPath(f, f1 string) string {
+ p, err := filepath.Rel(f, f1)
+ util.Fatal("Cannot get relative path", err)
+ return p
+}
diff --git a/json/json.go b/json/json.go
index 157ae9f..281b4d3 100644
--- a/json/json.go
+++ b/json/json.go
@@ -13,6 +13,8 @@ import (
"github.com/golang/protobuf/proto"
)
+var projectRoot = util.GetProjectRoot()
+
type specInfo struct {
Path string `json:"path"`
Name string `json:"name"`
@@ -27,10 +29,12 @@ type scenarioInfo struct {
func WriteJS(specs []*gauge_messages.ProtoSpec, files []string, outDir string, fileExtn string) {
specs = sortSpecs(specs, files)
specScenariosMap, tags := getSpecsScenariosMap(specs, fileExtn)
- f, _ := os.Create(outDir + string(filepath.Separator) + "data.js")
+ f, err := os.Create(outDir + string(filepath.Separator) + "data.js")
+ util.Fatal("Unable to create data.js file", err)
f.WriteString(fmt.Sprintf("%s\n%s", tags, specScenariosMap))
f.Close()
- f, _ = os.Create(outDir + string(filepath.Separator) + "index.js")
+ f, err = os.Create(outDir + string(filepath.Separator) + "index.js")
+ util.Fatal("Unable to create index.js file", err)
f.WriteString(constant.IndexJSContent)
f.Close()
}
@@ -58,7 +62,8 @@ func getSpecsScenariosMap(specs []*gauge_messages.ProtoSpec, fileExtn string) (s
var specsInfos []specInfo
tags := make(map[string]bool)
for _, spec := range specs {
- fileName := strings.TrimSuffix(filepath.Base(spec.GetFileName()), filepath.Ext(spec.GetFileName()))
+ relPath, _ := filepath.Rel(projectRoot, spec.GetFileName())
+ fileName := strings.TrimSuffix(relPath, filepath.Ext(spec.GetFileName()))
si := specInfo{Path: fileName + fileExtn, Name: spec.GetSpecHeading(), Scenarios: make([]scenarioInfo, 0)}
addTags(spec.Tags, tags)
for _, item := range spec.Items {
@@ -73,12 +78,12 @@ func getSpecsScenariosMap(specs []*gauge_messages.ProtoSpec, fileExtn string) (s
}
specsInfos = append(specsInfos, si)
}
- json, err := json.Marshal(specsInfos)
+ j, err := json.Marshal(specsInfos)
if err != nil {
- fmt.Println("Cannot convert specs to HTML. Reason: %s", err.Error())
+ fmt.Printf("Cannot convert specs to HTML. Reason: %s\n", err.Error())
return "", ""
}
- return fmt.Sprintf("var specs = %s", string(json)), fmt.Sprintf("var tags = [%s]", strings.Join(getUniqueTags(tags), ", "))
+ return fmt.Sprintf("var specs = %s", string(j)), fmt.Sprintf("var tags = [%s]", strings.Join(getUniqueTags(tags), ", "))
}
func addTags(tags []string, tagsMap map[string]bool) {
diff --git a/spectacle.go b/spectacle.go
index c5b5f28..b8a9f01 100644
--- a/spectacle.go
+++ b/spectacle.go
@@ -2,92 +2,63 @@ package main
import (
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/getgauge/spectacle/constant"
- "github.com/getgauge/spectacle/gauge_messages"
+ "github.com/getgauge/spectacle/conv"
"github.com/getgauge/spectacle/json"
"github.com/getgauge/spectacle/processor"
"github.com/getgauge/spectacle/util"
- "github.com/golang-commonmark/markdown"
)
const (
- html = ".html"
+ dotHtml = ".html"
localhost = "localhost"
gaugeSpecsDir = "GAUGE_SPEC_DIRS"
gaugeApiPort = "GAUGE_API_PORT"
space = " "
indexFile = "index.html"
- quotes = `""''`
- linkTemplate = "- %s
"
- htmlTemplate = `%s%s%s%s%s`
styleCSS = "style.css"
)
var outDir = util.GetOutDir()
+var projectRoot = util.GetProjectRoot()
func main() {
var files []string
for _, arg := range strings.Split(os.Getenv(gaugeSpecsDir), space) {
files = append(files, util.GetFiles(arg)...)
}
- p, _ := processor.NewMessageProcessor(localhost, os.Getenv(gaugeApiPort))
- msg, _ := p.GetSpecsResponse()
+ p, err := processor.NewMessageProcessor(localhost, os.Getenv(gaugeApiPort))
+ util.Fatal("Cannot connect to Gauge API", err)
+ msg, err := p.GetSpecsResponse()
+ util.Fatal("Cannot connect to Gauge API", err)
p.Connection.Close()
util.CreateDirectory(outDir)
- json.WriteJS(getSpecs(msg.SpecsResponse), files, outDir, html)
+ json.WriteJS(conv.GetSpecs(msg.SpecsResponse), files, outDir, dotHtml)
writeCSS()
- var lastSpec string
for i, file := range files {
- convertFile(file, files, i, &lastSpec)
+ conv.ConvertFile(file, files, i)
}
createIndex()
fmt.Printf("Succesfully converted specs to html => %s\n", outDir)
}
-func getSpecs(m *gauge_messages.SpecsResponse) []*gauge_messages.ProtoSpec {
- specs := make([]*gauge_messages.ProtoSpec, 0)
- for _, d := range m.Details {
- if d.Spec != nil {
- specs = append(specs, d.Spec)
- }
- }
- return specs
-}
-
-func convertFile(file string, files []string, index int, lastSpec *string) {
- md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true), markdown.Quotes(quotes))
- input, _ := ioutil.ReadFile(file)
- output := md.RenderToString(input)
- var next, previous string
- if index+1 < len(files) {
- next = fmt.Sprintf(linkTemplate, strings.TrimSuffix(filepath.Base(files[index+1]),
- filepath.Ext(files[index+1]))+html, ">")
- }
- if index != 0 {
- previous = fmt.Sprintf(linkTemplate, *lastSpec, "<")
- }
- output = fmt.Sprintf(htmlTemplate, constant.IncludeCSS, output, previous, constant.IncludeIndex, next)
- name := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
- f, _ := os.Create(outDir + string(filepath.Separator) + name + html)
- f.Write([]byte(output))
- f.Close()
- *lastSpec = name + html
-}
-
func createIndex() {
- f, _ := os.Create(outDir + string(filepath.Separator) + indexFile)
- input := constant.IncludeCSS + constant.DataFile + fmt.Sprintf(constant.IndexContent, filepath.Base(util.GetProjectRoot())) + constant.IndexJS
+ 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, filepath.Base(projectRoot))
+ input := style + constant.DataFile + header + constant.IndexJS
f.WriteString(input)
f.Close()
}
func writeCSS() {
- f, _ := os.Create(outDir + string(filepath.Separator) + styleCSS)
+ 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()
}
diff --git a/util/file_utils.go b/util/file_utils.go
index c9f9be9..04cb71b 100644
--- a/util/file_utils.go
+++ b/util/file_utils.go
@@ -34,7 +34,7 @@ func findFilesInDir(dirPath string, isValidFile func(path string) bool) []string
}
func findFilesIn(dirRoot string, isValidFile func(path string) bool) []string {
- absRoot, _ := filepath.Abs(dirRoot)
+ absRoot := getAbsPath(dirRoot)
files := findFilesInDir(absRoot, isValidFile)
return files
}
@@ -60,15 +60,18 @@ func GetFiles(path string) []string {
if dirExists(path) {
specFiles = append(specFiles, findFilesIn(path, isValidSpecExtension)...)
} else if fileExists(path) && isValidSpecExtension(path) {
- f, _ := filepath.Abs(path)
- specFiles = append(specFiles, f)
+ specFiles = append(specFiles, getAbsPath(path))
}
return specFiles
}
func CreateDirectory(dir string) {
- if err := os.MkdirAll(dir, 0755); err != nil {
- fmt.Printf("Failed to create directory %s: %s\n", dir, err)
- os.Exit(1)
- }
+ err := os.MkdirAll(dir, 0755)
+ Fatal(fmt.Sprintf("Failed to create directory %s", dir), err)
+}
+
+func getAbsPath(path string) string {
+ f, err := filepath.Abs(path)
+ Fatal("Cannot get absolute path", err)
+ return f
}
diff --git a/util/util.go b/util/util.go
index 4b04953..ff49d25 100644
--- a/util/util.go
+++ b/util/util.go
@@ -44,3 +44,10 @@ func GetOutDir() string {
func GetProjectRoot() string {
return projectRoot
}
+
+func Fatal(message string, err error) {
+ if err != nil {
+ fmt.Printf("%s. Error: %s", message, err.Error())
+ os.Exit(1)
+ }
+}