From 853793af8fa80f3f293e0a1a27c5a88c939e18d4 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Mon, 29 Sep 2014 15:06:41 -0400 Subject: [PATCH] Output TOC and tree in sorted order to make output stable between invocations. Addresses issue #49 --- asset.go | 7 +++++++ convert.go | 4 ++++ toc.go | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/asset.go b/asset.go index 95b6b94..51771a8 100644 --- a/asset.go +++ b/asset.go @@ -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 } diff --git a/convert.go b/convert.go index e0b1a3d..335ca62 100644 --- a/convert.go +++ b/convert.go @@ -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 { diff --git a/toc.go b/toc.go index 566e6e1..cbbae6e 100644 --- a/toc.go +++ b/toc.go @@ -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, "}}")