Output TOC and tree in sorted order to make output stable between invocations. Addresses issue #49

This commit is contained in:
Jordan Liggitt 2014-09-29 15:06:41 -04:00
parent 93b909d149
commit 853793af8f
3 changed files with 24 additions and 2 deletions

View File

@ -10,3 +10,10 @@ type Asset struct {
Name string // Key used in TOC -- name by which asset is referenced.
Func string // Function name for the procedure returning the asset contents.
}
// Implement sort.Interface for []Asset based on Path field
type ByPath []Asset
func (v ByPath) Len() int { return len(v) }
func (v ByPath) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v ByPath) Less(i, j int) bool { return v[i].Path < v[j].Path }

View File

@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"unicode"
)
@ -34,6 +35,9 @@ func Translate(c *Config) error {
}
}
// Sort to make output stable between invocations
sort.Sort(ByPath(toc))
// Create output file.
fd, err := os.Create(c.Output)
if err != nil {

15
toc.go
View File

@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"
)
@ -54,10 +55,20 @@ func (root *assetTree) funcOrNil() string {
func (root *assetTree) writeGoMap(w io.Writer, nident int) {
fmt.Fprintf(w, "&_bintree_t{%s, map[string]*_bintree_t{\n", root.funcOrNil())
for p, child := range root.Children {
// Sort to make output stable between invocations
filenames := make([]string, len(root.Children))
i := 0
for filename, _ := range root.Children {
filenames[i] = filename
i++
}
sort.Strings(filenames)
for _, p := range filenames {
ident(w, nident+1)
fmt.Fprintf(w, `"%s": `, p)
child.writeGoMap(w, nident+1)
root.Children[p].writeGoMap(w, nident+1)
}
ident(w, nident)
io.WriteString(w, "}}")