@@ -20,6 +20,7 @@ import (
20
20
"bytes"
21
21
"fmt"
22
22
"path"
23
+ "sort"
23
24
"strings"
24
25
"text/template"
25
26
@@ -181,8 +182,14 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
181
182
182
183
funcMap := e .alterFuncMap (t )
183
184
185
+ // We want to parse the templates in a predictable order. The order favors
186
+ // higher-level (in file system) templates over deeply nested templates.
187
+ keys := sortTemplates (tpls )
188
+
184
189
files := []string {}
185
- for fname , r := range tpls {
190
+ //for fname, r := range tpls {
191
+ for _ , fname := range keys {
192
+ r := tpls [fname ]
186
193
t = t .New (fname ).Funcs (funcMap )
187
194
if _ , err := t .Parse (r .tpl ); err != nil {
188
195
return map [string ]string {}, fmt .Errorf ("parse error in %q: %s" , fname , err )
@@ -215,6 +222,30 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
215
222
return rendered , nil
216
223
}
217
224
225
+ func sortTemplates (tpls map [string ]renderable ) []string {
226
+ keys := make ([]string , len (tpls ))
227
+ i := 0
228
+ for key := range tpls {
229
+ keys [i ] = key
230
+ i ++
231
+ }
232
+ sort .Sort (sort .Reverse (byPathLen (keys )))
233
+ return keys
234
+ }
235
+
236
+ type byPathLen []string
237
+
238
+ func (p byPathLen ) Len () int { return len (p ) }
239
+ func (p byPathLen ) Swap (i , j int ) { p [j ], p [i ] = p [i ], p [j ] }
240
+ func (p byPathLen ) Less (i , j int ) bool {
241
+ a , b := p [i ], p [j ]
242
+ ca , cb := strings .Count (a , "/" ), strings .Count (b , "/" )
243
+ if ca == cb {
244
+ return strings .Compare (a , b ) == - 1
245
+ }
246
+ return ca < cb
247
+ }
248
+
218
249
// allTemplates returns all templates for a chart and its dependencies.
219
250
//
220
251
// As it goes, it also prepares the values in a scope-sensitive manner.
0 commit comments